From c409ba37fa0e02b4c88744ba33cbc9d03c9cf967 Mon Sep 17 00:00:00 2001 From: Martijn Bodeman <11424653+skwasjer@users.noreply.github.com> Date: Tue, 21 Nov 2023 23:37:55 +0100 Subject: [PATCH] fix(xml): do not modify `XmlSettings` supplied to `XmlBody`. (#90) --- .../ResponseBuilderLinqToXmlExtensions.cs | 2 +- .../LinqToXmlBodyDoesNotModifySettings.cs | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/MockHttp.Tests/Language/Flow/Response/LinqToXmlBodyDoesNotModifySettings.cs diff --git a/src/MockHttp/Extensions/ResponseBuilderLinqToXmlExtensions.cs b/src/MockHttp/Extensions/ResponseBuilderLinqToXmlExtensions.cs index 9d596686..918bc3cf 100644 --- a/src/MockHttp/Extensions/ResponseBuilderLinqToXmlExtensions.cs +++ b/src/MockHttp/Extensions/ResponseBuilderLinqToXmlExtensions.cs @@ -33,7 +33,7 @@ public static IWithContentResult XmlBody(this IWithContent builder, XContainer x throw new ArgumentNullException(nameof(xmlContent)); } - settings ??= new XmlWriterSettings(); + settings = settings?.Clone() ?? new XmlWriterSettings(); // Ensure the stream is not closed/disposed on disposal of the XML writer. // It will be disposed by HttpClient instead. settings.CloseOutput = false; diff --git a/test/MockHttp.Tests/Language/Flow/Response/LinqToXmlBodyDoesNotModifySettings.cs b/test/MockHttp.Tests/Language/Flow/Response/LinqToXmlBodyDoesNotModifySettings.cs new file mode 100644 index 00000000..33c06470 --- /dev/null +++ b/test/MockHttp.Tests/Language/Flow/Response/LinqToXmlBodyDoesNotModifySettings.cs @@ -0,0 +1,22 @@ +using System.Xml; +using System.Xml.Linq; +using MockHttp.Specs; + +namespace MockHttp.Language.Flow.Response; + +public sealed class LinqToXmlBodyDoesNotModifySettings : ResponseSpec +{ + private static readonly XmlWriterSettings OriginalSettings = new() { CloseOutput = true, Async = false }; + + protected override void Given(IResponseBuilder with) + { + with.XmlBody(XDocument.Parse(""), OriginalSettings); + } + + protected override Task Should(HttpResponseMessage response) + { + OriginalSettings.Async.Should().BeFalse(); + OriginalSettings.CloseOutput.Should().BeTrue(); + return Task.CompletedTask; + } +}