From c38373eb1f551a301304aa8ae9ea736330075e5c Mon Sep 17 00:00:00 2001 From: Sebastian Bebrys Date: Thu, 23 Feb 2017 10:39:44 +0100 Subject: [PATCH 1/2] Body Encoding --- src/WireMock.Net/HttpListenerRequestMapper.cs | 14 +-- .../HttpListenerResponseMapper.cs | 18 +-- src/WireMock.Net/RequestMessage.cs | 10 +- .../ResponseBuilders/IBodyResponseBuilder.cs | 6 +- src/WireMock.Net/ResponseBuilders/Response.cs | 25 ++++- src/WireMock.Net/ResponseMessage.cs | 7 ++ .../HttpListenerResponseMapperTests.cs | 26 +++++ .../WireMock.Net.Tests/RequestMessageTests.cs | 6 +- test/WireMock.Net.Tests/RequestTests.cs | 106 +++++++++--------- test/WireMock.Net.Tests/ResponseTests.cs | 54 +++++++-- 10 files changed, 185 insertions(+), 87 deletions(-) diff --git a/src/WireMock.Net/HttpListenerRequestMapper.cs b/src/WireMock.Net/HttpListenerRequestMapper.cs index 2b055175..1aaa7b9d 100644 --- a/src/WireMock.Net/HttpListenerRequestMapper.cs +++ b/src/WireMock.Net/HttpListenerRequestMapper.cs @@ -18,19 +18,19 @@ namespace WireMock /// The . public RequestMessage Map(HttpListenerRequest listenerRequest) { - Uri url = listenerRequest.Url; - string verb = listenerRequest.HttpMethod; - byte[] body = GetRequestBody(listenerRequest); - string bodyAsString = body != null ? listenerRequest.ContentEncoding.GetString(body) : null; + var url = listenerRequest.Url; + var verb = listenerRequest.HttpMethod; + var body = GetRequestBody(listenerRequest); + var bodyEncoding = body != null ? listenerRequest.ContentEncoding : null; + var bodyAsString = bodyEncoding?.GetString(body); var listenerHeaders = listenerRequest.Headers; var headers = listenerHeaders.AllKeys.ToDictionary(k => k, k => listenerHeaders[k]); var cookies = new Dictionary(); + foreach (Cookie cookie in listenerRequest.Cookies) cookies.Add(cookie.Name, cookie.Value); - var message = new RequestMessage(url, verb, body, bodyAsString, headers, cookies) { DateTime = DateTime.Now }; - - return message; + return new RequestMessage(url, verb, body, bodyAsString, bodyEncoding, headers, cookies) { DateTime = DateTime.Now }; } /// diff --git a/src/WireMock.Net/HttpListenerResponseMapper.cs b/src/WireMock.Net/HttpListenerResponseMapper.cs index 6a0897b8..87dc9a27 100644 --- a/src/WireMock.Net/HttpListenerResponseMapper.cs +++ b/src/WireMock.Net/HttpListenerResponseMapper.cs @@ -24,14 +24,16 @@ namespace WireMock responseMessage.Headers.ToList().ForEach(pair => listenerResponse.AddHeader(pair.Key, pair.Value)); - if (responseMessage.Body != null) - { - byte[] buffer = _utf8NoBom.GetBytes(responseMessage.Body); - listenerResponse.ContentEncoding = _utf8NoBom; - listenerResponse.ContentLength64 = buffer.Length; - listenerResponse.OutputStream.Write(buffer, 0, buffer.Length); - listenerResponse.OutputStream.Flush(); - } + if (responseMessage.Body == null) + return; + + var encoding = responseMessage.BodyEncoding ?? _utf8NoBom; + var buffer = encoding.GetBytes(responseMessage.Body); + + listenerResponse.ContentEncoding = encoding; + listenerResponse.ContentLength64 = buffer.Length; + listenerResponse.OutputStream.Write(buffer, 0, buffer.Length); + listenerResponse.OutputStream.Flush(); } } } \ No newline at end of file diff --git a/src/WireMock.Net/RequestMessage.cs b/src/WireMock.Net/RequestMessage.cs index b4f82ce7..3f6f4b3e 100644 --- a/src/WireMock.Net/RequestMessage.cs +++ b/src/WireMock.Net/RequestMessage.cs @@ -4,6 +4,7 @@ using System.Linq; using JetBrains.Annotations; using WireMock.Util; using WireMock.Validation; +using System.Text; namespace WireMock { @@ -57,6 +58,11 @@ namespace WireMock /// public string Body { get; } + /// + /// Gets the body encoding. + /// + public Encoding BodyEncoding { get; } + /// /// Initializes a new instance of the class. /// @@ -64,9 +70,10 @@ namespace WireMock /// The verb. /// The bodyAsBytes byte[]. /// The body string. + /// The body encoding /// The headers. /// The cookies. - public RequestMessage([NotNull] Uri url, [NotNull] string verb, [CanBeNull] byte[] bodyAsBytes = null, [CanBeNull] string body = null, [CanBeNull] IDictionary headers = null, [CanBeNull] IDictionary cookies = null) + public RequestMessage([NotNull] Uri url, [NotNull] string verb, [CanBeNull] byte[] bodyAsBytes = null, [CanBeNull] string body = null, [CanBeNull] Encoding bodyEncoding = null, [CanBeNull] IDictionary headers = null, [CanBeNull] IDictionary cookies = null) { Check.NotNull(url, nameof(url)); Check.NotNull(verb, nameof(verb)); @@ -76,6 +83,7 @@ namespace WireMock Method = verb.ToLower(); BodyAsBytes = bodyAsBytes; Body = body; + BodyEncoding = bodyEncoding; Headers = headers; Cookies = cookies; diff --git a/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs b/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs index 8c589be4..4b4841af 100644 --- a/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs +++ b/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs @@ -12,15 +12,17 @@ namespace WireMock.ResponseBuilders /// The with body. /// /// The body. + /// The body encoding. /// A . - IResponseBuilder WithBody([NotNull] string body); + IResponseBuilder WithBody([NotNull] string body, [CanBeNull] Encoding encoding = null); /// /// The with body. /// /// The body. + /// The body encoding. /// A . - IResponseBuilder WithBodyAsJson([NotNull] object body); + IResponseBuilder WithBodyAsJson([NotNull] object body, [CanBeNull] Encoding encoding = null); /// /// The with body as base64. diff --git a/src/WireMock.Net/ResponseBuilders/Response.cs b/src/WireMock.Net/ResponseBuilders/Response.cs index 4209d0aa..2bf6c5ca 100644 --- a/src/WireMock.Net/ResponseBuilders/Response.cs +++ b/src/WireMock.Net/ResponseBuilders/Response.cs @@ -143,12 +143,15 @@ namespace WireMock.ResponseBuilders /// The with body. /// /// The body. + /// The body encoding. /// A . - public IResponseBuilder WithBody(string body) + public IResponseBuilder WithBody(string body, Encoding encoding = null) { Check.NotNull(body, nameof(body)); ResponseMessage.Body = body; + ResponseMessage.BodyEncoding = encoding ?? Encoding.UTF8; + return this; } @@ -156,12 +159,22 @@ namespace WireMock.ResponseBuilders /// The with body (AsJson object). /// /// The body. + /// The body encoding. /// A . - public IResponseBuilder WithBodyAsJson(object body) + public IResponseBuilder WithBodyAsJson(object body, Encoding encoding = null) { Check.NotNull(body, nameof(body)); - ResponseMessage.Body = JsonConvert.SerializeObject(body, new JsonSerializerSettings { Formatting = Formatting.None, NullValueHandling = NullValueHandling.Ignore }); + var jsonBody = JsonConvert.SerializeObject(body, new JsonSerializerSettings { Formatting = Formatting.None, NullValueHandling = NullValueHandling.Ignore }); + + if (encoding != null && !encoding.Equals(Encoding.UTF8)) + { + jsonBody = encoding.GetString(Encoding.UTF8.GetBytes(jsonBody)); + ResponseMessage.BodyEncoding = encoding; + } + + ResponseMessage.Body = jsonBody; + return this; } @@ -175,7 +188,11 @@ namespace WireMock.ResponseBuilders { Check.NotNull(bodyAsbase64, nameof(bodyAsbase64)); - ResponseMessage.Body = (encoding ?? Encoding.UTF8).GetString(Convert.FromBase64String(bodyAsbase64)); + encoding = encoding ?? Encoding.UTF8; + + ResponseMessage.Body = encoding.GetString(Convert.FromBase64String(bodyAsbase64)); + ResponseMessage.BodyEncoding = encoding; + return this; } diff --git a/src/WireMock.Net/ResponseMessage.cs b/src/WireMock.Net/ResponseMessage.cs index ed9e8a56..b60ed2ce 100644 --- a/src/WireMock.Net/ResponseMessage.cs +++ b/src/WireMock.Net/ResponseMessage.cs @@ -1,5 +1,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; +using System.Text; + namespace WireMock { /// @@ -27,6 +29,11 @@ namespace WireMock /// public string Body { get; set; } + /// + /// Gets or sets the body encoding. + /// + public Encoding BodyEncoding { get; set; } = new UTF8Encoding(false); + /// /// The add header. /// diff --git a/test/WireMock.Net.Tests/HttpListenerResponseMapperTests.cs b/test/WireMock.Net.Tests/HttpListenerResponseMapperTests.cs index cd0326c6..132e644e 100644 --- a/test/WireMock.Net.Tests/HttpListenerResponseMapperTests.cs +++ b/test/WireMock.Net.Tests/HttpListenerResponseMapperTests.cs @@ -1,5 +1,6 @@ using System.Net; using System.Net.Http; +using System.Text; using System.Threading; using System.Threading.Tasks; using NFluent; @@ -67,6 +68,31 @@ namespace WireMock.Net.Tests Check.That(contentTask.Result).IsEqualTo("Hello !!!"); } + [Test] + public void Should_map_encoded_body_from_original_response() + { + // given + var response = new ResponseMessage + { + Body = "Hello !!!", + BodyEncoding = Encoding.ASCII + }; + + var httpListenerResponse = CreateHttpListenerResponse(); + + // when + new HttpListenerResponseMapper().Map(response, httpListenerResponse); + + // then + Check.That(httpListenerResponse.ContentEncoding).Equals(Encoding.ASCII); + + var responseMessage = ToResponseMessage(httpListenerResponse); + Check.That(responseMessage).IsNotNull(); + + var contentTask = responseMessage.Content.ReadAsStringAsync(); + Check.That(contentTask.Result).IsEqualTo("Hello !!!"); + } + [TearDown] public void StopServer() { diff --git a/test/WireMock.Net.Tests/RequestMessageTests.cs b/test/WireMock.Net.Tests/RequestMessageTests.cs index 2e74c704..55ca493c 100644 --- a/test/WireMock.Net.Tests/RequestMessageTests.cs +++ b/test/WireMock.Net.Tests/RequestMessageTests.cs @@ -22,9 +22,9 @@ namespace WireMock.Net.Tests public void Should_parse_query_params() { // given - string bodyAsString = "whatever"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost?foo=bar&multi=1&multi=2"), "POST", body, bodyAsString); + var bodyAsString = "whatever"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost?foo=bar&multi=1&multi=2"), "POST", body, bodyAsString, Encoding.UTF8); // then Check.That(request.GetParameter("foo")).Contains("bar"); diff --git a/test/WireMock.Net.Tests/RequestTests.cs b/test/WireMock.Net.Tests/RequestTests.cs index c973c1ca..f0663b6b 100644 --- a/test/WireMock.Net.Tests/RequestTests.cs +++ b/test/WireMock.Net.Tests/RequestTests.cs @@ -144,9 +144,9 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath("/foo").UsingDelete(); // when - string bodyAsString = "whatever"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", body, bodyAsString); + var bodyAsString = "whatever"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -202,9 +202,9 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithHeader("X-toto", "tata"); // when - string bodyAsString = "whatever"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary { { "X-toto", "tata" } }); + var bodyAsString = "whatever"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "tata" } }); // then var requestMatchResult = new RequestMatchResult(); @@ -218,9 +218,9 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithHeader("X-toto", "tatata"); // when - string bodyAsString = "whatever"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary { { "X-toto", "tata" } }); + var bodyAsString = "whatever"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "tata" } }); // then var requestMatchResult = new RequestMatchResult(); @@ -234,9 +234,9 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithHeader("X-toto", "abc", false); // when - string bodyAsString = "whatever"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary { { "X-toto", "ABC" } }); + var bodyAsString = "whatever"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "ABC" } }); // then var requestMatchResult = new RequestMatchResult(); @@ -250,9 +250,9 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithHeader("X-toto", "tata*"); // when - string bodyAsString = "whatever"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary { { "X-toto", "TaTa" } }); + var bodyAsString = "whatever"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "TaTa" } }); // then var requestMatchResult = new RequestMatchResult(); @@ -266,7 +266,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithCookie("session", "a*"); // when - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", null, null, null, new Dictionary { { "session", "abc" } }); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", null, null, null, null, new Dictionary { { "session", "abc" } }); // then var requestMatchResult = new RequestMatchResult(); @@ -280,9 +280,9 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithBody("Hello world!"); // when - string bodyAsString = "Hello world!"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString); + var bodyAsString = "Hello world!"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -296,9 +296,9 @@ namespace WireMock.Net.Tests var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new ExactMatcher("cat")); // when - string bodyAsString = "cat"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString); + var bodyAsString = "cat"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -312,9 +312,9 @@ namespace WireMock.Net.Tests var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new ExactMatcher("cat", "dog")); // when - string bodyAsString = "cat"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString); + var bodyAsString = "cat"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -328,9 +328,9 @@ namespace WireMock.Net.Tests var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new ExactMatcher("cat")); // when - string bodyAsString = "caR"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString); + var bodyAsString = "caR"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -344,9 +344,9 @@ namespace WireMock.Net.Tests var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new SimMetricsMatcher("The cat walks in the street.")); // when - string bodyAsString = "The car drives in the street."; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString); + var bodyAsString = "The car drives in the street."; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -360,9 +360,9 @@ namespace WireMock.Net.Tests var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new SimMetricsMatcher("The cat walks in the street.")); // when - string bodyAsString = "Hello"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString); + var bodyAsString = "Hello"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -376,9 +376,9 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithBody(new WildcardMatcher("H*o*")); // when - string bodyAsString = "Hello world!"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary { { "X-toto", "tatata" } }); + var bodyAsString = "Hello world!"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "tatata" } }); // then var requestMatchResult = new RequestMatchResult(); @@ -392,9 +392,9 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithBody(new RegexMatcher("H.*o")); // when - string bodyAsString = "Hello world!"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString); + var bodyAsString = "Hello world!"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -414,8 +414,8 @@ namespace WireMock.Net.Tests def xyz "; - byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString); + var body = Encoding.UTF8.GetBytes(xmlBodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -435,8 +435,8 @@ namespace WireMock.Net.Tests def xyz "; - byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString); + var body = Encoding.UTF8.GetBytes(xmlBodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -450,9 +450,9 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")); // when - string bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString); + var bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -466,9 +466,9 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")); // when - string bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString); + var bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -482,9 +482,9 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithBody(" Hello world! "); // when - string bodyAsString = "xxx"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary { { "X-toto", "tatata" } }); + var bodyAsString = "xxx"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "tatata" } }); // then var requestMatchResult = new RequestMatchResult(); diff --git a/test/WireMock.Net.Tests/ResponseTests.cs b/test/WireMock.Net.Tests/ResponseTests.cs index 663c6ed9..685e2e95 100644 --- a/test/WireMock.Net.Tests/ResponseTests.cs +++ b/test/WireMock.Net.Tests/ResponseTests.cs @@ -15,9 +15,9 @@ namespace WireMock.Net.Tests public async Task Response_ProvideResponse_Handlebars_UrlPathVerb() { // given - string bodyAsString = "abc"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString); + var bodyAsString = "abc"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); var response = Response.Create() .WithBody("test {{request.url}} {{request.path}} {{request.method}}") @@ -34,9 +34,9 @@ namespace WireMock.Net.Tests public async Task Response_ProvideResponse_Handlebars_Query() { // given - string bodyAsString = "abc"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo?a=1&a=2&b=5"), "POST", body, bodyAsString); + var bodyAsString = "abc"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo?a=1&a=2&b=5"), "POST", body, bodyAsString, Encoding.UTF8); var response = Response.Create() .WithBody("test keya={{request.query.a}} idx={{request.query.a.[0]}} idx={{request.query.a.[1]}} keyb={{request.query.b}}") @@ -53,9 +53,9 @@ namespace WireMock.Net.Tests public async Task Response_ProvideResponse_Handlebars_Headers() { // given - string bodyAsString = "abc"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, new Dictionary { { "Content-Type", "text/plain" } }); + var bodyAsString = "abc"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8, new Dictionary { { "Content-Type", "text/plain" } }); var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}").WithBody("test").WithTransformer(); @@ -66,5 +66,41 @@ namespace WireMock.Net.Tests Check.That(responseMessage.Body).Equals("test"); Check.That(responseMessage.Headers).Contains(new KeyValuePair("x", "text/plain")); } + + [Test] + public async Task Response_ProvideResponse_Encoding_Body() + { + // given + var bodyAsString = "abc"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); + + var response = Response.Create().WithBody("test", Encoding.ASCII); + + // act + var responseMessage = await response.ProvideResponse(request); + + // then + Check.That(responseMessage.Body).Equals("test"); + Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII); + } + + [Test] + public async Task Response_ProvideResponse_Encoding_JsonBody() + { + // given + var bodyAsString = "abc"; + var body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); + + var response = Response.Create().WithBodyAsJson(new { value = "test" }, Encoding.ASCII); + + // act + var responseMessage = await response.ProvideResponse(request); + + // then + Check.That(responseMessage.Body).Equals("{\"value\":\"test\"}"); + Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII); + } } } \ No newline at end of file From 6513ac9de8339f3ffdaafa4d38fb9324faeab9f6 Mon Sep 17 00:00:00 2001 From: Sebastian Bebrys Date: Thu, 23 Feb 2017 14:45:43 +0100 Subject: [PATCH 2/2] Body Encoding - admin interface --- .../Admin/Mappings/EncodingModel.cs | 23 +++++++ .../Admin/Mappings/ResponseModel.cs | 9 +++ .../Admin/Requests/LogRequestModel.cs | 10 +++ .../Admin/Requests/LogResponseModel.cs | 7 ++ src/WireMock.Net/HttpListenerRequestMapper.cs | 11 +-- .../HttpListenerResponseMapper.cs | 2 +- src/WireMock.Net/ResponseBuilders/Response.cs | 2 +- .../Server/FluentMockServer.Admin.cs | 37 ++++++++-- .../WireMock.Net.Tests/RequestMessageTests.cs | 4 +- test/WireMock.Net.Tests/RequestTests.cs | 68 +++++++++---------- test/WireMock.Net.Tests/ResponseTests.cs | 20 +++--- 11 files changed, 134 insertions(+), 59 deletions(-) create mode 100644 src/WireMock.Net/Admin/Mappings/EncodingModel.cs diff --git a/src/WireMock.Net/Admin/Mappings/EncodingModel.cs b/src/WireMock.Net/Admin/Mappings/EncodingModel.cs new file mode 100644 index 00000000..3987e3ca --- /dev/null +++ b/src/WireMock.Net/Admin/Mappings/EncodingModel.cs @@ -0,0 +1,23 @@ +using System.Text; + +namespace WireMock.Admin.Mappings +{ + /// + /// EncodingModel + /// + public class EncodingModel + { + /// + /// Encoding CodePage + /// + public int CodePage { get; set; } + /// + /// Encoding EncodingName + /// + public string EncodingName { get; set; } + /// + /// Encoding WebName + /// + public string WebName { get; set; } + } +} diff --git a/src/WireMock.Net/Admin/Mappings/ResponseModel.cs b/src/WireMock.Net/Admin/Mappings/ResponseModel.cs index 333db82b..f6c3fb27 100644 --- a/src/WireMock.Net/Admin/Mappings/ResponseModel.cs +++ b/src/WireMock.Net/Admin/Mappings/ResponseModel.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Text; namespace WireMock.Admin.Mappings { @@ -39,6 +40,14 @@ namespace WireMock.Admin.Mappings /// public object BodyAsJson { get; set; } + /// + /// Gets or sets the body encoding. + /// + /// + /// The body encoding. + /// + public EncodingModel BodyEncoding { get; set; } + /// /// Gets or sets a value indicating whether [use transformer]. /// diff --git a/src/WireMock.Net/Admin/Requests/LogRequestModel.cs b/src/WireMock.Net/Admin/Requests/LogRequestModel.cs index faa75c95..822e4d94 100644 --- a/src/WireMock.Net/Admin/Requests/LogRequestModel.cs +++ b/src/WireMock.Net/Admin/Requests/LogRequestModel.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Text; +using WireMock.Admin.Mappings; using WireMock.Util; namespace WireMock.Admin.Requests @@ -66,5 +68,13 @@ namespace WireMock.Admin.Requests /// The body. /// public string Body { get; set; } + + /// + /// Gets or sets the body encoding. + /// + /// + /// The body encoding. + /// + public EncodingModel BodyEncoding { get; set; } } } \ No newline at end of file diff --git a/src/WireMock.Net/Admin/Requests/LogResponseModel.cs b/src/WireMock.Net/Admin/Requests/LogResponseModel.cs index 1388ae4b..a5208da8 100644 --- a/src/WireMock.Net/Admin/Requests/LogResponseModel.cs +++ b/src/WireMock.Net/Admin/Requests/LogResponseModel.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using System.Text; +using WireMock.Admin.Mappings; namespace WireMock.Admin.Requests { @@ -26,5 +28,10 @@ namespace WireMock.Admin.Requests /// Gets or sets the original body. /// public string BodyOriginal { get; set; } + + /// + /// Gets or sets the body. + /// + public EncodingModel BodyEncoding { get; set; } } } \ No newline at end of file diff --git a/src/WireMock.Net/HttpListenerRequestMapper.cs b/src/WireMock.Net/HttpListenerRequestMapper.cs index 1aaa7b9d..d0d6a948 100644 --- a/src/WireMock.Net/HttpListenerRequestMapper.cs +++ b/src/WireMock.Net/HttpListenerRequestMapper.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; +using System.Text; namespace WireMock { @@ -18,11 +19,11 @@ namespace WireMock /// The . public RequestMessage Map(HttpListenerRequest listenerRequest) { - var url = listenerRequest.Url; - var verb = listenerRequest.HttpMethod; - var body = GetRequestBody(listenerRequest); - var bodyEncoding = body != null ? listenerRequest.ContentEncoding : null; - var bodyAsString = bodyEncoding?.GetString(body); + Uri url = listenerRequest.Url; + string verb = listenerRequest.HttpMethod; + byte[] body = GetRequestBody(listenerRequest); + Encoding bodyEncoding = body != null ? listenerRequest.ContentEncoding : null; + string bodyAsString = bodyEncoding?.GetString(body); var listenerHeaders = listenerRequest.Headers; var headers = listenerHeaders.AllKeys.ToDictionary(k => k, k => listenerHeaders[k]); var cookies = new Dictionary(); diff --git a/src/WireMock.Net/HttpListenerResponseMapper.cs b/src/WireMock.Net/HttpListenerResponseMapper.cs index 87dc9a27..f7f5e28b 100644 --- a/src/WireMock.Net/HttpListenerResponseMapper.cs +++ b/src/WireMock.Net/HttpListenerResponseMapper.cs @@ -28,7 +28,7 @@ namespace WireMock return; var encoding = responseMessage.BodyEncoding ?? _utf8NoBom; - var buffer = encoding.GetBytes(responseMessage.Body); + byte[] buffer = encoding.GetBytes(responseMessage.Body); listenerResponse.ContentEncoding = encoding; listenerResponse.ContentLength64 = buffer.Length; diff --git a/src/WireMock.Net/ResponseBuilders/Response.cs b/src/WireMock.Net/ResponseBuilders/Response.cs index 2bf6c5ca..b0351c8e 100644 --- a/src/WireMock.Net/ResponseBuilders/Response.cs +++ b/src/WireMock.Net/ResponseBuilders/Response.cs @@ -165,7 +165,7 @@ namespace WireMock.ResponseBuilders { Check.NotNull(body, nameof(body)); - var jsonBody = JsonConvert.SerializeObject(body, new JsonSerializerSettings { Formatting = Formatting.None, NullValueHandling = NullValueHandling.Ignore }); + string jsonBody = JsonConvert.SerializeObject(body, new JsonSerializerSettings { Formatting = Formatting.None, NullValueHandling = NullValueHandling.Ignore }); if (encoding != null && !encoding.Equals(Encoding.UTF8)) { diff --git a/src/WireMock.Net/Server/FluentMockServer.Admin.cs b/src/WireMock.Net/Server/FluentMockServer.Admin.cs index f884e5c7..4a463f4d 100644 --- a/src/WireMock.Net/Server/FluentMockServer.Admin.cs +++ b/src/WireMock.Net/Server/FluentMockServer.Admin.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; using JetBrains.Annotations; using Newtonsoft.Json; using SimMetrics.Net; @@ -292,14 +293,26 @@ namespace WireMock.Server Method = logEntry.RequestMessage.Method, Body = logEntry.RequestMessage.Body, Headers = logEntry.RequestMessage.Headers, - Cookies = logEntry.RequestMessage.Cookies + Cookies = logEntry.RequestMessage.Cookies, + BodyEncoding = logEntry.RequestMessage.BodyEncoding != null ? new EncodingModel + { + EncodingName = logEntry.RequestMessage.BodyEncoding.EncodingName, + CodePage = logEntry.RequestMessage.BodyEncoding.CodePage, + WebName = logEntry.RequestMessage.BodyEncoding.WebName + } : null }, Response = new LogResponseModel { StatusCode = logEntry.ResponseMessage.StatusCode, Body = logEntry.ResponseMessage.Body, BodyOriginal = logEntry.ResponseMessage.BodyOriginal, - Headers = logEntry.ResponseMessage.Headers + Headers = logEntry.ResponseMessage.Headers, + BodyEncoding = logEntry.ResponseMessage.BodyEncoding != null ? new EncodingModel + { + EncodingName = logEntry.ResponseMessage.BodyEncoding.EncodingName, + CodePage = logEntry.ResponseMessage.BodyEncoding.CodePage, + WebName = logEntry.ResponseMessage.BodyEncoding.WebName + } : null }, MappingGuid = logEntry.MappingGuid, RequestMatchResult = logEntry.RequestMatchResult != null ? new LogRequestMatchModel @@ -418,11 +431,11 @@ namespace WireMock.Server responseBuilder = responseBuilder.WithHeaders(responseModel.Headers); if (responseModel.Body != null) - responseBuilder = responseBuilder.WithBody(responseModel.Body); + responseBuilder = responseBuilder.WithBody(responseModel.Body, ToEncoding(responseModel.BodyEncoding)); else if (responseModel.BodyAsJson != null) - responseBuilder = responseBuilder.WithBodyAsJson(responseModel.BodyAsJson); + responseBuilder = responseBuilder.WithBodyAsJson(responseModel.BodyAsJson, ToEncoding(responseModel.BodyEncoding)); else if (responseModel.BodyAsBase64 != null) - responseBuilder = responseBuilder.WithBodyAsBase64(responseModel.BodyAsBase64); + responseBuilder = responseBuilder.WithBodyAsBase64(responseModel.BodyAsBase64, ToEncoding(responseModel.BodyEncoding)); if (responseModel.UseTransformer) responseBuilder = responseBuilder.WithTransformer(); @@ -500,7 +513,14 @@ namespace WireMock.Server Headers = response.ResponseMessage.Headers, Body = response.ResponseMessage.Body, UseTransformer = response.UseTransformer, - Delay = response.Delay?.Milliseconds + Delay = response.Delay?.Milliseconds, + + BodyEncoding = response.ResponseMessage.BodyEncoding != null ? new EncodingModel + { + EncodingName = response.ResponseMessage.BodyEncoding.EncodingName, + CodePage = response.ResponseMessage.BodyEncoding.CodePage, + WebName = response.ResponseMessage.BodyEncoding.WebName + } : null } }; } @@ -590,5 +610,10 @@ namespace WireMock.Server Headers = new Dictionary { { "Content-Type", "application/json" } } }; } + + private Encoding ToEncoding(EncodingModel encodingModel) + { + return encodingModel != null ? Encoding.GetEncoding(encodingModel.CodePage) : null; + } } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/RequestMessageTests.cs b/test/WireMock.Net.Tests/RequestMessageTests.cs index 55ca493c..26b61236 100644 --- a/test/WireMock.Net.Tests/RequestMessageTests.cs +++ b/test/WireMock.Net.Tests/RequestMessageTests.cs @@ -22,8 +22,8 @@ namespace WireMock.Net.Tests public void Should_parse_query_params() { // given - var bodyAsString = "whatever"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "whatever"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost?foo=bar&multi=1&multi=2"), "POST", body, bodyAsString, Encoding.UTF8); // then diff --git a/test/WireMock.Net.Tests/RequestTests.cs b/test/WireMock.Net.Tests/RequestTests.cs index f0663b6b..033a6a9d 100644 --- a/test/WireMock.Net.Tests/RequestTests.cs +++ b/test/WireMock.Net.Tests/RequestTests.cs @@ -144,8 +144,8 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath("/foo").UsingDelete(); // when - var bodyAsString = "whatever"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "whatever"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", body, bodyAsString, Encoding.UTF8); // then @@ -202,8 +202,8 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithHeader("X-toto", "tata"); // when - var bodyAsString = "whatever"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "whatever"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "tata" } }); // then @@ -218,8 +218,8 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithHeader("X-toto", "tatata"); // when - var bodyAsString = "whatever"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "whatever"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "tata" } }); // then @@ -234,8 +234,8 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithHeader("X-toto", "abc", false); // when - var bodyAsString = "whatever"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "whatever"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "ABC" } }); // then @@ -250,8 +250,8 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithHeader("X-toto", "tata*"); // when - var bodyAsString = "whatever"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "whatever"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "TaTa" } }); // then @@ -280,8 +280,8 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithBody("Hello world!"); // when - var bodyAsString = "Hello world!"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "Hello world!"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8); // then @@ -296,8 +296,8 @@ namespace WireMock.Net.Tests var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new ExactMatcher("cat")); // when - var bodyAsString = "cat"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "cat"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); // then @@ -312,8 +312,8 @@ namespace WireMock.Net.Tests var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new ExactMatcher("cat", "dog")); // when - var bodyAsString = "cat"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "cat"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); // then @@ -328,8 +328,8 @@ namespace WireMock.Net.Tests var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new ExactMatcher("cat")); // when - var bodyAsString = "caR"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "caR"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); // then @@ -344,8 +344,8 @@ namespace WireMock.Net.Tests var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new SimMetricsMatcher("The cat walks in the street.")); // when - var bodyAsString = "The car drives in the street."; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "The car drives in the street."; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); // then @@ -360,8 +360,8 @@ namespace WireMock.Net.Tests var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new SimMetricsMatcher("The cat walks in the street.")); // when - var bodyAsString = "Hello"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "Hello"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); // then @@ -376,8 +376,8 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithBody(new WildcardMatcher("H*o*")); // when - var bodyAsString = "Hello world!"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "Hello world!"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "tatata" } }); // then @@ -392,8 +392,8 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithBody(new RegexMatcher("H.*o")); // when - var bodyAsString = "Hello world!"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "Hello world!"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8); // then @@ -414,7 +414,7 @@ namespace WireMock.Net.Tests def xyz "; - var body = Encoding.UTF8.GetBytes(xmlBodyAsString); + byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString, Encoding.UTF8); // then @@ -435,7 +435,7 @@ namespace WireMock.Net.Tests def xyz "; - var body = Encoding.UTF8.GetBytes(xmlBodyAsString); + byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString, Encoding.UTF8); // then @@ -450,8 +450,8 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")); // when - var bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8); // then @@ -466,8 +466,8 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")); // when - var bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8); // then @@ -482,8 +482,8 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithBody(" Hello world! "); // when - var bodyAsString = "xxx"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "xxx"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "tatata" } }); // then diff --git a/test/WireMock.Net.Tests/ResponseTests.cs b/test/WireMock.Net.Tests/ResponseTests.cs index 685e2e95..a0b7bd44 100644 --- a/test/WireMock.Net.Tests/ResponseTests.cs +++ b/test/WireMock.Net.Tests/ResponseTests.cs @@ -15,8 +15,8 @@ namespace WireMock.Net.Tests public async Task Response_ProvideResponse_Handlebars_UrlPathVerb() { // given - var bodyAsString = "abc"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "abc"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); var response = Response.Create() @@ -34,8 +34,8 @@ namespace WireMock.Net.Tests public async Task Response_ProvideResponse_Handlebars_Query() { // given - var bodyAsString = "abc"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "abc"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo?a=1&a=2&b=5"), "POST", body, bodyAsString, Encoding.UTF8); var response = Response.Create() @@ -53,8 +53,8 @@ namespace WireMock.Net.Tests public async Task Response_ProvideResponse_Handlebars_Headers() { // given - var bodyAsString = "abc"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "abc"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8, new Dictionary { { "Content-Type", "text/plain" } }); var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}").WithBody("test").WithTransformer(); @@ -71,8 +71,8 @@ namespace WireMock.Net.Tests public async Task Response_ProvideResponse_Encoding_Body() { // given - var bodyAsString = "abc"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "abc"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); var response = Response.Create().WithBody("test", Encoding.ASCII); @@ -89,8 +89,8 @@ namespace WireMock.Net.Tests public async Task Response_ProvideResponse_Encoding_JsonBody() { // given - var bodyAsString = "abc"; - var body = Encoding.UTF8.GetBytes(bodyAsString); + string bodyAsString = "abc"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8); var response = Response.Create().WithBodyAsJson(new { value = "test" }, Encoding.ASCII);