Repository navigation
Expand file tree
/
Copy pathApiControllerTestabilityTest.cs
More file actions
88 lines (75 loc) · 3.26 KB
/
Copy pathApiControllerTestabilityTest.cs
File metadata and controls
88 lines (75 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Net;
using System.Net.Http;
using System.Web.Http.Routing;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.Controllers
{
public class ApiControllerTestabilityTest
{
[Fact]
public void Post_With_EmptyRequest_And_MockUrlHelper()
{
// Arrange
CustomersController controller = new CustomersController();
controller.Configuration = new HttpConfiguration();
controller.Request = new HttpRequestMessage();
Mock<UrlHelper> url = new Mock<UrlHelper>();
url.Setup(u => u.Link("default", new { id = 42 })).Returns("http://location_header/").Verifiable();
controller.Url = url.Object;
// Act
var result = controller.Post(new Customer { ID = 42 });
// Assert
Customer customer;
Assert.Equal("http://location_header/", result.Headers.Location.AbsoluteUri);
Assert.True(result.TryGetContentValue<Customer>(out customer));
Assert.Equal(42, customer.ID);
}
[Fact]
public void Post_With_EmptyConfiguration_ThrowsNoRoute()
{
// Arrange
CustomersController controller = new CustomersController();
controller.Configuration = new HttpConfiguration();
controller.Request = new HttpRequestMessage();
// Act
Assert.ThrowsArgument(
() => controller.Post(new Customer { ID = 42 }),
"name",
"A route named 'default' could not be found in the route collection.");
}
[Fact]
public void Post_With_InitializeConfigurationAndRequestAndRouteData()
{
// Arrange
CustomersController controller = new CustomersController();
controller.Configuration = new HttpConfiguration();
controller.Configuration.Routes.MapHttpRoute("default", "{controller}/{id}", new { id = RouteParameter.Optional });
controller.RequestContext.RouteData = new HttpRouteData(new HttpRoute(), new HttpRouteValueDictionary { { "controller", "Customers" } });
controller.Request = new HttpRequestMessage { RequestUri = new Uri("http://locahost/Customers") };
// Act
var result = controller.Post(new Customer { ID = 42 });
// Assert
Customer customer;
Assert.Equal("http://locahost/Customers/42", result.Headers.Location.AbsoluteUri);
Assert.True(result.TryGetContentValue<Customer>(out customer));
Assert.Equal(42, customer.ID);
}
private class CustomersController : ApiController
{
public HttpResponseMessage Post(Customer c)
{
var response = Request.CreateResponse(HttpStatusCode.Created, c);
response.Headers.Location = new Uri(Url.Link("default", new { id = c.ID }));
return response;
}
}
private class Customer
{
public int ID { get; set; }
public string Name { get; set; }
}
}
}