From 9126beebee315f781ad22aa71a9012b3c5d2d909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renan=20Arag=C3=A3o?= Date: Sat, 16 Dec 2023 19:45:57 -0300 Subject: [PATCH 1/2] update to Flurl 4 --- Flurl.Http.Xml.sln | 44 -------- FlurlX.Http.Xml.sln | 40 +++++++ global.json | 2 +- .../FlurlHttpSettingsExtensions.cs | 37 ------- src/Flurl.Http.Xml/MicrosoftXmlSerializer.cs | 87 --------------- .../CapturedXmlContent.cs | 2 +- .../FlurlHttpSettingsExtensions.cs | 23 ++++ .../FlurlRequestExtensions.cs | 100 ++++++++++-------- .../FlurlX.Http.Xml.csproj} | 27 ++++- .../HttpResponseMessageExtensions.cs | 52 ++++----- src/FlurlX.Http.Xml/MicrosoftXmlSerializer.cs | 75 +++++++++++++ .../StringExtensions.cs | 54 ++++++---- .../UrlExtensions.cs | 62 +++++++---- .../Utf8StringWriter.cs | 2 +- .../XDocumentExtensions.cs | 2 +- .../Factories/EchoHttpClientFactory.cs | 47 -------- .../JsonTestModelHttpClientFactory.cs | 14 --- .../XmlTestModelHttpClientFactory.cs | 15 --- test/Flurl.Http.Xml.Tests/TestBase.cs | 20 ---- .../AssemblyInfo.cs | 0 .../Flurl.Http.Xml.Tests.xunit.runner.json | 0 .../FlurlX.Http.Xml.Tests.csproj | 35 ++++++ .../MicrosoftXmlSerializerShould.cs | 10 +- .../Models/TestModel.cs | 2 +- .../RequestExtensionsShould.cs | 68 ++++++++---- .../StringExtensionsShould.cs | 50 ++++++--- test/FlurlX.Http.Xml.Tests/TestBase.cs | 31 ++++++ .../UrlExtensionsShould.cs | 51 ++++++--- 28 files changed, 504 insertions(+), 448 deletions(-) delete mode 100644 Flurl.Http.Xml.sln create mode 100644 FlurlX.Http.Xml.sln delete mode 100644 src/Flurl.Http.Xml/FlurlHttpSettingsExtensions.cs delete mode 100644 src/Flurl.Http.Xml/MicrosoftXmlSerializer.cs rename src/{Flurl.Http.Xml => FlurlX.Http.Xml}/CapturedXmlContent.cs (96%) create mode 100644 src/FlurlX.Http.Xml/FlurlHttpSettingsExtensions.cs rename src/{Flurl.Http.Xml => FlurlX.Http.Xml}/FlurlRequestExtensions.cs (66%) rename src/{Flurl.Http.Xml/Flurl.Http.Xml.csproj => FlurlX.Http.Xml/FlurlX.Http.Xml.csproj} (52%) rename src/{Flurl.Http.Xml => FlurlX.Http.Xml}/HttpResponseMessageExtensions.cs (73%) create mode 100644 src/FlurlX.Http.Xml/MicrosoftXmlSerializer.cs rename src/{Flurl.Http.Xml => FlurlX.Http.Xml}/StringExtensions.cs (70%) rename src/{Flurl.Http.Xml => FlurlX.Http.Xml}/UrlExtensions.cs (68%) rename src/{Flurl.Http.Xml => FlurlX.Http.Xml}/Utf8StringWriter.cs (97%) rename src/{Flurl.Http.Xml => FlurlX.Http.Xml}/XDocumentExtensions.cs (95%) delete mode 100644 test/Flurl.Http.Xml.Tests/Factories/EchoHttpClientFactory.cs delete mode 100644 test/Flurl.Http.Xml.Tests/Factories/JsonTestModelHttpClientFactory.cs delete mode 100644 test/Flurl.Http.Xml.Tests/Factories/XmlTestModelHttpClientFactory.cs delete mode 100644 test/Flurl.Http.Xml.Tests/TestBase.cs rename test/{Flurl.Http.Xml.Tests => FlurlX.Http.Xml.Tests}/AssemblyInfo.cs (100%) rename test/{Flurl.Http.Xml.Tests => FlurlX.Http.Xml.Tests}/Flurl.Http.Xml.Tests.xunit.runner.json (100%) create mode 100644 test/FlurlX.Http.Xml.Tests/FlurlX.Http.Xml.Tests.csproj rename test/{Flurl.Http.Xml.Tests => FlurlX.Http.Xml.Tests}/MicrosoftXmlSerializerShould.cs (90%) rename test/{Flurl.Http.Xml.Tests => FlurlX.Http.Xml.Tests}/Models/TestModel.cs (74%) rename test/{Flurl.Http.Xml.Tests => FlurlX.Http.Xml.Tests}/RequestExtensionsShould.cs (78%) rename test/{Flurl.Http.Xml.Tests => FlurlX.Http.Xml.Tests}/StringExtensionsShould.cs (74%) create mode 100644 test/FlurlX.Http.Xml.Tests/TestBase.cs rename test/{Flurl.Http.Xml.Tests => FlurlX.Http.Xml.Tests}/UrlExtensionsShould.cs (76%) diff --git a/Flurl.Http.Xml.sln b/Flurl.Http.Xml.sln deleted file mode 100644 index b407d1c..0000000 --- a/Flurl.Http.Xml.sln +++ /dev/null @@ -1,44 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.28803.156 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F2114366-0322-434F-91D3-A25939F0CFA7}" - ProjectSection(SolutionItems) = preProject - .gitattributes = .gitattributes - .gitignore = .gitignore - appveyor.yml = appveyor.yml - global.json = global.json - README.md = README.md - EndProjectSection -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{5B74B194-432C-4581-B683-22FB89C46AD3}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Flurl.Http.Xml.Tests", "test\Flurl.Http.Xml.Tests\Flurl.Http.Xml.Tests.csproj", "{7BA9BC27-1501-47EA-90FF-BCDBD25CA8B8}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Flurl.Http.Xml", "src\Flurl.Http.Xml\Flurl.Http.Xml.csproj", "{B9826CF6-17DD-4D2D-A6BD-F15CFA18F9CB}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {7BA9BC27-1501-47EA-90FF-BCDBD25CA8B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7BA9BC27-1501-47EA-90FF-BCDBD25CA8B8}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7BA9BC27-1501-47EA-90FF-BCDBD25CA8B8}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B9826CF6-17DD-4D2D-A6BD-F15CFA18F9CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B9826CF6-17DD-4D2D-A6BD-F15CFA18F9CB}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B9826CF6-17DD-4D2D-A6BD-F15CFA18F9CB}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B9826CF6-17DD-4D2D-A6BD-F15CFA18F9CB}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(NestedProjects) = preSolution - {7BA9BC27-1501-47EA-90FF-BCDBD25CA8B8} = {5B74B194-432C-4581-B683-22FB89C46AD3} - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {7B202065-8A02-433F-93A2-248DC8ABB65B} - EndGlobalSection -EndGlobal diff --git a/FlurlX.Http.Xml.sln b/FlurlX.Http.Xml.sln new file mode 100644 index 0000000..7a4c0ae --- /dev/null +++ b/FlurlX.Http.Xml.sln @@ -0,0 +1,40 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34330.188 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F2114366-0322-434F-91D3-A25939F0CFA7}" + ProjectSection(SolutionItems) = preProject + .gitattributes = .gitattributes + .gitignore = .gitignore + appveyor.yml = appveyor.yml + global.json = global.json + README.md = README.md + EndProjectSection +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlurlX.Http.Xml.Tests", "test\FlurlX.Http.Xml.Tests\FlurlX.Http.Xml.Tests.csproj", "{C01421A9-2746-4319-A96B-BEB21E6C349F}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlurlX.Http.Xml", "src\FlurlX.Http.Xml\FlurlX.Http.Xml.csproj", "{A9B5243D-9BB9-4F70-BF1F-A3716A5AF4CD}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C01421A9-2746-4319-A96B-BEB21E6C349F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C01421A9-2746-4319-A96B-BEB21E6C349F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C01421A9-2746-4319-A96B-BEB21E6C349F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C01421A9-2746-4319-A96B-BEB21E6C349F}.Release|Any CPU.Build.0 = Release|Any CPU + {A9B5243D-9BB9-4F70-BF1F-A3716A5AF4CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A9B5243D-9BB9-4F70-BF1F-A3716A5AF4CD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A9B5243D-9BB9-4F70-BF1F-A3716A5AF4CD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A9B5243D-9BB9-4F70-BF1F-A3716A5AF4CD}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7B202065-8A02-433F-93A2-248DC8ABB65B} + EndGlobalSection +EndGlobal diff --git a/global.json b/global.json index 48934da..0396c1f 100644 --- a/global.json +++ b/global.json @@ -1,7 +1,7 @@ { "projects": [ "src", "test" ], "sdk": { - "version": "3.1.202", + "version": "8.0.100", "rollForward": "latestFeature" } } diff --git a/src/Flurl.Http.Xml/FlurlHttpSettingsExtensions.cs b/src/Flurl.Http.Xml/FlurlHttpSettingsExtensions.cs deleted file mode 100644 index 0267663..0000000 --- a/src/Flurl.Http.Xml/FlurlHttpSettingsExtensions.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Text; -using System.Xml; -using Flurl.Http.Configuration; - -namespace Flurl.Http.Xml -{ - /// - /// FlurlHttpSettingsExtensions - /// - public static class FlurlHttpSettingsExtensions - { - private static XmlWriterSettings s_xmlWriterSettings = new XmlWriterSettings { Encoding = new UTF8Encoding(false, false), Indent = true, OmitXmlDeclaration = false }; - private static readonly Lazy s_xmlSerializerInstance = new Lazy(() => new MicrosoftXmlSerializer(s_xmlWriterSettings)); - - /// - /// XMLs the serializer. - /// - /// The settings. - public static MicrosoftXmlSerializer XmlSerializer(this FlurlHttpSettings settings) - { - return settings.XmlSerializer(s_xmlWriterSettings); - } - - /// - /// XMLs the serializer. - /// - /// The settings. - /// The XML writer settings. - // ReSharper disable once UnusedParameter.Local - private static MicrosoftXmlSerializer XmlSerializer(this FlurlHttpSettings settings, XmlWriterSettings xmlWriterSettings) - { - s_xmlWriterSettings = xmlWriterSettings; - return s_xmlSerializerInstance.Value; - } - } -} diff --git a/src/Flurl.Http.Xml/MicrosoftXmlSerializer.cs b/src/Flurl.Http.Xml/MicrosoftXmlSerializer.cs deleted file mode 100644 index 18f7605..0000000 --- a/src/Flurl.Http.Xml/MicrosoftXmlSerializer.cs +++ /dev/null @@ -1,87 +0,0 @@ -using System.IO; -using System.Xml; -using System.Xml.Linq; -using System.Xml.Serialization; -using Flurl.Http.Configuration; - -namespace Flurl.Http.Xml -{ - /// - /// ISerializer implementation that uses Microsoft XmlSerializer. - /// Default serializer used in calls to GetXmlAsync, PostXmlAsync, etc. - /// - public class MicrosoftXmlSerializer : ISerializer - { - private readonly XmlWriterSettings _xmlWriterSettings; - - /// - /// Initializes a new instance of the class. - /// - /// The XML writer settings. - public MicrosoftXmlSerializer(XmlWriterSettings xmlWriterSettings) - { - _xmlWriterSettings = xmlWriterSettings; - } - - /// - /// Serializes an object to a string representation. - /// - /// - public string Serialize(object obj) - { - using (var writer = new Utf8StringWriter()) - using (var xmlWriter = XmlWriter.Create(writer, _xmlWriterSettings)) - { - var serializer = new XmlSerializer(obj.GetType()); - serializer.Serialize(xmlWriter, obj); - return writer.ToString(); - } - } - - private static string TextReaderToString(TextReader reader) - { - using (reader) - { - return reader.ReadToEnd(); - } - } - - private static XDocument StringToXDocument(string s) - { - var doc = XDocument.Parse(s); - if (doc.Declaration == null) - { - doc.Declaration = new XDeclaration("1.0", "utf-8", "yes"); - } - - return doc; - } - - private static T DeserializeInternal(TextReader reader) - { - string xml = TextReaderToString(reader); - var doc = StringToXDocument(xml); - string xmlWithDeclaration = doc.ToStringWithDeclaration(); - - using (var textReader = new StringReader(xmlWithDeclaration)) - { - var serializer = new XmlSerializer(typeof(T)); - return (T)serializer.Deserialize(textReader); - } - } - - /// - /// Deserializes an object from a string representation. - /// - /// - /// The string. - public T Deserialize(string s) => string.IsNullOrWhiteSpace(s) ? default : DeserializeInternal(new StringReader(s)); - - /// - /// Deserializes an object from a stream representation. - /// - /// - /// The stream. - public T Deserialize(Stream stream) => stream == null ? default : DeserializeInternal(new StreamReader(stream)); - } -} \ No newline at end of file diff --git a/src/Flurl.Http.Xml/CapturedXmlContent.cs b/src/FlurlX.Http.Xml/CapturedXmlContent.cs similarity index 96% rename from src/Flurl.Http.Xml/CapturedXmlContent.cs rename to src/FlurlX.Http.Xml/CapturedXmlContent.cs index dd6e1b0..0a10b74 100644 --- a/src/Flurl.Http.Xml/CapturedXmlContent.cs +++ b/src/FlurlX.Http.Xml/CapturedXmlContent.cs @@ -1,6 +1,6 @@ using Flurl.Http.Content; -namespace Flurl.Http.Xml +namespace FlurlX.Http.Xml { /// /// Provides HTTP content based on a serialized XML object, with the XML string captured to a property diff --git a/src/FlurlX.Http.Xml/FlurlHttpSettingsExtensions.cs b/src/FlurlX.Http.Xml/FlurlHttpSettingsExtensions.cs new file mode 100644 index 0000000..26820fd --- /dev/null +++ b/src/FlurlX.Http.Xml/FlurlHttpSettingsExtensions.cs @@ -0,0 +1,23 @@ +using System; +using System.Text; +using System.Xml; + +namespace FlurlX.Http.Xml +{ + /// + /// FlurlHttpSettingsExtensions + /// + public static class FlurlHttpSettingsExtensions + { + private static XmlWriterSettings s_xmlWriterSettings = new XmlWriterSettings { Encoding = new UTF8Encoding(false, false), Indent = true, OmitXmlDeclaration = false }; + private static readonly Lazy s_xmlSerializerInstance = new Lazy(() => new MicrosoftXmlSerializer(s_xmlWriterSettings)); + + /// + /// XMLs the serializer. + /// + public static MicrosoftXmlSerializer XmlSerializer() + { + return s_xmlSerializerInstance.Value; + } + } +} diff --git a/src/Flurl.Http.Xml/FlurlRequestExtensions.cs b/src/FlurlX.Http.Xml/FlurlRequestExtensions.cs similarity index 66% rename from src/Flurl.Http.Xml/FlurlRequestExtensions.cs rename to src/FlurlX.Http.Xml/FlurlRequestExtensions.cs index 689f51b..2a6d7b6 100644 --- a/src/Flurl.Http.Xml/FlurlRequestExtensions.cs +++ b/src/FlurlX.Http.Xml/FlurlRequestExtensions.cs @@ -1,4 +1,5 @@ -using System; +using Flurl.Http; +using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; @@ -7,28 +8,13 @@ using System.Xml; using System.Xml.Linq; -namespace Flurl.Http.Xml +namespace FlurlX.Http.Xml { /// /// FlurlRequestExtensions /// public static class FlurlRequestExtensions { - /// - /// Sends an asynchronous GET request. - /// - /// - /// The request. - /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional. - /// The HttpCompletionOption used in the request. Optional. - /// - /// A Task whose result is the XML response body deserialized to an object of type T. - /// - public static Task GetXmlAsync(this IFlurlRequest request, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) - { - return request.SendAsync(HttpMethod.Get, null, cancellationToken, completionOption).ReceiveXml(); - } /// /// Sends an asynchronous GET request. @@ -40,10 +26,9 @@ public static Task GetXmlAsync(this IFlurlRequest request, /// A Task whose result is the XML response body parsed into an XDocument. /// public static Task GetXDocumentAsync(this IFlurlRequest request, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) - { - return request.SendAsync(HttpMethod.Get, null, cancellationToken, completionOption).ReceiveXDocument(); - } + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => request.SendAsync(HttpMethod.Get, null, cancellationToken: cancellationToken, completionOption: completionOption).ReceiveXDocument(); /// /// Sends an asynchronous GET request. @@ -56,10 +41,9 @@ public static Task GetXDocumentAsync(this IFlurlRequest request, /// A Task whose result is the XML response body parsed into a collection of XElements. /// public static Task> GetXElementsFromXPath(this IFlurlRequest request, string expression, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) - { - return request.SendAsync(HttpMethod.Get, null, cancellationToken, completionOption).ReceiveXElementsFromXPath(expression); - } + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => request.SendAsync(HttpMethod.Get, null, cancellationToken: cancellationToken, completionOption: completionOption).ReceiveXElementsFromXPath(expression); /// /// Sends an asynchronous GET request. @@ -72,22 +56,25 @@ public static Task> GetXElementsFromXPath(this IFlurlReque /// /// A Task whose result is the XML response body parsed into a collection of XElements. /// - public static Task> GetXElementsFromXPath(this IFlurlRequest request, string expression, IXmlNamespaceResolver resolver, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) - { - return request.SendAsync(HttpMethod.Get, null, cancellationToken, completionOption).ReceiveXElementsFromXPath(expression, resolver); - } + public static Task> GetXElementsFromXPath(this IFlurlRequest request, + string expression, + IXmlNamespaceResolver resolver, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => request.SendAsync(HttpMethod.Get, null, cancellationToken: cancellationToken, completionOption: completionOption).ReceiveXElementsFromXPath(expression, resolver); - private static string GetMediaType(this IHttpSettingsContainer request) + private static string GetMediaType(this IHeadersContainer request) { + const string defaultDediaType = "application/xml"; + var acceptHeaders = request.Headers .Where(x => x.Name == "Accept") .ToList(); - if (!acceptHeaders.Any() || acceptHeaders.All(x => x.Value == null)) + if (acceptHeaders.Count == 0 || acceptHeaders.All(x => x.Value == null)) { // no accepted media type present, return default - return "application/xml"; + return defaultDediaType; } // return media type of first accepted media type containing "xml", else of first accepted media type @@ -97,10 +84,27 @@ private static string GetMediaType(this IHttpSettingsContainer request) .Select(x => x.Trim()) .ToList(); - return mediaTypes.FirstOrDefault(x => x.IndexOf("xml", StringComparison.OrdinalIgnoreCase) >= 0) + string? result = mediaTypes.FirstOrDefault(x => x.Contains("xml", StringComparison.OrdinalIgnoreCase)) ?? mediaTypes.FirstOrDefault(); + + return result ?? defaultDediaType; } + /// + /// Sends an asynchronous GET request. + /// + /// + /// The request. + /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional. + /// The HttpCompletionOption used in the request. Optional. + /// + /// A Task whose result is the XML response body deserialized to an object of type T. + /// + public static Task GetXmlAsync(this IFlurlRequest request, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => request.SendAsync(HttpMethod.Get, null, cancellationToken: cancellationToken, completionOption: completionOption).ReceiveXml(); + /// /// Sends an asynchronous HTTP request. /// @@ -112,11 +116,15 @@ private static string GetMediaType(this IHttpSettingsContainer request) /// /// A Task whose result is the received IFlurlResponse. /// - public static Task SendXmlAsync(this IFlurlRequest request, HttpMethod httpMethod, object data, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) + public static Task SendXmlAsync(this IFlurlRequest request, + HttpMethod httpMethod, + object data, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) { - var content = new CapturedXmlContent(request.Settings.XmlSerializer().Serialize(data), request.GetMediaType()); - return request.SendAsync(httpMethod, content, cancellationToken, completionOption); + var content = new CapturedXmlContent(FlurlHttpSettingsExtensions.XmlSerializer().Serialize(data), request.GetMediaType()); + return request.SendAsync(httpMethod, content, cancellationToken: cancellationToken, completionOption: completionOption); } /// @@ -129,9 +137,11 @@ public static Task SendXmlAsync(this IFlurlRequest request, Http /// /// A Task whose result is the received IFlurlResponse. /// - public static Task PostXmlAsync(this IFlurlRequest request, object data, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - SendXmlAsync(request, HttpMethod.Post, data, cancellationToken, completionOption); + public static Task PostXmlAsync(this IFlurlRequest request, + object data, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => SendXmlAsync(request, HttpMethod.Post, data, completionOption, cancellationToken); /// /// Sends an asynchronous PUT request. @@ -143,8 +153,10 @@ public static Task PostXmlAsync(this IFlurlRequest request, obje /// /// A Task whose result is the received IFlurlResponse. /// - public static Task PutXmlAsync(this IFlurlRequest request, object data, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - SendXmlAsync(request, HttpMethod.Put, data, cancellationToken, completionOption); + public static Task PutXmlAsync(this IFlurlRequest request, + object data, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => SendXmlAsync(request, HttpMethod.Put, data, completionOption, cancellationToken); } } diff --git a/src/Flurl.Http.Xml/Flurl.Http.Xml.csproj b/src/FlurlX.Http.Xml/FlurlX.Http.Xml.csproj similarity index 52% rename from src/Flurl.Http.Xml/Flurl.Http.Xml.csproj rename to src/FlurlX.Http.Xml/FlurlX.Http.Xml.csproj index 75469b3..c56e071 100644 --- a/src/Flurl.Http.Xml/Flurl.Http.Xml.csproj +++ b/src/FlurlX.Http.Xml/FlurlX.Http.Xml.csproj @@ -1,24 +1,41 @@  - netstandard2.0;netstandard2.1;net461;net472 + net8.0 false false Luk Vermeulen - https://github.com/lvermeulen/Flurl.Http.Xml/blob/master/LICENSE - https://github.com/lvermeulen/Flurl.Http.Xml + https://github.com/renanaragao/FlurlX.Http.Xml/blob/master/LICENSE + https://github.com/renanaragao/FlurlX.Http.Xml Copyright 2016-2020 by Luk Vermeulen. All rights reserved. - https://github.com/lvermeulen/Flurl.Http.Xml + https://github.com/renanaragao/FlurlX.Http.Xml git Flurl;Http;Xml http://i.imgur.com/llEKpRL.png?1 XML extensions to Flurl.Http true true + enable + + + + 9999 + + + + 9999 + + + + 9999 + + + + 9999 - + diff --git a/src/Flurl.Http.Xml/HttpResponseMessageExtensions.cs b/src/FlurlX.Http.Xml/HttpResponseMessageExtensions.cs similarity index 73% rename from src/Flurl.Http.Xml/HttpResponseMessageExtensions.cs rename to src/FlurlX.Http.Xml/HttpResponseMessageExtensions.cs index ba6a7ff..5440b7b 100644 --- a/src/Flurl.Http.Xml/HttpResponseMessageExtensions.cs +++ b/src/FlurlX.Http.Xml/HttpResponseMessageExtensions.cs @@ -1,4 +1,5 @@ -using System; +using Flurl.Http; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -9,35 +10,37 @@ using System.Xml.Linq; using System.Xml.XPath; -namespace Flurl.Http.Xml +namespace FlurlX.Http.Xml { /// /// IFlurlResponseExtensions /// public static class IFlurlResponseExtensions { - private static FlurlCall GetHttpCall(HttpRequestMessage request) + private static FlurlCall? GetHttpCall(HttpRequestMessage? request) { - if (request?.Properties != null && request.Properties.TryGetValue("FlurlHttpCall", out var obj) && obj is FlurlCall call) + if (request?.Options != null && request.Options.TryGetValue(new HttpRequestOptionsKey("FlurlHttpCall"), out object? obj) && obj is FlurlCall call) { return call; } return null; } - private static string GetMediaType(HttpRequestMessage request) + private static string GetMediaType(HttpRequestMessage? request) { - if (request.Headers.Accept.Any()) + const string defaultMediaType = "application/xml"; + + if (request?.Headers.Accept.Count != 0) { // return media type of first accepted media type containing "xml", else of first accepted media type - var acceptHeader = request.Headers.Accept.FirstOrDefault(x => x.MediaType.IndexOf("xml", StringComparison.OrdinalIgnoreCase) >= 0) - ?? request.Headers.Accept.FirstOrDefault(); + var acceptHeader = request?.Headers.Accept.FirstOrDefault(x => x.MediaType?.IndexOf("xml", StringComparison.OrdinalIgnoreCase) >= 0) + ?? request?.Headers.Accept.FirstOrDefault(); - return acceptHeader?.MediaType; + return acceptHeader?.MediaType ?? defaultMediaType; } // no accepted media type present, return default - return "application/xml"; + return defaultMediaType; } /// @@ -54,17 +57,15 @@ public static async Task ReceiveXmlResponseMessage(this Task ReceiveFromXmlStream(this Task response, Func streamHandler) + private static async Task ReceiveFromXmlStream(this Task response, Func streamHandler) { var resp = await ReceiveXmlResponseMessage(response); var call = GetHttpCall(resp.ResponseMessage.RequestMessage); try { - using (var stream = await resp.ResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false)) - { - return streamHandler(call, stream); - } + using var stream = await resp.ResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false); + return streamHandler(call, stream); } catch (Exception ex) { @@ -80,11 +81,8 @@ private static async Task ReceiveFromXmlStream(this Task r /// The response. /// A Task whose result is an object containing data in the response body. /// x = await url.PostAsync(data).ReceiveXml<T>() - public static async Task ReceiveXml(this Task response) - { - return await ReceiveFromXmlStream(response, (call, stm) => - call.Request.Settings.XmlSerializer().Deserialize(stm)); - } + public static Task ReceiveXml(this Task response) + => ReceiveFromXmlStream(response, (call, stm) => FlurlHttpSettingsExtensions.XmlSerializer().Deserialize(stm)); /// /// Parses XML-formatted HTTP response body into an XDocument. Intended to chain off an async call. @@ -92,16 +90,12 @@ public static async Task ReceiveXml(this Task response) /// The response. /// A Task whose result is an XDocument containing XML data from the response body. /// d = await url.PostAsync(data).ReceiveXDocument() - public static async Task ReceiveXDocument(this Task response) + public static Task ReceiveXDocument(this Task response) + => ReceiveFromXmlStream(response, (call, stm) => { - return await ReceiveFromXmlStream(response, (call, stm) => - { - using (var streamReader = new StreamReader(stm)) - { - return XDocument.Parse(streamReader.ReadToEnd()); - } - }); - } + using var streamReader = new StreamReader(stm); + return XDocument.Parse(streamReader.ReadToEnd()); + }); /// /// Parses XML-formatted HTTP response body into a collection of XElements. Intended to chain off an async call. diff --git a/src/FlurlX.Http.Xml/MicrosoftXmlSerializer.cs b/src/FlurlX.Http.Xml/MicrosoftXmlSerializer.cs new file mode 100644 index 0000000..c74ee67 --- /dev/null +++ b/src/FlurlX.Http.Xml/MicrosoftXmlSerializer.cs @@ -0,0 +1,75 @@ +using System.IO; +using System.Xml; +using System.Xml.Linq; +using System.Xml.Serialization; +using Flurl.Http.Configuration; + +namespace FlurlX.Http.Xml +{ + /// + /// ISerializer implementation that uses Microsoft XmlSerializer. + /// Default serializer used in calls to GetXmlAsync, PostXmlAsync, etc. + /// + /// + /// Initializes a new instance of the class. + /// + /// The XML writer settings. + public class MicrosoftXmlSerializer(XmlWriterSettings xmlWriterSettings) : ISerializer + { + private readonly XmlWriterSettings _xmlWriterSettings = xmlWriterSettings; + + /// + /// Serializes an object to a string representation. + /// + /// + public string Serialize(object obj) + { + using var writer = new Utf8StringWriter(); + using var xmlWriter = XmlWriter.Create(writer, _xmlWriterSettings); + var serializer = new XmlSerializer(obj.GetType()); + serializer.Serialize(xmlWriter, obj); + return writer.ToString(); + } + + private static string TextReaderToString(TextReader reader) + { + using (reader) + { + return reader.ReadToEnd(); + } + } + + private static XDocument StringToXDocument(string s) + { + var doc = XDocument.Parse(s); + doc.Declaration ??= new XDeclaration("1.0", "utf-8", "yes"); + + return doc; + } + + private static T? DeserializeInternal(TextReader reader) + { + string xml = TextReaderToString(reader); + var doc = StringToXDocument(xml); + string xmlWithDeclaration = doc.ToStringWithDeclaration(); + + using var textReader = new StringReader(xmlWithDeclaration); + var serializer = new XmlSerializer(typeof(T)); + return (T?)serializer.Deserialize(textReader); + } + + /// + /// Deserializes an object from a string representation. + /// + /// + /// The string. + public T? Deserialize(string s) => string.IsNullOrWhiteSpace(s) ? default : DeserializeInternal(new StringReader(s)); + + /// + /// Deserializes an object from a stream representation. + /// + /// + /// The stream. + public T? Deserialize(Stream stream) => stream == null ? default : DeserializeInternal(new StreamReader(stream)); + } +} \ No newline at end of file diff --git a/src/Flurl.Http.Xml/StringExtensions.cs b/src/FlurlX.Http.Xml/StringExtensions.cs similarity index 70% rename from src/Flurl.Http.Xml/StringExtensions.cs rename to src/FlurlX.Http.Xml/StringExtensions.cs index f130290..a7b72fe 100644 --- a/src/Flurl.Http.Xml/StringExtensions.cs +++ b/src/FlurlX.Http.Xml/StringExtensions.cs @@ -1,11 +1,12 @@ -using System.Collections.Generic; +using Flurl.Http; +using System.Collections.Generic; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; -namespace Flurl.Http.Xml +namespace FlurlX.Http.Xml { /// /// StringExtensions @@ -22,9 +23,10 @@ public static class StringExtensions /// /// A Task whose result is the XML response body deserialized to an object of type T. /// - public static Task GetXmlAsync(this string url, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - new FlurlRequest(url).GetXmlAsync(cancellationToken, completionOption); + public static Task GetXmlAsync(this string url, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => new FlurlRequest(url).GetXmlAsync(completionOption, cancellationToken); /// /// Sends an asynchronous GET request. @@ -33,9 +35,10 @@ public static Task GetXmlAsync(this string url, /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional. /// The HttpCompletionOption used in the request. Optional. /// A Task whose result is the XML response body parsed into an XDocument. - public static Task GetXDocumentAsync(this string url, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - new FlurlRequest(url).GetXDocumentAsync(cancellationToken, completionOption); + public static Task GetXDocumentAsync(this string url, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => new FlurlRequest(url).GetXDocumentAsync(completionOption, cancellationToken); /// /// Sends an asynchronous GET request. @@ -45,9 +48,10 @@ public static Task GetXDocumentAsync(this string url, /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional. /// The HttpCompletionOption used in the request. Optional. /// A Task whose result is the XML response body parsed into a collection of XElements. - public static Task> GetXElementsFromXPath(this string url, string expression, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - new FlurlRequest(url).GetXElementsFromXPath(expression, cancellationToken, completionOption); + public static Task> GetXElementsFromXPath(this string url, string expression, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => new FlurlRequest(url).GetXElementsFromXPath(expression, completionOption, cancellationToken); /// /// Sends an asynchronous GET request. @@ -58,9 +62,10 @@ public static Task> GetXElementsFromXPath(this string url, /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional. /// The HttpCompletionOption used in the request. Optional. /// A Task whose result is the XML response body parsed into a collection of XElements. - public static Task> GetXElementsFromXPath(this string url, string expression, IXmlNamespaceResolver resolver, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - new FlurlRequest(url).GetXElementsFromXPath(expression, resolver, cancellationToken, completionOption); + public static Task> GetXElementsFromXPath(this string url, string expression, IXmlNamespaceResolver resolver, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => new FlurlRequest(url).GetXElementsFromXPath(expression, resolver, completionOption, cancellationToken); /// /// Creates a FlurlClient from the URL and sends an asynchronous HTTP request. @@ -71,9 +76,10 @@ public static Task> GetXElementsFromXPath(this string url, /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional. /// The HttpCompletionOption used in the request. Optional. /// A Task whose result is the received IFlurlResponse. - public static Task SendXmlAsync(this string url, HttpMethod method, object data, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - new FlurlRequest(url).SendXmlAsync(method, data, cancellationToken, completionOption); + public static Task SendXmlAsync(this string url, HttpMethod method, object data, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => new FlurlRequest(url).SendXmlAsync(method, data, completionOption, cancellationToken); /// /// Creates a FlurlClient from the URL and sends an asynchronous POST request. @@ -83,9 +89,10 @@ public static Task SendXmlAsync(this string url, HttpMethod meth /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional. /// The HttpCompletionOption used in the request. Optional. /// A Task whose result is the received IFlurlResponse. - public static Task PostXmlAsync(this string url, object data, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - new FlurlRequest(url).PostXmlAsync(data, cancellationToken, completionOption); + public static Task PostXmlAsync(this string url, object data, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => new FlurlRequest(url).PostXmlAsync(data, completionOption, cancellationToken); /// /// Creates a FlurlClient from the URL and sends an asynchronous PUT request. @@ -95,8 +102,9 @@ public static Task PostXmlAsync(this string url, object data, /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional. /// The HttpCompletionOption used in the request. Optional. /// A Task whose result is the received IFlurlResponse. - public static Task PutXmlAsync(this string url, object data, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - new FlurlRequest(url).PutXmlAsync(data, cancellationToken, completionOption); + public static Task PutXmlAsync(this string url, object data, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => new FlurlRequest(url).PutXmlAsync(data, completionOption, cancellationToken); } } \ No newline at end of file diff --git a/src/Flurl.Http.Xml/UrlExtensions.cs b/src/FlurlX.Http.Xml/UrlExtensions.cs similarity index 68% rename from src/Flurl.Http.Xml/UrlExtensions.cs rename to src/FlurlX.Http.Xml/UrlExtensions.cs index d15f157..0e4b7a0 100644 --- a/src/Flurl.Http.Xml/UrlExtensions.cs +++ b/src/FlurlX.Http.Xml/UrlExtensions.cs @@ -1,11 +1,13 @@ -using System.Collections.Generic; +using Flurl; +using Flurl.Http; +using System.Collections.Generic; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; -namespace Flurl.Http.Xml +namespace FlurlX.Http.Xml { /// /// UrlExtensions @@ -22,9 +24,10 @@ public static class UrlExtensions /// /// A Task whose result is the XML response body deserialized to an object of type T. /// - public static Task GetXmlAsync(this Url url, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - new FlurlRequest(url).GetXmlAsync(cancellationToken, completionOption); + public static Task GetXmlAsync(this Url url, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => new FlurlRequest(url).GetXmlAsync(completionOption, cancellationToken); /// /// Sends an asynchronous GET request. @@ -33,9 +36,10 @@ public static Task GetXmlAsync(this Url url, /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional. /// The HttpCompletionOption used in the request. Optional. /// A Task whose result is the XML response body parsed into an XDocument. - public static Task GetXDocumentAsync(this Url url, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - new FlurlRequest(url).GetXDocumentAsync(cancellationToken, completionOption); + public static Task GetXDocumentAsync(this Url url, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => new FlurlRequest(url).GetXDocumentAsync(completionOption, cancellationToken); /// /// Sends an asynchronous GET request. @@ -45,9 +49,11 @@ public static Task GetXDocumentAsync(this Url url, /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional. /// The HttpCompletionOption used in the request. Optional. /// A Task whose result is the XML response body parsed into a collection of XElements. - public static Task> GetXElementsFromXPath(this Url url, string expression, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - new FlurlRequest(url).GetXElementsFromXPath(expression, cancellationToken, completionOption); + public static Task> GetXElementsFromXPath(this Url url, + string expression, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => new FlurlRequest(url).GetXElementsFromXPath(expression, completionOption, cancellationToken); /// /// Sends an asynchronous GET request. @@ -58,9 +64,12 @@ public static Task> GetXElementsFromXPath(this Url url, st /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional. /// The HttpCompletionOption used in the request. Optional. /// A Task whose result is the XML response body parsed into a collection of XElements. - public static Task> GetXElementsFromXPath(this Url url, string expression, IXmlNamespaceResolver resolver, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - new FlurlRequest(url).GetXElementsFromXPath(expression, resolver, cancellationToken, completionOption); + public static Task> GetXElementsFromXPath(this Url url, + string expression, + IXmlNamespaceResolver resolver, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => new FlurlRequest(url).GetXElementsFromXPath(expression, resolver, completionOption, cancellationToken); /// /// Creates a FlurlClient from the URL and sends an asynchronous HTTP request. @@ -71,9 +80,12 @@ public static Task> GetXElementsFromXPath(this Url url, st /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional. /// The HttpCompletionOption used in the request. Optional. /// A Task whose result is the received IFlurlResponse. - public static Task SendXmlAsync(this Url url, HttpMethod method, object data, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - new FlurlRequest(url).SendXmlAsync(method, data, cancellationToken, completionOption); + public static Task SendXmlAsync(this Url url, + HttpMethod method, + object data, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => new FlurlRequest(url).SendXmlAsync(method, data, completionOption, cancellationToken); /// /// Creates a FlurlClient from the URL and sends an asynchronous POST request. @@ -83,9 +95,11 @@ public static Task SendXmlAsync(this Url url, HttpMethod method, /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional. /// The HttpCompletionOption used in the request. Optional. /// A Task whose result is the received IFlurlResponse. - public static Task PostXmlAsync(this Url url, object data, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - new FlurlRequest(url).PostXmlAsync(data, cancellationToken, completionOption); + public static Task PostXmlAsync(this Url url, + object data, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => new FlurlRequest(url).PostXmlAsync(data, completionOption, cancellationToken); /// /// Creates a FlurlClient from the URL and sends an asynchronous PUT request. @@ -95,8 +109,10 @@ public static Task PostXmlAsync(this Url url, object data, /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional. /// The HttpCompletionOption used in the request. Optional. /// A Task whose result is the received IFlurlResponse. - public static Task PutXmlAsync(this Url url, object data, - CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) => - new FlurlRequest(url).PutXmlAsync(data, cancellationToken, completionOption); + public static Task PutXmlAsync(this Url url, + object data, + HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, + CancellationToken cancellationToken = default + ) => new FlurlRequest(url).PutXmlAsync(data, completionOption, cancellationToken); } } \ No newline at end of file diff --git a/src/Flurl.Http.Xml/Utf8StringWriter.cs b/src/FlurlX.Http.Xml/Utf8StringWriter.cs similarity index 97% rename from src/Flurl.Http.Xml/Utf8StringWriter.cs rename to src/FlurlX.Http.Xml/Utf8StringWriter.cs index 4ec134b..60eb838 100644 --- a/src/Flurl.Http.Xml/Utf8StringWriter.cs +++ b/src/FlurlX.Http.Xml/Utf8StringWriter.cs @@ -1,7 +1,7 @@ using System.IO; using System.Text; -namespace Flurl.Http.Xml +namespace FlurlX.Http.Xml { /// /// Utf8StringWriter diff --git a/src/Flurl.Http.Xml/XDocumentExtensions.cs b/src/FlurlX.Http.Xml/XDocumentExtensions.cs similarity index 95% rename from src/Flurl.Http.Xml/XDocumentExtensions.cs rename to src/FlurlX.Http.Xml/XDocumentExtensions.cs index bd0870f..eb3ba13 100644 --- a/src/Flurl.Http.Xml/XDocumentExtensions.cs +++ b/src/FlurlX.Http.Xml/XDocumentExtensions.cs @@ -1,7 +1,7 @@ using System.Text; using System.Xml.Linq; -namespace Flurl.Http.Xml +namespace FlurlX.Http.Xml { /// /// XDocumentExtensions diff --git a/test/Flurl.Http.Xml.Tests/Factories/EchoHttpClientFactory.cs b/test/Flurl.Http.Xml.Tests/Factories/EchoHttpClientFactory.cs deleted file mode 100644 index 97fea24..0000000 --- a/test/Flurl.Http.Xml.Tests/Factories/EchoHttpClientFactory.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System.IO; -using System.Net.Http; -using System.Text; -using Flurl.Http.Configuration; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.TestHost; - -namespace Flurl.Http.Xml.Tests.Factories -{ - public class EchoHttpClientFactory : DefaultHttpClientFactory - { - private string HttpRequestBodyToString(HttpRequest httpRequest) - { - string result; - - httpRequest.EnableBuffering(); - - using (var reader = new StreamReader(httpRequest.Body, Encoding.UTF8, true, 1024, true)) - { - result = reader.ReadToEnd(); - } - - httpRequest.Body.Position = 0; - - return result; - } - - private HttpClient GetClient() - { - var builder = new WebHostBuilder().Configure(app => - { - app.Use(async (context, next) => - { - string requestBody = HttpRequestBodyToString(context.Request); - await context.Response.WriteAsync(requestBody); - }); - }); - - var server = new TestServer(builder) { AllowSynchronousIO = true }; - return server.CreateClient(); - } - - public override HttpClient CreateHttpClient(HttpMessageHandler handler) => GetClient(); - } -} \ No newline at end of file diff --git a/test/Flurl.Http.Xml.Tests/Factories/JsonTestModelHttpClientFactory.cs b/test/Flurl.Http.Xml.Tests/Factories/JsonTestModelHttpClientFactory.cs deleted file mode 100644 index 73c2d7e..0000000 --- a/test/Flurl.Http.Xml.Tests/Factories/JsonTestModelHttpClientFactory.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Flurl.Http.Xml.Tests.Factories -{ - public class JsonTestModelHttpClientFactory : BaseTestModelClientFactory - { - private const string REQUEST_BODY_JSON = @"{ - ""number"": 3, - ""text"": ""Test"" -}"; - - public JsonTestModelHttpClientFactory() - : base(REQUEST_BODY_JSON) - { } - } -} \ No newline at end of file diff --git a/test/Flurl.Http.Xml.Tests/Factories/XmlTestModelHttpClientFactory.cs b/test/Flurl.Http.Xml.Tests/Factories/XmlTestModelHttpClientFactory.cs deleted file mode 100644 index f4569c4..0000000 --- a/test/Flurl.Http.Xml.Tests/Factories/XmlTestModelHttpClientFactory.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Flurl.Http.Xml.Tests.Factories -{ - public class XmlTestModelHttpClientFactory : BaseTestModelClientFactory - { - private const string REQUEST_BODY_XML = @" - - 3 - Test -"; - - public XmlTestModelHttpClientFactory() - : base(REQUEST_BODY_XML) - { } - } -} \ No newline at end of file diff --git a/test/Flurl.Http.Xml.Tests/TestBase.cs b/test/Flurl.Http.Xml.Tests/TestBase.cs deleted file mode 100644 index 1a5e683..0000000 --- a/test/Flurl.Http.Xml.Tests/TestBase.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Collections.Generic; -using System.Net.Http; - -namespace Flurl.Http.Xml.Tests -{ - public abstract class TestBase - { - public enum HttpMethodTypes - { - Post, - Put - } - - protected readonly Dictionary HttpMethodByType = new Dictionary - { - [HttpMethodTypes.Post] = HttpMethod.Post, - [HttpMethodTypes.Put] = HttpMethod.Put - }; - } -} diff --git a/test/Flurl.Http.Xml.Tests/AssemblyInfo.cs b/test/FlurlX.Http.Xml.Tests/AssemblyInfo.cs similarity index 100% rename from test/Flurl.Http.Xml.Tests/AssemblyInfo.cs rename to test/FlurlX.Http.Xml.Tests/AssemblyInfo.cs diff --git a/test/Flurl.Http.Xml.Tests/Flurl.Http.Xml.Tests.xunit.runner.json b/test/FlurlX.Http.Xml.Tests/Flurl.Http.Xml.Tests.xunit.runner.json similarity index 100% rename from test/Flurl.Http.Xml.Tests/Flurl.Http.Xml.Tests.xunit.runner.json rename to test/FlurlX.Http.Xml.Tests/Flurl.Http.Xml.Tests.xunit.runner.json diff --git a/test/FlurlX.Http.Xml.Tests/FlurlX.Http.Xml.Tests.csproj b/test/FlurlX.Http.Xml.Tests/FlurlX.Http.Xml.Tests.csproj new file mode 100644 index 0000000..8647aa0 --- /dev/null +++ b/test/FlurlX.Http.Xml.Tests/FlurlX.Http.Xml.Tests.csproj @@ -0,0 +1,35 @@ + + + + net8.0 + FlurlX.Http.Xml.Tests + FlurlX.Http.Xml.Tests + true + + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers + + + + + + + + + + Always + + + + diff --git a/test/Flurl.Http.Xml.Tests/MicrosoftXmlSerializerShould.cs b/test/FlurlX.Http.Xml.Tests/MicrosoftXmlSerializerShould.cs similarity index 90% rename from test/Flurl.Http.Xml.Tests/MicrosoftXmlSerializerShould.cs rename to test/FlurlX.Http.Xml.Tests/MicrosoftXmlSerializerShould.cs index ef94d95..a6f633c 100644 --- a/test/Flurl.Http.Xml.Tests/MicrosoftXmlSerializerShould.cs +++ b/test/FlurlX.Http.Xml.Tests/MicrosoftXmlSerializerShould.cs @@ -1,17 +1,17 @@ using System.Text; using System.Xml; -using Flurl.Http.Xml.Tests.Models; +using FlurlX.Http.Xml.Tests.Models; using Xunit; -namespace Flurl.Http.Xml.Tests +namespace FlurlX.Http.Xml.Tests { public class MicrosoftXmlSerializerShould { const string XML_WITHOUT_DECLARATION = @" - 3 - Test -"; + 3 + Test + "; const string XML_WITH_DECLARATION = @"" + "\r\n" + XML_WITHOUT_DECLARATION; diff --git a/test/Flurl.Http.Xml.Tests/Models/TestModel.cs b/test/FlurlX.Http.Xml.Tests/Models/TestModel.cs similarity index 74% rename from test/Flurl.Http.Xml.Tests/Models/TestModel.cs rename to test/FlurlX.Http.Xml.Tests/Models/TestModel.cs index f5fc63f..cfba8cf 100644 --- a/test/Flurl.Http.Xml.Tests/Models/TestModel.cs +++ b/test/FlurlX.Http.Xml.Tests/Models/TestModel.cs @@ -1,4 +1,4 @@ -namespace Flurl.Http.Xml.Tests.Models +namespace FlurlX.Http.Xml.Tests.Models { public class TestModel { diff --git a/test/Flurl.Http.Xml.Tests/RequestExtensionsShould.cs b/test/FlurlX.Http.Xml.Tests/RequestExtensionsShould.cs similarity index 78% rename from test/Flurl.Http.Xml.Tests/RequestExtensionsShould.cs rename to test/FlurlX.Http.Xml.Tests/RequestExtensionsShould.cs index 0fcfe17..b50962d 100644 --- a/test/Flurl.Http.Xml.Tests/RequestExtensionsShould.cs +++ b/test/FlurlX.Http.Xml.Tests/RequestExtensionsShould.cs @@ -2,22 +2,24 @@ using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; -using Flurl.Http.Xml.Tests.Factories; -using Flurl.Http.Xml.Tests.Models; +using Flurl; +using Flurl.Http; +using Flurl.Http.Testing; +using FlurlX.Http.Xml.Tests.Models; using Xunit; using Xunit.Extensions.Ordering; -namespace Flurl.Http.Xml.Tests +namespace FlurlX.Http.Xml.Tests { public class RequestExtensionsShould : TestBase { - private void AssertTestModel(TestModel testModel, int expectedNumber, string expectedText) + private static void AssertTestModel(TestModel testModel, int expectedNumber, string expectedText) { Assert.Equal(expectedNumber, testModel.Number); Assert.Equal(expectedText, testModel.Text); } - private void AssertXDocument(XDocument document, int expectedNumber, string expectedText) + private static void AssertXDocument(XDocument document, int expectedNumber, string expectedText) { Assert.Equal(expectedNumber.ToString(), document?.Element("TestModel")?.Element("Number")?.Value); Assert.Equal(expectedText, document?.Element("TestModel")?.Element("Text")?.Value); @@ -26,11 +28,13 @@ private void AssertXDocument(XDocument document, int expectedNumber, string expe [Fact, Order(1)] public async Task GetXmlAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory()); + using var httpTest = new HttpTest(); - var result = await new Url("https://some.url") + httpTest.RespondWith(REQUEST_BODY_XML); + + var result = await FlurlRequestExtensions.GetXmlAsync(new Url("https://some.url") .AllowAnyHttpStatus() - .GetXmlAsync(); +); AssertTestModel(result, 3, "Test"); } @@ -38,7 +42,9 @@ public async Task GetXmlAsync() [Fact, Order(1)] public async Task GetXDocumentAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .AllowAnyHttpStatus() @@ -50,7 +56,9 @@ public async Task GetXDocumentAsync() [Fact, Order(1)] public async Task GetXElementsFromXPathAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .AllowAnyHttpStatus() @@ -62,7 +70,9 @@ public async Task GetXElementsFromXPathAsync() [Fact, Order(1)] public async Task GetXElementsFromXPathNamespaceResolverAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .AllowAnyHttpStatus() @@ -76,7 +86,9 @@ public async Task GetXElementsFromXPathNamespaceResolverAsync() [InlineData(HttpMethodTypes.Put)] public async Task SendXmlToModelAsync(HttpMethodTypes methodType) { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var method = HttpMethodByType[methodType]; var result = await new Url("https://some.url") @@ -92,7 +104,9 @@ public async Task SendXmlToModelAsync(HttpMethodTypes methodType) [InlineData(HttpMethodTypes.Put)] public async Task SendXmlToXDocumentAsync(HttpMethodTypes methodType) { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var method = HttpMethodByType[methodType]; var result = await new Url("https://some.url") @@ -106,7 +120,9 @@ public async Task SendXmlToXDocumentAsync(HttpMethodTypes methodType) [Fact] public async Task PostXmlToModelAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .AllowAnyHttpStatus() @@ -119,7 +135,9 @@ public async Task PostXmlToModelAsync() [Fact] public async Task PostXmlToXDocumentAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .AllowAnyHttpStatus() @@ -132,7 +150,9 @@ public async Task PostXmlToXDocumentAsync() [Fact] public async Task PutXmlToModelAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .AllowAnyHttpStatus() @@ -145,7 +165,9 @@ public async Task PutXmlToModelAsync() [Fact] public async Task PutXmlToXDocumentAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .AllowAnyHttpStatus() @@ -163,7 +185,9 @@ public async Task PutXmlToXDocumentAsync() [InlineData("Accept", null, "application/xml")] public async Task ReceiveCorrectMediaType(string headerName, string acceptMediaType, string expectedContentType) { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .WithHeader(headerName, acceptMediaType) @@ -181,7 +205,9 @@ public async Task ReceiveCorrectMediaType(string headerName, string acceptMediaT [InlineData("Accept", null)] public async Task ReceiveCorrectMediaTypeWithXmlResponse(string headerName, string acceptMediaType) { - FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .WithHeader(headerName, acceptMediaType) @@ -199,7 +225,9 @@ public async Task ReceiveCorrectMediaTypeWithXmlResponse(string headerName, stri [InlineData("Accept", null)] public async Task ReceiveCorrectMediaTypeWithJsonResponse(string headerName, string acceptMediaType) { - FlurlHttp.Configure(c => c.HttpClientFactory = new JsonTestModelHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_JSON); var result = await new Url("https://some.url") .WithHeader(headerName, acceptMediaType) diff --git a/test/Flurl.Http.Xml.Tests/StringExtensionsShould.cs b/test/FlurlX.Http.Xml.Tests/StringExtensionsShould.cs similarity index 74% rename from test/Flurl.Http.Xml.Tests/StringExtensionsShould.cs rename to test/FlurlX.Http.Xml.Tests/StringExtensionsShould.cs index 54738f9..5ca1269 100644 --- a/test/Flurl.Http.Xml.Tests/StringExtensionsShould.cs +++ b/test/FlurlX.Http.Xml.Tests/StringExtensionsShould.cs @@ -2,22 +2,22 @@ using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; -using Flurl.Http.Xml.Tests.Factories; -using Flurl.Http.Xml.Tests.Models; +using Flurl.Http.Testing; +using FlurlX.Http.Xml.Tests.Models; using Xunit; using Xunit.Extensions.Ordering; -namespace Flurl.Http.Xml.Tests +namespace FlurlX.Http.Xml.Tests { public class StringExtensionsShould : TestBase { - private void AssertTestModel(TestModel testModel, int expectedNumber, string expectedText) + private static void AssertTestModel(TestModel testModel, int expectedNumber, string expectedText) { Assert.Equal(expectedNumber, testModel.Number); Assert.Equal(expectedText, testModel.Text); } - private void AssertXDocument(XDocument document, int expectedNumber, string expectedText) + private static void AssertXDocument(XDocument document, int expectedNumber, string expectedText) { Assert.Equal(expectedNumber.ToString(), document?.Element("TestModel")?.Element("Number")?.Value); Assert.Equal(expectedText, document?.Element("TestModel")?.Element("Text")?.Value); @@ -26,7 +26,9 @@ private void AssertXDocument(XDocument document, int expectedNumber, string expe [Fact, Order(2)] public async Task GetXmlAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await "https://some.url" .GetXmlAsync(); @@ -37,7 +39,9 @@ public async Task GetXmlAsync() [Fact, Order(2)] public async Task GetXDocumentAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await "https://some.url" .GetXDocumentAsync(); @@ -48,7 +52,9 @@ public async Task GetXDocumentAsync() [Fact, Order(2)] public async Task GetXElementsFromXPathAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await "https://some.url" .GetXElementsFromXPath("/TestModel"); @@ -59,7 +65,9 @@ public async Task GetXElementsFromXPathAsync() [Fact, Order(2)] public async Task GetXElementsFromXPathNamespaceResolverAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await "https://some.url" .GetXElementsFromXPath("/TestModel", new XmlNamespaceManager(new NameTable())); @@ -72,7 +80,9 @@ public async Task GetXElementsFromXPathNamespaceResolverAsync() [InlineData(HttpMethodTypes.Put)] public async Task SendXmlToModelAsync(HttpMethodTypes methodType) { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var method = HttpMethodByType[methodType]; var result = await "http://some.url" @@ -87,7 +97,9 @@ public async Task SendXmlToModelAsync(HttpMethodTypes methodType) [InlineData(HttpMethodTypes.Put)] public async Task SendXmlToXDocumentAsync(HttpMethodTypes methodType) { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var method = HttpMethodByType[methodType]; var result = await "http://some.url" @@ -100,7 +112,9 @@ public async Task SendXmlToXDocumentAsync(HttpMethodTypes methodType) [Fact] public async Task PostXmlToModelAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await "http://some.url" .PostXmlAsync(new TestModel { Number = 3, Text = "Test" }) @@ -112,7 +126,9 @@ public async Task PostXmlToModelAsync() [Fact] public async Task PostXmlToXDocumentAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await "http://some.url" .PostXmlAsync(new TestModel { Number = 3, Text = "Test" }) @@ -124,7 +140,9 @@ public async Task PostXmlToXDocumentAsync() [Fact] public async Task PutXmlToModelAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await "http://some.url" .PutXmlAsync(new TestModel { Number = 3, Text = "Test" }) @@ -136,7 +154,9 @@ public async Task PutXmlToModelAsync() [Fact] public async Task PutXmlToXDocumentAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await "http://some.url" .PutXmlAsync(new TestModel { Number = 3, Text = "Test" }) diff --git a/test/FlurlX.Http.Xml.Tests/TestBase.cs b/test/FlurlX.Http.Xml.Tests/TestBase.cs new file mode 100644 index 0000000..000b125 --- /dev/null +++ b/test/FlurlX.Http.Xml.Tests/TestBase.cs @@ -0,0 +1,31 @@ +using System.Collections.Generic; +using System.Net.Http; + +namespace FlurlX.Http.Xml.Tests +{ + public abstract class TestBase + { + protected const string REQUEST_BODY_XML = @" + + 3 + Test + "; + + protected const string REQUEST_BODY_JSON = @"{ + ""number"": 3, + ""text"": ""Test"" + }"; + + public enum HttpMethodTypes + { + Post, + Put + } + + protected readonly Dictionary HttpMethodByType = new() + { + [HttpMethodTypes.Post] = HttpMethod.Post, + [HttpMethodTypes.Put] = HttpMethod.Put + }; + } +} diff --git a/test/Flurl.Http.Xml.Tests/UrlExtensionsShould.cs b/test/FlurlX.Http.Xml.Tests/UrlExtensionsShould.cs similarity index 76% rename from test/Flurl.Http.Xml.Tests/UrlExtensionsShould.cs rename to test/FlurlX.Http.Xml.Tests/UrlExtensionsShould.cs index 0d22c1e..bddf583 100644 --- a/test/Flurl.Http.Xml.Tests/UrlExtensionsShould.cs +++ b/test/FlurlX.Http.Xml.Tests/UrlExtensionsShould.cs @@ -2,22 +2,23 @@ using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; -using Flurl.Http.Xml.Tests.Factories; -using Flurl.Http.Xml.Tests.Models; +using Flurl; +using Flurl.Http.Testing; +using FlurlX.Http.Xml.Tests.Models; using Xunit; using Xunit.Extensions.Ordering; -namespace Flurl.Http.Xml.Tests +namespace FlurlX.Http.Xml.Tests { public class UrlExtensionsShould : TestBase { - private void AssertTestModel(TestModel testModel, int expectedNumber, string expectedText) + private static void AssertTestModel(TestModel testModel, int expectedNumber, string expectedText) { Assert.Equal(expectedNumber, testModel.Number); Assert.Equal(expectedText, testModel.Text); } - private void AssertXDocument(XDocument document, int expectedNumber, string expectedText) + private static void AssertXDocument(XDocument document, int expectedNumber, string expectedText) { Assert.Equal(expectedNumber.ToString(), document?.Element("TestModel")?.Element("Number")?.Value); Assert.Equal(expectedText, document?.Element("TestModel")?.Element("Text")?.Value); @@ -26,7 +27,9 @@ private void AssertXDocument(XDocument document, int expectedNumber, string expe [Fact, Order(3)] public async Task GetXmlAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .GetXmlAsync() @@ -38,7 +41,9 @@ public async Task GetXmlAsync() [Fact, Order(3)] public async Task GetXDocumentAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .GetXDocumentAsync() @@ -50,7 +55,9 @@ public async Task GetXDocumentAsync() [Fact, Order(3)] public async Task GetXElementsFromXPathAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .GetXElementsFromXPath("/TestModel") @@ -62,7 +69,9 @@ public async Task GetXElementsFromXPathAsync() [Fact, Order(3)] public async Task GetXElementsFromXPathNamespaceResolverAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .GetXElementsFromXPath("/TestModel", new XmlNamespaceManager(new NameTable())) @@ -76,7 +85,9 @@ public async Task GetXElementsFromXPathNamespaceResolverAsync() [InlineData(HttpMethodTypes.Put)] public async Task SendXmlToModelAsync(HttpMethodTypes methodType) { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var method = HttpMethodByType[methodType]; var result = await new Url("https://some.url") @@ -92,7 +103,9 @@ public async Task SendXmlToModelAsync(HttpMethodTypes methodType) [InlineData(HttpMethodTypes.Put)] public async Task SendXmlToXDocumentAsync(HttpMethodTypes methodType) { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var method = HttpMethodByType[methodType]; var result = await new Url("https://some.url") @@ -106,7 +119,9 @@ public async Task SendXmlToXDocumentAsync(HttpMethodTypes methodType) [Fact] public async Task PostXmlToModelAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .PostXmlAsync(new TestModel { Number = 3, Text = "Test" }) @@ -119,7 +134,9 @@ public async Task PostXmlToModelAsync() [Fact] public async Task PostXmlToXDocumentAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .PostXmlAsync(new TestModel { Number = 3, Text = "Test" }) @@ -132,7 +149,9 @@ public async Task PostXmlToXDocumentAsync() [Fact] public async Task PutXmlToModelAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .PutXmlAsync(new TestModel { Number = 3, Text = "Test" }) @@ -145,7 +164,9 @@ public async Task PutXmlToModelAsync() [Fact] public async Task PutXmlToXDocumentAsync() { - FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory()); + using var httpTest = new HttpTest(); + + httpTest.RespondWith(REQUEST_BODY_XML); var result = await new Url("https://some.url") .PutXmlAsync(new TestModel { Number = 3, Text = "Test" }) From 1b8a0385a5fc7c93c9526661839c76abd2ea13b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renan=20Arag=C3=A3o?= Date: Sat, 16 Dec 2023 19:51:02 -0300 Subject: [PATCH 2/2] update read.md --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index af9f698..e4632d5 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,12 @@ ![Icon](http://i.imgur.com/llEKpRL.png?1) -# Flurl.Http.Xml +# FlurlX.Http.Xml [![Build status](https://ci.appveyor.com/api/projects/status/16qwl13xsaylb450?svg=true)](https://ci.appveyor.com/project/lvermeulen/flurl-http-xml) [![license](https://img.shields.io/github/license/lvermeulen/Flurl.Http.Xml.svg?maxAge=2592000)](https://github.com/lvermeulen/Flurl.Http.Xml/blob/master/LICENSE) [![NuGet](https://img.shields.io/nuget/v/Flurl.Http.Xml.svg?maxAge=86400)](https://www.nuget.org/packages/Flurl.Http.Xml/) ![downloads](https://img.shields.io/nuget/dt/Flurl.Http.Xml) -![](https://img.shields.io/badge/.net-4.6.1-yellowgreen.svg) -![](https://img.shields.io/badge/netstandard-2.0-yellowgreen.svg) +![](https://img.shields.io/badge/.net-8.0.100-yellowgreen.svg) -XML extension to the excellent [Flurl](https://github.com/tmenier/Flurl) library +XML extension to the excellent [Flurl 4](https://github.com/tmenier/Flurl) library ## Features: * Get, post and receive XML models