public async void PassesTheResponseAndItsDataToTheValidator() { var expectedData = "It lives"; var expectedResponse = new Response(expectedData, true, "text/plain", new List <KeyValuePair <string, IEnumerable <string> > >(), OK); apiClient.Send(Arg.Any <Request>()).Returns(x => expectedResponse); serializer.Deserialize <string>(Arg.Any <string>()).Returns(expectedData); var credentials = Credentials.WithPassword( "*****@*****.**".ToEmail(), "theirobotmoviesucked123".ToPassword()); var endpoint = Endpoint.Get(BaseUrls.ForApi(ApiEnvironment.Staging), ""); var testApi = new TestApi(endpoint, apiClient, serializer, credentials, endpoint); IRequest receivedRequest = null; IResponse receivedResponse = null; string receivedData = null; var observable = testApi.TestCreateObservable <string>(endpoint, Enumerable.Empty <HttpHeader>(), "", (request, response, data) => (receivedRequest, receivedResponse, receivedData) = (request, response, data)); await observable.SingleAsync(); receivedRequest.HttpMethod.Should().Be(endpoint.Method); receivedRequest.Endpoint.Should().Be(endpoint.Url); receivedResponse.Should().Be(expectedResponse); receivedData.Should().Be(expectedData); }
public async Task CreatesAnObservableThatReturnsASingleValue() { apiClient.Send(Arg.Any <Request>()).Returns(x => new Response("It lives", true, "text/plain", OK)); var credentials = Credentials.WithPassword("*****@*****.**".ToEmail(), "theirobotmoviesucked123"); var endpoint = Endpoint.Get(ApiUrls.ForEnvironment(ApiEnvironment.Staging), ""); var testApi = new TestApi(endpoint, apiClient, serializer, credentials); var observable = testApi.TestCreateObservable <string>(endpoint, Enumerable.Empty <HttpHeader>(), ""); await observable.SingleAsync(); }
public void EmitsADeserializationErrorIfTheJsonSerializerThrowsAnException() { const string rawResponse = "It lives"; serializer.Deserialize <string>(Arg.Any <string>()).Returns(_ => throw new Exception()); apiClient.Send(Arg.Any <Request>()).Returns(x => new Response(rawResponse, true, "text/plain", OK)); var credentials = Credentials.WithPassword("*****@*****.**".ToEmail(), "theirobotmoviesucked123"); var endpoint = Endpoint.Get(ApiUrls.ForEnvironment(ApiEnvironment.Staging), ""); var testApi = new TestApi(endpoint, apiClient, serializer, credentials); var observable = testApi.TestCreateObservable <string>(endpoint, Enumerable.Empty <HttpHeader>(), ""); Func <Task> theObservableReturnedWhenTheApiFails = async() => await observable; theObservableReturnedWhenTheApiFails .ShouldThrow <DeserializationException <string> >() .Which.Json.Should().Be(rawResponse); }
public void EmitsTheThrownExceptionIfTheValidatorThrows() { apiClient.Send(Arg.Any <Request>()).Returns(x => new Response("It lives", true, "text/plain", new List <KeyValuePair <string, IEnumerable <string> > >(), OK)); var credentials = Credentials.WithPassword( "*****@*****.**".ToEmail(), "theirobotmoviesucked123".ToPassword()); var endpoint = Endpoint.Get(BaseUrls.ForApi(ApiEnvironment.Staging), ""); var testApi = new TestApi(endpoint, apiClient, serializer, credentials, endpoint); var exampleExceptionMessage = "What is this."; var exception = new TestException(exampleExceptionMessage); var observable = testApi.TestCreateObservable <string>(endpoint, Enumerable.Empty <HttpHeader>(), "", (request, response, data) => throw exception); Func <Task> theObservableReturnedWhenTheValidatorThrows = async() => await observable; theObservableReturnedWhenTheValidatorThrows .Should().Throw <TestException>() .WithMessage(exampleExceptionMessage); }
public void EmitsAnOfflineExceptionIfTheApiClientThrowsAnHttpRequestException() { Exception caughtException = null; var httpRequestException = new HttpRequestException(); apiClient.Send(Arg.Any <Request>()).Returns <IResponse>(_ => throw httpRequestException); var credentials = Credentials.None; var endpoint = Endpoint.Get(BaseUrls.ForApi(ApiEnvironment.Staging), ""); var testApi = new TestApi(endpoint, apiClient, serializer, credentials, endpoint); try { testApi.TestCreateObservable <string>(endpoint, Enumerable.Empty <HttpHeader>(), "").Wait(); } catch (Exception e) { caughtException = e; } caughtException.Should().NotBeNull(); caughtException.Should().BeOfType <OfflineException>(); caughtException.InnerException.Should().Be(httpRequestException); }