From 0d046daac587f2ca042230d39ec83af1f14acbb6 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Fri, 20 Jan 2017 13:04:58 +0100 Subject: [PATCH] WithBodyAsBase64 (#14) --- .../ResponseBuilders/IBodyResponseBuilder.cs | 23 ++++++++++------ src/WireMock/ResponseBuilders/Response.cs | 20 ++++++++++++++ .../FluentMockServerTests.cs | 26 ++++++++++++++----- 3 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/WireMock/ResponseBuilders/IBodyResponseBuilder.cs b/src/WireMock/ResponseBuilders/IBodyResponseBuilder.cs index 2c09d673..11d7bd43 100644 --- a/src/WireMock/ResponseBuilders/IBodyResponseBuilder.cs +++ b/src/WireMock/ResponseBuilders/IBodyResponseBuilder.cs @@ -1,4 +1,7 @@ -namespace WireMock.ResponseBuilders +using System.Text; +using JetBrains.Annotations; + +namespace WireMock.ResponseBuilders { /// /// The BodyResponseBuilder interface. @@ -8,12 +11,16 @@ /// /// The with body. /// - /// - /// The body. - /// - /// - /// The . - /// - IResponseBuilder WithBody(string body); + /// The body. + /// A . + IResponseBuilder WithBody([NotNull] string body); + + /// + /// The with body as base64. + /// + /// The body asbase64. + /// The Encoding. + /// A . + IResponseBuilder WithBodyAsBase64([NotNull] string bodyAsbase64, [CanBeNull] Encoding encoding = null); } } \ No newline at end of file diff --git a/src/WireMock/ResponseBuilders/Response.cs b/src/WireMock/ResponseBuilders/Response.cs index b69f3975..6aafd92a 100644 --- a/src/WireMock/ResponseBuilders/Response.cs +++ b/src/WireMock/ResponseBuilders/Response.cs @@ -1,8 +1,12 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Text; using System.Threading.Tasks; +using System.Xml; using HandlebarsDotNet; +using JetBrains.Annotations; +using WireMock.Validation; [module: SuppressMessage("StyleCop.CSharp.ReadabilityRules", @@ -146,10 +150,26 @@ namespace WireMock.ResponseBuilders /// public IResponseBuilder WithBody(string body) { + Check.NotNull(body, nameof(body)); + _responseMessage.Body = body; return this; } + /// + /// The with body as base64. + /// + /// The body asbase64. + /// + /// A . + public IResponseBuilder WithBodyAsBase64(string bodyAsbase64, Encoding encoding = null) + { + Check.NotNull(bodyAsbase64, nameof(bodyAsbase64)); + + _responseMessage.Body = (encoding ?? Encoding.UTF8).GetString(Convert.FromBase64String(bodyAsbase64)); + return this; + } + /// /// The with transformer. /// diff --git a/test/WireMock.Net.Tests/FluentMockServerTests.cs b/test/WireMock.Net.Tests/FluentMockServerTests.cs index 2289f523..54de33df 100644 --- a/test/WireMock.Net.Tests/FluentMockServerTests.cs +++ b/test/WireMock.Net.Tests/FluentMockServerTests.cs @@ -52,13 +52,27 @@ namespace WireMock.Net.Tests .WithBody(@"{ msg: ""Hello world!""}")); // when - var response - = await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo"); + var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo"); // then Check.That(response).IsEqualTo(@"{ msg: ""Hello world!""}"); } + [Test] + public async Task Should_respond_to_request_bodyAsBase64() + { + // given + _server = FluentMockServer.Start(); + + _server.Given(Request.WithUrl("/foo").UsingGet()).RespondWith(Response.WithSuccess().WithBodyAsBase64("SGVsbG8gV29ybGQ/")); + + // when + var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo"); + + // then + Check.That(response).IsEqualTo("Hello World?"); + } + [Test] public async Task Should_respond_404_for_unexpected_request() { @@ -66,8 +80,7 @@ namespace WireMock.Net.Tests _server = FluentMockServer.Start(); // when - var response - = await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo"); + var response = await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo"); // then Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound); @@ -167,8 +180,7 @@ namespace WireMock.Net.Tests .WithBody("REDIRECT SUCCESSFUL")); // when - var response - = await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo"); + var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo"); // then Check.That(response).IsEqualTo("REDIRECT SUCCESSFUL"); @@ -227,4 +239,4 @@ namespace WireMock.Net.Tests _server.Stop(); } } -} +} \ No newline at end of file