File tree Expand file tree Collapse file tree 5 files changed +25
-8
lines changed
MyTested.WebApi.Tests/BuildersTests Expand file tree Collapse file tree 5 files changed +25
-8
lines changed Original file line number Diff line number Diff line change 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,20 @@ 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+
94+ [ To top] ( #table-of-contents )
95+
8196### Building route request
8297
8398You can test routes using the internal ASP.NET Web API
@@ -2630,6 +2645,8 @@ MyWebApi
26302645 .ContainingContentHeader (HttpContentHeader .ContentType );
26312646
26322647// configure global base address
2648+ // ** this step is not necessary, if you already provided
2649+ // ** a base address through the configuration
26332650MyWebApi .Server ().IsLocatedAt (" http://mytestedasp.net" );
26342651
26352652MyWebApi
Original file line number Diff line number Diff line change @@ -42,20 +42,20 @@ public void DefaultErrorDetailPolicyShouldBeAlways()
4242 public void WithBaseAddressShouldChangedDefaultAddress ( )
4343 {
4444 Assert . IsFalse ( RemoteServer . GlobalIsConfigured ) ;
45- Assert . AreEqual ( MyWebApi . DefaultHost , MyWebApi . BaseAddress ) ;
45+ Assert . AreEqual ( MyWebApi . DefaultHost , MyWebApi . BaseAddress . OriginalString ) ;
4646
4747 string address = "http://mytestedasp.net" ;
4848
4949 MyWebApi
5050 . IsUsingDefaultHttpConfiguration ( )
5151 . WithBaseAddress ( address ) ;
5252
53- Assert . AreEqual ( address , MyWebApi . BaseAddress ) ;
53+ Assert . AreEqual ( address , MyWebApi . BaseAddress . OriginalString ) ;
5454 Assert . IsTrue ( RemoteServer . GlobalIsConfigured ) ;
5555
5656 MyWebApi . IsUsing ( TestObjectFactory . GetHttpConfigurationWithRoutes ( ) ) ;
5757
58- Assert . AreEqual ( MyWebApi . DefaultHost , MyWebApi . BaseAddress ) ;
58+ Assert . AreEqual ( MyWebApi . DefaultHost , MyWebApi . BaseAddress . OriginalString ) ;
5959
6060 RemoteServer . DisposeGlobal ( ) ;
6161 }
Original file line number Diff line number Diff line change 44// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
55namespace MyTested . WebApi . Builders
66{
7+ using System ;
78 using System . Web . Http ;
89 using Common . Servers ;
910 using Contracts ;
@@ -53,7 +54,7 @@ public IHttpConfigurationBuilder WithErrorDetailPolicy(IncludeErrorDetailPolicy
5354 /// <returns>The same HTTP configuration builder.</returns>
5455 public IHttpConfigurationBuilder WithBaseAddress ( string baseAddress )
5556 {
56- MyWebApi . BaseAddress = baseAddress ;
57+ MyWebApi . BaseAddress = new Uri ( baseAddress , UriKind . Absolute ) ;
5758
5859 if ( ! RemoteServer . GlobalIsConfigured )
5960 {
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ public static void TransformToAbsoluteRequestUri(this HttpRequestMessage request
2020 {
2121 if ( request . RequestUri != null && ! request . RequestUri . IsAbsoluteUri )
2222 {
23- request . RequestUri = new Uri ( new Uri ( MyWebApi . BaseAddress ) , request . RequestUri ) ;
23+ request . RequestUri = new Uri ( MyWebApi . BaseAddress , request . RequestUri ) ;
2424 }
2525 }
2626 }
Original file line number Diff line number Diff line change @@ -34,7 +34,6 @@ public static class MyWebApi
3434
3535 static MyWebApi ( )
3636 {
37- BaseAddress = DefaultHost ;
3837 IsUsingDefaultHttpConfiguration ( ) ;
3938 }
4039
@@ -48,7 +47,7 @@ static MyWebApi()
4847 /// Gets the current base address used in the testing.
4948 /// </summary>
5049 /// <value>Instance of String.</value>
51- public static string BaseAddress { get ; internal set ; }
50+ public static Uri BaseAddress { get ; internal set ; }
5251
5352 /// <summary>
5453 /// Sets the default HttpConfiguration which will be used in all tests.
@@ -76,7 +75,7 @@ public static IHttpConfigurationBuilder IsUsingDefaultHttpConfiguration()
7675 public static IHttpConfigurationBuilder IsUsing ( HttpConfiguration httpConfiguration )
7776 {
7877 Configuration = httpConfiguration ;
79- BaseAddress = DefaultHost ;
78+ BaseAddress = new Uri ( DefaultHost , UriKind . Absolute ) ;
8079 return new HttpConfigurationBuilder ( httpConfiguration ) ;
8180 }
8281
You can’t perform that action at this time.
0 commit comments