Generate C# code from Mapping (#842)

* 1

* .

* v

* .

* .

* -

* b

* res b

* Fix UT

* .

* Verify

* v

* ...

* .

* .

* dir

* m
This commit is contained in:
Stef Heyenrath
2023-01-24 16:45:47 +01:00
committed by GitHub
parent b4c8779d68
commit 770a670e53
83 changed files with 3792 additions and 2942 deletions

View File

@@ -9,109 +9,109 @@ using WireMock.Types;
using WireMock.Util;
using Xunit;
namespace WireMock.Net.Tests.Util
namespace WireMock.Net.Tests.Util;
public class BodyParserTests
{
public class BodyParserTests
[Theory]
[InlineData("application/json", "{ \"x\": 1 }", BodyType.Json, BodyType.Json)]
[InlineData("application/json; charset=utf-8", "{ \"x\": 1 }", BodyType.Json, BodyType.Json)]
[InlineData("application/json; odata.metadata=minimal", "{ \"x\": 1 }", BodyType.Json, BodyType.Json)]
[InlineData("application/vnd.api+json", "{ \"x\": 1 }", BodyType.Json, BodyType.Json)]
[InlineData("application/vnd.test+json", "{ \"x\": 1 }", BodyType.Json, BodyType.Json)]
public async Task BodyParser_Parse_ContentTypeJson(string contentType, string bodyAsJson, BodyType detectedBodyType, BodyType detectedBodyTypeFromContentType)
{
[Theory]
[InlineData("application/json", "{ \"x\": 1 }", BodyType.Json, BodyType.Json)]
[InlineData("application/json; charset=utf-8", "{ \"x\": 1 }", BodyType.Json, BodyType.Json)]
[InlineData("application/json; odata.metadata=minimal", "{ \"x\": 1 }", BodyType.Json, BodyType.Json)]
[InlineData("application/vnd.api+json", "{ \"x\": 1 }", BodyType.Json, BodyType.Json)]
[InlineData("application/vnd.test+json", "{ \"x\": 1 }", BodyType.Json, BodyType.Json)]
public async Task BodyParser_Parse_ContentTypeJson(string contentType, string bodyAsJson, BodyType detectedBodyType, BodyType detectedBodyTypeFromContentType)
// Arrange
var bodyParserSettings = new BodyParserSettings
{
// Arrange
var bodyParserSettings = new BodyParserSettings
{
Stream = new MemoryStream(Encoding.UTF8.GetBytes(bodyAsJson)),
ContentType = contentType,
DeserializeJson = true
};
Stream = new MemoryStream(Encoding.UTF8.GetBytes(bodyAsJson)),
ContentType = contentType,
DeserializeJson = true
};
// Act
var body = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// Act
var body = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// Assert
Check.That(body.BodyAsBytes).IsNotNull();
Check.That(body.BodyAsJson).IsNotNull();
Check.That(body.BodyAsString).Equals(bodyAsJson);
Check.That(body.DetectedBodyType).IsEqualTo(detectedBodyType);
Check.That(body.DetectedBodyTypeFromContentType).IsEqualTo(detectedBodyTypeFromContentType);
}
// Assert
Check.That(body.BodyAsBytes).IsNotNull();
Check.That(body.BodyAsJson).IsNotNull();
Check.That(body.BodyAsString).Equals(bodyAsJson);
Check.That(body.DetectedBodyType).IsEqualTo(detectedBodyType);
Check.That(body.DetectedBodyTypeFromContentType).IsEqualTo(detectedBodyTypeFromContentType);
}
[Theory]
[InlineData("application/xml", "<xml>hello</xml>", BodyType.String, BodyType.String)]
[InlineData("something", "hello", BodyType.String, BodyType.Bytes)]
public async Task BodyParser_Parse_ContentTypeString(string contentType, string bodyAsString, BodyType detectedBodyType, BodyType detectedBodyTypeFromContentType)
[Theory]
[InlineData("application/xml", "<xml>hello</xml>", BodyType.String, BodyType.String)]
[InlineData("something", "hello", BodyType.String, BodyType.Bytes)]
public async Task BodyParser_Parse_ContentTypeString(string contentType, string bodyAsString, BodyType detectedBodyType, BodyType detectedBodyTypeFromContentType)
{
// Arrange
var bodyParserSettings = new BodyParserSettings
{
// Arrange
var bodyParserSettings = new BodyParserSettings
{
Stream = new MemoryStream(Encoding.UTF8.GetBytes(bodyAsString)),
ContentType = contentType,
DeserializeJson = true
};
Stream = new MemoryStream(Encoding.UTF8.GetBytes(bodyAsString)),
ContentType = contentType,
DeserializeJson = true
};
// Act
var body = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// Act
var body = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// Assert
Check.That(body.BodyAsBytes).IsNotNull();
Check.That(body.BodyAsJson).IsNull();
Check.That(body.BodyAsString).Equals(bodyAsString);
Check.That(body.DetectedBodyType).IsEqualTo(detectedBodyType);
Check.That(body.DetectedBodyTypeFromContentType).IsEqualTo(detectedBodyTypeFromContentType);
}
// Assert
Check.That(body.BodyAsBytes).IsNotNull();
Check.That(body.BodyAsJson).IsNull();
Check.That(body.BodyAsString).Equals(bodyAsString);
Check.That(body.DetectedBodyType).IsEqualTo(detectedBodyType);
Check.That(body.DetectedBodyTypeFromContentType).IsEqualTo(detectedBodyTypeFromContentType);
}
[Theory]
[InlineData(new byte[] { 34, 97, 34 }, BodyType.Json)]
[InlineData(new byte[] { 97 }, BodyType.String)]
[InlineData(new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 }, BodyType.Bytes)]
public async Task BodyParser_Parse_DetectedBodyType(byte[] content, BodyType detectedBodyType)
[Theory]
[InlineData(new byte[] { 34, 97, 34 }, BodyType.Json)]
[InlineData(new byte[] { 97 }, BodyType.String)]
[InlineData(new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 }, BodyType.Bytes)]
public async Task BodyParser_Parse_DetectedBodyType(byte[] content, BodyType detectedBodyType)
{
// arrange
var bodyParserSettings = new BodyParserSettings
{
// arrange
var bodyParserSettings = new BodyParserSettings
{
Stream = new MemoryStream(content),
ContentType = null,
DeserializeJson = true
};
Stream = new MemoryStream(content),
ContentType = null,
DeserializeJson = true
};
// act
var body = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// act
var body = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// assert
Check.That(body.DetectedBodyType).IsEqualTo(detectedBodyType);
}
// assert
Check.That(body.DetectedBodyType).IsEqualTo(detectedBodyType);
}
[Theory]
[InlineData(new byte[] { 34, 97, 34 }, BodyType.String)]
[InlineData(new byte[] { 97 }, BodyType.String)]
[InlineData(new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 }, BodyType.Bytes)]
public async Task BodyParser_Parse_DetectedBodyTypeNoJsonParsing(byte[] content, BodyType detectedBodyType)
[Theory]
[InlineData(new byte[] { 34, 97, 34 }, BodyType.String)]
[InlineData(new byte[] { 97 }, BodyType.String)]
[InlineData(new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 }, BodyType.Bytes)]
public async Task BodyParser_Parse_DetectedBodyTypeNoJsonParsing(byte[] content, BodyType detectedBodyType)
{
// arrange
var bodyParserSettings = new BodyParserSettings
{
// arrange
var bodyParserSettings = new BodyParserSettings
{
Stream = new MemoryStream(content),
ContentType = null,
DeserializeJson = false
};
Stream = new MemoryStream(content),
ContentType = null,
DeserializeJson = false
};
// act
var body = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// act
var body = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// assert
Check.That(body.DetectedBodyType).IsEqualTo(detectedBodyType);
}
// assert
Check.That(body.DetectedBodyType).IsEqualTo(detectedBodyType);
}
[Fact]
public async Task BodyParser_Parse_WithUTF8EncodingAndContentTypeMultipart_DetectedBodyTypeEqualsString()
{
// Arrange
string contentType = "multipart/form-data";
string body = @"
[Fact]
public async Task BodyParser_Parse_WithUTF8EncodingAndContentTypeMultipart_DetectedBodyTypeEqualsString()
{
// Arrange
string contentType = "multipart/form-data";
string body = @"
-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name=""text""
@@ -131,163 +131,162 @@ Content-Type: text/html
-----------------------------9051914041544843365972754266--";
var bodyParserSettings = new BodyParserSettings
{
Stream = new MemoryStream(Encoding.UTF8.GetBytes(body)),
ContentType = contentType,
DeserializeJson = true
};
// Act
var result = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// Assert
Check.That(result.DetectedBodyType).IsEqualTo(BodyType.String);
Check.That(result.DetectedBodyTypeFromContentType).IsEqualTo(BodyType.MultiPart);
Check.That(result.BodyAsBytes).IsNotNull();
Check.That(result.BodyAsJson).IsNull();
Check.That(result.BodyAsString).IsNotNull();
}
[Fact]
public async Task BodyParser_Parse_WithUTF16EncodingAndContentTypeMultipart_DetectedBodyTypeEqualsString()
var bodyParserSettings = new BodyParserSettings
{
// Arrange
string contentType = "multipart/form-data";
string body = char.ConvertFromUtf32(0x1D161); //U+1D161 = MUSICAL SYMBOL SIXTEENTH NOTE
var bodyParserSettings = new BodyParserSettings
{
Stream = new MemoryStream(Encoding.UTF8.GetBytes(body)),
ContentType = contentType,
DeserializeJson = true
};
Stream = new MemoryStream(Encoding.UTF8.GetBytes(body)),
ContentType = contentType,
DeserializeJson = true
};
// Act
var result = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// Act
var result = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// Assert
Check.That(result.DetectedBodyType).IsEqualTo(BodyType.Bytes);
Check.That(result.DetectedBodyTypeFromContentType).IsEqualTo(BodyType.MultiPart);
Check.That(result.BodyAsBytes).IsNotNull();
Check.That(result.BodyAsJson).IsNull();
Check.That(result.BodyAsString).IsNull();
}
// Assert
Check.That(result.DetectedBodyType).IsEqualTo(BodyType.String);
Check.That(result.DetectedBodyTypeFromContentType).IsEqualTo(BodyType.MultiPart);
Check.That(result.BodyAsBytes).IsNotNull();
Check.That(result.BodyAsJson).IsNull();
Check.That(result.BodyAsString).IsNotNull();
}
[Theory]
[InlineData("hello", BodyType.String, BodyType.Bytes)]
public async Task BodyParser_Parse_ContentTypeIsNull(string bodyAsString, BodyType detectedBodyType, BodyType detectedBodyTypeFromContentType)
[Fact]
public async Task BodyParser_Parse_WithUTF16EncodingAndContentTypeMultipart_DetectedBodyTypeEqualsString()
{
// Arrange
string contentType = "multipart/form-data";
string body = char.ConvertFromUtf32(0x1D161); //U+1D161 = MUSICAL SYMBOL SIXTEENTH NOTE
var bodyParserSettings = new BodyParserSettings
{
// Arrange
var bodyParserSettings = new BodyParserSettings
{
Stream = new MemoryStream(Encoding.UTF8.GetBytes(bodyAsString)),
ContentType = null,
DeserializeJson = true
};
Stream = new MemoryStream(Encoding.UTF8.GetBytes(body)),
ContentType = contentType,
DeserializeJson = true
};
// Act
var body = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// Act
var result = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// Assert
Check.That(body.BodyAsBytes).IsNotNull();
Check.That(body.BodyAsJson).IsNull();
Check.That(body.BodyAsString).Equals(bodyAsString);
Check.That(body.DetectedBodyType).IsEqualTo(detectedBodyType);
Check.That(body.DetectedBodyTypeFromContentType).IsEqualTo(detectedBodyTypeFromContentType);
}
// Assert
Check.That(result.DetectedBodyType).IsEqualTo(BodyType.Bytes);
Check.That(result.DetectedBodyTypeFromContentType).IsEqualTo(BodyType.MultiPart);
Check.That(result.BodyAsBytes).IsNotNull();
Check.That(result.BodyAsJson).IsNull();
Check.That(result.BodyAsString).IsNull();
}
[Theory]
[InlineData("gzip")]
[InlineData("deflate")]
public async Task BodyParser_Parse_ContentEncoding_GZip_And_DecompressGzipAndDeflate_Is_True_Should_Decompress(string compression)
[Theory]
[InlineData("hello", BodyType.String, BodyType.Bytes)]
public async Task BodyParser_Parse_ContentTypeIsNull(string bodyAsString, BodyType detectedBodyType, BodyType detectedBodyTypeFromContentType)
{
// Arrange
var bodyParserSettings = new BodyParserSettings
{
// Arrange
var bytes = Encoding.ASCII.GetBytes("0");
var compressed = CompressionUtils.Compress(compression, bytes);
var bodyParserSettings = new BodyParserSettings
{
Stream = new MemoryStream(compressed),
ContentType = "text/plain",
DeserializeJson = false,
ContentEncoding = compression.ToUpperInvariant(),
DecompressGZipAndDeflate = true
};
Stream = new MemoryStream(Encoding.UTF8.GetBytes(bodyAsString)),
ContentType = null,
DeserializeJson = true
};
// Act
var result = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// Act
var body = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// Assert
result.DetectedBodyType.Should().Be(BodyType.String);
result.DetectedBodyTypeFromContentType.Should().Be(BodyType.String);
result.BodyAsBytes.Should().BeEquivalentTo(new byte[] { 48 });
result.BodyAsJson.Should().BeNull();
result.BodyAsString.Should().Be("0");
result.DetectedCompression.Should().Be(compression);
}
// Assert
Check.That(body.BodyAsBytes).IsNotNull();
Check.That(body.BodyAsJson).IsNull();
Check.That(body.BodyAsString).Equals(bodyAsString);
Check.That(body.DetectedBodyType).IsEqualTo(detectedBodyType);
Check.That(body.DetectedBodyTypeFromContentType).IsEqualTo(detectedBodyTypeFromContentType);
}
[Theory]
[InlineData("gzip")]
[InlineData("deflate")]
public async Task BodyParser_Parse_ContentEncoding_GZip_And_DecompressGzipAndDeflate_Is_False_Should_Not_Decompress(string compression)
[Theory]
[InlineData("gzip")]
[InlineData("deflate")]
public async Task BodyParser_Parse_ContentEncoding_GZip_And_DecompressGzipAndDeflate_Is_True_Should_Decompress(string compression)
{
// Arrange
var bytes = Encoding.ASCII.GetBytes("0");
var compressed = CompressionUtils.Compress(compression, bytes);
var bodyParserSettings = new BodyParserSettings
{
// Arrange
var bytes = Encoding.ASCII.GetBytes(Guid.NewGuid().ToString());
var compressed = CompressionUtils.Compress(compression, bytes);
var bodyParserSettings = new BodyParserSettings
{
Stream = new MemoryStream(compressed),
ContentType = "text/plain",
DeserializeJson = false,
ContentEncoding = compression.ToUpperInvariant(),
DecompressGZipAndDeflate = false
};
Stream = new MemoryStream(compressed),
ContentType = "text/plain",
DeserializeJson = false,
ContentEncoding = compression.ToUpperInvariant(),
DecompressGZipAndDeflate = true
};
// Act
var result = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// Act
var result = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
// Assert
result.BodyAsBytes.Should().BeEquivalentTo(compressed);
result.DetectedCompression.Should().BeNull();
}
// Assert
result.DetectedBodyType.Should().Be(BodyType.String);
result.DetectedBodyTypeFromContentType.Should().Be(BodyType.String);
result.BodyAsBytes.Should().BeEquivalentTo(new byte[] { 48 });
result.BodyAsJson.Should().BeNull();
result.BodyAsString.Should().Be("0");
result.DetectedCompression.Should().Be(compression);
}
[Theory]
[InlineData("HEAD", false)]
[InlineData("GET", false)]
[InlineData("PUT", true)]
[InlineData("POST", true)]
[InlineData("DELETE", true)]
[InlineData("TRACE", false)]
[InlineData("OPTIONS", true)]
[InlineData("CONNECT", false)]
[InlineData("PATCH", true)]
public void BodyParser_ShouldParseBodyForMethodAndAllowAllIsFalse_ExpectedResultForKnownMethods(string method, bool resultShouldBe)
[Theory]
[InlineData("gzip")]
[InlineData("deflate")]
public async Task BodyParser_Parse_ContentEncoding_GZip_And_DecompressGzipAndDeflate_Is_False_Should_Not_Decompress(string compression)
{
// Arrange
var bytes = Encoding.ASCII.GetBytes(Guid.NewGuid().ToString());
var compressed = CompressionUtils.Compress(compression, bytes);
var bodyParserSettings = new BodyParserSettings
{
Check.That(BodyParser.ShouldParseBody(method, false)).Equals(resultShouldBe);
}
Stream = new MemoryStream(compressed),
ContentType = "text/plain",
DeserializeJson = false,
ContentEncoding = compression.ToUpperInvariant(),
DecompressGZipAndDeflate = false
};
[Theory]
[InlineData("HEAD")]
[InlineData("GET")]
[InlineData("PUT")]
[InlineData("POST")]
[InlineData("DELETE")]
[InlineData("TRACE")]
[InlineData("OPTIONS")]
[InlineData("CONNECT")]
[InlineData("PATCH")]
[InlineData("REPORT")]
[InlineData("SOME-UNKNOWN-METHOD")]
public void BodyParser_ShouldParseBodyForMethodAndAllowAllIsTrue_ExpectedResultShouldBeTrue(string method)
{
Check.That(BodyParser.ShouldParseBody(method, true)).IsTrue();
}
// Act
var result = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
[Theory]
[InlineData("REPORT")]
[InlineData("SOME-UNKNOWN-METHOD")]
public void BodyParser_ShouldParseBody_DefaultIsTrueForUnknownMethods(string method)
{
Check.That(BodyParser.ShouldParseBody(method, false)).IsTrue();
}
// Assert
result.BodyAsBytes.Should().BeEquivalentTo(compressed);
result.DetectedCompression.Should().BeNull();
}
[Theory]
[InlineData("HEAD", false)]
[InlineData("GET", false)]
[InlineData("PUT", true)]
[InlineData("POST", true)]
[InlineData("DELETE", true)]
[InlineData("TRACE", false)]
[InlineData("OPTIONS", true)]
[InlineData("CONNECT", false)]
[InlineData("PATCH", true)]
public void BodyParser_ShouldParseBodyForMethodAndAllowAllIsFalse_ExpectedResultForKnownMethods(string method, bool resultShouldBe)
{
Check.That(BodyParser.ShouldParseBody(method, false)).Equals(resultShouldBe);
}
[Theory]
[InlineData("HEAD")]
[InlineData("GET")]
[InlineData("PUT")]
[InlineData("POST")]
[InlineData("DELETE")]
[InlineData("TRACE")]
[InlineData("OPTIONS")]
[InlineData("CONNECT")]
[InlineData("PATCH")]
[InlineData("REPORT")]
[InlineData("SOME-UNKNOWN-METHOD")]
public void BodyParser_ShouldParseBodyForMethodAndAllowAllIsTrue_ExpectedResultShouldBeTrue(string method)
{
Check.That(BodyParser.ShouldParseBody(method, true)).IsTrue();
}
[Theory]
[InlineData("REPORT")]
[InlineData("SOME-UNKNOWN-METHOD")]
public void BodyParser_ShouldParseBody_DefaultIsTrueForUnknownMethods(string method)
{
Check.That(BodyParser.ShouldParseBody(method, false)).IsTrue();
}
}

