Workarounds to make SPA template tests work

This commit is contained in:
Steve Sanderson 2017-11-17 10:56:20 +00:00
parent 849a0843c9
commit a76a891865
2 changed files with 38 additions and 2 deletions

View File

@ -8,6 +8,7 @@ using System.Net.Http;
using Microsoft.Extensions.CommandLineUtils;
using Xunit;
using Xunit.Abstractions;
using System.Threading;
namespace Templates.Test.Helpers
{
@ -92,16 +93,46 @@ namespace Templates.Test.Helpers
public void AssertNotFound(string requestUrl)
=> AssertStatusCode(requestUrl, HttpStatusCode.NotFound);
public void AssertStatusCode(string requestUrl, HttpStatusCode statusCode)
public void AssertStatusCode(string requestUrl, HttpStatusCode statusCode, string acceptContentType = null)
{
var request = new HttpRequestMessage(
HttpMethod.Get,
new Uri(_listeningUri, requestUrl));
if (!string.IsNullOrEmpty(acceptContentType))
{
request.Headers.Add("Accept", acceptContentType);
}
var response = _httpClient.SendAsync(request).Result;
Assert.Equal(statusCode, response.StatusCode);
}
public void AssertStatusCodeWithRetry(string requestUrl, HttpStatusCode statusCode, string acceptContentType = null)
{
const int MaxAttempts = 3;
for (var attemptNumber = 1; ; attemptNumber++)
{
try
{
AssertStatusCode(requestUrl, statusCode, acceptContentType);
return;
}
catch
{
if (attemptNumber >= MaxAttempts)
{
_output.WriteLine($"Giving up requesting '{requestUrl}' after {attemptNumber} attempts.");
throw;
}
_output.WriteLine($"Request to '{requestUrl}' failed, but will retry...");
Thread.Sleep(3000);
}
}
}
public IWebDriver VisitInBrowser()
{
_output.WriteLine($"Opening browser at {_listeningUri}...");

View File

@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Testing.xunit;
using OpenQA.Selenium;
using System.Net;
using Templates.Test.Helpers;
using Xunit;
using Xunit.Abstractions;
@ -45,7 +46,11 @@ namespace Templates.Test
{
using (var aspNetProcess = StartAspNetProcess(targetFrameworkOverride, publish))
{
aspNetProcess.AssertOk("/");
// TODO: Remove the retry from here. Instead, work around the issue in the actual
// SpaServices.Extensions code (by retrying). The issue is that @angular/cli rejects
// connections for the first second or so after it claims to be listening.
// However, you do still need to keep sending "Accept: text/html" or it returns 404.
aspNetProcess.AssertStatusCodeWithRetry("/", HttpStatusCode.OK, "text/html");
if (WebDriverFactory.HostSupportsBrowserAutomation)
{