mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-20 00:03:48 +01:00
* Fixes for JsonPath * More tests * Fixes + added tests
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
@@ -198,5 +200,86 @@ namespace WireMock.Net.Tests
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyAsJson_Object_JsonPathMatcher_true()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));
|
||||
|
||||
// when
|
||||
string jsonString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }";
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
||||
Encoding = Encoding.UTF8
|
||||
};
|
||||
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
[Fact]
|
||||
public void Request_WithBodyAsJson_Array_JsonPathMatcher_true()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.books[?(@.price < 10)]"));
|
||||
|
||||
// when
|
||||
string jsonString = "{ \"books\": [ { \"category\": \"test1\", \"price\": 8.95 }, { \"category\": \"test2\", \"price\": 20 } ] }";
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
||||
Encoding = Encoding.UTF8
|
||||
};
|
||||
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyAsObject_ExactObjectMatcher_true()
|
||||
{
|
||||
// Assign
|
||||
object body = DateTime.MinValue;
|
||||
var requestBuilder = Request.Create().UsingAnyVerb().WithBody(body);
|
||||
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = DateTime.MinValue
|
||||
};
|
||||
|
||||
// Act
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyAsBytes_ExactObjectMatcher_true()
|
||||
{
|
||||
// Assign
|
||||
byte[] body = { 123 };
|
||||
var requestBuilder = Request.Create().UsingAnyVerb().WithBody(body);
|
||||
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsBytes = new byte[] { 123 }
|
||||
};
|
||||
|
||||
// Act
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using WireMock.RequestBuilders;
|
||||
@@ -69,7 +70,7 @@ namespace WireMock.Net.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Scenario_and_State_TodoList_Example()
|
||||
public void Scenario_and_State_TodoList_Example()
|
||||
{
|
||||
// Assign
|
||||
_server = FluentMockServer.Start();
|
||||
@@ -104,18 +105,18 @@ namespace WireMock.Net.Tests
|
||||
|
||||
// Act and Assert
|
||||
string url = "http://localhost:" + _server.Ports[0];
|
||||
string getResponse1 = await new HttpClient().GetStringAsync(url + "/todo/items");
|
||||
string getResponse1 = new HttpClient().GetStringAsync(url + "/todo/items").Result;
|
||||
Check.That(getResponse1).Equals("Buy milk");
|
||||
|
||||
var postResponse = await new HttpClient().PostAsync(url + "/todo/items", new StringContent("Cancel newspaper subscription"));
|
||||
var postResponse = new HttpClient().PostAsync(url + "/todo/items", new StringContent("Cancel newspaper subscription")).Result;
|
||||
Check.That(postResponse.StatusCode).Equals(HttpStatusCode.Created);
|
||||
|
||||
string getResponse2 = await new HttpClient().GetStringAsync(url + "/todo/items");
|
||||
string getResponse2 = new HttpClient().GetStringAsync(url + "/todo/items").Result;
|
||||
Check.That(getResponse2).Equals("Buy milk;Cancel newspaper subscription");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_process_request_if_equals_state_and_multiple_state_defined()
|
||||
public void Should_process_request_if_equals_state_and_multiple_state_defined()
|
||||
{
|
||||
// given
|
||||
_server = FluentMockServer.Start();
|
||||
@@ -156,19 +157,20 @@ namespace WireMock.Net.Tests
|
||||
.RespondWith(Response.Create()
|
||||
.WithBody("Test state msg 2"));
|
||||
|
||||
Thread.Sleep(500);
|
||||
|
||||
// when / then
|
||||
string url = "http://localhost:" + _server.Ports[0];
|
||||
var http = new HttpClient();
|
||||
var responseNoState1 = await http.GetStringAsync(url + "/state1");
|
||||
var responseNoState1 = new HttpClient().GetStringAsync(url + "/state1").Result;
|
||||
Check.That(responseNoState1).Equals("No state msg 1");
|
||||
|
||||
var responseNoState2 = await http.GetStringAsync(url + "/state2");
|
||||
var responseNoState2 = new HttpClient().GetStringAsync(url + "/state2").Result;
|
||||
Check.That(responseNoState2).Equals("No state msg 2");
|
||||
|
||||
var responseWithState1 = await http.GetStringAsync(url + "/foo");
|
||||
var responseWithState1 = new HttpClient().GetStringAsync(url + "/foo").Result;
|
||||
Check.That(responseWithState1).Equals("Test state msg 1");
|
||||
|
||||
var responseWithState2 = await http.GetStringAsync(url + "/foo");
|
||||
var responseWithState2 = new HttpClient().GetStringAsync(url + "/foo").Result;
|
||||
Check.That(responseWithState2).Equals("Test state msg 2");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user