mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-24 02:12:02 +01:00
@@ -1,9 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using FluentAssertions;
|
||||
using WireMock.Models;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Serialization;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Serialization
|
||||
@@ -25,7 +29,26 @@ namespace WireMock.Net.Tests.Serialization
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
var response = Response.Create();
|
||||
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 0, null, null, null, null);
|
||||
var webhook = new Webhook
|
||||
{
|
||||
Request = new WebhookRequest
|
||||
{
|
||||
Url = "https://test.com",
|
||||
Headers = new Dictionary<string, WireMockList<string>>
|
||||
{
|
||||
{ "Single", new WireMockList<string>("x") },
|
||||
{ "Multi", new WireMockList<string>("a", "b") }
|
||||
},
|
||||
Method = "post",
|
||||
BodyData = new BodyData
|
||||
{
|
||||
BodyAsString = "b",
|
||||
DetectedBodyType = BodyType.String,
|
||||
DetectedBodyTypeFromContentType = BodyType.String
|
||||
}
|
||||
}
|
||||
};
|
||||
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 0, null, null, null, null, webhook);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -33,9 +56,16 @@ namespace WireMock.Net.Tests.Serialization
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Priority.Should().BeNull();
|
||||
|
||||
model.Response.BodyAsJsonIndented.Should().BeNull();
|
||||
model.Response.UseTransformer.Should().BeNull();
|
||||
model.Response.Headers.Should().BeNull();
|
||||
|
||||
model.Webhook.Request.Method.Should().Be("post");
|
||||
model.Webhook.Request.Url.Should().Be("https://test.com");
|
||||
model.Webhook.Request.Headers.Should().HaveCount(2);
|
||||
model.Webhook.Request.Body.Should().Be("b");
|
||||
model.Webhook.Request.BodyAsJson.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -44,7 +74,7 @@ namespace WireMock.Net.Tests.Serialization
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithBodyAsJson(new { x = "x" }).WithTransformer();
|
||||
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null);
|
||||
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
101
test/WireMock.Net.Tests/Serialization/WebhookMapperTests.cs
Normal file
101
test/WireMock.Net.Tests/Serialization/WebhookMapperTests.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System.Collections.Generic;
|
||||
using FluentAssertions;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Serialization;
|
||||
using WireMock.Types;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Serialization
|
||||
{
|
||||
public class WebhookMapperTests
|
||||
{
|
||||
[Fact]
|
||||
public void WebhookMapper_Map_Model_BodyAsString_And_UseTransformerIsFalse()
|
||||
{
|
||||
// Assign
|
||||
var model = new WebhookModel
|
||||
{
|
||||
Request = new WebhookRequestModel
|
||||
{
|
||||
Url = "https://localhost",
|
||||
Method = "get",
|
||||
Headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "x", "y" }
|
||||
},
|
||||
Body = "test",
|
||||
UseTransformer = false
|
||||
}
|
||||
};
|
||||
|
||||
var result = WebhookMapper.Map(model);
|
||||
|
||||
result.Request.Url.Should().Be("https://localhost");
|
||||
result.Request.Method.Should().Be("get");
|
||||
result.Request.Headers.Should().HaveCount(1);
|
||||
result.Request.BodyData.BodyAsJson.Should().BeNull();
|
||||
result.Request.BodyData.BodyAsString.Should().Be("test");
|
||||
result.Request.BodyData.DetectedBodyType.Should().Be(BodyType.String);
|
||||
result.Request.UseTransformer.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebhookMapper_Map_Model_BodyAsString_And_UseTransformerIsTrue()
|
||||
{
|
||||
// Assign
|
||||
var model = new WebhookModel
|
||||
{
|
||||
Request = new WebhookRequestModel
|
||||
{
|
||||
Url = "https://localhost",
|
||||
Method = "get",
|
||||
Headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "x", "y" }
|
||||
},
|
||||
Body = "test",
|
||||
UseTransformer = true
|
||||
}
|
||||
};
|
||||
|
||||
var result = WebhookMapper.Map(model);
|
||||
|
||||
result.Request.Url.Should().Be("https://localhost");
|
||||
result.Request.Method.Should().Be("get");
|
||||
result.Request.Headers.Should().HaveCount(1);
|
||||
result.Request.BodyData.BodyAsJson.Should().BeNull();
|
||||
result.Request.BodyData.BodyAsString.Should().Be("test");
|
||||
result.Request.BodyData.DetectedBodyType.Should().Be(BodyType.String);
|
||||
result.Request.UseTransformer.Should().BeTrue();
|
||||
result.Request.TransformerType.Should().Be(TransformerType.Handlebars);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebhookMapper_Map_Model_BodyAsJson()
|
||||
{
|
||||
// Assign
|
||||
var model = new WebhookModel
|
||||
{
|
||||
Request = new WebhookRequestModel
|
||||
{
|
||||
Url = "https://localhost",
|
||||
Method = "get",
|
||||
Headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "x", "y" }
|
||||
},
|
||||
BodyAsJson = new { n = 12345 }
|
||||
}
|
||||
};
|
||||
|
||||
var result = WebhookMapper.Map(model);
|
||||
|
||||
result.Request.Url.Should().Be("https://localhost");
|
||||
result.Request.Method.Should().Be("get");
|
||||
result.Request.Headers.Should().HaveCount(1);
|
||||
result.Request.BodyData.BodyAsString.Should().BeNull();
|
||||
result.Request.BodyData.BodyAsJson.Should().NotBeNull();
|
||||
result.Request.BodyData.DetectedBodyType.Should().Be(BodyType.Json);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user