Ejemplo n.º 1
0
            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);
            }
Ejemplo n.º 2
0
            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);
            }
Ejemplo n.º 3
0
            private async Task deleteDefaultWorkspaceUsingV8Api(IUser user)
            {
                var credentials    = Credentials.WithApiToken(user.ApiToken);
                var httpClient     = new HttpClient();
                var urlPrefixForV8 = BaseUrls.ForApi(ApiEnvironment.Staging).AbsoluteUri.Replace("/v9/", "/v8/");
                var requestMessage = new HttpRequestMessage(HttpMethod.Delete, $"{urlPrefixForV8}/workspaces/{user.DefaultWorkspaceId}/leave");

                requestMessage.Headers.AddRange(new[] { credentials.Header });
                await httpClient.SendAsync(requestMessage).ConfigureAwait(false);
            }
Ejemplo n.º 4
0
            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();
            }
Ejemplo n.º 5
0
            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)));
            }
Ejemplo n.º 6
0
            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);
            }
Ejemplo n.º 7
0
            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);
            }
Ejemplo n.º 8
0
 public Endpoints(ApiEnvironment environment)
 {
     baseUrl          = BaseUrls.ForApi(environment);
     ReportsEndpoints = new ReportsEndpoints(environment);
 }