Add WithBody with IDictionary (form-urlencoded values) (#903)

* .

* x

* fx

* fix

* f

* tests

* fix tests

* add tst
This commit is contained in:
Stef Heyenrath
2023-03-17 17:08:45 +01:00
committed by GitHub
parent 19701f5260
commit 78b94d2ebc
13 changed files with 286 additions and 106 deletions

View File

@@ -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