Skip to content

Commit c8d7ff7

Browse files
Chris Martinezcommonsensesoftware
Chris Martinez
authored andcommitted
Add acceptance tests for action-level API version declarations
1 parent 03497ff commit c8d7ff7

File tree

46 files changed

+1455
-21
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1455
-21
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Microsoft.Web.Http.Basic
22
{
3-
using Controllers;
3+
using Microsoft.Web.Http.Basic.Controllers;
44
using Microsoft.Web.Http.Routing;
55
using System.Web.Http;
66
using System.Web.Http.Routing;
@@ -19,6 +19,7 @@ protected BasicAcceptanceTest()
1919
FilteredControllerTypes.Add( typeof( HelloWorldController ) );
2020
FilteredControllerTypes.Add( typeof( PingController ) );
2121
FilteredControllerTypes.Add( typeof( OverlappingRouteTemplateController ) );
22+
FilteredControllerTypes.Add( typeof( OrdersController ) );
2223
Configuration.AddApiVersioning( options => options.ReportApiVersions = true );
2324
Configuration.MapHttpAttributeRoutes( constraintResolver );
2425
Configuration.EnsureInitialized();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
[RoutePrefix( "api/orders" )]
8+
public class OrdersController : ApiController
9+
{
10+
[Route]
11+
[ApiVersion( "1.0" )]
12+
[ApiVersion( "2.0" )]
13+
public IHttpActionResult Get() => Ok();
14+
15+
[Route( "{id}", Name = "GetOrderById" )]
16+
[ApiVersion( "0.9" )]
17+
[ApiVersion( "1.0" )]
18+
[ApiVersion( "2.0" )]
19+
public IHttpActionResult Get( int id ) => Ok();
20+
21+
[Route]
22+
[ApiVersion( "1.0" )]
23+
[ApiVersion( "2.0" )]
24+
public IHttpActionResult Post( [FromBody] Order order )
25+
{
26+
order.Id = 42;
27+
return CreatedAtRoute( "GetOrderById", new { id = order.Id }, order );
28+
}
29+
30+
[Route( "{id}" )]
31+
[ApiVersion( "2.0" )]
32+
public IHttpActionResult Put( int id, [FromBody] Order order ) => StatusCode( NoContent );
33+
34+
[Route( "{id}" )]
35+
[ApiVersionNeutral]
36+
public IHttpActionResult Delete( int id ) => StatusCode( NoContent );
37+
}
38+
39+
public class Order
40+
{
41+
public int Id { get; set; }
42+
43+
public string Customer { get; set; }
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
namespace given_a_versioned_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_using_an_action : BasicAcceptanceTest
10+
{
11+
[Theory]
12+
[InlineData( "api/orders/42?api-version=0.9" )]
13+
[InlineData( "api/orders/42?api-version=1.0" )]
14+
[InlineData( "api/orders/42?api-version=2.0" )]
15+
[InlineData( "api/orders?api-version=1.0" )]
16+
[InlineData( "api/orders?api-version=2.0" )]
17+
public async Task then_get_should_return_200( string requestUrl )
18+
{
19+
// arrange
20+
21+
// act
22+
var response = await GetAsync( requestUrl );
23+
24+
// assert
25+
response.StatusCode.Should().Be( OK );
26+
}
27+
28+
[Theory]
29+
[InlineData( "api/orders?api-version=1.0" )]
30+
[InlineData( "api/orders?api-version=2.0" )]
31+
public async Task then_post_should_return_201( string requestUrl )
32+
{
33+
// arrange
34+
var content = new { customer = "Bill Mei" };
35+
36+
// act
37+
var response = await PostAsync( requestUrl, content );
38+
39+
// assert
40+
response.StatusCode.Should().Be( Created );
41+
}
42+
43+
[Fact]
44+
public async Task then_put_should_return_204()
45+
{
46+
// arrange
47+
var requestUrl = "api/orders/42?api-version=2.0";
48+
var content = new { customer = "Bill Mei" };
49+
50+
// act
51+
var response = await PutAsync( requestUrl, content );
52+
53+
// assert
54+
response.StatusCode.Should().Be( NoContent );
55+
}
56+
57+
[Theory]
58+
[InlineData( "api/orders/42" )]
59+
[InlineData( "api/orders/42?api-version=0.9" )]
60+
[InlineData( "api/orders/42?api-version=1.0" )]
61+
[InlineData( "api/orders/42?api-version=2.0" )]
62+
public async Task then_delete_should_return_204( string requestUrl )
63+
{
64+
// arrange
65+
66+
// act
67+
var response = await DeleteAsync( requestUrl );
68+
69+
// assert
70+
response.StatusCode.Should().Be( NoContent );
71+
}
72+
}
73+
}

test/Microsoft.AspNet.WebApi.Acceptance.Tests/Http/ByNamespace/ByNamespaceAcceptanceTest.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ protected enum SetupKind
1212
{
1313
None,
1414
HelloWorld,
15-
Agreements
15+
Agreements,
16+
Orders,
1617
}
1718

1819
protected ByNamespaceAcceptanceTest( SetupKind kind )
@@ -25,6 +26,9 @@ protected ByNamespaceAcceptanceTest( SetupKind kind )
2526
case SetupKind.Agreements:
2627
ConfigureAgreements();
2728
break;
29+
case SetupKind.Orders:
30+
ConfigureOrders();
31+
break;
2832
}
2933