View File

@@ -1,30 +1,29 @@
using System.Text;
using System.Text;
using FluentAssertions;
using WireMock.Util;
using Xunit;
namespace WireMock.Net.Tests.Util
namespace WireMock.Net.Tests.Util;
public class BytesEncodingUtilsTests
{
public class BytesEncodingUtilsTests
[Fact]
public void TryGetEncoding_UTF32()
{
[Fact]
public void TryGetEncoding_UTF32()
{
var result = BytesEncodingUtils.TryGetEncoding(new byte[] { 0xff, 0xfe, 0x00, 0x00 }, out Encoding encoding);
var result = BytesEncodingUtils.TryGetEncoding(new byte[] { 0xff, 0xfe, 0x00, 0x00 }, out Encoding encoding);
// Assert
result.Should().BeTrue();
encoding.CodePage.Should().Be(Encoding.UTF32.CodePage);
}
// Assert
result.Should().BeTrue();
encoding.CodePage.Should().Be(Encoding.UTF32.CodePage);
}
[Fact]
public void TryGetEncoding_Invalid()
{
var result = BytesEncodingUtils.TryGetEncoding(new byte[] { 0xff }, out Encoding encoding);
[Fact]
public void TryGetEncoding_Invalid()
{
var result = BytesEncodingUtils.TryGetEncoding(new byte[] { 0xff }, out Encoding encoding);
// Assert
result.Should().BeFalse();
encoding.Should().BeNull();
}
// Assert
result.Should().BeFalse();
encoding.Should().BeNull();
}
}

