mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-14 14:22:02 +01:00
ContentType "application/vnd.api+json" is not recognized as json (#186)
* Admin API GetRequestsAsync Json/JsonApi request body tests * Fix code + update unit-tests
This commit is contained in:
committed by
Stef Heyenrath
parent
b57d118c3d
commit
fb6b25a9c5
@@ -10,6 +10,12 @@ namespace WireMock.Util
|
||||
{
|
||||
internal static class BodyParser
|
||||
{
|
||||
private static readonly string[] JsonContentTypes =
|
||||
{
|
||||
"application/json",
|
||||
"application/vnd.api+json"
|
||||
};
|
||||
|
||||
private static readonly string[] TextContentTypes =
|
||||
{
|
||||
"text/",
|
||||
@@ -41,7 +47,7 @@ namespace WireMock.Util
|
||||
{
|
||||
var data = new BodyData();
|
||||
|
||||
if (contentTypeHeaderValue != null && TextContentTypes.Any(t => contentTypeHeaderValue.StartsWith(t, StringComparison.OrdinalIgnoreCase)))
|
||||
if (contentTypeHeaderValue != null && TextContentTypes.Any(text => contentTypeHeaderValue.StartsWith(text, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -55,7 +61,7 @@ namespace WireMock.Util
|
||||
data.BodyAsBytes = await ReadBytesAsync(stream);
|
||||
}
|
||||
}
|
||||
else if (contentTypeHeaderValue != null && contentTypeHeaderValue.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
|
||||
else if (contentTypeHeaderValue != null && JsonContentTypes.Any(json => contentTypeHeaderValue.StartsWith(json, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
var stringData = await ReadStringAsync(stream);
|
||||
data.BodyAsString = stringData.Item1;
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using RestEase;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Client;
|
||||
using WireMock.Server;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
{
|
||||
// TODO : move these to FluentMockServerAdminRestClientTests
|
||||
public class ClientTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Client_IFluentMockServerAdmin_SettingsGet()
|
||||
{
|
||||
// Assign
|
||||
var server = FluentMockServer.StartWithAdminInterface();
|
||||
var api = RestClient.For<IFluentMockServerAdmin>(server.Urls[0]);
|
||||
|
||||
// Act
|
||||
var settings = await api.GetSettingsAsync();
|
||||
Check.That(settings).IsNotNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Client_IFluentMockServerAdmin_PostMappingAsync()
|
||||
{
|
||||
// Assign
|
||||
var server = FluentMockServer.StartWithAdminInterface();
|
||||
var api = RestClient.For<IFluentMockServerAdmin>(server.Urls[0]);
|
||||
|
||||
// Act
|
||||
var model = new MappingModel
|
||||
{
|
||||
Request = new RequestModel
|
||||
{
|
||||
Path = "/1"
|
||||
},
|
||||
Response = new ResponseModel
|
||||
{
|
||||
Body = "txt",
|
||||
StatusCode = 200
|
||||
},
|
||||
Priority = 500,
|
||||
Title = "test"
|
||||
};
|
||||
var result = await api.PostMappingAsync(model);
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsNotNull();
|
||||
Check.That(result.Status).IsNotNull();
|
||||
Check.That(result.Guid).IsNotNull();
|
||||
|
||||
var mapping = server.Mappings.Single(m => m.Priority == 500);
|
||||
Check.That(mapping).IsNotNull();
|
||||
Check.That(mapping.Title).Equals("test");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using RestEase;
|
||||
@@ -15,11 +15,63 @@ namespace WireMock.Net.Tests
|
||||
{
|
||||
public class FluentMockServerAdminRestClientTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task IFluentMockServerAdmin_GetSettingsAsync()
|
||||
{
|
||||
// Assign
|
||||
var server = FluentMockServer.StartWithAdminInterface();
|
||||
var api = RestClient.For<IFluentMockServerAdmin>(server.Urls[0]);
|
||||
|
||||
// Act
|
||||
var settings = await api.GetSettingsAsync();
|
||||
Check.That(settings).IsNotNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IFluentMockServerAdmin_PostMappingAsync()
|
||||
{
|
||||
// Assign
|
||||
var server = FluentMockServer.StartWithAdminInterface();
|
||||
var api = RestClient.For<IFluentMockServerAdmin>(server.Urls[0]);
|
||||
|
||||
// Act
|
||||
var model = new MappingModel
|
||||
{
|
||||
Request = new RequestModel
|
||||
{
|
||||
Path = "/1"
|
||||
},
|
||||
Response = new ResponseModel
|
||||
{
|
||||
Body = "txt",
|
||||
StatusCode = 200
|
||||
},
|
||||
Priority = 500,
|
||||
Title = "test"
|
||||
};
|
||||
var result = await api.PostMappingAsync(model);
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsNotNull();
|
||||
Check.That(result.Status).IsNotNull();
|
||||
Check.That(result.Guid).IsNotNull();
|
||||
|
||||
var mapping = server.Mappings.Single(m => m.Priority == 500);
|
||||
Check.That(mapping).IsNotNull();
|
||||
Check.That(mapping.Title).Equals("test");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IFluentMockServerAdmin_FindRequestsAsync()
|
||||
{
|
||||
// given
|
||||
var server = FluentMockServer.Start(new FluentMockServerSettings { StartAdminInterface = true, Logger = new WireMockNullLogger() });
|
||||
var server = FluentMockServer.Start(new FluentMockServerSettings
|
||||
{
|
||||
StartAdminInterface = true,
|
||||
Logger = new WireMockNullLogger()
|
||||
});
|
||||
var serverUrl = "http://localhost:" + server.Ports[0];
|
||||
await new HttpClient().GetAsync(serverUrl + "/foo");
|
||||
var api = RestClient.For<IFluentMockServerAdmin>(serverUrl);
|
||||
@@ -39,7 +91,11 @@ namespace WireMock.Net.Tests
|
||||
public async Task IFluentMockServerAdmin_GetRequestsAsync()
|
||||
{
|
||||
// given
|
||||
var server = FluentMockServer.Start(new FluentMockServerSettings { StartAdminInterface = true, Logger = new WireMockNullLogger() });
|
||||
var server = FluentMockServer.Start(new FluentMockServerSettings
|
||||
{
|
||||
StartAdminInterface = true,
|
||||
Logger = new WireMockNullLogger()
|
||||
});
|
||||
var serverUrl = "http://localhost:" + server.Ports[0];
|
||||
await new HttpClient().GetAsync(serverUrl + "/foo");
|
||||
var api = RestClient.For<IFluentMockServerAdmin>(serverUrl);
|
||||
@@ -54,5 +110,75 @@ namespace WireMock.Net.Tests
|
||||
Check.That(requestLogged.Request.Body).IsNull();
|
||||
Check.That(requestLogged.Request.Path).IsEqualTo("/foo");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IFluentMockServerAdmin_GetRequestsAsync_JsonApi()
|
||||
{
|
||||
// given
|
||||
var server = FluentMockServer.Start(new FluentMockServerSettings
|
||||
{
|
||||
StartAdminInterface = true,
|
||||
Logger = new WireMockNullLogger()
|
||||
});
|
||||
string serverUrl = server.Urls[0];
|
||||
string data = "{\"data\":[{\"type\":\"program\",\"attributes\":{\"alias\":\"T000001\",\"title\":\"Title Group Entity\"}}]}";
|
||||
string jsonApiAcceptHeader = "application/vnd.api+json";
|
||||
string jsonApiContentType = "application/vnd.api+json";
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, serverUrl);
|
||||
request.Headers.Accept.Clear();
|
||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(jsonApiAcceptHeader));
|
||||
request.Content = new StringContent(data);
|
||||
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(jsonApiContentType);
|
||||
|
||||
var response = await new HttpClient().SendAsync(request);
|
||||
|
||||
var api = RestClient.For<IFluentMockServerAdmin>(serverUrl);
|
||||
|
||||
// when
|
||||
var requests = await api.GetRequestsAsync();
|
||||
|
||||
// then
|
||||
Check.That(requests).HasSize(1);
|
||||
var requestLogged = requests.First();
|
||||
Check.That(requestLogged.Request.Method).IsEqualTo("post");
|
||||
Check.That(requestLogged.Request.Body).IsNotNull();
|
||||
Check.That(requestLogged.Request.Body).Contains("T000001");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IFluentMockServerAdmin_GetRequestsAsync_Json()
|
||||
{
|
||||
// given
|
||||
var server = FluentMockServer.Start(new FluentMockServerSettings
|
||||
{
|
||||
StartAdminInterface = true,
|
||||
Logger = new WireMockNullLogger()
|
||||
});
|
||||
string serverUrl = server.Urls[0];
|
||||
string data = "{\"alias\": \"T000001\"}";
|
||||
string jsonAcceptHeader = "application/json";
|
||||
string jsonApiContentType = "application/json";
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, serverUrl);
|
||||
request.Headers.Accept.Clear();
|
||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(jsonAcceptHeader));
|
||||
request.Content = new StringContent(data);
|
||||
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(jsonApiContentType);
|
||||
var response = await new HttpClient().SendAsync(request);
|
||||
|
||||
var api = RestClient.For<IFluentMockServerAdmin>(serverUrl);
|
||||
|
||||
// when
|
||||
var requests = await api.GetRequestsAsync();
|
||||
|
||||
// then
|
||||
Check.That(requests).HasSize(1);
|
||||
var requestLogged = requests.First();
|
||||
Check.That(requestLogged.Request.Method).IsEqualTo("post");
|
||||
Check.That(requestLogged.Request.Body).IsNotNull();
|
||||
Check.That(requestLogged.Request.Body).Contains("T000001");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,6 +9,36 @@ namespace WireMock.Net.Tests.Util
|
||||
{
|
||||
public class BodyParserTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task BodyParser_Parse_ApplicationJson()
|
||||
{
|
||||
// Assign
|
||||
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes("{ \"x\": 1 }"));
|
||||
|
||||
// Act
|
||||
var body = await BodyParser.Parse(memoryStream, "application/json");
|
||||
|
||||
// Assert
|
||||
Check.That(body.BodyAsBytes).IsNull();
|
||||
Check.That(body.BodyAsJson).IsNotNull();
|
||||
Check.That(body.BodyAsString).Equals("{ \"x\": 1 }");
|
||||
}
|
||||
|
||||
[Fact] // http://jsonapi.org/
|
||||
public async Task BodyParser_Parse_ApplicationJsonApi()
|
||||
{
|
||||
// Assign
|
||||
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes("{ \"x\": 1 }"));
|
||||
|
||||
// Act
|
||||
var body = await BodyParser.Parse(memoryStream, "application/vnd.api+json");
|
||||
|
||||
// Assert
|
||||
Check.That(body.BodyAsBytes).IsNull();
|
||||
Check.That(body.BodyAsJson).IsNotNull();
|
||||
Check.That(body.BodyAsString).Equals("{ \"x\": 1 }");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BodyParser_Parse_ApplicationXml()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user