Skip to content

Commit 69821c9

Browse files
committed
Merge pull request #240 from ivaylokenov/mocking-controller-properties
Base address for all requests
2 parents 378c320 + 2333575 commit 69821c9

File tree

18 files changed

+171
-31
lines changed

18 files changed

+171
-31
lines changed
-92.8 KB
Binary file not shown.
93.4 KB
Binary file not shown.

documentation/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
- Global configuration
99
- [Using custom HttpConfiguration](#using-custom-httpconfiguration)
10+
- [Base address](#base-address)
1011
- Route validations
1112
- [Building route request](#building-route-request)
1213
- [Testing routes](#testing-routes)
@@ -78,6 +79,29 @@ MyWebApi
7879

7980
[To top](#table-of-contents)
8081

82+
### Base address
83+
84+
By default all unit tests are run with request host **"http://localhost"** and **Url.Link** will use it as base address. Another host can be configured easily after setting the HTTP configuration:
85+
86+
```c#
87+
// sets all test requests to come from custom base address
88+
// ** this will also set the global remote server
89+
MyWebApi
90+
.IsRegisteredWith(WebApiConfig.Register)
91+
.WithBaseAddress("http://mytestedasp.net");
92+
93+
// these are also available
94+
MyWebApi
95+
.IsUsingDefaultHttpConfiguration()
96+
.WithBaseAddress("http://mytestedasp.net");
97+
98+
MyWebApi
99+
.IsUsing(httpConfiguration)
100+
.WithBaseAddress("http://mytestedasp.net");
101+
```
102+
103+
[To top](#table-of-contents)
104+
81105
### Building route request
82106

83107
You can test routes using the internal ASP.NET Web API
@@ -2630,6 +2654,8 @@ MyWebApi
26302654
.ContainingContentHeader(HttpContentHeader.ContentType);
26312655

26322656
// configure global base address
2657+
// ** this step is not necessary, if you already provided
2658+
// ** a base address through the configuration
26332659
MyWebApi.Server().IsLocatedAt("http://mytestedasp.net");
26342660

26352661
MyWebApi

src/MyTested.WebApi.Tests/BuildersTests/AttributesTests/ControllerAttributesTestBuilderTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void ChangingRoutePrefixToShouldNotThrowExceptionWithCorrectTheAttribute(
123123
MyWebApi
124124
.Controller<WebApiController>()
125125
.ShouldHave()
126-
.Attributes(attributes => attributes.ChangingRoutePrefixTo("/api/test"));
126+
.Attributes(attributes => attributes.ChangingRoutePrefixTo("api/test"));
127127
}
128128

129129
[Test]
@@ -132,19 +132,19 @@ public void ChangingRoutePrefixToShouldNotThrowExceptionWithCorrectTheAttributeA
132132
MyWebApi
133133
.Controller<WebApiController>()
134134
.ShouldHave()
135-
.Attributes(attributes => attributes.ChangingRoutePrefixTo("/api/Test"));
135+
.Attributes(attributes => attributes.ChangingRoutePrefixTo("api/Test"));
136136
}
137137

138138
[Test]
139139
[ExpectedException(
140140
typeof(AttributeAssertionException),
141-
ExpectedMessage = "When testing WebApiController was expected to have RoutePrefixAttribute with '/api/another' prefix, but in fact found '/api/test'.")]
141+
ExpectedMessage = "When testing WebApiController was expected to have RoutePrefixAttribute with 'api/another' prefix, but in fact found 'api/test'.")]
142142
public void ChangingRoutePrefixToShouldThrowExceptionWithControllerWithTheAttributeAndWrongPrefix()
143143
{
144144
MyWebApi
145145
.Controller<WebApiController>()
146146
.ShouldHave()
147-
.Attributes(attributes => attributes.ChangingRoutePrefixTo("/api/another"));
147+
.Attributes(attributes => attributes.ChangingRoutePrefixTo("api/another"));
148148
}
149149

150150
[Test]
@@ -156,7 +156,7 @@ public void ChangingActionNameToShouldThrowExceptionWithActionWithoutTheAttribut
156156
MyWebApi
157157
.Controller<AttributesController>()
158158
.ShouldHave()
159-
.Attributes(attributes => attributes.ChangingRoutePrefixTo("/api/test"));
159+
.Attributes(attributes => attributes.ChangingRoutePrefixTo("api/test"));
160160
}
161161

162162
[Test]

src/MyTested.WebApi.Tests/BuildersTests/ControllersTests/ControllerBuilderTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace MyTested.WebApi.Tests.BuildersTests.ControllersTests
1212
using System.Web.Http.Results;
1313
using Builders.Contracts.Actions;
1414
using Builders.Contracts.Base;
15+
using Common.Servers;
1516
using Exceptions;
1617
using NUnit.Framework;
1718
using Setups;
@@ -420,6 +421,39 @@ public void WithHttpConfigurationShouldOverrideTheDefaultOne()
420421
Assert.AreSame(config, controllerConfigFromApi);
421422
}
422423

424+
[Test]
425+
public void LinkGenerationShouldWorkCorrectlyWithDefaultConfiguration()
426+
{
427+
MyWebApi.IsUsingDefaultHttpConfiguration();
428+
429+
MyWebApi
430+
.Controller<WebApiController>()
431+
.Calling(c => c.WithGeneratedLink(1))
432+
.ShouldReturn()
433+
.Created()
434+
.AtLocation("http://localhost/api/test?id=1");
435+
436+
MyWebApi.IsUsing(TestObjectFactory.GetHttpConfigurationWithRoutes());
437+
}
438+
439+
[Test]
440+
public void LinkGenerationShouldWorkCorrectlyWithCustomBaseAddress()
441+
{
442+
MyWebApi
443+
.IsUsingDefaultHttpConfiguration()
444+
.WithBaseAddress("http://mytestedasp.net");
445+
446+
MyWebApi
447+
.Controller<WebApiController>()
448+
.Calling(c => c.WithGeneratedLink(1))
449+
.ShouldReturn()
450+
.Created()
451+
.AtLocation("http://mytestedasp.net/api/test?id=1");
452+
453+
RemoteServer.DisposeGlobal();
454+
MyWebApi.IsUsing(TestObjectFactory.GetHttpConfigurationWithRoutes());
455+
}
456+
423457
private void CheckActionResultTestBuilder<TActionResult>(
424458
IActionResultTestBuilder<TActionResult> actionResultTestBuilder,
425459
string expectedActionName)

src/MyTested.WebApi.Tests/BuildersTests/HttpConfigurationBuilderTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ public void DefaultErrorDetailPolicyShouldBeAlways()
3838
MyWebApi.IsUsing(TestObjectFactory.GetHttpConfigurationWithRoutes());
3939
}
4040

