public static async Task <TestResponse> FromHttpResponseMessageAsync( string relativePath, HttpResponseMessage httpResponseMessage) { if (httpResponseMessage == null) { throw new ArgumentNullException(nameof(httpResponseMessage)); } var hasContentType = httpResponseMessage.Content.Headers.TryGetValues("Content-Type", out var contentTypes); var hasLocation = httpResponseMessage.Headers.TryGetValues("Location", out var locations); var contentBytes = await httpResponseMessage.Content.ReadAsByteArrayAsync(); var contentString = await httpResponseMessage.Content.ReadAsStringAsync(); var output = new TestResponse( relativePath, httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, hasContentType ? contentTypes.Single() : null, hasLocation ? locations.Single() : null, contentBytes.Length, contentString); return(output); }
private async Task <TestResponse> GetTestResponseAsync(string relativePath, HttpRequestMessage request) { TestOutputHelper.WriteLine($"Request: {request.Method} {request.RequestUri.AbsoluteUri}"); var stopwatch = Stopwatch.StartNew(); using (var httpResponseMessage = await _httpClient.SendAsync(request)) { var response = await TestResponse.FromHttpResponseMessageAsync(relativePath, httpResponseMessage); TestOutputHelper.WriteLine($"Duration: {stopwatch.ElapsedMilliseconds}ms"); TestOutputHelper.WriteLine(response.ToString()); return(response); } }
private static void AssertContentTypeIsHtml(TestResponse response) { Assert.StartsWith("text/html", response.ContentTypeHeader); }
private static void AssertContentTypeIsXml(TestResponse response) { Assert.StartsWith("application/xml", response.ContentTypeHeader); }
private static void AssertContentIsPrettyHtml(TestResponse response) { Assert.True(response.IsPrettyHtml, "The content should be the pretty HTML error page."); }