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