View File

@@ -1,75 +1,74 @@
using FluentAssertions;
using FluentAssertions;
using System;
using System.Net;
using WireMock.Util;
using Xunit;
namespace WireMock.Net.Tests.Util
namespace WireMock.Net.Tests.Util;
/// <summary>
/// Based on https://raw.githubusercontent.com/tmenier/Flurl/129565361e135e639f1d44a35a78aea4302ac6ca/Test/Flurl.Test/Http/HttpStatusRangeParserTests.cs
/// </summary>
public class HttpStatusRangeParserTests
{
/// <summary>
/// Based on https://raw.githubusercontent.com/tmenier/Flurl/129565361e135e639f1d44a35a78aea4302ac6ca/Test/Flurl.Test/Http/HttpStatusRangeParserTests.cs
/// </summary>
public class HttpStatusRangeParserTests
[Theory]
[InlineData("4**", 399, false)]
[InlineData("4**", 400, true)]
[InlineData("4**", 499, true)]
[InlineData("4**", 500, false)]
[InlineData("4xx", 399, false)]
[InlineData("4xx", 400, true)]
[InlineData("4xx", 499, true)]
[InlineData("4xx", 500, false)]
[InlineData("4XX", 399, false)]
[InlineData("4XX", 400, true)]
[InlineData("4XX", 499, true)]
[InlineData("4XX", 500, false)]
[InlineData("400-499", 399, false)]
[InlineData("400-499", 400, true)]
[InlineData("400-499", 499, true)]
[InlineData("400-499", 500, false)]
[InlineData("100,3xx,600", 100, true)]
[InlineData("100,3xx,600", 101, false)]
[InlineData("100,3xx,600", 300, true)]
[InlineData("100,3xx,600", 399, true)]
[InlineData("100,3xx,600", 400, false)]
[InlineData("100,3xx,600", 600, true)]
[InlineData("400-409,490-499", 399, false)]
[InlineData("400-409,490-499", 405, true)]
[InlineData("400-409,490-499", 450, false)]
[InlineData("400-409,490-499", 495, true)]
[InlineData("400-409,490-499", 500, false)]
[InlineData("*", 0, true)]
[InlineData(",,,*", 9999, true)]
[InlineData("", 0, false)]
[InlineData(",,,", 9999, false)]
[InlineData(null, 399, true)]
public void HttpStatusRangeParser_ValidPattern_IsMatch(string pattern, int value, bool expectedResult)
{
[Theory]
[InlineData("4**", 399, false)]
[InlineData("4**", 400, true)]
[InlineData("4**", 499, true)]
[InlineData("4**", 500, false)]
HttpStatusRangeParser.IsMatch(pattern, value).Should().Be(expectedResult);
}
[InlineData("4xx", 399, false)]
[InlineData("4xx", 400, true)]
[InlineData("4xx", 499, true)]
[InlineData("4xx", 500, false)]
[Fact]
public void HttpStatusRangeParser_ValidPattern_HttpStatusCode_IsMatch()
{
HttpStatusRangeParser.IsMatch("4xx", HttpStatusCode.BadRequest).Should().BeTrue();
}
[InlineData("4XX", 399, false)]
[InlineData("4XX", 400, true)]
[InlineData("4XX", 499, true)]
[InlineData("4XX", 500, false)]
[InlineData("400-499", 399, false)]
[InlineData("400-499", 400, true)]
[InlineData("400-499", 499, true)]
[InlineData("400-499", 500, false)]
[InlineData("100,3xx,600", 100, true)]
[InlineData("100,3xx,600", 101, false)]
[InlineData("100,3xx,600", 300, true)]
[InlineData("100,3xx,600", 399, true)]
[InlineData("100,3xx,600", 400, false)]
[InlineData("100,3xx,600", 600, true)]
[InlineData("400-409,490-499", 399, false)]
[InlineData("400-409,490-499", 405, true)]
[InlineData("400-409,490-499", 450, false)]
[InlineData("400-409,490-499", 495, true)]
[InlineData("400-409,490-499", 500, false)]
[InlineData("*", 0, true)]
[InlineData(",,,*", 9999, true)]
[InlineData("", 0, false)]
[InlineData(",,,", 9999, false)]
[InlineData(null, 399, true)]
public void HttpStatusRangeParser_ValidPattern_IsMatch(string pattern, int value, bool expectedResult)
{
HttpStatusRangeParser.IsMatch(pattern, value).Should().Be(expectedResult);
}
[Fact]
public void HttpStatusRangeParser_ValidPattern_HttpStatusCode_IsMatch()
{
HttpStatusRangeParser.IsMatch("4xx", HttpStatusCode.BadRequest).Should().BeTrue();
}
[Theory]
[InlineData("-100")]
[InlineData("100-")]
[InlineData("1yy")]
public void HttpStatusRangeParser_InvalidPattern_ThrowsException(string pattern)
{
Assert.Throws<ArgumentException>(() => HttpStatusRangeParser.IsMatch(pattern, 100));
}
[Theory]
[InlineData("-100")]
[InlineData("100-")]
[InlineData("1yy")]
public void HttpStatusRangeParser_InvalidPattern_ThrowsException(string pattern)
{
Assert.Throws<ArgumentException>(() => HttpStatusRangeParser.IsMatch(pattern, 100));
}
}

