Skip to content

Commit 6326917

Browse files
committed
fix content type negotiation test
1 parent d4bf48d commit 6326917

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

src/JsonApiDotNetCore/Formatters/JsonApiOutputFormatter.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,31 @@ public async Task WriteAsync(OutputFormatterWriteContext context)
3434
logger?.LogInformation("Formatting response as JSONAPI");
3535

3636
var response = context.HttpContext.Response;
37-
3837
using (var writer = context.WriterFactory(response.Body, Encoding.UTF8))
3938
{
4039
var jsonApiContext = GetService<IJsonApiContext>(context);
41-
40+
4241
string responseContent;
4342
try
4443
{
4544
if(context.Object.GetType() == typeof(Error) || jsonApiContext.RequestEntity == null)
4645
{
4746
logger?.LogInformation("Response was not a JSONAPI entity. Serializing as plain JSON.");
48-
4947
responseContent = JsonConvert.SerializeObject(context.Object);
5048
}
5149
else
50+
{
51+
response.ContentType = "application/vnd.api+json";
5252
responseContent = JsonApiSerializer.Serialize(context.Object, jsonApiContext);
53+
}
5354
}
5455
catch(Exception e)
5556
{
5657
logger?.LogError(new EventId(), e, "An error ocurred while formatting the response");
5758
responseContent = new Error("400", e.Message).GetJson();
5859
response.StatusCode = 400;
5960
}
60-
61+
6162
await writer.WriteAsync(responseContent);
6263
await writer.FlushAsync();
6364
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Net;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
using DotNetCoreDocs;
5+
using DotNetCoreDocs.Models;
6+
using DotNetCoreDocs.Writers;
7+
using JsonApiDotNetCoreExample;
8+
using Microsoft.AspNetCore.Hosting;
9+
using Microsoft.AspNetCore.TestHost;
10+
using Xunit;
11+
12+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Spec
13+
{
14+
[Collection("WebHostCollection")]
15+
public class ContentNegotiation
16+
{
17+
private DocsFixture<Startup, JsonDocWriter> _fixture;
18+
public ContentNegotiation(DocsFixture<Startup, JsonDocWriter> fixture)
19+
{
20+
_fixture = fixture;
21+
}
22+
23+
[Fact]
24+
public async Task Server_Sends_Correct_ContentType_Header()
25+
{
26+
// arrange
27+
var builder = new WebHostBuilder()
28+
.UseStartup<Startup>();
29+
var httpMethod = new HttpMethod("GET");
30+
var route = "/api/v1/todo-items";
31+
var description = new RequestProperties("Server Sends Correct Content Type Header");
32+
var server = new TestServer(builder);
33+
var client = server.CreateClient();
34+
var request = new HttpRequestMessage(httpMethod, route);
35+
36+
// act
37+
var response = await client.SendAsync(request);
38+
39+
// assert
40+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
41+
Assert.Equal("application/vnd.api+json", response.Content.Headers.ContentType.ToString());
42+
}
43+
}
44+
}

test/JsonApiDotNetCoreExampleTests/IntegrationTests/TodoItemsControllerTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,13 @@ public async Task Can_Post_TodoItem()
190190

191191
var request = new HttpRequestMessage(httpMethod, route);
192192
request.Content = new StringContent(JsonConvert.SerializeObject(content));
193-
Console.WriteLine(">>>" + JsonConvert.SerializeObject(content));
194193
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
195194

196195
var description = new RequestProperties("Post TodoItem");
197196

198197
// Act
199198
var response = await _fixture.MakeRequest<TodoItem>(description, request);
200199
var body = await response.Content.ReadAsStringAsync();
201-
Console.WriteLine(">>>>>>>>" + body + response.StatusCode);
202200
var deserializedBody = (TodoItem)JsonApiDeSerializer.Deserialize(body, _jsonApiContext);
203201

204202
// Assert

0 commit comments

Comments
 (0)