public async Task WorksForNoContent() { using (var server = new NewSetupJsonApiServer()) { var client = server.GetClient(); var result = await client.DeleteAsync("/api/companies/123"); Assert.Equal(HttpStatusCode.NoContent, result.StatusCode); } }
public async Task UsesQueryFilterExpression() { var config = new JsonApiConfiguration(); config.QueryFilterExpressions.SetExpression<string>((left, right) => left != right); using (var server = new NewSetupJsonApiServer(config)) { var client = server.GetClient(); var result = await client.GetJsonResponseAsync("api/query/people?filter[last-name]=Russel"); _output.WriteLine(result.ToString()); var names = ((JArray)result["data"]) .Select(p => p["attributes"]["last-name"].Value<string>()) .ToList(); var filtered = names.Where(a => a != "Russel").ToList(); Assert.Equal(filtered.Count, names.Count); } }
public async Task PassesThrough400Errors() { using (var server = new NewSetupJsonApiServer(new JsonApiConfiguration())) { var client = server.GetClient(); var response = await client.GetAsync("does/not/exist"); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); } }
public async Task PassesThroughHttpErrors() { using (var server = new NewSetupJsonApiServer()) { var client = server.GetClient(); var response = await client.GetFullJsonResponseAsync("api/broken"); _output.WriteLine(response.Content.ToString()); var errorText = response.Content["errors"][0]["title"].Value<string>(); Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); Assert.Equal("Authorization has been denied for this request.", errorText); } }
public async Task AppliesSortingBeforePagination(string path) { using (var server = new NewSetupJsonApiServer(new JsonApiConfiguration())) { var client = server.GetClient(); var result1 = await client.GetJsonResponseAsync($"api/{path}/people?sort=age"); var result2 = await client.GetJsonResponseAsync($"api/{path}/people?sort=age&page[number]=1"); _output.WriteLine(result1.ToString()); _output.WriteLine(result2.ToString()); var ages1 = ((JArray)result1["data"]) .Select(p => p["attributes"]["age"].Value<int>()) .ToList(); var ages2 = ((JArray)result2["data"]) .Select(p => p["attributes"]["age"].Value<int>()) .ToList(); var sorted = ages1.Concat(ages2).OrderBy(a => a).ToList(); Assert.Equal(sorted, ages1.Concat(ages2).ToList()); } }
public async Task GivesUsefulError() { using (var server = new NewSetupJsonApiServer(new JsonApiConfiguration())) { var client = server.GetClient(); var response = await client.GetFullJsonResponseAsync("api/broken/123/"); _output.WriteLine(response.ToString()); var result = response.Content; var error = result["errors"][0]; Assert.Equal("https://github.com/joukevandermaas/saule/wiki", error["links"]["about"].Value<string>()); Assert.Equal("Saule.JsonApiException", error["code"].Value<string>()); Assert.Equal("Saule.JsonApiException: You must add a [ReturnsResourceAttribute] to action methods.", error["detail"].Value<string>()); Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); } }
public async Task AppliesSortingConditionally() { using (var server = new NewSetupJsonApiServer(new JsonApiConfiguration())) { var client = server.GetClient(); var result = await client.GetJsonResponseAsync("api/people?sort=age"); _output.WriteLine(result.ToString()); var ages = ((JArray)result["data"]) .Select(p => p["attributes"]["age"].Value<int>()) .ToList(); var sorted = ages.OrderBy(a => a).ToList(); Assert.NotEqual(sorted, ages); } }
public async Task AppliesPagination() { using (var server = new NewSetupJsonApiServer(new JsonApiConfiguration())) { var client = server.GetClient(); var result = await client.GetJsonResponseAsync("api/companies/"); _output.WriteLine(result.ToString()); Assert.Equal(12, (result["data"] as JArray)?.Count); } }
public async Task BuilderAndConverterConstructor() { var config = new JsonApiConfiguration { UrlPathBuilder = new CanonicalUrlPathBuilder(), JsonConverters = { new StringEnumConverter() } }; using (var server = new NewSetupJsonApiServer(config)) { var client = server.GetClient(); var result = await client.GetJsonResponseAsync("api/companies/456/"); _output.WriteLine(result.ToString()); Assert.Equal("National", result["data"]["attributes"]["location"].Value<string>()); result = await client.GetJsonResponseAsync("api/people/"); _output.WriteLine(result.ToString()); var relatedUrl = result["data"][0]["relationships"]["job"]["links"]["related"] .Value<string>(); Assert.Equal("http://localhost/corporations/456/", relatedUrl); } }
public async Task ConverterConstructor() { var config = new JsonApiConfiguration { JsonConverters = { new StringEnumConverter() } }; using (var server = new NewSetupJsonApiServer(config)) { var client = server.GetClient(); var result = await client.GetJsonResponseAsync("api/companies/456/"); _output.WriteLine(result.ToString()); Assert.Equal("National", result["data"]["attributes"]["location"].Value<string>()); } }
public async Task DefaultConstructor() { using (var server = new NewSetupJsonApiServer()) { var client = server.GetClient(); var result = await client.GetJsonResponseAsync("api/companies/456/"); _output.WriteLine(result.ToString()); Assert.Equal(1, result["data"]["attributes"]["location"].Value<int>()); result = await client.GetJsonResponseAsync("api/people/"); _output.WriteLine(result.ToString()); var relatedUrl = result["data"][0]["relationships"]["job"]["links"]["related"] .Value<string>(); Assert.Equal("http://localhost/api/people/0/employer/", relatedUrl); } }