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:
Steve Land
2018-08-17 18:15:01 +01:00
committed by Stef Heyenrath
parent b57d118c3d
commit fb6b25a9c5
4 changed files with 168 additions and 69 deletions
+8 -2
View File
@@ -10,6 +10,12 @@ namespace WireMock.Util
{ {
internal static class BodyParser internal static class BodyParser
{ {
private static readonly string[] JsonContentTypes =
{
"application/json",
"application/vnd.api+json"
};
private static readonly string[] TextContentTypes = private static readonly string[] TextContentTypes =
{ {
"text/", "text/",
@@ -41,7 +47,7 @@ namespace WireMock.Util
{ {
var data = new BodyData(); 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 try
{ {
@@ -55,7 +61,7 @@ namespace WireMock.Util
data.BodyAsBytes = await ReadBytesAsync(stream); 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); var stringData = await ReadStringAsync(stream);
data.BodyAsString = stringData.Item1; data.BodyAsString = stringData.Item1;
-63
View File
@@ -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;
using System.Net.Http.Headers;
using System.Threading.Tasks; using System.Threading.Tasks;
using NFluent; using NFluent;
using RestEase; using RestEase;
@@ -15,11 +15,63 @@ namespace WireMock.Net.Tests
{ {
public class FluentMockServerAdminRestClientTests 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] [Fact]
public async Task IFluentMockServerAdmin_FindRequestsAsync() public async Task IFluentMockServerAdmin_FindRequestsAsync()
{ {
// given // 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]; var serverUrl = "http://localhost:" + server.Ports[0];
await new HttpClient().GetAsync(serverUrl + "/foo"); await new HttpClient().GetAsync(serverUrl + "/foo");
var api = RestClient.For<IFluentMockServerAdmin>(serverUrl); var api = RestClient.For<IFluentMockServerAdmin>(serverUrl);
@@ -39,7 +91,11 @@ namespace WireMock.Net.Tests
public async Task IFluentMockServerAdmin_GetRequestsAsync() public async Task IFluentMockServerAdmin_GetRequestsAsync()
{ {
// given // 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]; var serverUrl = "http://localhost:" + server.Ports[0];
await new HttpClient().GetAsync(serverUrl + "/foo"); await new HttpClient().GetAsync(serverUrl + "/foo");
var api = RestClient.For<IFluentMockServerAdmin>(serverUrl); var api = RestClient.For<IFluentMockServerAdmin>(serverUrl);
@@ -54,5 +110,75 @@ namespace WireMock.Net.Tests
Check.That(requestLogged.Request.Body).IsNull(); Check.That(requestLogged.Request.Body).IsNull();
Check.That(requestLogged.Request.Path).IsEqualTo("/foo"); 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 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] [Fact]
public async Task BodyParser_Parse_ApplicationXml() public async Task BodyParser_Parse_ApplicationXml()
{ {