41+
[Test]
42+
public void WithBaseAddressShouldChangedDefaultAddress()
43+
{
44+
Assert.IsFalse(RemoteServer.GlobalIsConfigured);
45+
Assert.AreEqual(MyWebApi.DefaultHost, MyWebApi.BaseAddress.OriginalString);
46+
47+
string address = "http://mytestedasp.net";
48+
49+
MyWebApi
50+
.IsUsingDefaultHttpConfiguration()
51+
.WithBaseAddress(address);
52+
53+
Assert.AreEqual(address, MyWebApi.BaseAddress.OriginalString);
54+
Assert.IsTrue(RemoteServer.GlobalIsConfigured);
55+
56+
MyWebApi.IsUsing(TestObjectFactory.GetHttpConfigurationWithRoutes());
57+
58+
Assert.AreEqual(MyWebApi.DefaultHost, MyWebApi.BaseAddress.OriginalString);
59+
60+
RemoteServer.DisposeGlobal();
61+
}
62+
4163
[Test]
4264
public void WithErrorDetailPolicyShouldSetCorrectErrorDetailPolicy()
4365
{

src/MyTested.WebApi.Tests/MyWebApiTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void IsUsingDefaultConfigurationShouldWorkCorrectly()
124124
MyWebApi.IsUsingDefaultHttpConfiguration();
125125

126126
Assert.IsNotNull(MyWebApi.Configuration);
127-
Assert.AreEqual(0, MyWebApi.Configuration.Routes.Count);
127+
Assert.IsTrue(MyWebApi.Configuration.Routes.ContainsKey("API Default"));
128128

129129
MyWebApi.IsUsing(TestObjectFactory.GetHttpConfigurationWithRoutes());
130130
}

src/MyTested.WebApi.Tests/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
[assembly: ComVisible(false)]
1515
[assembly: Guid("29f6a15e-c6d1-4c93-83c8-18ff839950e8")]
1616

17-
[assembly: AssemblyVersion("1.1.5.0")]
18-
[assembly: AssemblyFileVersion("1.1.5.0")]
17+
[assembly: AssemblyVersion("1.1.9.0")]
18+
[assembly: AssemblyFileVersion("1.1.9.0")]

src/MyTested.WebApi.Tests/Setups/Controllers/WebApiController.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace MyTested.WebApi.Tests.Setups.Controllers
2020
using Services;
2121

2222
[Authorize(Roles = "Admin,Moderator", Users = "John,George")]
23-
[RoutePrefix("/api/test")]
23+
[RoutePrefix("api/test")]
2424
internal class WebApiController : ApiController
2525
{
2626
private readonly ICollection<ResponseModel> responseModel;
@@ -70,6 +70,12 @@ public ICollection<ResponseModel> ResponseModel
7070

7171
public RequestModel InjectedRequestModel { get; private set; }
7272

73+
public IHttpActionResult WithGeneratedLink(int id)
74+
{
75+
var link = this.Url.Link("TestRouteAttributes", new { id = 1 });
76+
return this.Created(link, "content");
77+
}
78+
7379
public IHttpActionResult CustomRequestAction()
7480
{
7581
if (this.Request.Method == HttpMethod.Post && this.Request.Headers.Contains("TestHeader"))

src/MyTested.WebApi/Builders/Contracts/IHttpConfigurationBuilder.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,12 @@ public interface IHttpConfigurationBuilder
2323
/// <param name="errorDetailPolicy">Error details policy to use.</param>
2424
/// <returns>The same HTTP configuration builder.</returns>
2525
IHttpConfigurationBuilder WithErrorDetailPolicy(IncludeErrorDetailPolicy errorDetailPolicy);
26+
27+
/// <summary>
28+
/// Sets the global base address to be used across the test cases. Default is local host.
29+
/// </summary>
30+
/// <param name="baseAddress">Base address to use.</param>
31+
/// <returns>The same HTTP configuration builder.</returns>
32+
IHttpConfigurationBuilder WithBaseAddress(string baseAddress);
2633
}
2734
}

0 commit comments

Comments
 (0)