3034
Configuration.EnsureInitialized();
@@ -77,5 +81,25 @@ void ConfigureHelloWorld()
7781
options.Conventions.Add( new VersionByNamespaceConvention() );
7882
} );
7983
}
84+
85+
void ConfigureOrders()
86+
{
87+
FilteredControllerTypes.Add( typeof( Controllers.V1.OrdersController ) );
88+
FilteredControllerTypes.Add( typeof( Controllers.V2.OrdersController ) );
89+
FilteredControllerTypes.Add( typeof( Controllers.V3.OrdersController ) );
90+
91+
var constraintResolver = new DefaultInlineConstraintResolver()
92+
{
93+
ConstraintMap = { ["apiVersion"] = typeof( ApiVersionRouteConstraint ) }
94+
};
95+
96+
Configuration.MapHttpAttributeRoutes( constraintResolver );
97+
Configuration.AddApiVersioning(
98+
options =>
99+
{
100+
options.ReportApiVersions = true;
101+
options.Conventions.Add( new VersionByNamespaceConvention() );
102+
} );
103+
}
80104
}
81105
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace Microsoft.Web.Http.ByNamespace.Controllers.V1
2+
{
3+
using Microsoft.Web.Http.ByNamespace.Models;
4+
using System.Web.Http;
5+
using static System.Net.HttpStatusCode;
6+
7+
[RoutePrefix( "api/orders" )]
8+
public class OrdersController : ApiController
9+
{
10+
[Route( "{id}", Name = "GetOrderByIdV1" )]
11+
public virtual IHttpActionResult Get( int id ) => Ok();
12+
13+
[Route]
14+
public virtual IHttpActionResult Post( [FromBody] Order order )
15+
{
16+
order.Id = 1;
17+
return CreatedAtRoute( "GetOrderByIdV1", new { id = order.Id }, order );
18+
}
19+
20+
[Route( "{id}" )]
21+
public virtual IHttpActionResult Put( int id, [FromBody] Order order ) => StatusCode( NoContent );
22+
23+
[Route( "{id}" )]
24+
[ApiVersionNeutral]
25+
public IHttpActionResult Delete( int id ) => StatusCode( NoContent );
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace Microsoft.Web.Http.ByNamespace.Controllers.V2
2+
{
3+
using Microsoft.Web.Http.ByNamespace.Models;
4+
using System.Web.Http;
5+
using static System.Net.HttpStatusCode;
6+
7+
[RoutePrefix( "api/orders" )]
8+
public class OrdersController : V1.OrdersController
9+
{
10+
[Route]
11+
public virtual IHttpActionResult Get() => Ok();
12+
13+
[Route( "{id}", Name = "GetOrderByIdV2" )]
14+
public override IHttpActionResult Get( int id ) => Ok();
15+
16+
[Route]
17+
public override IHttpActionResult Post( [FromBody] Order order )
18+
{
19+
order.Id = 2;
20+
return CreatedAtRoute( "GetOrderByIdV2", new { id = order.Id }, order );
21+
}
22+
23+
[Route( "{id}" )]
24+
public override IHttpActionResult Put( int id, [FromBody] Order order ) => StatusCode( NoContent );
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace Microsoft.Web.Http.ByNamespace.Controllers.V3
2+
{
3+
using Microsoft.Web.Http.ByNamespace.Models;
4+
using System.Web.Http;
5+
using static System.Net.HttpStatusCode;
6+
7+
[RoutePrefix( "api/orders" )]
8+
public class OrdersController : V2.OrdersController
9+
{
10+
[Route]
11+
public override IHttpActionResult Get() => Ok();
12+
13+
[Route( "{id}", Name = "GetOrderByIdV3" )]
14+
public override IHttpActionResult Get( int id ) => Ok();
15+
16+
[Route]
17+
public override IHttpActionResult Post( [FromBody] Order order )
18+
{
19+
order.Id = 3;
20+
return CreatedAtRoute( "GetOrderByIdV3", new { id = order.Id }, order );
21+
}
22+
23+
[Route( "{id}" )]
24+
public override IHttpActionResult Put( int id, [FromBody] Order order ) => StatusCode( NoContent );
25+
}
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Microsoft.Web.Http.ByNamespace.Models
2+
{
3+
using System;
4+
5+
public class Order
6+
{
7+
public int Id { get; set; }
8+
9+
public string Customer { get; set; }
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
namespace given_a_versioned_ApiController_per_namespace
2+
{
3+
using FluentAssertions;
4+
using Microsoft.Web.Http.ByNamespace;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
using static System.Net.HttpStatusCode;
8+
9+
public class when_using_an_action : ByNamespaceAcceptanceTest
10+
{
11+
public when_using_an_action() : base( SetupKind.Orders ) { }
12+
13+
[Theory]
14+
[InlineData( "api/orders/42?api-version=1.0" )]
15+
[InlineData( "api/orders/42?api-version=2.0" )]
16+
[InlineData( "api/orders?api-version=2.0" )]
17+
public async Task then_get_should_return_200( string requestUrl )
18+
{
19+
// arrange
20+
21+
// act
22+
var response = await GetAsync( requestUrl );
23+
24+
// assert
25+
response.StatusCode.Should().Be( OK );
26+
}
27+
28+
[Theory]
29+
[InlineData( "api/orders?api-version=1.0" )]
30+
[InlineData( "api/orders?api-version=2.0" )]
31+
public async Task then_post_should_return_201( string requestUrl )
32+
{
33+
// arrange
34+
var content = new { customer = "Bill Mei" };
35+
36+
// act
37+
var response = await PostAsync( requestUrl, content );
38+
39+
// assert
40+
response.StatusCode.Should().Be( Created );
41+
}
42+
43+
[Fact]
44+
public async Task then_put_should_return_204()
45+
{
46+
// arrange
47+
var requestUrl = "api/orders/42?api-version=2.0";
48+
var content = new { customer = "Bill Mei" };
49+
50+
// act
51+
var response = await PutAsync( requestUrl, content );
52+
53+
// assert
54+
response.StatusCode.Should().Be( NoContent );
55+
}
56+
57+
[Theory]
58+
[InlineData( "api/orders/42" )]
59+
[InlineData( "api/orders/42?api-version=1.0" )]
60+
[InlineData( "api/orders/42?api-version=2.0" )]
61+
public async Task then_delete_should_return_204( string requestUrl )
62+
{
63+
// arrange
64+
65+
// act
66+
var response = await DeleteAsync( requestUrl );
67+
68+
// assert
69+
response.StatusCode.Should().Be( NoContent );
70+
}
71+
}
72+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace Microsoft.Web.Http.Conventions.Controllers
2+
{
3+
using System.Web.Http;
4+
using static System.Net.HttpStatusCode;
5+
6+
[RoutePrefix( "api/orders" )]
7+
public class OrdersController : ApiController
8+
{
9+
[Route]
10+
public IHttpActionResult Get() => Ok();
11+
12+
[Route( "{id}", Name = "GetOrderById" )]
13+
public IHttpActionResult Get( int id ) => Ok();
14+
15+
[Route]
16+
public IHttpActionResult Post( [FromBody] Order order )
17+
{
18+
order.Id = 42;
19+
return CreatedAtRoute( "GetOrderById", new { id = order.Id }, order );
20+
}
21+
22+
[Route( "{id}" )]
23+
public IHttpActionResult Put( int id, [FromBody] Order order ) => StatusCode( NoContent );
24+
25+
[Route( "{id}" )]
26+
public IHttpActionResult Delete( int id ) => StatusCode( NoContent );
27+
}
28+
29+
public class Order
30+
{
31+
public int Id { get; set; }
32+
33+
public string Customer { get; set; }
34+
}
35+
}

0 commit comments

Comments
 (0)