/
githubmirror
/
aspnetcore
Обзор
Документация
Войти
/
githubmirror
/
aspnetcore
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Mvc/test/Mvc.FunctionalTests/Infrastructure/HttpClientExtensions.cs
54 строки
2 KB
Youssef Fahmy
Update AngleSharp to latest (#67898)
20 июл 2026, 15:48
Не верифицирован
20 июл 2026, 15:48
fc4e6e0
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Net; using System.Net.Http; using AngleSharp.Html.Dom; using AngleSharp.Html.Parser; using Xunit.Sdk; namespace Microsoft.AspNetCore.Mvc.FunctionalTests; public static class HttpClientExtensions { public static async Task<IHtmlDocument> GetHtmlDocumentAsync(this HttpClient client, string requestUri) { var response = await client.GetAsync(requestUri); await AssertStatusCodeAsync(response, HttpStatusCode.OK); return await GetHtmlDocumentAsync(response); } public static async Task<IHtmlDocument> GetHtmlDocumentAsync(this HttpResponseMessage response) { var content = await response.Content.ReadAsStringAsync(); var parser = new HtmlParser(); var document = parser.ParseDocument(content); if (document == null) { throw new InvalidOperationException("Response content could not be parsed as HTML: " + Environment.NewLine + content); } return document; } public static async Task<HttpResponseMessage> AssertStatusCodeAsync(this HttpResponseMessage response, HttpStatusCode expectedStatusCode) { if (response.StatusCode == expectedStatusCode) { return response; } string responseContent = string.Join(Environment.NewLine, response.Headers); try { responseContent = await response.Content.ReadAsStringAsync(); } catch { // No-op } throw EqualException.ForMismatchedValues(expectedStatusCode, response.StatusCode, $"Expected status code {expectedStatusCode}. Actual {response.StatusCode}. Response Content:" + Environment.NewLine + responseContent); } }