本文整理汇总了C#中System.Net.Connection.GetResponse方法的典型用法代码示例。如果您正苦于以下问题:C# Connection.GetResponse方法的具体用法?C# Connection.GetResponse怎么用?C# Connection.GetResponse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Connection
的用法示例。
在下文中一共展示了Connection.GetResponse方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CanMakeMutipleRequestsWithSameConnection
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);
}
示例2: ParsesApiInfoOnResponse
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());
}
示例3: SendsProperlyFormattedRequest
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);
}
示例4: ThrowsAuthorizationExceptionExceptionForUnauthorizedResponse
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);
}
示例5: ThrowsForbiddenExceptionForUnknownForbiddenResponse
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);
}
示例6: ThrowsLoginAttemptsExceededExceptionForForbiddenResponse
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);
}
示例7: ThrowsRateLimitExceededExceptionForForbidderResponse
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);
}
示例8: ThrowsApiValidationExceptionFor422Response
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);
}
示例9: ThrowsTwoFactorExceptionExceptionWhenChallenged
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);
}
示例10: ThrowsNotFoundExceptionForFileNotFoundResponse
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);
}
示例11: ThrowsAuthorizationExceptionExceptionForUnauthorizedResponse
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);
}
示例12: ThrowsForbiddenExceptionForUnknownForbiddenResponse
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);
}
示例13: ThrowsTwoFactorExceptionExceptionWhenChallenged
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);
}