Skip to content

Commit

Permalink
Merge pull request #39 from robertbjarum/main
Browse files Browse the repository at this point in the history
Added methods to assert Method Not Allowed (405) Responses
  • Loading branch information
ardalis authored Jun 27, 2023
2 parents 759f788 + 7154fe5 commit c5e0dc8
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ public static async Task<HttpResponseMessage> DeleteAndEnsureNotFoundAsync(
return response;
}

/// <summary>
/// Ensures a DELETE to a requestUri returns a 405 Method Not Allowed response status code
/// </summary>
/// <param name="client"></param>
/// <param name="requestUri"></param>
/// <param name="output">Optional; used to provide details to standard output.</param>
/// <returns></returns>
public static async Task<HttpResponseMessage> DeleteAndEnsureMethodNotAllowedAsync(
this HttpClient client,
string requestUri,
ITestOutputHelper output = null)
{
var response = await client.DeleteAsync(requestUri, output);
response.EnsureMethodNotAllowed();
return response;
}

/// <summary>
/// Ensures a DELETE to a requestUri returns a 204 No Content response status code
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ public static async Task<HttpResponseMessage> GetAndEnsureNotFoundAsync(
return response;
}

/// <summary>
/// Ensures a GET to a requestUri returns a 405 Method Not Allowed response status code
/// </summary>
/// <param name="client"></param>
/// <param name="requestUri"></param>
/// <param name="output">Optional; used to provide details to standard output.</param>
/// <returns></returns>
public static async Task<HttpResponseMessage> GetAndEnsureMethodNotAllowedAsync(
this HttpClient client,
string requestUri,
ITestOutputHelper output = null)
{
var response = await client.GetAsync(requestUri, output);
response.EnsureMethodNotAllowed();
return response;
}

/// <summary>
/// Makes a GET request to a requestUri and returns the response string
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ public static async Task<HttpResponseMessage> PostAndEnsureForbiddenAsync(
return response;
}

/// <summary>
/// Ensures a POST to a requestUri returns a 405 Method Not Allowed response status code
/// </summary>
/// <param name="client"></param>
/// <param name="requestUri"></param>
/// <param name="content"></param>
/// <param name="output">Optional; used to provide details to standard output.</param>
/// <returns></returns>
public static async Task<HttpResponseMessage> PostAndEnsureMethodNotAllowedAsync(
this HttpClient client,
string requestUri,
HttpContent content,
ITestOutputHelper output = null)
{
var response = await client.PostAsync(requestUri, content, output);
response.EnsureMethodNotAllowed();
return response;
}

/// <summary>
/// Makes a POST request to a requestUri and ensures the response contains a substring
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public static async Task<T> PutAndDeserializeAsync<T>(
return result;
}


/// <summary>
/// Ensures a PUT to a requestUri returns a 404 Not Found response status code
/// </summary>
Expand All @@ -50,6 +49,24 @@ public static async Task<HttpResponseMessage> PutAndEnsureNotFoundAsync(this Htt
return response;
}

/// <summary>
/// Ensures a PUT to a requestUri returns a 405 Method Not Allowed response status code
/// </summary>
/// <param name="client"></param>
/// <param name="requestUri"></param>
/// <param name="content"></param>
/// <param name="output">Optional; used to provide details to standard output.</param>
/// <returns></returns>
public static async Task<HttpResponseMessage> PutAndEnsureMethodNotAllowedAsync(this HttpClient client,
string requestUri,
HttpContent content,
ITestOutputHelper output = null)
{
var response = await client.PutAsync(requestUri, content, output);
response.EnsureMethodNotAllowed();
return response;
}

/// <summary>
/// Ensures a PUT to a requestUri returns a 401 Unauthorized response status code
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ public static void EnsureNotFound(this HttpResponseMessage response)
response.Ensure(HttpStatusCode.NotFound);
}

/// <summary>
/// Ensures a response has a status code 405 Method Not Allowed
/// </summary>
/// <param name="response"></param>
/// <return></return>
public static void EnsureMethodNotAllowed(this HttpResponseMessage response)
{
response.Ensure(HttpStatusCode.MethodNotAllowed);
}

/// <summary>
/// Ensures a response has a status code 204 No Content
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions src/Ardalis.HttpClientTestExtensions/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ await client.GetAndEnsureForbiddenAsync("/authors/1");

// GET and assert a 404 is returned
await client.GetAndEnsureNotFoundAsync("/authors/-1");

// GET and assert a 405 is returned
await client.GetAndEnsureMethodNotAllowedAsync("/wrongendpoint", content)
```

#### [POST](src\Ardalis.HttpClientTestExtensions\HttpClientPostExtensionMethods.cs)
Expand Down Expand Up @@ -138,6 +141,9 @@ await client.PostAndEnsureForbiddenAsync("/authors", content);

// POST and assert a 404 is returned
await client.PostAndEnsureNotFoundAsync("/wrongendpoint", content)

// POST and assert a 405 is returned
await client.PostAndEnsureMethodNotAllowedAsync("/wrongendpoint", content)
```

#### [PUT](src\Ardalis.HttpClientTestExtensions\HttpClientPutExtensionMethods.cs)
Expand Down Expand Up @@ -166,6 +172,9 @@ await client.PutAndEnsureForbiddenAsync("/authors/1", content);

