-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support matching requests using headers (#72)
* Support matching requests using headers * Tidy up matching on request headers - Add null check for RequestHeaders - Make HeaderMatch and QueryParamMatch fields of EndpointMatchingRule
- Loading branch information
Showing
9 changed files
with
229 additions
and
5 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
89 changes: 89 additions & 0 deletions
89
src/HttpMock.Integration.Tests/UsingTheSameStubServerAndDifferentRequestHeaders.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,89 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net; | ||
using NUnit.Framework; | ||
|
||
namespace HttpMock.Integration.Tests | ||
{ | ||
[TestFixture] | ||
public class UsingTheSameStubServerAndDifferentRequestHeaders | ||
{ | ||
private string _endpointToHit; | ||
private IHttpServer _httpMockRepository; | ||
|
||
private readonly IDictionary<string, string> _firstSetOfHeaders = new Dictionary<string, string> | ||
{ | ||
{"X-HeaderOne", "one"}, | ||
{"X-HeaderTwo", "a"} | ||
}; | ||
|
||
private readonly IDictionary<string, string> _secondSetOfHeaders = new Dictionary<string, string> | ||
{ | ||
{"X-HeaderOne", "one"}, | ||
{"X-HeaderTwo", "b"} | ||
}; | ||
|
||
private readonly IDictionary<string, string> _thirdSetOfHeaders = new Dictionary<string, string> | ||
{ | ||
{"X-HeaderOne", "two"}, | ||
{"X-HeaderTwo", "a"} | ||
}; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
var hostUrl = HostHelper.GenerateAHostUrlForAStubServer(); | ||
_endpointToHit = hostUrl + "/endpoint"; | ||
_httpMockRepository = HttpMockRepository.At(hostUrl); | ||
|
||
_httpMockRepository.Stub(x => x.Get("/endpoint")) | ||
.WithHeaders(_firstSetOfHeaders) | ||
.Return("I was the first one") | ||
.OK(); | ||
_httpMockRepository.Stub(x => x.Get("/endpoint")) | ||
.WithHeaders(_secondSetOfHeaders) | ||
.Return("I was the second one") | ||
.OK(); | ||
_httpMockRepository.Stub(x => x.Get("/endpoint")) | ||
.WithHeaders(_thirdSetOfHeaders) | ||
.Return("I was the third one") | ||
.OK(); | ||
} | ||
|
||
[Test] | ||
public void Should_return_first_one() | ||
{ | ||
AssertResponse("I was the first one", _firstSetOfHeaders); | ||
} | ||
|
||
[Test] | ||
public void Should_return_second_one() | ||
{ | ||
AssertResponse("I was the second one", _secondSetOfHeaders); | ||
} | ||
|
||
[Test] | ||
public void Should_return_third_one() | ||
{ | ||
AssertResponse("I was the third one", _thirdSetOfHeaders); | ||
} | ||
|
||
private void AssertResponse(string expected, IEnumerable<KeyValuePair<string, string>> headers) | ||
{ | ||
var webRequest = | ||
(HttpWebRequest) WebRequest.Create(string.Format("{0}?abirdinthehand=twointhebush", _endpointToHit)); | ||
foreach (var header in headers) | ||
{ | ||
webRequest.Headers.Add(header.Key, header.Value); | ||
} | ||
using (var response = webRequest.GetResponse()) | ||
{ | ||
using (var sr = new StreamReader(response.GetResponseStream())) | ||
{ | ||
var readToEnd = sr.ReadToEnd(); | ||
Assert.That(readToEnd, Is.EqualTo(expected)); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace HttpMock | ||
{ | ||
public class HeaderMatch { | ||
internal bool MatchHeaders(IRequestHandler requestHandler, IDictionary<string, string> requestHeaders) | ||
{ | ||
return requestHandler.RequestHeaders.All( | ||
expectedHeader => requestHeaders.Any(header => HeadersMatch(expectedHeader, header))); | ||
} | ||
|
||
private static bool HeadersMatch(KeyValuePair<string, string> expectedHeader, KeyValuePair<string, string> header) | ||
{ | ||
if (!string.Equals(expectedHeader.Key, header.Key, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return false; | ||
} | ||
return string.Equals(expectedHeader.Value, header.Value, StringComparison.OrdinalIgnoreCase); | ||
} | ||
} | ||
} |
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
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