public async Task CanMakeMutipleRequestsWithSameConnection() { var httpClient = Substitute.For<IHttpClient>(); IResponse<string> response = new ApiResponse<string>(); httpClient.Send<string>(Args.Request).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), ExampleUri, Substitute.For<ICredentialStore>(), httpClient, Substitute.For<IJsonSerializer>()); await connection.GetAsync<string>(new Uri("endpoint", UriKind.Relative)); await connection.GetAsync<string>(new Uri("endpoint", UriKind.Relative)); await connection.GetAsync<string>(new Uri("endpoint", UriKind.Relative)); httpClient.Received(3).Send<string>(Arg.Is<IRequest>(req => req.BaseAddress == ExampleUri && req.Method == HttpMethod.Get && req.Endpoint == new Uri("endpoint", UriKind.Relative))); }
public async Task SendsProperlyFormattedRequest() { var httpClient = Substitute.For<IHttpClient>(); IResponse<string> response = new ApiResponse<string>(); httpClient.Send<string>(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), ExampleUri, Substitute.For<ICredentialStore>(), httpClient, Substitute.For<IJsonSerializer>()); await connection.GetAsync<string>(new Uri("endpoint", UriKind.Relative)); httpClient.Received(1).Send<string>(Arg.Is<IRequest>(req => req.BaseAddress == ExampleUri && req.ContentType == null && req.Body == null && req.Method == HttpMethod.Get && req.Endpoint == new Uri("endpoint", UriKind.Relative)), Args.CancellationToken); }
public async Task ParsesApiInfoOnResponse() { var httpClient = Substitute.For<IHttpClient>(); IResponse<string> response = new ApiResponse<string> { Headers = { { "X-Accepted-OAuth-Scopes", "user" }, } }; httpClient.Send<string>(Args.Request).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), ExampleUri, Substitute.For<ICredentialStore>(), httpClient, Substitute.For<IJsonSerializer>()); var resp = await connection.GetAsync<string>(new Uri("endpoint", UriKind.Relative)); Assert.NotNull(resp.ApiInfo); Assert.Equal("user", resp.ApiInfo.AcceptedOauthScopes.First()); }
public async Task ThrowsAuthorizationExceptionExceptionForUnauthorizedResponse() { var httpClient = Substitute.For<IHttpClient>(); IResponse<string> response = new ApiResponse<string> { StatusCode = HttpStatusCode.Unauthorized}; httpClient.Send<string>(Args.Request).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), ExampleUri, Substitute.For<ICredentialStore>(), httpClient, Substitute.For<IJsonSerializer>()); var exception = await AssertEx.Throws<AuthorizationException>( async () => await connection.GetAsync<string>(new Uri("endpoint", UriKind.Relative))); Assert.NotNull(exception); }
public async Task ThrowsForbiddenExceptionForUnknownForbiddenResponse() { var httpClient = Substitute.For<IHttpClient>(); IResponse<string> response = new ApiResponse<string> { StatusCode = HttpStatusCode.Forbidden, Body = "YOU SHALL NOT PASS!" }; httpClient.Send<string>(Args.Request).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), ExampleUri, Substitute.For<ICredentialStore>(), httpClient, Substitute.For<IJsonSerializer>()); var exception = await AssertEx.Throws<ForbiddenException>( async () => await connection.GetAsync<string>(new Uri("endpoint", UriKind.Relative))); Assert.Equal("YOU SHALL NOT PASS!", exception.Message); }
public async Task ThrowsLoginAttemptsExceededExceptionForForbiddenResponse() { var httpClient = Substitute.For<IHttpClient>(); IResponse<string> response = new ApiResponse<string> { StatusCode = HttpStatusCode.Forbidden, Body = "{\"message\":\"Maximum number of login attempts exceeded\"," + "\"documentation_url\":\"http://developer.github.com/v3\"}" }; httpClient.Send<string>(Args.Request).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), ExampleUri, Substitute.For<ICredentialStore>(), httpClient, Substitute.For<IJsonSerializer>()); var exception = await AssertEx.Throws<LoginAttemptsExceededException>( async () => await connection.GetAsync<string>(new Uri("endpoint", UriKind.Relative))); Assert.Equal("Maximum number of login attempts exceeded", exception.Message); Assert.Equal("http://developer.github.com/v3", exception.ApiError.DocumentationUrl); }
public async Task ThrowsRateLimitExceededExceptionForForbidderResponse() { var httpClient = Substitute.For<IHttpClient>(); IResponse<string> response = new ApiResponse<string> { StatusCode = HttpStatusCode.Forbidden, Body = "{\"message\":\"API rate limit exceeded. " + "See http://developer.github.com/v3/#rate-limiting for details.\"}" }; httpClient.Send<string>(Args.Request).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), ExampleUri, Substitute.For<ICredentialStore>(), httpClient, Substitute.For<IJsonSerializer>()); var exception = await AssertEx.Throws<RateLimitExceededException>( async () => await connection.GetAsync<string>(new Uri("endpoint", UriKind.Relative))); Assert.Equal("API rate limit exceeded. See http://developer.github.com/v3/#rate-limiting for details.", exception.Message); }
public async Task ThrowsApiValidationExceptionFor422Response() { var httpClient = Substitute.For<IHttpClient>(); IResponse<string> response = new ApiResponse<string> { StatusCode = (HttpStatusCode)422, Body = @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " + @"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}" }; httpClient.Send<string>(Args.Request).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), ExampleUri, Substitute.For<ICredentialStore>(), httpClient, Substitute.For<IJsonSerializer>()); var exception = await AssertEx.Throws<ApiValidationException>( async () => await connection.GetAsync<string>(new Uri("endpoint", UriKind.Relative))); Assert.Equal("Validation Failed", exception.Message); Assert.Equal("key is already in use", exception.ApiError.Errors[0].Message); }
public async Task ThrowsTwoFactorExceptionExceptionWhenChallenged( string headerKey, string otpHeaderValue, TwoFactorType expectedFactorType) { var httpClient = Substitute.For<IHttpClient>(); IResponse<string> response = new ApiResponse<string> { StatusCode = HttpStatusCode.Unauthorized, }; response.Headers[headerKey] = otpHeaderValue; httpClient.Send<string>(Args.Request).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), ExampleUri, Substitute.For<ICredentialStore>(), httpClient, Substitute.For<IJsonSerializer>()); var exception = await AssertEx.Throws<TwoFactorRequiredException>( async () => await connection.GetAsync<string>(new Uri("endpoint", UriKind.Relative))); Assert.Equal(expectedFactorType, exception.TwoFactorType); }
public async Task ThrowsNotFoundExceptionForFileNotFoundResponse() { var httpClient = Substitute.For<IHttpClient>(); IResponse<string> response = new ApiResponse<string> { StatusCode = HttpStatusCode.NotFound, Body = "GONE BYE BYE!" }; httpClient.Send<string>(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); var connection = new Connection(new ProductHeaderValue("OctokitTests"), ExampleUri, Substitute.For<ICredentialStore>(), httpClient, Substitute.For<IJsonSerializer>()); var exception = await AssertEx.Throws<NotFoundException>( async () => await connection.GetAsync<string>(new Uri("endpoint", UriKind.Relative))); Assert.Equal("GONE BYE BYE!", exception.Message); }