Skip to content

Commit 44653f8

Browse files
author
Bart Koelman
committed
Refactored tests for non-json:api controllers
1 parent 8756f25 commit 44653f8

File tree

5 files changed

+204
-159
lines changed

5 files changed

+204
-159
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.IO;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace JsonApiDotNetCoreExample.Controllers
6+
{
7+
[Route("[controller]")]
8+
public sealed class NonJsonApiController : ControllerBase
9+
{
10+
[HttpGet]
11+
public IActionResult Get()
12+
{
13+
var result = new[] {"Welcome!"};
14+
return Ok(result);
15+
}
16+
17+
[HttpPost]
18+
public async Task<IActionResult> PostAsync()
19+
{
20+
string name = await new StreamReader(Request.Body).ReadToEndAsync();
21+
22+
if (string.IsNullOrEmpty(name))
23+
{
24+
return BadRequest("Please send your name.");
25+
}
26+
27+
var result = "Hello, " + name;
28+
return Ok(result);
29+
}
30+
31+
[HttpPut]
32+
public IActionResult Put([FromBody] string name)
33+
{
34+
var result = "Hi, " + name;
35+
return Ok(result);
36+
}
37+
38+
[HttpPatch]
39+
public IActionResult Patch(string name)
40+
{
41+
var result = "Good day, " + name;
42+
return Ok(result);
43+
}
44+
45+
[HttpDelete]
46+
public IActionResult Delete()
47+
{
48+
return Ok("Bye.");
49+
}
50+
}
51+
}

src/Examples/JsonApiDotNetCoreExample/Controllers/TestValuesController.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

test/JsonApiDotNetCoreExampleTests/Acceptance/Extensibility/CustomControllerTests.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,6 @@ public CustomControllerTests(TestFixture<TestStartup> fixture)
3737
.RuleFor(p => p.LastName, f => f.Name.LastName());
3838
}
3939

