Skip to content

Commit

Permalink
Update response content type for ExecutionResultActionResult (#923)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 authored Sep 11, 2022
1 parent 5ba9cd9 commit af5ac03
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Transports.AspNetCore/ExecutionResultActionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ExecutionResultActionResult(ExecutionResult executionResult, HttpStatusCo
}

/// <inheritdoc cref="HttpResponse.ContentType"/>
public string ContentType { get; set; } = GraphQLHttpMiddleware.CONTENTTYPE_GRAPHQLJSON;
public string ContentType { get; set; } = GraphQLHttpMiddleware.CONTENTTYPE_GRAPHQLRESPONSEJSON;

/// <inheritdoc/>
public async Task ExecuteResultAsync(ActionContext context)
Expand Down
4 changes: 2 additions & 2 deletions src/Transports.AspNetCore/GraphQLHttpMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public class GraphQLHttpMiddleware : IUserContextBuilder
private const string MEDIATYPE_GRAPHQLJSON = "application/graphql+json"; // deprecated
private const string MEDIATYPE_JSON = "application/json";
private const string MEDIATYPE_GRAPHQL = "application/graphql";
internal const string CONTENTTYPE_JSON = "application/json; charset=utf-8";
internal const string CONTENTTYPE_GRAPHQLJSON = "application/graphql+json; charset=utf-8"; // deprecated
private const string CONTENTTYPE_JSON = "application/json; charset=utf-8";
private const string CONTENTTYPE_GRAPHQLJSON = "application/graphql+json; charset=utf-8"; // deprecated
internal const string CONTENTTYPE_GRAPHQLRESPONSEJSON = "application/graphql-response+json; charset=utf-8";

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Tests;

public class ExecutionResultActionResultTests
public class ExecutionResultActionResultTests : IDisposable
{
[Fact]
public async Task Basic()
private readonly TestServer _server;

public ExecutionResultActionResultTests()
{
var _hostBuilder = new WebHostBuilder();
_hostBuilder.ConfigureServices(services =>
Expand Down Expand Up @@ -40,21 +41,49 @@ public async Task Basic()
});
#endif
});
var server = new TestServer(_hostBuilder);
_server = new TestServer(_hostBuilder);
}

var str = await server.ExecuteGet("/graphql?query={count}");
[Fact]
public async Task Basic()
{
var str = await _server.ExecuteGet("/graphql?query={count}");
str.ShouldBe("{\"data\":{\"count\":0}}");
}

var str2 = await server.ExecuteGet("/graphql?query={}&resultCode=200");
[Fact]
public async Task ForcedResultCode()
{
var str2 = await _server.ExecuteGet("/graphql?query={}&resultCode=200");
str2.ShouldBe(@"{""errors"":[{""message"":""Error parsing query: Expected Name, found }; for more information see http://spec.graphql.org/October2021/#Field"",""locations"":[{""line"":1,""column"":2}],""extensions"":{""code"":""SYNTAX_ERROR"",""codes"":[""SYNTAX_ERROR""]}}]}");
}

using var httpClient = server.CreateClient();
[Fact]
public async Task AltContentType()
{
using var httpClient = _server.CreateClient();
using var request = new HttpRequestMessage(HttpMethod.Get, "/graphql?query={count}&jsonType=true");
var response = await httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
var contentType = response.Content.Headers.ContentType;
contentType.ShouldNotBeNull();
contentType.MediaType.ShouldBe("application/json");
var str3 = await response.Content.ReadAsStringAsync();
str3.ShouldBe("{\"data\":{\"count\":0}}");
}

[Fact]
public async Task ReturnsBadRequestForUnexecutedResults()
{
using var httpClient = _server.CreateClient();
var request = new HttpRequestMessage(HttpMethod.Get, "/graphql?query={}");
var response = await httpClient.SendAsync(request);
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.BadRequest);
var str3 = await response.Content.ReadAsStringAsync();
str3.ShouldBe(@"{""errors"":[{""message"":""Error parsing query: Expected Name, found }; for more information see http://spec.graphql.org/October2021/#Field"",""locations"":[{""line"":1,""column"":2}],""extensions"":{""code"":""SYNTAX_ERROR"",""codes"":[""SYNTAX_ERROR""]}}]}");
}

public void Dispose() => _server.Dispose();
}

[Route("/")]
Expand All @@ -69,17 +98,24 @@ public TestController(IDocumentExecuter<ISchema> documentExecuter)

[HttpGet]
[Route("graphql")]
public async Task<IActionResult> Test(string query, int? resultCode = null)
public async Task<IActionResult> Test(string query, int? resultCode = null, bool jsonType = false)
{
var result = await _documentExecuter.ExecuteAsync(new()
{
Query = query,
RequestServices = HttpContext.RequestServices,
CancellationToken = HttpContext.RequestAborted,
});

ExecutionResultActionResult result2;
if (resultCode == null)
return new ExecutionResultActionResult(result);
result2 = new ExecutionResultActionResult(result);
else
return new ExecutionResultActionResult(result, (System.Net.HttpStatusCode)resultCode.Value);
result2 = new ExecutionResultActionResult(result, (System.Net.HttpStatusCode)resultCode.Value);

if (jsonType)
result2.ContentType = "application/json";

return result2;
}
}
8 changes: 8 additions & 0 deletions tests/Transports.AspNetCore.Tests/TestServerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ public static async Task<string> ExecuteGet(this TestServer server, string url)
var client = server.CreateClient();
using var response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
var contentType = response.Content.Headers.ContentType;
contentType.ShouldNotBeNull();
contentType.MediaType.ShouldBe("application/graphql-response+json");
contentType.CharSet.ShouldBe("utf-8");
var str = await response.Content.ReadAsStringAsync();
return str;
}
Expand All @@ -18,6 +22,10 @@ public static async Task<string> ExecutePost(this TestServer server, string url,
using var content = new StringContent(data, Encoding.UTF8, "application/json");
using var response = await client.PostAsync(url, content);
response.EnsureSuccessStatusCode();
var contentType = response.Content.Headers.ContentType;
contentType.ShouldNotBeNull();
contentType.MediaType.ShouldBe("application/graphql-response+json");
contentType.CharSet.ShouldBe("utf-8");
var str = await response.Content.ReadAsStringAsync();
return str;
}
Expand Down

0 comments on commit af5ac03

Please sign in to comment.