View File

@@ -2,103 +2,102 @@ using FluentAssertions;
using WireMock.Util;
using Xunit;
namespace WireMock.Net.Tests.Util
namespace WireMock.Net.Tests.Util;
public class StringUtilsTests
{
public class StringUtilsTests
[Theory]
[InlineData("'s")]
[InlineData("\"s")]
public void StringUtils_TryParseQuotedString_With_UnexpectedUnclosedString_Returns_False(string input)
{
[Theory]
[InlineData("'s")]
[InlineData("\"s")]
public void StringUtils_TryParseQuotedString_With_UnexpectedUnclosedString_Returns_False(string input)
{
// Act
bool valid = StringUtils.TryParseQuotedString(input, out var result, out var quote);
// Act
bool valid = StringUtils.TryParseQuotedString(input, out var result, out var quote);
// Assert
valid.Should().BeFalse();
}
// Assert
valid.Should().BeFalse();
}
[Theory]
[InlineData("")]
[InlineData(null)]
[InlineData("x")]
public void StringUtils_TryParseQuotedString_With_InvalidStringLength_Returns_False(string input)
{
// Act
bool valid = StringUtils.TryParseQuotedString(input, out var result, out var quote);
[Theory]
[InlineData("")]
[InlineData(null)]
[InlineData("x")]
public void StringUtils_TryParseQuotedString_With_InvalidStringLength_Returns_False(string input)
{
// Act
bool valid = StringUtils.TryParseQuotedString(input, out var result, out var quote);
// Assert
valid.Should().BeFalse();
}
// Assert
valid.Should().BeFalse();
}
[Theory]
[InlineData("xx")]
[InlineData(" ")]
public void StringUtils_TryParseQuotedString_With_InvalidStringQuoteCharacter_Returns_False(string input)
{
// Act
bool valid = StringUtils.TryParseQuotedString(input, out var result, out var quote);
[Theory]
[InlineData("xx")]
[InlineData(" ")]
public void StringUtils_TryParseQuotedString_With_InvalidStringQuoteCharacter_Returns_False(string input)
{
// Act
bool valid = StringUtils.TryParseQuotedString(input, out var result, out var quote);
// Assert
valid.Should().BeFalse();
}
// Assert
valid.Should().BeFalse();
}
[Fact]
public void StringUtils_TryParseQuotedString_With_UnexpectedUnrecognizedEscapeSequence_Returns_False()
{
// Arrange
string input = new string(new[] { '"', '\\', 'u', '?', '"' });
[Fact]
public void StringUtils_TryParseQuotedString_With_UnexpectedUnrecognizedEscapeSequence_Returns_False()
{
// Arrange
string input = new string(new[] { '"', '\\', 'u', '?', '"' });
// Act
bool valid = StringUtils.TryParseQuotedString(input, out var result, out var quote);
// Act
bool valid = StringUtils.TryParseQuotedString(input, out var result, out var quote);
// Assert
valid.Should().BeFalse();
}
// Assert
valid.Should().BeFalse();
}
[Theory]
[InlineData("''", "")]
[InlineData("'s'", "s")]
[InlineData("'\\\\'", "\\")]
[InlineData("'\\n'", "\n")]
public void StringUtils_TryParseQuotedString_SingleQuotedString(string input, string expectedResult)
{
// Act
bool valid = StringUtils.TryParseQuotedString(input, out var result, out var quote);
[Theory]
[InlineData("''", "")]
[InlineData("'s'", "s")]
[InlineData("'\\\\'", "\\")]
[InlineData("'\\n'", "\n")]
public void StringUtils_TryParseQuotedString_SingleQuotedString(string input, string expectedResult)
{
// Act
bool valid = StringUtils.TryParseQuotedString(input, out var result, out var quote);
// Assert
valid.Should().BeTrue();
result.Should().Be(expectedResult);
quote.Should().Be('\'');
}
// Assert
valid.Should().BeTrue();
result.Should().Be(expectedResult);
quote.Should().Be('\'');
}
[Theory]
[InlineData("\"\"", "")]
[InlineData("\"\\\\\"", "\\")]
[InlineData("\"\\n\"", "\n")]
[InlineData("\"\\\\n\"", "\\n")]
[InlineData("\"\\\\new\"", "\\new")]
[InlineData("\"[]\"", "[]")]
[InlineData("\"()\"", "()")]
[InlineData("\"(\\\"\\\")\"", "(\"\")")]
[InlineData("\"/\"", "/")]
[InlineData("\"a\"", "a")]
[InlineData("\"This \\\"is\\\" a test.\"", "This \"is\" a test.")]
[InlineData(@"""This \""is\"" b test.""", @"This ""is"" b test.")]
[InlineData("\"ab\\\"cd\"", "ab\"cd")]
[InlineData("\"\\\"\"", "\"")]
[InlineData("\"\\\"\\\"\"", "\"\"")]
[InlineData("\"AB YZ 19 \uD800\udc05 \u00e4\"", "AB YZ 19 \uD800\udc05 \u00e4")]
[InlineData("\"\\\\\\\\192.168.1.1\\\\audio\\\\new\"", "\\\\192.168.1.1\\audio\\new")]
public void StringUtils_TryParseQuotedString_DoubleQuotedString(string input, string expectedResult)
{
// Act
bool valid = StringUtils.TryParseQuotedString(input, out var result, out var quote);
[Theory]
[InlineData("\"\"", "")]
[InlineData("\"\\\\\"", "\\")]
[InlineData("\"\\n\"", "\n")]
[InlineData("\"\\\\n\"", "\\n")]
[InlineData("\"\\\\new\"", "\\new")]
[InlineData("\"[]\"", "[]")]
[InlineData("\"()\"", "()")]
[InlineData("\"(\\\"\\\")\"", "(\"\")")]
[InlineData("\"/\"", "/")]
[InlineData("\"a\"", "a")]
[InlineData("\"This \\\"is\\\" a test.\"", "This \"is\" a test.")]
[InlineData(@"""This \""is\"" b test.""", @"This ""is"" b test.")]
[InlineData("\"ab\\\"cd\"", "ab\"cd")]
[InlineData("\"\\\"\"", "\"")]
[InlineData("\"\\\"\\\"\"", "\"\"")]
[InlineData("\"AB YZ 19 \uD800\udc05 \u00e4\"", "AB YZ 19 \uD800\udc05 \u00e4")]
[InlineData("\"\\\\\\\\192.168.1.1\\\\audio\\\\new\"", "\\\\192.168.1.1\\audio\\new")]
public void StringUtils_TryParseQuotedString_DoubleQuotedString(string input, string expectedResult)
{
// Act
bool valid = StringUtils.TryParseQuotedString(input, out var result, out var quote);
// Assert
valid.Should().BeTrue();
result.Should().Be(expectedResult);
quote.Should().Be('"');
}
// Assert
valid.Should().BeTrue();
result.Should().Be(expectedResult);
quote.Should().Be('"');
}
}

