diff --git a/test/Templates.Test/Helpers/AspNetProcess.cs b/test/Templates.Test/Helpers/AspNetProcess.cs index 48d7a00f14..268383f09f 100644 --- a/test/Templates.Test/Helpers/AspNetProcess.cs +++ b/test/Templates.Test/Helpers/AspNetProcess.cs @@ -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}..."); diff --git a/test/Templates.Test/SpaTemplateTest.cs b/test/Templates.Test/SpaTemplateTest.cs index 44efcf9423..0fcf506d9e 100644 --- a/test/Templates.Test/SpaTemplateTest.cs +++ b/test/Templates.Test/SpaTemplateTest.cs @@ -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) {