mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-17 23:03:46 +01:00
Add WithBody with IDictionary (form-urlencoded values) (#903)
* . * x * fx * fix * f * tests * fix tests * add tst
This commit is contained in:
@@ -431,7 +431,7 @@ public class RequestMessageBodyMatcherTests
|
||||
// JSON match +++
|
||||
{json, new RequestMessageBodyMatcher((object? o) => ((dynamic) o!).a == "b"), true},
|
||||
{json, new RequestMessageBodyMatcher((string? s) => s == json), true},
|
||||
{json, new RequestMessageBodyMatcher((byte[]? b) => b.SequenceEqual(Encoding.UTF8.GetBytes(json))), true},
|
||||
{json, new RequestMessageBodyMatcher((byte[]? b) => b?.SequenceEqual(Encoding.UTF8.GetBytes(json)) == true), true},
|
||||
|
||||
// JSON no match ---
|
||||
{json, new RequestMessageBodyMatcher((object? o) => false), false},
|
||||
@@ -442,7 +442,7 @@ public class RequestMessageBodyMatcherTests
|
||||
// string match +++
|
||||
{str, new RequestMessageBodyMatcher((object? o) => o == null), true},
|
||||
{str, new RequestMessageBodyMatcher((string? s) => s == str), true},
|
||||
{str, new RequestMessageBodyMatcher((byte[]? b) => b.SequenceEqual(Encoding.UTF8.GetBytes(str))), true},
|
||||
{str, new RequestMessageBodyMatcher((byte[]? b) => b?.SequenceEqual(Encoding.UTF8.GetBytes(str)) == true), true},
|
||||
|
||||
// string no match ---
|
||||
{str, new RequestMessageBodyMatcher((object? o) => false), false},
|
||||
@@ -453,13 +453,13 @@ public class RequestMessageBodyMatcherTests
|
||||
// binary match +++
|
||||
{bytes, new RequestMessageBodyMatcher((object? o) => o == null), true},
|
||||
{bytes, new RequestMessageBodyMatcher((string? s) => s == null), true},
|
||||
{bytes, new RequestMessageBodyMatcher((byte[]? b) => b.SequenceEqual(bytes)), true},
|
||||
{bytes, new RequestMessageBodyMatcher((byte[]? b) => b?.SequenceEqual(bytes) == true), true},
|
||||
|
||||
// binary no match ---
|
||||
{bytes, new RequestMessageBodyMatcher((object? o) => false), false},
|
||||
{bytes, new RequestMessageBodyMatcher((string? s) => false), false},
|
||||
{bytes, new RequestMessageBodyMatcher((byte[]? b) => false), false},
|
||||
{bytes, new RequestMessageBodyMatcher(), false },
|
||||
{bytes, new RequestMessageBodyMatcher(), false }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using FluentAssertions;
|
||||
using Newtonsoft.Json;
|
||||
@@ -55,11 +56,31 @@ namespace WireMock.Net.Tests
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_FuncFormUrlEncoded()
|
||||
{
|
||||
// Assign
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody((IDictionary<string, string>? values) => values != null);
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsFormUrlEncoded = new Dictionary<string, string>(),
|
||||
DetectedBodyTypeFromContentType = BodyType.FormUrlEncoded,
|
||||
DetectedBodyType = BodyType.FormUrlEncoded
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_FuncBodyData()
|
||||
{
|
||||
// Assign
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody((IBodyData b) => b != null);
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody((IBodyData? b) => b != null);
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
@@ -78,7 +99,7 @@ namespace WireMock.Net.Tests
|
||||
public void Request_WithBody_FuncByteArray()
|
||||
{
|
||||
// Assign
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody((byte[] b) => b != null);
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody((byte[]? b) => b != null);
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using FluentAssertions;
|
||||
using System.Collections.Generic;
|
||||
using FluentAssertions;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
@@ -8,6 +8,36 @@ namespace WireMock.Net.Tests.Util;
|
||||
|
||||
public class QueryStringParserTests
|
||||
{
|
||||
public static IEnumerable<object?[]> QueryStringTestData => new List<object?[]>
|
||||
{
|
||||
new object?[] { null, false, false, null },
|
||||
new object?[] { string.Empty, false, true, new Dictionary<string, string>() },
|
||||
new object?[] { "test", false, true, new Dictionary<string, string>() },
|
||||
new object?[] { "&", false, true, new Dictionary<string, string>() },
|
||||
new object?[] { "&&", false, true, new Dictionary<string, string>() },
|
||||
new object?[] { "a=", false, true, new Dictionary<string, string> { { "a", "" } } },
|
||||
new object?[] { "&a", false, true, new Dictionary<string, string>() },
|
||||
new object?[] { "&a=", false, true, new Dictionary<string, string> { { "a", "" } } },
|
||||
new object?[] { "&key1=value1", false, true, new Dictionary<string, string> { { "key1", "value1" } } },
|
||||
new object?[] { "key1=value1", false, true, new Dictionary<string, string> { { "key1", "value1" } } },
|
||||
new object?[] { "key1=value1&key2=value2", false, true, new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } } },
|
||||
new object?[] { "key1=value1&key2=value2&", false, true, new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } } },
|
||||
new object?[] { "key1=value1&&key2=value2", false, true, new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } } },
|
||||
new object?[] { "&key1=value1&key2=value2&&", false, true, new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } } },
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(QueryStringTestData))]
|
||||
public void TryParse_Should_Parse_QueryString(string queryString, bool caseIgnore, bool expectedResult, IDictionary<string, string> expectedOutput)
|
||||
{
|
||||
// Act
|
||||
var result = QueryStringParser.TryParse(queryString, caseIgnore, out var actual);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedResult);
|
||||
actual.Should().BeEquivalentTo(expectedOutput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parse_WithNullString()
|
||||
{
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#if !NET452
|
||||
//using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
//using System.Net.Http.Json;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using WireMock.Matchers;
|
||||
@@ -10,61 +12,88 @@ using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
namespace WireMock.Net.Tests;
|
||||
|
||||
public partial class WireMockServerTests
|
||||
{
|
||||
public partial class WireMockServerTests
|
||||
public class DummyClass
|
||||
{
|
||||
public class DummyClass
|
||||
public string? Hi { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_WildcardMatcher_ShouldMatch()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
server.Given(
|
||||
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
|
||||
)
|
||||
.RespondWith(
|
||||
Response.Create().WithStatusCode(200)
|
||||
);
|
||||
|
||||
var jsonObject = new DummyClass
|
||||
{
|
||||
public string Hi { get; set; }
|
||||
}
|
||||
Hi = "Hello World!"
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_WildcardMatcher_ShouldMatch()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
server.Given(
|
||||
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
|
||||
)
|
||||
.RespondWith(
|
||||
Response.Create().WithStatusCode(200)
|
||||
);
|
||||
// Act
|
||||
var response = await new HttpClient().PostAsJsonAsync("http://localhost:" + server.Ports[0] + "/foo", jsonObject).ConfigureAwait(false);
|
||||
|
||||
var jsonObject = new DummyClass
|
||||
{
|
||||
Hi = "Hello World!"
|
||||
};
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().PostAsJsonAsync("http://localhost:" + server.Ports[0] + "/foo", jsonObject).ConfigureAwait(false);
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_WildcardMatcher_ShouldMatch()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
server.Given(
|
||||
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
|
||||
)
|
||||
.RespondWith(
|
||||
Response.Create().WithStatusCode(200)
|
||||
);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
// Act
|
||||
var response = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/foo", new StringContent("{ Hi = \"Hello World\" }")).ConfigureAwait(false);
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_WildcardMatcher_ShouldMatch()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
server.Given(
|
||||
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
|
||||
)
|
||||
.RespondWith(
|
||||
Response.Create().WithStatusCode(200)
|
||||
);
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/foo", new StringContent("{ Hi = \"Hello World\" }")).ConfigureAwait(false);
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFunc()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
server.Given(
|
||||
Request.Create()
|
||||
.UsingPost()
|
||||
.WithPath("/foo")
|
||||
.WithBody(values => values != null && values["key1"] == "value1")
|
||||
)
|
||||
.RespondWith(
|
||||
Response.Create()
|
||||
);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
// Act
|
||||
var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("key1", "value1") });
|
||||
var response = await new HttpClient()
|
||||
.PostAsync($"{server.Url}/foo", content)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user