40-
[Fact]
41-
public async Task NonJsonApiControllers_DoNotUse_Dasherized_Routes()
42-
{
43-
// Arrange
44-
var builder = WebHost.CreateDefaultBuilder()
45-
.UseStartup<TestStartup>();
46-
var httpMethod = new HttpMethod("GET");
47-
var route = "testValues";
48-
49-
var server = new TestServer(builder);
50-
var client = server.CreateClient();
51-
var request = new HttpRequestMessage(httpMethod, route);
52-
53-
// Act
54-
var response = await client.SendAsync(request);
55-
56-
// Assert
57-
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
58-
}
59-
6040
[Fact]
6141
public async Task CustomRouteControllers_Uses_Dasherized_Collection_Route()
6242
{

test/JsonApiDotNetCoreExampleTests/Acceptance/NonJsonApiControllerTests.cs

Lines changed: 0 additions & 104 deletions
This file was deleted.
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
using System.Net;
2+
using System.Net.Http;
3+
using System.Net.Http.Headers;
4+
using System.Threading.Tasks;
5+
using FluentAssertions;
6+
using JsonApiDotNetCoreExample;
7+
using Microsoft.AspNetCore.Mvc.Testing;
8+
using Xunit;
9+
10+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.NonJsonApiControllers
11+
{
12+
public sealed class NonJsonApiControllerTests : IClassFixture<WebApplicationFactory<Startup>>
13+
{
14+
private readonly WebApplicationFactory<Startup> _factory;
15+
16+
public NonJsonApiControllerTests(WebApplicationFactory<Startup> factory)
17+
{
18+
_factory = factory;
19+
}
20+
21+
[Fact]
22+
public async Task Get_skips_middleware_and_formatters()
23+
{
24+
// Arrange
25+
var request = new HttpRequestMessage(HttpMethod.Get, "/NonJsonApi");
26+
27+
var client = _factory.CreateClient();
28+
29+
// Act
30+
var httpResponse = await client.SendAsync(request);
31+
32+
// Assert
33+
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
34+
httpResponse.Content.Headers.ContentType.ToString().Should().Be("application/json; charset=utf-8");
35+
36+
string responseText = await httpResponse.Content.ReadAsStringAsync();
37+
responseText.Should().Be("[\"Welcome!\"]");
38+
}
39+
40+
[Fact]
41+
public async Task Post_skips_middleware_and_formatters()
42+
{
43+
// Arrange
44+
var request = new HttpRequestMessage(HttpMethod.Post, "/NonJsonApi")
45+
{
46+
Content = new StringContent("Jack")
47+
{
48+
Headers =
49+
{
50+
ContentType = new MediaTypeHeaderValue("text/plain")
51+
}
52+
}
53+
};
54+
55+
var client = _factory.CreateClient();
56+
57+
// Act
58+
var httpResponse = await client.SendAsync(request);
59+
60+
// Assert
61+
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
62+
httpResponse.Content.Headers.ContentType.ToString().Should().Be("text/plain; charset=utf-8");
63+
64+
string responseText = await httpResponse.Content.ReadAsStringAsync();
65+
responseText.Should().Be("Hello, Jack");
66+
}
67+
68+
[Fact]
69+
public async Task Post_skips_error_handler()
70+
{
71+
// Arrange
72+
var request = new HttpRequestMessage(HttpMethod.Post, "/NonJsonApi");
73+
74+
var client = _factory.CreateClient();
75+
76+
// Act
77+
var httpResponse = await client.SendAsync(request);
78+
79+
// Assert
80+
httpResponse.Should().HaveStatusCode(HttpStatusCode.BadRequest);
81+
httpResponse.Content.Headers.ContentType.ToString().Should().Be("text/plain; charset=utf-8");
82+
83+
string responseText = await httpResponse.Content.ReadAsStringAsync();
84+
responseText.Should().Be("Please send your name.");
85+
}
86+
87+
[Fact]
88+
public async Task Put_skips_middleware_and_formatters()
89+
{
90+
// Arrange
91+
var request = new HttpRequestMessage(HttpMethod.Put, "/NonJsonApi")
92+
{
93+
Content = new StringContent("\"Jane\"")
94+
{
95+
Headers =
96+
{
97+
ContentType = new MediaTypeHeaderValue("application/json")
98+
}
99+
}
100+
};
101+
102+
var client = _factory.CreateClient();
103+
104+
// Act
105+
var httpResponse = await client.SendAsync(request);
106+
107+
// Assert
108+
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
109+
httpResponse.Content.Headers.ContentType.ToString().Should().Be("text/plain; charset=utf-8");
110+
111+
string responseText = await httpResponse.Content.ReadAsStringAsync();
112+
responseText.Should().Be("Hi, Jane");
113+
}
114+
115+
[Fact]
116+
public async Task Patch_skips_middleware_and_formatters()
117+
{
118+
// Arrange
119+
var request = new HttpRequestMessage(HttpMethod.Patch, "/NonJsonApi?name=Janice");
120+
121+
var client = _factory.CreateClient();
122+
123+
// Act
124+
var httpResponse = await client.SendAsync(request);
125+
126+
// Assert
127+
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
128+
httpResponse.Content.Headers.ContentType.ToString().Should().Be("text/plain; charset=utf-8");
129+
130+
string responseText = await httpResponse.Content.ReadAsStringAsync();
131+
responseText.Should().Be("Good day, Janice");
132+
}
133+
134+
[Fact]
135+
public async Task Delete_skips_middleware_and_formatters()
136+
{
137+
// Arrange
138+
var request = new HttpRequestMessage(HttpMethod.Delete, "/NonJsonApi");
139+
140+
var client = _factory.CreateClient();
141+
142+
// Act
143+
var httpResponse = await client.SendAsync(request);
144+
145+
// Assert
146+
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
147+
httpResponse.Content.Headers.ContentType.ToString().Should().Be("text/plain; charset=utf-8");
148+
149+
string responseText = await httpResponse.Content.ReadAsStringAsync();
150+
responseText.Should().Be("Bye.");
151+
}
152+
}
153+
}

0 commit comments

Comments
 (0)