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>(), "") .ToObservable() .Wait(); } catch (Exception e) { caughtException = e; } caughtException.Should().NotBeNull(); caughtException.Should().BeOfType <OfflineException>(); caughtException.InnerException.Should().Be(httpRequestException); }
public async Task CreatesAnObservableThatReturnsASingleValue() { 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 observable = testApi.TestCreateObservable <string>(endpoint, Enumerable.Empty <HttpHeader>(), ""); await observable.SingleAsync(); }
public async Task CreatesRequestWithAppropriateHeaders() { var apiClient = Substitute.For <IApiClient>(); var serializer = Substitute.For <IJsonSerializer>(); var endpoint = Endpoint.Get(BaseUrls.ForApi(ApiEnvironment.Staging), ""); 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()); const string expectedHeader = "c3VzYW5jYWx2aW5AcHN5Y2hvaGlzdG9yaWFuLm11c2V1bTp0aGVpcm9ib3Rtb3ZpZXN1Y2tlZDEyMw=="; var testApi = new TestApi(endpoint, apiClient, serializer, credentials, endpoint); await testApi.Get(); await apiClient.Received().Send(Arg.Is <Request>(request => verifyAuthHeader(request, expectedHeader))); }
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", 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 observable = testApi.TestCreateObservable <string>(endpoint, Enumerable.Empty <HttpHeader>(), ""); Func <Task> theObservableReturnedWhenTheApiFails = async() => await observable; theObservableReturnedWhenTheApiFails .Should().Throw <DeserializationException <string> >() .Which.Json.Should().Be(rawResponse); }