|
| 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