using System.Text;
using Elsa.Http.ContentWriters;
using Xunit;
namespace Elsa.Http.UnitTests.ContentWriters;
///
/// Tests for the implementations.
///
public class ContentFactoryTests
{
///
/// Tests that doesn't append charset to content type.
///
[Fact]
public void JsonContentFactory_ShouldNotAppendCharset()
{
// Arrange
const string contentType = "application/json";
const string content = "{\"test\": \"value\"}";
var factory = new JsonContentFactory();
// Act
var httpContent = factory.CreateHttpContent(content, contentType);
// Assert
Assert.Equal(contentType, httpContent.Headers.ContentType?.MediaType);
Assert.Null(httpContent.Headers.ContentType?.CharSet);
}
///
/// Tests that doesn't append charset to content type.
///
[Fact]
public void XmlContentFactory_ShouldNotAppendCharset()
{
// Arrange
const string contentType = "text/xml";
const string content = "test";
var factory = new XmlContentFactory();
// Act
var httpContent = factory.CreateHttpContent(content, contentType);
// Assert
Assert.Equal(contentType, httpContent.Headers.ContentType?.MediaType);
Assert.Null(httpContent.Headers.ContentType?.CharSet);
}
///
/// Tests that doesn't append charset to content type.
///
[Fact]
public void TextContentFactory_ShouldNotAppendCharset()
{
// Arrange
const string contentType = "text/html";
const string content = "
test";
var factory = new TextContentFactory();
// Act
var httpContent = factory.CreateHttpContent(content, contentType);
// Assert
Assert.Equal(contentType, httpContent.Headers.ContentType?.MediaType);
Assert.Null(httpContent.Headers.ContentType?.CharSet);
}
///
/// Tests that produces correct content length without BOM.
///
[Fact]
public async Task JsonContentFactory_ShouldProduceCorrectContentLength()
{
// Arrange
const string contentType = "application/json";
const string content = "{\"test\": \"value\"}";
var factory = new JsonContentFactory();
// Act
var httpContent = factory.CreateHttpContent(content, contentType);
var bytes = await httpContent.ReadAsByteArrayAsync();
// Assert
// Content length should match the actual bytes (no BOM)
Assert.Equal(Encoding.UTF8.GetByteCount(content), bytes.Length);
Assert.Equal(httpContent.Headers.ContentLength, bytes.Length);
// Verify no BOM is present (BOM would be EF-BB-BF at start)
Assert.False(bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF);
}
///
/// Tests that handles multi-byte UTF-8 characters correctly.
///
[Fact]
public async Task JsonContentFactory_ShouldHandleMultiByteCharacters()
{
// Arrange
const string contentType = "application/json";
const string content = "{\"emoji\": \"😀\", \"chinese\": \"ä½ å¥½\"}"; // Contains multi-byte UTF-8 characters
var factory = new JsonContentFactory();
// Act
var httpContent = factory.CreateHttpContent(content, contentType);
var bytes = await httpContent.ReadAsByteArrayAsync();
// Assert
// Byte count should be greater than character count due to multi-byte characters
Assert.True(bytes.Length > content.Length);
// Content length should match the actual UTF-8 byte count
Assert.Equal(Encoding.UTF8.GetByteCount(content), bytes.Length);
Assert.Equal(httpContent.Headers.ContentLength, bytes.Length);
}
}