View File

@@ -1,4 +1,4 @@
using System;
using System;
#if NET452
using Microsoft.Owin;
#else
@@ -8,50 +8,49 @@ using NFluent;
using WireMock.Util;
using Xunit;
namespace WireMock.Net.Tests.Util
namespace WireMock.Net.Tests.Util;
public class UrlUtilsTests
{
public class UrlUtilsTests
[Fact]
public void UriUtils_CreateUri_WithValidPathString()
{
[Fact]
public void UriUtils_CreateUri_WithValidPathString()
{
// Assign
Uri uri = new Uri("https://localhost:1234/a/b?x=0");
// Assign
Uri uri = new Uri("https://localhost:1234/a/b?x=0");
// Act
var result = UrlUtils.Parse(uri, new PathString("/a"));
// Act
var result = UrlUtils.Parse(uri, new PathString("/a"));
// Assert
Check.That(result.Url.ToString()).Equals("https://localhost:1234/b?x=0");
Check.That(result.AbsoluteUrl.ToString()).Equals("https://localhost:1234/a/b?x=0");
}
[Fact]
public void UriUtils_CreateUri_WithEmptyPathString()
{
// Assign
Uri uri = new Uri("https://localhost:1234/a/b?x=0");
// Act
var result = UrlUtils.Parse(uri, new PathString());
// Assert
Check.That(result.Url.ToString()).Equals("https://localhost:1234/a/b?x=0");
Check.That(result.AbsoluteUrl.ToString()).Equals("https://localhost:1234/a/b?x=0");
}
[Fact]
public void UriUtils_CreateUri_WithDifferentPathString()
{
// Assign
Uri uri = new Uri("https://localhost:1234/a/b?x=0");
// Act
var result = UrlUtils.Parse(uri, new PathString("/test"));
// Assert
Check.That(result.Url.ToString()).Equals("https://localhost:1234/a/b?x=0");
Check.That(result.AbsoluteUrl.ToString()).Equals("https://localhost:1234/a/b?x=0");
}
// Assert
Check.That(result.Url.ToString()).Equals("https://localhost:1234/b?x=0");
Check.That(result.AbsoluteUrl.ToString()).Equals("https://localhost:1234/a/b?x=0");
}
}
[Fact]
public void UriUtils_CreateUri_WithEmptyPathString()
{
// Assign
Uri uri = new Uri("https://localhost:1234/a/b?x=0");
// Act
var result = UrlUtils.Parse(uri, new PathString());
// Assert
Check.That(result.Url.ToString()).Equals("https://localhost:1234/a/b?x=0");
Check.That(result.AbsoluteUrl.ToString()).Equals("https://localhost:1234/a/b?x=0");
}
[Fact]
public void UriUtils_CreateUri_WithDifferentPathString()
{
// Assign
Uri uri = new Uri("https://localhost:1234/a/b?x=0");
// Act
var result = UrlUtils.Parse(uri, new PathString("/test"));
// Assert
Check.That(result.Url.ToString()).Equals("https://localhost:1234/a/b?x=0");
Check.That(result.AbsoluteUrl.ToString()).Equals("https://localhost:1234/a/b?x=0");
}
}