-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test message sender with fake handler (#14)
- Loading branch information
1 parent
9eb6bff
commit 98d64b4
Showing
14 changed files
with
310 additions
and
42 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
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,16 @@ | ||
using System; | ||
|
||
namespace Castle.Infrastructure | ||
{ | ||
internal static class QueryStringBuilder | ||
{ | ||
public static string Append(string url, string key, string value) | ||
{ | ||
if (string.IsNullOrEmpty(value)) | ||
return url; | ||
|
||
var prefix = url.Contains("?") ? "&" : "?"; | ||
return $"{url}{prefix}{key}={value}"; | ||
} | ||
} | ||
} |
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,40 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Tests.SetUp | ||
{ | ||
public class FakeHttpMessageHandler : HttpMessageHandler | ||
{ | ||
private readonly Exception _throws; | ||
private readonly HttpStatusCode _statusCode = HttpStatusCode.OK; | ||
|
||
public FakeHttpMessageHandler() | ||
{ | ||
|
||
} | ||
|
||
public FakeHttpMessageHandler(HttpStatusCode statusCode) | ||
{ | ||
_statusCode = statusCode; | ||
} | ||
|
||
public FakeHttpMessageHandler(Exception throws) | ||
{ | ||
_throws = throws; | ||
} | ||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
if (_throws != null) | ||
throw _throws; | ||
|
||
return Task.FromResult(new HttpResponseMessage(_statusCode) | ||
{ | ||
Content = new StringContent("{\"prop\":\"fake\"}") | ||
}); | ||
} | ||
} | ||
} |
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,31 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using AutoFixture; | ||
|
||
namespace Tests.SetUp | ||
{ | ||
public class HttpMessageHandlerSuccessCustomization : ICustomization | ||
{ | ||
public void Customize(IFixture fixture) | ||
{ | ||
fixture.Register<HttpMessageHandler>(() => new FakeHttpMessageHandler()); | ||
} | ||
} | ||
|
||
public class HttpMessageHandlerFailureCustomization : ICustomization | ||
{ | ||
public void Customize(IFixture fixture) | ||
{ | ||
fixture.Register<HttpMessageHandler>(() => new FakeHttpMessageHandler(HttpStatusCode.InternalServerError)); | ||
} | ||
} | ||
|
||
public class HttpMessageHandlerCancelledCustomization : ICustomization | ||
{ | ||
public void Customize(IFixture fixture) | ||
{ | ||
fixture.Register<HttpMessageHandler>(() => new FakeHttpMessageHandler(new OperationCanceledException())); | ||
} | ||
} | ||
} |
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,48 @@ | ||
using Castle.Infrastructure; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Tests | ||
{ | ||
public class When_building_query_strings | ||
{ | ||
[Fact] | ||
public void Should_append_with_questionmark_if_first_param() | ||
{ | ||
const string url = "http://test.com"; | ||
const string key = "my_param"; | ||
const string value = "my_value"; | ||
const string expected = "http://test.com?my_param=my_value"; | ||
|
||
var result = QueryStringBuilder.Append(url, key, value); | ||
|
||
result.Should().Be(expected); | ||
} | ||
|
||
[Fact] | ||
public void Should_append_with_ampersand_if_not_first_param() | ||
{ | ||
const string url = "http://test.com?other_param=other_value"; | ||
const string key = "my_param"; | ||
const string value = "my_value"; | ||
const string expected = "http://test.com?other_param=other_value&my_param=my_value"; | ||
|
||
var result = QueryStringBuilder.Append(url, key, value); | ||
|
||
result.Should().Be(expected); | ||
} | ||
|
||
[Fact] | ||
public void Should_not_append_empty_value() | ||
{ | ||
const string url = "http://test.com"; | ||
const string key = "my_param"; | ||
const string value = null; | ||
const string expected = "http://test.com"; | ||
|
||
var result = QueryStringBuilder.Append(url, key, value); | ||
|
||
result.Should().Be(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
Oops, something went wrong.