public async Task Executes_OnException_WhenParsingFails_WithCorrectContext( Exception exception, HttpRequestMessage request, [Frozen] IJSendParser parser, [Frozen] MessageInterceptor interceptor, [Greedy, MockHttpClient] JSendClient client) { // Fixture setup Mock.Get(parser) .Setup(c => c.ParseAsync <object>(It.IsAny <JsonSerializerSettings>(), It.IsAny <HttpResponseMessage>())) .ThrowsAsync(exception); // Exercise system try { await client.SendAsync <object>(request); } catch { } // Verify outcome Mock.Get(interceptor) .Verify(i => i.OnException(It.Is <ExceptionContext>( ctx => ctx.HttpRequest == request && ctx.Exception == exception )), Times.Once); }
public async Task Executes_OnException_WhenSendingFails_WithCorrectContext( Exception exception, HttpRequestMessage request, [MockHttpClient] HttpClient httpClient, [Frozen] MessageInterceptor interceptor, [Greedy] JSendClient client) { // Fixture setup Mock.Get(httpClient) .Setup(c => c.SendAsync(It.IsAny <HttpRequestMessage>(), It.IsAny <CancellationToken>())) .ThrowsAsync(exception); // Exercise system try { await client.SendAsync <object>(request); } catch { } // Verify outcome Mock.Get(interceptor) .Verify(i => i.OnException(It.Is <ExceptionContext>( ctx => ctx.HttpRequest == request && ctx.Exception == exception )), Times.Once); }
public void JSendParser_IsDefaultJSendParser_ByDefault() { // Exercise system var client = new JSendClient(); // Verify outcome client.Parser.Should().BeSameAs(DefaultJSendParser.Instance); }
public void MessageInterceptor_IsNullMessageInterceptor_ByDefault() { // Exercise system var client = new JSendClient(); // Verify outcome client.MessageInterceptor.Should().BeSameAs(NullMessageInterceptor.Instance); }
public void Encoding_IsNull_ByDefault() { // Exercise system var client = new JSendClient(); // Verify outcome client.Encoding.Should().BeNull(); }
public void SerializerSettings_IsCorrectlyInitialized(JSendClientSettings settings) { // Exercise system var client = new JSendClient(settings); // Verify outcome client.SerializerSettings.Should().BeSameAs(settings.SerializerSettings); }
public void SerializerSettings_IsNull_ByDefault() { // Exercise system var client = new JSendClient(); // Verify outcome client.SerializerSettings.Should().BeNull(); }
public void HttpClient_IsCorrectlyInitialized(HttpClient httpClient) { // Exercise system var client = new JSendClient(null, httpClient); // Verify outcome client.HttpClient.Should().BeSameAs(httpClient); }
public void HttpClient_IsNotNull_ByDefault() { // Exercise system var client = new JSendClient(); // Verify outcome client.HttpClient.Should().NotBeNull(); }
public void MessageInterceptor_IsCorrectlyInitialized(JSendClientSettings settings) { // Exercise system var client = new JSendClient(settings); // Verify outcome client.MessageInterceptor.Should().BeSameAs(settings.MessageInterceptor); }
public void DoesNotParseInvalidResponses(string route, JSendClient client) { using (client) { // Exercise system and verify outcome client .Awaiting(c => c.GetAsync <User>("http://localhost/users/" + route)) .ShouldThrow <JSendParseException>(); } }
public async Task PutsContentInEndpoint(User inputData, JSendClient client) { // Exercise system using (client) using (var response = await client.PutAsync <User>("http://localhost/users/put-echo", inputData)) { // Verify outcome response.Data.ShouldBeEquivalentTo(inputData); } }
public void DisposingOfTheJSendClient_DisposesOfTheHttpClient(HttpClientSpy spy) { // Fixture setup var client = new JSendClient(null, spy); // Exercise system client.Dispose(); // Verify outcome spy.Disposed.Should().BeTrue(); }
public void DoesNotParseInvalidResponses(string route, JSendClient client) { using (client) { // Exercise system and verify outcome client .Awaiting(c => c.GetAsync<User>("http://localhost/users/" + route)) .ShouldThrow<JSendParseException>(); } }
public void InterceptsResponsesBeforeBeingParsed( [WithInterceptor(typeof(ReplaceEmptyBodyInterceptor))] JSendClient client) { using (client) { // Exercise system and verify outcome client.Awaiting(c => c.GetAsync <string>("http://localhost/users/empty-body")) .ShouldNotThrow(); } }
public async Task ParsesSuccessResponseWithoutData(JSendClient client) { // Exercise system using (client) using (var response = await client.GetAsync <User>("http://localhost/users/success")) { // Verify outcome response.Status.Should().Be(JSendStatus.Success); response.HasData.Should().BeFalse(); } }
public async Task ParsesSuccessResponseWithoutData(JSendClient client) { // Exercise system using (client) using (var response = await client.GetAsync<User>("http://localhost/users/success")) { // Verify outcome response.Status.Should().Be(JSendStatus.Success); response.HasData.Should().BeFalse(); } }
public async Task ParsesFailResponse(JSendClient client) { // Exercise system using (client) using (var response = await client.GetAsync <User>("http://localhost/users/fail")) { // Verify outcome response.Status.Should().Be(JSendStatus.Fail); response.HasData.Should().BeFalse(); response.Error.Data.Value <string>().Should().Be(UsersController.ErrorData); } }
public async Task DeleteAsync_SendsPostRequest( [FrozenAsHttpClient] HttpClientSpy httpClientSpy, Uri uri, [Greedy] JSendClient client) { // Exercise system await client.DeleteAsync(uri); // Verify outcome var request = httpClientSpy.Request; request.Method.Should().Be(HttpMethod.Delete); }
public async Task ParsesSuccessResponseWithData(JSendClient client) { // Exercise system using (client) using (var response = await client.GetAsync<User>("http://localhost/users/success-with-user")) { // Verify outcome response.Status.Should().Be(JSendStatus.Success); response.HasData.Should().BeTrue(); response.Data.ShouldBeEquivalentTo(UsersController.TestUser); } }
public async Task PutAsync_SendsPutRequest( [FrozenAsHttpClient] HttpClientSpy httpClientSpy, Uri uri, object content, [Greedy] JSendClient client) { // Exercise system await client.PutAsync(uri, content); // Verify outcome var request = httpClientSpy.Request; request.Method.Should().Be(HttpMethod.Put); }
public async Task PutAsync_SetsContentTypeHeader( [FrozenAsHttpClient] HttpClientSpy httpClientSpy, Uri uri, object content, [Greedy] JSendClient client) { // Exercise system await client.PutAsync(uri, content); // Verify outcome var request = httpClientSpy.Request; request.Content.Headers.ContentType.MediaType.Should().Be("application/json"); }
public async Task Executes_OnSending_WithCorrectRequest( HttpRequestMessage request, [Frozen] MessageInterceptor interceptor, [Greedy, MockHttpClient] JSendClient client) { // Exercise system await client.SendAsync <object>(request); // Verify outcome Mock.Get(interceptor) .Verify(i => i.OnSending(request)); }
public async Task ParsesFailResponse(JSendClient client) { // Exercise system using (client) using (var response = await client.GetAsync<User>("http://localhost/users/fail")) { // Verify outcome response.Status.Should().Be(JSendStatus.Fail); response.HasData.Should().BeFalse(); response.Error.Data.Value<string>().Should().Be(UsersController.ErrorData); } }
public async Task GetAsync_SendsGetRequest( [FrozenAsHttpClient] HttpClientSpy httpClientSpy, Uri uri, [Greedy] JSendClient client) { // Exercise system await client.GetAsync <Model>(uri); // Verify outcome var request = httpClientSpy.Request; request.Method.Should().Be(HttpMethod.Get); }
public async Task ParsesSuccessResponseWithData(JSendClient client) { // Exercise system using (client) using (var response = await client.GetAsync <User>("http://localhost/users/success-with-user")) { // Verify outcome response.Status.Should().Be(JSendStatus.Success); response.HasData.Should().BeTrue(); response.Data.ShouldBeEquivalentTo(UsersController.TestUser); } }
public async Task InterceptsRequestsBeforeBeingSent( [WithInterceptor(typeof(VersionHeaderInterceptor))] JSendClient client) { // Exercise system using (client) using (var response = await client.GetAsync <Dictionary <string, List <string> > >("http://localhost/users/echo-headers")) { // Verify outcome response.Data.Should().ContainKey("Version") .WhichValue.Should().ContainSingle("1"); } }
public async Task InterceptsParsedResponses( [FrozenWithoutAutoProperties] InterceptorSpy spy, [WithInterceptor(typeof(InterceptorSpy))] JSendClient client) { using (client) using (var response = await client.GetAsync <string>("http://localhost/users/get")) { // Exercise system and verify outcome spy.ResponseParsedContext.Should().NotBeNull(); spy.ResponseParsedContext.As <ResponseParsedContext <string> >() .JSendResponse.Should().Be(response); } }
public void BlockingDoesNotCauseDeadlocks(JSendClient client) { // Fixture setup // Use a synchronization context that schedules continuations on the initial thread to mimic an UI application. // This scenario is also somewhat equivalent to an ASP.NET environment, where the request context is not tied to a specific thread, // but is limited to one thread at a time (http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html). Action action = () => AsyncContext.Run(() => { client.GetAsync <string>("http://localhost/users/get").Wait(); }); // Exercise system and verify outcome action.ExecutionTime().ShouldNotExceed(3.Seconds()); }
public async Task PutAsync_SetsCharSet( [FrozenAsHttpClient] HttpClientSpy httpClientSpy, Uri uri, object content, [Greedy] JSendClient client) { // Fixture setup var expectedCharSet = client.Encoding.WebName; // Exercise system await client.PutAsync(uri, content); // Verify outcome var request = httpClientSpy.Request; request.Content.Headers.ContentType.CharSet.Should().Be(expectedCharSet); }
public async Task PutAsync_SerializesContent( [FrozenAsHttpClient] HttpClientSpy httpClientSpy, Uri uri, Model content, [Greedy] JSendClient client) { // Fixture setup var expectedContent = JsonConvert.SerializeObject(content); // Exercise system await client.PutAsync(uri, content); // Verify outcome var actualContent = httpClientSpy.Content; actualContent.Should().Be(expectedContent); }
public async Task DeleteAsync_SetsUri( string uri, Uri expectedUri, [FrozenAsHttpClient] HttpClientSpy httpClientSpy, [Greedy] JSendClient client) { // Exercise system await client.DeleteAsync(uri); // Verify outcome var request = httpClientSpy.Request; request.RequestUri.Should().Be(expectedUri); }
public void HttpClientExceptionsAreWrappedAndRethrown( HttpRequestMessage request, [MockHttpClient] HttpClient httpClient, [Greedy] JSendClient client) { // Fixture setup Mock.Get(httpClient) .Setup(c => c.SendAsync(request, It.IsAny <CancellationToken>())) .Throws <HttpRequestException>(); // Exercise system and verify outcome client .Awaiting(c => c.SendAsync <object>(request)) .ShouldThrow <JSendRequestException>() .WithInnerException <HttpRequestException>(); }
public void ParserExceptionsBubbleUp( HttpRequestMessage request, JSendParseException exception, [Frozen] IJSendParser parser, [Greedy, MockHttpClient] JSendClient client) { // Fixture setup Mock.Get(parser) .Setup(p => p.ParseAsync <object>(It.IsAny <JsonSerializerSettings>(), It.IsAny <HttpResponseMessage>())) .ThrowsAsync(exception); // Exercise system and verify outcome client .Awaiting(c => c.SendAsync <object>(request)) .ShouldThrow <JSendParseException>(); }
public async Task SendsGetRequestsToTheCorrectEndpoint(string baseAddress, string requestUri, JSendClient client) { using (client) { // Fixture setup client.HttpClient.BaseAddress = baseAddress == null ? null : new Uri(baseAddress); // Exercise system using (var response = await client.GetAsync<string>(requestUri)) { // Verify outcome response.Status.Should().Be(JSendStatus.Success); response.Data.Should().Be("get"); } } }
public async Task PutsContentInEndpoint(User inputData, JSendClient client) { // Exercise system using (client) using (var response = await client.PutAsync<User>("http://localhost/users/put-echo", inputData)) { // Verify outcome response.Data.ShouldBeEquivalentTo(inputData); } }