elsa-core/test/unit/Elsa.Http.UnitTests/ContentWriters/RawStringContentTests.cs
Copilot 144edc3a85
Fix Content-Type header charset auto-appending in HTTP requests (#6676)
* Initial plan for issue

* Implement RawStringContent for fixing charset issue

Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>
2025-05-24 21:39:31 +02:00

55 lines
1.9 KiB
C#

using System.Net.Http.Headers;
using System.Text;
using Elsa.Http.ContentWriters;
using Xunit;
namespace Elsa.Http.UnitTests.ContentWriters;
/// <summary>
/// Tests for the <see cref="RawStringContent"/> class.
/// </summary>
public class RawStringContentTests
{
/// <summary>
/// Tests that the content type is set exactly as provided without appending charset information.
/// </summary>
[Fact]
public void ContentType_ShouldNotAppendCharset()
{
// Arrange
const string contentType = "text/xml";
const string content = "<root>test</root>";
// Act
var rawContent = new RawStringContent(content, Encoding.UTF8, contentType);
// Assert
Assert.Equal(contentType, rawContent.Headers.ContentType?.MediaType);
Assert.Null(rawContent.Headers.ContentType?.CharSet);
}
/// <summary>
/// Tests that the content type with parameters is preserved exactly as provided.
/// </summary>
[Fact]
public void ContentType_WithParameters_ShouldPreserveParameters()
{
// Arrange
const string contentType = "application/json; custom-param=value";
var expectedMediaType = new MediaTypeHeaderValue(contentType);
const string content = "{\"test\": \"value\"}";
// Act
var rawContent = new RawStringContent(content, Encoding.UTF8, contentType);
// Assert
Assert.Equal(expectedMediaType.MediaType, rawContent.Headers.ContentType?.MediaType);
Assert.Equal(expectedMediaType.Parameters.Count(), rawContent.Headers.ContentType?.Parameters.Count());
var expectedParam = expectedMediaType.Parameters.First();
var actualParam = rawContent.Headers.ContentType?.Parameters.First();
Assert.Equal(expectedParam.Name, actualParam?.Name);
Assert.Equal(expectedParam.Value, actualParam?.Value);
}
}