Skip to content

Commit

Permalink
Restore functionality to report versions when unmatched. Fixes #876. F…
Browse files Browse the repository at this point in the history
…ixes #918.
  • Loading branch information
commonsensesoftware committed Nov 19, 2022
1 parent 4c53a86 commit a9c46c1
Show file tree
Hide file tree
Showing 34 changed files with 338 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task then_get_should_return_200( string controller, string apiVersi
}

[Fact]
public async Task then_get_should_return_404_for_an_unsupported_version()
public async Task then_get_should_return_400_for_an_unsupported_version()
{
// arrange

Expand All @@ -44,7 +44,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
var problem = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task then_get_should_return_200( string controller, string apiVersi
}

[Fact]
public async Task then_get_should_return_404_for_an_unsupported_version()
public async Task then_get_should_return_400_for_an_unsupported_version()
{
// arrange

Expand All @@ -45,7 +45,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
var problem = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task then_get_should_return_200( Type controllerType, string apiVer
}

[Fact]
public async Task then_get_should_return_404_for_an_unsupported_version()
public async Task then_get_should_return_400_for_an_unsupported_version()
{
// arrange

Expand All @@ -42,7 +42,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
var problem = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace given_a_versioned_ODataController_mixed_with_Web_API_controllers;
public class when_people_is_any_version : AdvancedAcceptanceTest
{
[Fact]
public async Task then_patch_should_return_404_for_an_unsupported_version()
public async Task then_patch_should_return_400_for_an_unsupported_version()
{
// arrange
var person = new { lastName = "Me" };
Expand All @@ -19,7 +19,7 @@ public async Task then_patch_should_return_404_for_an_unsupported_version()
var problem = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task then_get_should_return_200( string requestUrl )
}

[Fact]
public async Task then_get_should_return_404_for_an_unsupported_version()
public async Task then_get_should_return_400_for_an_unsupported_version()
{
// arrange

Expand All @@ -38,7 +38,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
var problem = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
}

Expand Down Expand Up @@ -73,7 +73,7 @@ public async Task then_patch_should_return_405_if_supported_in_any_version( stri
}

[Fact]
public async Task then_patch_should_return_404_for_an_unsupported_version()
public async Task then_patch_should_return_400_for_an_unsupported_version()
{
// arrange
var person = new { id = 42, firstName = "John", lastName = "Doe", email = "john.doe@somewhere.com" };
Expand All @@ -83,7 +83,7 @@ public async Task then_patch_should_return_404_for_an_unsupported_version()
var problem = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task then_get_should_return_200( string requestUrl )
}

[Fact]
public async Task then_get_should_return_404_for_an_unsupported_version()
public async Task then_get_should_return_400_for_an_unsupported_version()
{
// arrange

Expand All @@ -34,7 +34,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
var problem = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task then_the_service_document_should_be_versionX2Dspecific( string
}

[Fact]
public async Task then_the_service_document_should_return_404_for_an_unsupported_version()
public async Task then_the_service_document_should_return_400_for_an_unsupported_version()
{
// arrange

Expand All @@ -47,7 +47,7 @@ public async Task then_the_service_document_should_return_404_for_an_unsupported
var problem = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
}

Expand Down Expand Up @@ -83,7 +83,7 @@ public async Task then_X24metadata_should_be_versionX2Dspecific( string apiVersi
}

[Fact]
public async Task then_X24metadata_should_return_404_for_an_unsupported_version()
public async Task then_X24metadata_should_return_400_for_an_unsupported_version()
{
// arrange
Client.DefaultRequestHeaders.Clear();
Expand All @@ -93,7 +93,7 @@ public async Task then_X24metadata_should_return_404_for_an_unsupported_version(
var problem = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task then_get_should_return_200( string requestUrl )
}

[Fact]
public async Task then_get_should_return_404_for_an_unsupported_version()
public async Task then_get_should_return_400_for_an_unsupported_version()
{
// arrange

Expand All @@ -38,7 +38,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
var problem = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
}

Expand Down Expand Up @@ -73,7 +73,7 @@ public async Task then_patch_should_return_405_if_supported_in_any_version( stri
}

[Fact]
public async Task then_patch_should_return_404_for_an_unsupported_version()
public async Task then_patch_should_return_400_for_an_unsupported_version()
{
// arrange
var person = new { id = 42, firstName = "John", lastName = "Doe", email = "john.doe@somewhere.com" };
Expand All @@ -83,7 +83,7 @@ public async Task then_patch_should_return_404_for_an_unsupported_version()
var problem = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task then_get_should_return_200( string requestUrl )
}

[Fact]
public async Task then_get_should_return_404_for_an_unsupported_version()
public async Task then_get_should_return_400_for_an_unsupported_version()
{
// arrange

Expand All @@ -34,7 +34,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
var problem = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace Asp.Versioning.Dispatcher;
using System.Web.Http.Tracing;
using static System.Net.HttpStatusCode;

#pragma warning disable CA2000 // Dispose objects before losing scope

internal sealed class HttpResponseExceptionFactory
{
private const string Allow = nameof( Allow );
Expand Down Expand Up @@ -64,7 +62,8 @@ internal HttpResponseException NewUnmatchedException(
}
}

var versionsOnlyByMediaType = Options.ApiVersionReader.VersionsByMediaType( allowMultipleLocations: false );
var options = Options;
var versionsOnlyByMediaType = options.ApiVersionReader.VersionsByMediaType( allowMultipleLocations: false );

if ( versionsOnlyByMediaType )
{
Expand All @@ -75,9 +74,28 @@ internal HttpResponseException NewUnmatchedException(
if ( couldMatch )
{
properties ??= request.ApiVersionProperties();
response = properties.RequestedApiVersion is ApiVersion apiVersion
? CreateResponseForUnsupportedApiVersion( apiVersion, NotFound )
: CreateNotFound( conventionRouteResult );

if ( properties.RequestedApiVersion is ApiVersion apiVersion )
{
HttpStatusCode statusCode;
var matchedUrlSegment = !string.IsNullOrEmpty( properties.RouteParameter );

if ( matchedUrlSegment )
{
statusCode = NotFound;
}
else
{
var versionsByUrlOnly = options.ApiVersionReader.VersionsByUrl( allowMultipleLocations: false );
statusCode = versionsByUrlOnly ? NotFound : options.UnsupportedApiVersionStatusCode;
}

response = CreateResponseForUnsupportedApiVersion( apiVersion, statusCode );
}
else
{
response = CreateNotFound( conventionRouteResult );
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void select_controller_should_return_correct_versionX2DneutralX2C_convent
}

[Fact]
public async Task select_controller_should_return_404_for_unmatchedX2C_attributeX2Dbased_controller_version()
public async Task select_controller_should_return_400_for_unmatchedX2C_attributeX2Dbased_controller_version()
{
// arrange
var detail = "The HTTP resource that matches the request URI 'http://localhost/api/test' does not support the API version '42.0'.";
Expand All @@ -190,13 +190,13 @@ public async Task select_controller_should_return_404_for_unmatchedX2C_attribute
var content = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
response.Headers.GetValues( "api-supported-versions" ).Single().Should().Be( "1.0, 2.0, 3.0, 4.0" );
response.Headers.GetValues( "api-deprecated-versions" ).Single().Should().Be( "3.0-Alpha" );
content.Should().BeEquivalentTo(
new ProblemDetails()
{
Status = 404,
Status = 400,
Title = "Unsupported API version",
Type = ProblemDetailsDefaults.Unsupported.Type,
Detail = detail,
Expand Down Expand Up @@ -255,7 +255,7 @@ public async Task select_controller_should_return_400_for_attributeX2Dbased_cont
}

[Fact]
public async Task select_controller_should_return_404_for_unmatchedX2C_conventionX2Dbased_controller_version()
public async Task select_controller_should_return_400_for_unmatchedX2C_conventionX2Dbased_controller_version()
{
// arrange
var detail = "The HTTP resource that matches the request URI 'http://localhost/api/test' does not support the API version '4.0'.";
Expand Down Expand Up @@ -283,13 +283,13 @@ public async Task select_controller_should_return_404_for_unmatchedX2C_conventio
var content = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
response.Headers.GetValues( "api-supported-versions" ).Single().Should().Be( "1.0, 2.0, 3.0" );
response.Headers.GetValues( "api-deprecated-versions" ).Single().Should().Be( "1.8, 1.9" );
content.Should().BeEquivalentTo(
new ProblemDetails()
{
Status = 404,
Status = 400,
Title = "Unsupported API version",
Type = ProblemDetailsDefaults.Unsupported.Type,
Detail = detail,
Expand Down Expand Up @@ -413,7 +413,7 @@ public void select_controller_should_return_400_when_no_version_is_specified_and
}

[Fact]
public void select_controller_should_return_404_for_unmatched_action()
public void select_controller_should_return_400_for_unmatched_action()
{
// arrange
var configuration = AttributeRoutingEnabledConfiguration;
Expand All @@ -433,7 +433,7 @@ public void select_controller_should_return_404_for_unmatched_action()
var response = selectController.Should().Throw<HttpResponseException>().Subject.Single().Response;

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
response.Headers.GetValues( "api-supported-versions" ).Single().Should().Be( "1.0, 2.0, 3.0, 4.0" );
response.Headers.GetValues( "api-deprecated-versions" ).Single().Should().Be( "3.0-Alpha" );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace given_a_versioned_minimal_API;
public class when_using_an_endpoint : AcceptanceTest
{
[Theory]
[InlineData( "api/order?api-version=0.9", NotFound )]
[InlineData( "api/order?api-version=0.9", BadRequest )]
[InlineData( "api/order?api-version=1.0", OK )]
[InlineData( "api/order?api-version=2.0", OK )]
[InlineData( "api/order/42?api-version=0.9", OK )]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public async Task then_delete_should_return_405( string apiVersion )
}

[Fact]
public async Task then_get_should_return_404_for_an_unsupported_version()
public async Task then_get_should_return_400_for_an_unsupported_version()
{
// arrange

Expand All @@ -96,7 +96,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
var response = await GetAsync( "api/values?api-version=3.0" );

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task then_get_should_return_200( string controller, string apiVersi
}

[Fact]
public async Task then_get_should_return_404_for_an_unsupported_version()
public async Task then_get_should_return_400_for_an_unsupported_version()
{
// arrange

Expand All @@ -38,7 +38,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
var response = await GetAsync( "api/values?api-version=4.0" );

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task then_get_should_return_200( Type controllerType, string apiVer
}

[Fact]
public async Task then_get_should_return_404_for_an_unsupported_version()
public async Task then_get_should_return_400_for_an_unsupported_version()
{
// arrange

Expand All @@ -43,7 +43,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
var response = await GetAsync( "api/agreements/42?api-version=4.0" );

// assert
response.StatusCode.Should().Be( NotFound );
response.StatusCode.Should().Be( BadRequest );
}

[Fact]
Expand Down
Loading

0 comments on commit a9c46c1

Please sign in to comment.