Skip to content

Commit a66ec1c

Browse files
committed
test(spec): server should return 400 on bad query
1 parent a12ff93 commit a66ec1c

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/JsonApiDotNetCore/Internal/Error.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ namespace JsonApiDotNetCore.Internal
44
{
55
public class Error
66
{
7+
public Error()
8+
{ }
9+
710
public Error(string status, string title)
811
{
912
Status = status;

src/JsonApiDotNetCore/Internal/Query/QuerySet.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,16 @@ private void BuildQuerySet(IQueryCollection query)
4545
if (pair.Key.StartsWith("include"))
4646
{
4747
IncludedRelationships = ParseIncludedRelationships(pair.Value);
48+
continue;
4849
}
4950

5051
if (pair.Key.StartsWith("page"))
5152
{
5253
PageQuery = ParsePageQuery(pair.Key, pair.Value);
54+
continue;
5355
}
56+
57+
throw new JsonApiException("400", $"{pair} is not a valid query.");
5458
}
5559
}
5660

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 Newtonsoft.Json;
11+
using Xunit;
12+
using JsonApiDotNetCore.Internal;
13+
14+
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
15+
{
16+
[Collection("WebHostCollection")]
17+
public class QueryParameters
18+
{
19+
private DocsFixture<Startup, JsonDocWriter> _fixture;
20+
public QueryParameters(DocsFixture<Startup, JsonDocWriter> fixture)
21+
{
22+
_fixture = fixture;
23+
}
24+
25+
[Fact]
26+
public async Task Server_Returns_400_ForUnknownQueryParam()
27+
{
28+
// arrange
29+
const string queryKey = "unknownKey";
30+
const string queryValue = "value";
31+
var builder = new WebHostBuilder()
32+
.UseStartup<Startup>();
33+
var httpMethod = new HttpMethod("GET");
34+
var route = $"/api/v1/todo-items?{queryKey}={queryValue}";
35+
var description = new RequestProperties("Server Returns 400 For Unknown Query Params");
36+
var server = new TestServer(builder);
37+
var client = server.CreateClient();
38+
var request = new HttpRequestMessage(httpMethod, route);
39+
40+
// act
41+
var response = await client.SendAsync(request);
42+
var body = JsonConvert.DeserializeObject<Error>(await response.Content.ReadAsStringAsync());
43+
44+
// assert
45+
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
46+
Assert.Equal($"[{queryKey}, {queryValue}] is not a valid query.", body.Title);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)