Skip to content

Commit ec7ed8b

Browse files
author
Chris Martinez
committed
Mirror acceptance tests from ASP.NET Core to Web API
1 parent 6949f1b commit ec7ed8b

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

test/Microsoft.AspNet.WebApi.Acceptance.Tests/Http/Basic/BasicAcceptanceTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ protected BasicAcceptanceTest()
1717
FilteredControllerTypes.Add( typeof( ValuesController ) );
1818
FilteredControllerTypes.Add( typeof( Values2Controller ) );
1919
FilteredControllerTypes.Add( typeof( HelloWorldController ) );
20+
FilteredControllerTypes.Add( typeof( PingController ) );
21+
FilteredControllerTypes.Add( typeof( OverlappingRouteTemplateController ) );
2022
Configuration.AddApiVersioning( options => options.ReportApiVersions = true );
2123
Configuration.MapHttpAttributeRoutes( constraintResolver );
2224
Configuration.EnsureInitialized();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace Microsoft.Web.Http.Basic.Controllers
2+
{
3+
using Microsoft.Web.Http;
4+
using System.Web.Http;
5+
6+
[ApiVersion( "1.0" )]
7+
[RoutePrefix( "api/v{version:apiVersion}/values" )]
8+
public class OverlappingRouteTemplateController : ApiController
9+
{
10+
[Route( "{id:int}/{childId}" )]
11+
public IHttpActionResult Get( int id, string childId ) => Ok( new { id, childId } );
12+
13+
[Route( "{id:int}/children" )]
14+
public IHttpActionResult Get( int id ) => Ok( new { id } );
15+
16+
[HttpGet]
17+
[Route( "{id:int}/ambiguous" )]
18+
public IHttpActionResult Ambiguous( int id ) => Ok();
19+
20+
[HttpGet]
21+
[Route( "{id:int}/ambiguous" )]
22+
public IHttpActionResult Ambiguous2( int id ) => Ok();
23+
}
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Microsoft.Web.Http.Basic.Controllers
2+
{
3+
using Microsoft.Web.Http;
4+
using System.Web.Http;
5+
using static System.Net.HttpStatusCode;
6+
7+
[ApiVersionNeutral]
8+
[RoutePrefix( "api/ping" )]
9+
public class PingController : ApiController
10+
{
11+
[Route]
12+
public IHttpActionResult Get() => StatusCode( NoContent );
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace given_a_version_neutral_ApiController
2+
{
3+
using FluentAssertions;
4+
using Microsoft.Web.Http.Basic;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
using static System.Net.HttpStatusCode;
8+
9+
public class when_no_version_is_specified : BasicAcceptanceTest
10+
{
11+
[Fact]
12+
public async Task then_get_should_return_204()
13+
{
14+
// arrange
15+
16+
17+
// act
18+
var response = await GetAsync( "api/ping" );
19+
20+
// assert
21+
response.StatusCode.Should().Be( NoContent );
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
namespace given_a_versioned_ApiController
2+
{
3+
using FluentAssertions;
4+
using Microsoft.Web.Http.Basic;
5+
using System;
6+
using System.Threading.Tasks;
7+
using Xunit;
8+
using static System.Net.HttpStatusCode;
9+
10+
public class when_two_route_templates_overlap : BasicAcceptanceTest
11+
{
12+
[Fact]
13+
public async Task then_the_higher_precedence_route_should_be_selected_during_the_first_request()
14+
{
15+
// arrange
16+
var response = await Client.GetAsync( "api/v1/values/42/children" );
17+
var result1 = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync();
18+
19+
// act
20+
response = await Client.GetAsync( "api/v1/values/42/abc" );
21+
var result2 = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync();
22+
23+
// assert
24+
result1.Should().Be( "{\"id\":42}" );
25+
result2.Should().Be( "{\"id\":42,\"childId\":\"abc\"}" );
26+
}
27+
28+
[Fact]
29+
public async Task then_the_higher_precedence_route_should_be_selected_during_the_second_request()
30+
{
31+
// arrange
32+
var response = await Client.GetAsync( "api/v1/values/42/abc" );
33+
var result1 = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync();
34+
35+
// act
36+
response = await Client.GetAsync( "api/v1/values/42/children" );
37+
var result2 = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync();
38+
39+
// assert
40+
result1.Should().Be( "{\"id\":42,\"childId\":\"abc\"}" );
41+
result2.Should().Be( "{\"id\":42}" );
42+
}
43+
44+
[Fact]
45+
public async Task then_the_higher_precedence_route_should_result_in_500_during_the_second_request()
46+
{
47+
// arrange
48+
var response = await Client.GetAsync( "api/v1/values/42/abc" );
49+
var result1 = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync();
50+
51+
// act
52+
response = await Client.GetAsync( "api/v1/values/42/ambiguous" );
53+
54+
// assert
55+
result1.Should().Be( "{\"id\":42,\"childId\":\"abc\"}" );
56+
response.StatusCode.Should().Be( InternalServerError );
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)