// PUT and assert a 404 is returned
await client.PutAndEnsureNotFoundAsync("/wrongendpoint", content)

// PUT and assert a 405 is returned
await client.PutAndEnsureMethodNotAllowedAsync("/wrongendpoint", content)
```

#### [DELETE](src\Ardalis.HttpClientTestExtensions\HttpClientDeleteExtensionMethods.cs)
Expand Down Expand Up @@ -195,6 +204,9 @@ await client.DeleteAndEnsureForbiddenAsync("/authors/1");

// DELETE and assert a 404 is returned
await client.DeleteAndEnsureNotFoundAsync("/wrongendpoint");

// DELETE and assert a 405 is returned
await client.DeleteAndEnsureMethodNotAllowedAsync("/wrongendpoint", content)
```

### [HttpResponseMessage](src\Ardalis.HttpClientTestExtensions\HttpResponseMessageExtensionMethods.cs)
Expand All @@ -220,6 +232,9 @@ response.EnsureForbidden();
// Assert a response has a status code of 404
response.EnsureNotFound();

// Assert a response has a status code of 405
response.EnsureMethodNotAllowed();

// Assert a response has a given status code
response.Ensure(HttpStatusCode.Created);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Ardalis.ApiEndpoints;
using Microsoft.AspNetCore.Mvc;

namespace Ardalis.HttpClientTestExtensions.Api.Endpoints.MethodNotAllowedEndpoints;

public class NoDelete : EndpointBaseSync
.WithoutRequest
.WithResult<NoContentResult>

{
[Route("/nodelete")]
[AcceptVerbs("GET", "PUT", "POST")]
public override NoContentResult Handle()
{
return NoContent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Ardalis.ApiEndpoints;
using Microsoft.AspNetCore.Mvc;

namespace Ardalis.HttpClientTestExtensions.Api.Endpoints.MethodNotAllowedEndpoints;

public class NoGet : EndpointBaseSync
.WithoutRequest
.WithResult<NoContentResult>

{
[Route("/noget")]
[AcceptVerbs("DELETE", "PUT", "POST")]
public override NoContentResult Handle()
{
return NoContent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Ardalis.ApiEndpoints;
using Microsoft.AspNetCore.Mvc;

namespace Ardalis.HttpClientTestExtensions.Api.Endpoints.ErrorEndpoints;

public class NoPost : EndpointBaseSync
.WithoutRequest
.WithResult<NoContentResult>

{
[Route("/nopost")]
[AcceptVerbs("DELETE", "PUT", "GET")]
public override NoContentResult Handle()
{
return NoContent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Ardalis.ApiEndpoints;
using Microsoft.AspNetCore.Mvc;

namespace Ardalis.HttpClientTestExtensions.Api.Endpoints.ErrorEndpoints;

public class NoPut : EndpointBaseSync
.WithoutRequest
.WithResult<NoContentResult>

{
[Route("/noput")]
[AcceptVerbs("DELETE", "GET", "POST")]
public override NoContentResult Handle()
{
return NoContent();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,12 @@ public async Task DeleteAndRedirectAsync()
var client = _factory.CreateClient(new WebApplicationFactoryClientOptions() { AllowAutoRedirect = false });
_ = await client.DeleteAndRedirectAsync("/redirect", "/redirected", _outputHelper);
}

[Fact]
public async Task DeleteAndEnsureMethodNotAllowedAsync()
{
var client = _factory.CreateClient(new WebApplicationFactoryClientOptions() { AllowAutoRedirect = false });
_ = await client.DeleteAndEnsureMethodNotAllowedAsync("/nodelete", _outputHelper);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ public async Task GetAndEnsureBadRequestAsync()
_ = await _client.GetAndEnsureBadRequestAsync("/badrequest", _outputHelper);
}

[Fact]
public async Task GetAndEnsureMethodNotAllowedAsync()
{
_ = await _client.GetAndEnsureMethodNotAllowedAsync("/noget", _outputHelper);
}

[Fact]
public async Task GetAndRedirectAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,12 @@ public async Task PostAndRedirectAsync()
var client = _factory.CreateClient(new WebApplicationFactoryClientOptions() { AllowAutoRedirect = false });
_ = await client.PostAndRedirectAsync("/redirect", content, "/redirected", _outputHelper);
}

[Fact]
public async Task PostAndEnsureMethodNotAllowedAsync()
{
var dto = new CountryDto { Id = "ESP", Name = "Spain" };
var content = new StringContent(JsonSerializer.Serialize(dto), Encoding.UTF8, "application/json");
_ = await _client.PostAndEnsureMethodNotAllowedAsync("/nopost", content, _outputHelper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ public async Task PutAndEnsureBadRequestAsync()
_ = await _client.PutAndEnsureBadRequestAsync("/badrequest", content, _outputHelper);
}

[Fact]
public async Task PutAndEnsureMethodNotAllowedAsync()
{
var dto = new CountryDto { Id = SeedData.TestCountry1.Id, Name = "'Merica" };
var content = new StringContent(JsonSerializer.Serialize(dto), Encoding.UTF8, "application/json");
_ = await _client.PutAndEnsureMethodNotAllowedAsync("/noput", content, _outputHelper);
}

[Fact]
public async Task PutAndRedirectAsync()
{
Expand Down

0 comments on commit c5e0dc8

Please sign in to comment.