diff --git a/src/Flurl.Http.Xml/HttpResponseMessageExtensions.cs b/src/Flurl.Http.Xml/HttpResponseMessageExtensions.cs
index 3dfcc7f..7813bb9 100644
--- a/src/Flurl.Http.Xml/HttpResponseMessageExtensions.cs
+++ b/src/Flurl.Http.Xml/HttpResponseMessageExtensions.cs
@@ -32,10 +32,8 @@ private static string GetMediaType(HttpRequestMessage request)
// 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();
- if (acceptHeader != null)
- {
- return acceptHeader.MediaType;
- }
+
+ return acceptHeader?.MediaType;
}
// no accepted media type present, return default
diff --git a/test/Flurl.Http.Xml.Tests/Factories/TestModelHttpClientFactory.cs b/test/Flurl.Http.Xml.Tests/Factories/BaseTestModelClientFactory.cs
similarity index 60%
rename from test/Flurl.Http.Xml.Tests/Factories/TestModelHttpClientFactory.cs
rename to test/Flurl.Http.Xml.Tests/Factories/BaseTestModelClientFactory.cs
index a855fdc..c98b443 100644
--- a/test/Flurl.Http.Xml.Tests/Factories/TestModelHttpClientFactory.cs
+++ b/test/Flurl.Http.Xml.Tests/Factories/BaseTestModelClientFactory.cs
@@ -1,4 +1,4 @@
-using System.Net.Http;
+using System.Net.Http;
using Flurl.Http.Configuration;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
@@ -7,13 +7,14 @@
namespace Flurl.Http.Xml.Tests.Factories
{
- public class TestModelHttpClientFactory : DefaultHttpClientFactory
+ public abstract class BaseTestModelClientFactory : DefaultHttpClientFactory
{
- private const string REQUEST_BODY = @"
-
- 3
- Test
-";
+ private readonly string _responseBody;
+
+ protected BaseTestModelClientFactory(string responseBody)
+ {
+ _responseBody = responseBody;
+ }
private HttpClient GetClient()
{
@@ -21,7 +22,7 @@ private HttpClient GetClient()
{
app.Use(async (context, next) =>
{
- await context.Response.WriteAsync(REQUEST_BODY);
+ await context.Response.WriteAsync(_responseBody);
});
});
@@ -31,4 +32,4 @@ private HttpClient GetClient()
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
new file mode 100644
index 0000000..73c2d7e
--- /dev/null
+++ b/test/Flurl.Http.Xml.Tests/Factories/JsonTestModelHttpClientFactory.cs
@@ -0,0 +1,14 @@
+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
new file mode 100644
index 0000000..f4569c4
--- /dev/null
+++ b/test/Flurl.Http.Xml.Tests/Factories/XmlTestModelHttpClientFactory.cs
@@ -0,0 +1,15 @@
+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/RequestExtensionsShould.cs b/test/Flurl.Http.Xml.Tests/RequestExtensionsShould.cs
index ca21dea..3a8c2b4 100644
--- a/test/Flurl.Http.Xml.Tests/RequestExtensionsShould.cs
+++ b/test/Flurl.Http.Xml.Tests/RequestExtensionsShould.cs
@@ -1,5 +1,4 @@
using System.Linq;
-using System.Net.Http;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
@@ -26,7 +25,7 @@ private void AssertXDocument(XDocument document, int expectedNumber, string expe
[Fact]
public async Task GetXmlAsync()
{
- FlurlHttp.Configure(c => c.HttpClientFactory = new TestModelHttpClientFactory());
+ FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory());
var result = await new Url("https://some.url")
.AllowAnyHttpStatus()
@@ -38,7 +37,7 @@ public async Task GetXmlAsync()
[Fact]
public async Task GetXDocumentAsync()
{
- FlurlHttp.Configure(c => c.HttpClientFactory = new TestModelHttpClientFactory());
+ FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory());
var result = await new Url("https://some.url")
.AllowAnyHttpStatus()
@@ -50,7 +49,7 @@ public async Task GetXDocumentAsync()
[Fact]
public async Task GetXElementsFromXPathAsync()
{
- FlurlHttp.Configure(c => c.HttpClientFactory = new TestModelHttpClientFactory());
+ FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory());
var result = await new Url("https://some.url")
.AllowAnyHttpStatus()
@@ -62,7 +61,7 @@ public async Task GetXElementsFromXPathAsync()
[Fact]
public async Task GetXElementsFromXPathNamespaceResolverAsync()
{
- FlurlHttp.Configure(c => c.HttpClientFactory = new TestModelHttpClientFactory());
+ FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory());
var result = await new Url("https://some.url")
.AllowAnyHttpStatus()
@@ -156,20 +155,57 @@ public async Task PutXmlToXDocumentAsync()
}
[Theory]
- [InlineData("Accept", "text/xml, application/xml", "text/xml")]
+ [InlineData("", null, "application/xml")]
+ [InlineData("Accept", "application/json", "application/json")]
[InlineData("Accept", "text/something+xml", "text/something+xml")]
+ [InlineData("Accept", "text/xml, application/xml", "text/xml")]
[InlineData("Accept", null, "application/xml")]
- [InlineData("", null, "application/xml")]
public async Task ReceiveCorrectMediaType(string headerName, string acceptMediaType, string expectedContentType)
{
FlurlHttp.Configure(c => c.HttpClientFactory = new EchoHttpClientFactory());
var result = await new Url("https://some.url")
.WithHeader(headerName, acceptMediaType)
- .SendXmlAsync(HttpMethod.Post, new TestModel { Number = 3, Text = "Test" })
+ .PostXmlAsync(new TestModel { Number = 3, Text = "Test" })
.ReceiveXmlResponseMessage();
Assert.Equal(expectedContentType, result?.Content?.Headers?.ContentType?.MediaType);
}
+
+ [Theory]
+ [InlineData("", null)]
+ [InlineData("Accept", "application/json")]
+ [InlineData("Accept", "text/something+xml")]
+ [InlineData("Accept", "text/xml, application/xml")]
+ [InlineData("Accept", null)]
+ public async Task ReceiveCorrectMediaTypeWithXmlResponse(string headerName, string acceptMediaType)
+ {
+ FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory());
+
+ var result = await new Url("https://some.url")
+ .WithHeader(headerName, acceptMediaType)
+ .PostXmlAsync(new TestModel { Number = 3, Text = "Test" })
+ .ReceiveXml();
+
+ AssertTestModel(result, 3, "Test");
+ }
+
+ [Theory]
+ [InlineData("", null)]
+ [InlineData("Accept", "application/json")]
+ [InlineData("Accept", "text/something+xml")]
+ [InlineData("Accept", "text/xml, application/xml")]
+ [InlineData("Accept", null)]
+ public async Task ReceiveCorrectMediaTypeWithJsonResponse(string headerName, string acceptMediaType)
+ {
+ FlurlHttp.Configure(c => c.HttpClientFactory = new JsonTestModelHttpClientFactory());
+
+ var result = await new Url("https://some.url")
+ .WithHeader(headerName, acceptMediaType)
+ .PostXmlAsync(new TestModel { Number = 3, Text = "Test" })
+ .ReceiveJson();
+
+ AssertTestModel(result, 3, "Test");
+ }
}
}
diff --git a/test/Flurl.Http.Xml.Tests/StringExtensionsShould.cs b/test/Flurl.Http.Xml.Tests/StringExtensionsShould.cs
index b166e69..458779a 100644
--- a/test/Flurl.Http.Xml.Tests/StringExtensionsShould.cs
+++ b/test/Flurl.Http.Xml.Tests/StringExtensionsShould.cs
@@ -25,7 +25,7 @@ private void AssertXDocument(XDocument document, int expectedNumber, string expe
[Fact]
public async Task GetXmlAsync()
{
- FlurlHttp.Configure(c => c.HttpClientFactory = new TestModelHttpClientFactory());
+ FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory());
var result = await "https://some.url"
.GetXmlAsync();
@@ -36,7 +36,7 @@ public async Task GetXmlAsync()
[Fact]
public async Task GetXDocumentAsync()
{
- FlurlHttp.Configure(c => c.HttpClientFactory = new TestModelHttpClientFactory());
+ FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory());
var result = await "https://some.url"
.GetXDocumentAsync();
@@ -47,7 +47,7 @@ public async Task GetXDocumentAsync()
[Fact]
public async Task GetXElementsFromXPathAsync()
{
- FlurlHttp.Configure(c => c.HttpClientFactory = new TestModelHttpClientFactory());
+ FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory());
var result = await "https://some.url"
.GetXElementsFromXPath("/TestModel");
@@ -58,7 +58,7 @@ public async Task GetXElementsFromXPathAsync()
[Fact]
public async Task GetXElementsFromXPathNamespaceResolverAsync()
{
- FlurlHttp.Configure(c => c.HttpClientFactory = new TestModelHttpClientFactory());
+ FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory());
var result = await "https://some.url"
.GetXElementsFromXPath("/TestModel", new XmlNamespaceManager(new NameTable()));
diff --git a/test/Flurl.Http.Xml.Tests/UrlExtensionsShould.cs b/test/Flurl.Http.Xml.Tests/UrlExtensionsShould.cs
index 3c95bcc..6d6b7e3 100644
--- a/test/Flurl.Http.Xml.Tests/UrlExtensionsShould.cs
+++ b/test/Flurl.Http.Xml.Tests/UrlExtensionsShould.cs
@@ -25,7 +25,7 @@ private void AssertXDocument(XDocument document, int expectedNumber, string expe
[Fact]
public async Task GetXmlAsync()
{
- FlurlHttp.Configure(c => c.HttpClientFactory = new TestModelHttpClientFactory());
+ FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory());
var result = await new Url("https://some.url")
.GetXmlAsync();
@@ -36,7 +36,7 @@ public async Task GetXmlAsync()
[Fact]
public async Task GetXDocumentAsync()
{
- FlurlHttp.Configure(c => c.HttpClientFactory = new TestModelHttpClientFactory());
+ FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory());
var result = await new Url("https://some.url")
.GetXDocumentAsync();
@@ -47,7 +47,7 @@ public async Task GetXDocumentAsync()
[Fact]
public async Task GetXElementsFromXPathAsync()
{
- FlurlHttp.Configure(c => c.HttpClientFactory = new TestModelHttpClientFactory());
+ FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory());
var result = await new Url("https://some.url")
.GetXElementsFromXPath("/TestModel");
@@ -58,7 +58,7 @@ public async Task GetXElementsFromXPathAsync()
[Fact]
public async Task GetXElementsFromXPathNamespaceResolverAsync()
{
- FlurlHttp.Configure(c => c.HttpClientFactory = new TestModelHttpClientFactory());
+ FlurlHttp.Configure(c => c.HttpClientFactory = new XmlTestModelHttpClientFactory());
var result = await new Url("https://some.url")
.GetXElementsFromXPath("/TestModel", new XmlNamespaceManager(new NameTable()));