-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from ppittle/multiple-creators-per-session
added HttpWebRequestWrapperDelegateCreator
- Loading branch information
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.Net; | ||
using Moq; | ||
using Should; | ||
using Xunit; | ||
|
||
namespace HttpWebRequestWrapper.Tests | ||
{ | ||
/// <summary> | ||
/// Tests for <see cref="HttpWebRequestWrapperDelegateCreator"/> | ||
/// </summary> | ||
public class DelegateCreatorTests | ||
{ | ||
[Fact] | ||
public void CanUseMultipleWebRequestCreators() | ||
{ | ||
// ARRANGE | ||
var url1 = new Uri("http://fakesite.fake/1"); | ||
var url2 = new Uri("http://fakesite.fake/2"); | ||
|
||
var mockRequest1 = new Mock<WebRequest>(); | ||
var mockRequest2 = new Mock<WebRequest>(); | ||
|
||
var mockRequestCreator1 = new Mock<IWebRequestCreate>(); | ||
mockRequestCreator1 | ||
.Setup(x => x.Create(It.IsAny<Uri>())) | ||
.Returns(mockRequest1.Object); | ||
|
||
var mockRequestCreator2 = new Mock<IWebRequestCreate>(); | ||
mockRequestCreator2 | ||
.Setup(x => x.Create(It.IsAny<Uri>())) | ||
.Returns(mockRequest2.Object); | ||
|
||
var creatorSelector = new Func<Uri, IWebRequestCreate>(url => | ||
url == url1 | ||
? mockRequestCreator1.Object | ||
: mockRequestCreator2.Object); | ||
|
||
var delegateCreator = new HttpWebRequestWrapperDelegateCreator(creatorSelector); | ||
|
||
WebRequest request1, request2; | ||
|
||
// ACT | ||
using (new HttpWebRequestWrapperSession(delegateCreator)) | ||
{ | ||
request1 = WebRequest.Create(url1); | ||
request2 = WebRequest.Create(url2); | ||
} | ||
|
||
// ASSERT | ||
request1.ShouldEqual(mockRequest1.Object); | ||
request2.ShouldEqual(mockRequest2.Object); | ||
|
||
mockRequestCreator1.Verify(x => | ||
x.Create(It.Is<Uri>(v => v == url1)), | ||
Times.Once); | ||
mockRequestCreator1.Verify(x => | ||
x.Create(It.Is<Uri>(v => v == url2)), | ||
Times.Never); | ||
|
||
mockRequestCreator2.Verify(x => | ||
x.Create(It.Is<Uri>(v => v == url1)), | ||
Times.Never); | ||
mockRequestCreator2.Verify(x => | ||
x.Create(It.Is<Uri>(v => v == url2)), | ||
Times.Once); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/HttpWebRequestWrapper/HttpWebRequestWrapperDelegateCreator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Net; | ||
|
||
namespace HttpWebRequestWrapper | ||
{ | ||
/// <summary> | ||
/// Helper component that supports just-in-time selection of a | ||
/// <see cref="IWebRequestCreate"/> based on the requested <see cref="Uri"/>. | ||
/// <para /> | ||
/// This is only anticipated to be useful when writing BDD style | ||
/// tests that cover a large application surface area - where the system | ||
/// might be making calls to two different api endpoints and it is helpful to | ||
/// have requests be routed to different <see cref="IWebRequestCreate"/>s | ||
/// (ie <see cref="HttpWebRequestWrapperInterceptorCreator"/>) based on url. | ||
/// <para /> | ||
/// For example, requests to http://api1/ could go to one Interceptor with a specific | ||
/// <see cref="RecordingSession"/> and playback behavior and requests to http://api2/ | ||
/// could go to a different Interceptor with its own <see cref="RecordingSession"/> | ||
/// and different playback behavior. | ||
/// </summary> | ||
public class HttpWebRequestWrapperDelegateCreator : IWebRequestCreate | ||
{ | ||
private readonly Func<Uri, IWebRequestCreate> _creatorSelectorFunc; | ||
|
||
/// <summary> | ||
/// Helper component that supports just-in-time selection of a | ||
/// <see cref="IWebRequestCreate"/> based on the requested <see cref="Uri"/> | ||
/// via <paramref name="creatorSelector"/>. | ||
/// <para /> | ||
/// This is only anticipated to be useful when writing BDD style | ||
/// tests that cover a large application surface area - where the system | ||
/// might be making calls to two different api endpoints and it is helpful to | ||
/// have requests be routed to different <see cref="IWebRequestCreate"/>s | ||
/// (ie <see cref="HttpWebRequestWrapperInterceptorCreator"/>) based on url. | ||
/// <para /> | ||
/// For example, requests to http://api1/ could go to one Interceptor with a specific | ||
/// <see cref="RecordingSession"/> and playback behavior and requests to http://api2/ | ||
/// could go to a different Interceptor with its own <see cref="RecordingSession"/> | ||
/// and different playback behavior. | ||
/// </summary> | ||
public HttpWebRequestWrapperDelegateCreator(Func<Uri, IWebRequestCreate> creatorSelector) | ||
{ | ||
_creatorSelectorFunc = creatorSelector; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public WebRequest Create(Uri uri) | ||
{ | ||
return _creatorSelectorFunc(uri).Create(uri); | ||
} | ||
} | ||
} |