mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-24 09:18:27 +02:00
WithBodyAsBase64 (#14)
This commit is contained in:
@@ -1,4 +1,7 @@
|
|||||||
namespace WireMock.ResponseBuilders
|
using System.Text;
|
||||||
|
using JetBrains.Annotations;
|
||||||
|
|
||||||
|
namespace WireMock.ResponseBuilders
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The BodyResponseBuilder interface.
|
/// The BodyResponseBuilder interface.
|
||||||
@@ -8,12 +11,16 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The with body.
|
/// The with body.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="body">
|
/// <param name="body">The body.</param>
|
||||||
/// The body.
|
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||||
/// </param>
|
IResponseBuilder WithBody([NotNull] string body);
|
||||||
/// <returns>
|
|
||||||
/// The <see cref="IResponseBuilder"/>.
|
/// <summary>
|
||||||
/// </returns>
|
/// The with body as base64.
|
||||||
IResponseBuilder WithBody(string body);
|
/// </summary>
|
||||||
|
/// <param name="bodyAsbase64">The body asbase64.</param>
|
||||||
|
/// <param name="encoding">The Encoding.</param>
|
||||||
|
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||||
|
IResponseBuilder WithBodyAsBase64([NotNull] string bodyAsbase64, [CanBeNull] Encoding encoding = null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Xml;
|
||||||
using HandlebarsDotNet;
|
using HandlebarsDotNet;
|
||||||
|
using JetBrains.Annotations;
|
||||||
|
using WireMock.Validation;
|
||||||
|
|
||||||
[module:
|
[module:
|
||||||
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
|
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
|
||||||
@@ -146,10 +150,26 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// </returns>
|
/// </returns>
|
||||||
public IResponseBuilder WithBody(string body)
|
public IResponseBuilder WithBody(string body)
|
||||||
{
|
{
|
||||||
|
Check.NotNull(body, nameof(body));
|
||||||
|
|
||||||
_responseMessage.Body = body;
|
_responseMessage.Body = body;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The with body as base64.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bodyAsbase64">The body asbase64.</param>
|
||||||
|
/// <param name="encoding"></param>
|
||||||
|
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||||
|
public IResponseBuilder WithBodyAsBase64(string bodyAsbase64, Encoding encoding = null)
|
||||||
|
{
|
||||||
|
Check.NotNull(bodyAsbase64, nameof(bodyAsbase64));
|
||||||
|
|
||||||
|
_responseMessage.Body = (encoding ?? Encoding.UTF8).GetString(Convert.FromBase64String(bodyAsbase64));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The with transformer.
|
/// The with transformer.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -52,13 +52,27 @@ namespace WireMock.Net.Tests
|
|||||||
.WithBody(@"{ msg: ""Hello world!""}"));
|
.WithBody(@"{ msg: ""Hello world!""}"));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var response
|
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo");
|
||||||
= await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo");
|
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Check.That(response).IsEqualTo(@"{ msg: ""Hello world!""}");
|
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]
|
[Test]
|
||||||
public async Task Should_respond_404_for_unexpected_request()
|
public async Task Should_respond_404_for_unexpected_request()
|
||||||
{
|
{
|
||||||
@@ -66,8 +80,7 @@ namespace WireMock.Net.Tests
|
|||||||
_server = FluentMockServer.Start();
|
_server = FluentMockServer.Start();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var response
|
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo");
|
||||||
= await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo");
|
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound);
|
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound);
|
||||||
@@ -167,8 +180,7 @@ namespace WireMock.Net.Tests
|
|||||||
.WithBody("REDIRECT SUCCESSFUL"));
|
.WithBody("REDIRECT SUCCESSFUL"));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var response
|
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo");
|
||||||
= await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo");
|
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Check.That(response).IsEqualTo("REDIRECT SUCCESSFUL");
|
Check.That(response).IsEqualTo("REDIRECT SUCCESSFUL");
|
||||||
@@ -227,4 +239,4 @@ namespace WireMock.Net.Tests
|
|||||||
_server.Stop();
|
_server.Stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user