本文整理汇总了C#中HttpMessageHandlerMock.Enqueue方法的典型用法代码示例。如果您正苦于以下问题:C# HttpMessageHandlerMock.Enqueue方法的具体用法?C# HttpMessageHandlerMock.Enqueue怎么用?C# HttpMessageHandlerMock.Enqueue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpMessageHandlerMock
的用法示例。
在下文中一共展示了HttpMessageHandlerMock.Enqueue方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetThumbnailInfoAsync_RequestTest
public async Task GetThumbnailInfoAsync_RequestTest()
{
var handler = new HttpMessageHandlerMock();
handler.Enqueue(x =>
{
Assert.Equal(HttpMethod.Get, x.Method);
Assert.Equal("https://api.tumblr.com/v2/blog/hoge.tumblr.com/posts",
x.RequestUri.GetLeftPart(UriPartial.Path));
var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
Assert.Equal(ApplicationSettings.TumblrConsumerKey, query["api_key"]);
Assert.Equal("1234567", query["id"]);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(@"{
""meta"": { ""status"": 200, ""msg"": ""OK"" },
""response"": { ""blog"": { }, ""posts"": { } }
}"),
};
});
using (var http = new HttpClient(handler))
{
var service = new Tumblr(http);
var url = "http://hoge.tumblr.com/post/1234567/tetetete";
var thumb = await service.GetThumbnailInfoAsync(url, null, CancellationToken.None)
.ConfigureAwait(false);
}
Assert.Equal(0, handler.QueueCount);
}
示例2: GetAsync_Test
public async Task GetAsync_Test()
{
using (var mockHandler = new HttpMessageHandlerMock())
using (var http = new HttpClient(mockHandler))
using (var apiConnection = new TwitterApiConnection("", ""))
{
apiConnection.http = http;
mockHandler.Enqueue(x =>
{
Assert.Equal(HttpMethod.Get, x.Method);
Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json",
x.RequestUri.GetLeftPart(UriPartial.Path));
var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
Assert.Equal("1111", query["aaaa"]);
Assert.Equal("2222", query["bbbb"]);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("\"hogehoge\""),
};
});
var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
var param = new Dictionary<string, string>
{
["aaaa"] = "1111",
["bbbb"] = "2222",
};
var result = await apiConnection.GetAsync<string>(endpoint, param, endpointName: "/hoge/tetete")
.ConfigureAwait(false);
Assert.Equal("hogehoge", result);
Assert.Equal(0, mockHandler.QueueCount);
}
}
示例3: TranslateAsync_Test
public async Task TranslateAsync_Test()
{
using (var mockHandler = new HttpMessageHandlerMock())
using (var http = new HttpClient(mockHandler))
{
var mock = new Mock<MicrosoftTranslatorApi>(http);
mock.Setup(x => x.GetAccessTokenAsync())
.ReturnsAsync(Tuple.Create("1234abcd", TimeSpan.FromSeconds(1000)));
var translateApi = mock.Object;
mockHandler.Enqueue(x =>
{
Assert.Equal(HttpMethod.Get, x.Method);
Assert.Equal(MicrosoftTranslatorApi.TranslateEndpoint.AbsoluteUri,
x.RequestUri.GetLeftPart(UriPartial.Path));
var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
Assert.Equal("hogehoge", query["text"]);
Assert.Equal("ja", query["to"]);
Assert.Equal("en", query["from"]);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("ほげほげ"),
};
});
var result = await translateApi.TranslateAsync("hogehoge", langTo: "ja", langFrom: "en")
.ConfigureAwait(false);
Assert.Equal("ほげほげ", result);
mock.Verify(x => x.GetAccessTokenAsync(), Times.Once());
Assert.Equal(0, mockHandler.QueueCount);
}
}
示例4: GetThumbnailInfoAsync_RequestTest
public async Task GetThumbnailInfoAsync_RequestTest()
{
var handler = new HttpMessageHandlerMock();
using (var http = new HttpClient(handler))
{
var service = new FoursquareCheckin(http);
handler.Enqueue(x =>
{
Assert.Equal(HttpMethod.Get, x.Method);
Assert.Equal("https://api.foursquare.com/v2/checkins/xxxxxxxx",
x.RequestUri.GetLeftPart(UriPartial.Path));
var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
Assert.Equal(ApplicationSettings.FoursquareClientId, query["client_id"]);
Assert.Equal(ApplicationSettings.FoursquareClientSecret, query["client_secret"]);
Assert.NotNull(query["v"]);
Assert.Null(query["signature"]);
// リクエストに対するテストなのでレスポンスは適当に返す
return new HttpResponseMessage(HttpStatusCode.NotFound);
});
var post = new PostClass
{
PostGeo = new PostClass.StatusGeo { },
};
var thumb = await service.GetThumbnailInfoAsync(
"https://foursquare.com/hogehoge/checkin/xxxxxxxx",
post, CancellationToken.None);
Assert.Equal(0, handler.QueueCount);
}
}
示例5: GetThumbnailInfoAsync_GeoLocatedTweetTest
public async Task GetThumbnailInfoAsync_GeoLocatedTweetTest()
{
var handler = new HttpMessageHandlerMock();
using (var http = new HttpClient(handler))
{
var service = new FoursquareCheckin(http);
handler.Enqueue(x =>
{
// このリクエストは実行されないはず
Assert.True(false);
return new HttpResponseMessage(HttpStatusCode.NotFound);
});
// 既にジオタグが付いているツイートに対しては何もしない
var post = new PostClass
{
PostGeo = new PostClass.StatusGeo
{
Lat = 34.35067978344854,
Lng = 134.04693603515625,
},
};
var thumb = await service.GetThumbnailInfoAsync(
"https://foursquare.com/hogehoge/checkin/xxxxxxxx?s=aaaaaaa",
post, CancellationToken.None);
Assert.Equal(1, handler.QueueCount);
}
}
示例6: GetAccessTokenAsync_Test
public async Task GetAccessTokenAsync_Test()
{
using (var mockHandler = new HttpMessageHandlerMock())
using (var http = new HttpClient(mockHandler))
{
var translateApi = new MicrosoftTranslatorApi(http);
mockHandler.Enqueue(async x =>
{
Assert.Equal(HttpMethod.Post, x.Method);
Assert.Equal(MicrosoftTranslatorApi.OAuthEndpoint, x.RequestUri);
var body = await x.Content.ReadAsStringAsync()
.ConfigureAwait(false);
var query = HttpUtility.ParseQueryString(body);
Assert.Equal("client_credentials", query["grant_type"]);
Assert.Equal(ApplicationSettings.AzureClientId, query["client_id"]);
Assert.Equal(ApplicationSettings.AzureClientSecret, query["client_secret"]);
Assert.Equal("http://api.microsofttranslator.com", query["scope"]);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(@"
{
""access_token"": ""12345acbde"",
""token_type"": ""bearer"",
""expires_in"": 3600
}"),
};
});
var result = await translateApi.GetAccessTokenAsync()
.ConfigureAwait(false);
var expectedToken = Tuple.Create(@"12345acbde", TimeSpan.FromSeconds(3600));
Assert.Equal(expectedToken, result);
Assert.Equal(0, mockHandler.QueueCount);
}
}
示例7: PostJsonAsync_Test
public async Task PostJsonAsync_Test()
{
using (var mockHandler = new HttpMessageHandlerMock())
using (var http = new HttpClient(mockHandler))
using (var apiConnection = new TwitterApiConnection("", ""))
{
apiConnection.http = http;
mockHandler.Enqueue(async x =>
{
Assert.Equal(HttpMethod.Post, x.Method);
Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json",
x.RequestUri.AbsoluteUri);
Assert.Equal("application/json; charset=utf-8", x.Content.Headers.ContentType.ToString());
var body = await x.Content.ReadAsStringAsync()
.ConfigureAwait(false);
Assert.Equal("{\"aaaa\": 1111}", body);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("\"hogehoge\""),
};
});
var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
await apiConnection.PostJsonAsync(endpoint, "{\"aaaa\": 1111}")
.ConfigureAwait(false);
Assert.Equal(0, mockHandler.QueueCount);
}
}
示例8: PostLazyAsync_Multipart_NullTest
public async Task PostLazyAsync_Multipart_NullTest()
{
using (var mockHandler = new HttpMessageHandlerMock())
using (var http = new HttpClient(mockHandler))
using (var apiConnection = new TwitterApiConnection("", ""))
{
apiConnection.httpUpload = http;
mockHandler.Enqueue(async x =>
{
Assert.Equal(HttpMethod.Post, x.Method);
Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json",
x.RequestUri.AbsoluteUri);
Assert.IsType<MultipartFormDataContent>(x.Content);
var boundary = x.Content.Headers.ContentType.Parameters.Cast<NameValueHeaderValue>()
.First(y => y.Name == "boundary").Value;
// 前後のダブルクオーテーションを除去
boundary = boundary.Substring(1, boundary.Length - 2);
var expectedText =
$"--{boundary}\r\n" +
$"\r\n--{boundary}--\r\n";
var expected = Encoding.UTF8.GetBytes(expectedText);
Assert.Equal(expected, await x.Content.ReadAsByteArrayAsync().ConfigureAwait(false));
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("\"hogehoge\""),
};
});
var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
var result = await apiConnection.PostLazyAsync<string>(endpoint, param: null, media: null)
.ConfigureAwait(false);
Assert.Equal("hogehoge", await result.LoadJsonAsync()
.ConfigureAwait(false));
Assert.Equal(0, mockHandler.QueueCount);
}
}
示例9: PostLazyAsync_MultipartTest
public async Task PostLazyAsync_MultipartTest()
{
using (var mockHandler = new HttpMessageHandlerMock())
using (var http = new HttpClient(mockHandler))
using (var apiConnection = new TwitterApiConnection("", ""))
{
apiConnection.httpUpload = http;
using (var image = TestUtils.CreateDummyImage())
using (var media = new MemoryImageMediaItem(image))
{
mockHandler.Enqueue(async x =>
{
Assert.Equal(HttpMethod.Post, x.Method);
Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json",
x.RequestUri.AbsoluteUri);
Assert.IsType<MultipartFormDataContent>(x.Content);
var boundary = x.Content.Headers.ContentType.Parameters.Cast<NameValueHeaderValue>()
.First(y => y.Name == "boundary").Value;
// 前後のダブルクオーテーションを除去
boundary = boundary.Substring(1, boundary.Length - 2);
var expectedText =
$"--{boundary}\r\n" +
"Content-Type: text/plain; charset=utf-8\r\n" +
"Content-Disposition: form-data; name=aaaa\r\n" +
"\r\n" +
"1111\r\n"+
$"--{boundary}\r\n" +
"Content-Type: text/plain; charset=utf-8\r\n" +
"Content-Disposition: form-data; name=bbbb\r\n" +
"\r\n" +
"2222\r\n" +
$"--{boundary}\r\n" +
$"Content-Disposition: form-data; name=media1; filename={media.Name}; filename*=utf-8''{media.Name}\r\n" +
"\r\n";
var expected = Encoding.UTF8.GetBytes(expectedText)
.Concat(image.Stream.ToArray())
.Concat(Encoding.UTF8.GetBytes($"\r\n--{boundary}--\r\n"));
Assert.Equal(expected, await x.Content.ReadAsByteArrayAsync().ConfigureAwait(false));
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("\"hogehoge\""),
};
});
var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
var param = new Dictionary<string, string>
{
["aaaa"] = "1111",
["bbbb"] = "2222",
};
var mediaParam = new Dictionary<string, IMediaItem>
{
["media1"] = media,
};
var result = await apiConnection.PostLazyAsync<string>(endpoint, param, mediaParam)
.ConfigureAwait(false);
Assert.Equal("hogehoge", await result.LoadJsonAsync()
.ConfigureAwait(false));
Assert.Equal(0, mockHandler.QueueCount);
}
}
}
示例10: PostLazyAsync_Test
public async Task PostLazyAsync_Test()
{
using (var mockHandler = new HttpMessageHandlerMock())
using (var http = new HttpClient(mockHandler))
using (var apiConnection = new TwitterApiConnection("", ""))
{
apiConnection.http = http;
mockHandler.Enqueue(async x =>
{
Assert.Equal(HttpMethod.Post, x.Method);
Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json",
x.RequestUri.AbsoluteUri);
var body = await x.Content.ReadAsStringAsync()
.ConfigureAwait(false);
var query = HttpUtility.ParseQueryString(body);
Assert.Equal("1111", query["aaaa"]);
Assert.Equal("2222", query["bbbb"]);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("\"hogehoge\""),
};
});
var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
var param = new Dictionary<string, string>
{
["aaaa"] = "1111",
["bbbb"] = "2222",
};
var result = await apiConnection.PostLazyAsync<string>(endpoint, param)
.ConfigureAwait(false);
Assert.Equal("hogehoge", await result.LoadJsonAsync()
.ConfigureAwait(false));
Assert.Equal(0, mockHandler.QueueCount);
}
}
示例11: GetStreamAsync_Test
public async Task GetStreamAsync_Test()
{
using (var mockHandler = new HttpMessageHandlerMock())
using (var http = new HttpClient(mockHandler))
using (var apiConnection = new TwitterApiConnection("", ""))
using (var image = TestUtils.CreateDummyImage())
{
apiConnection.http = http;
mockHandler.Enqueue(x =>
{
Assert.Equal(HttpMethod.Get, x.Method);
Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json",
x.RequestUri.GetLeftPart(UriPartial.Path));
var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
Assert.Equal("1111", query["aaaa"]);
Assert.Equal("2222", query["bbbb"]);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(image.Stream.ToArray()),
};
});
var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
var param = new Dictionary<string, string>
{
["aaaa"] = "1111",
["bbbb"] = "2222",
};
var stream = await apiConnection.GetStreamAsync(endpoint, param)
.ConfigureAwait(false);
using (var memoryStream = new MemoryStream())
{
// 内容の比較のために MemoryStream にコピー
await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
Assert.Equal(image.Stream.ToArray(), memoryStream.ToArray());
}
Assert.Equal(0, mockHandler.QueueCount);
}
}
示例12: GetAsync_ErrorJsonTest
public async Task GetAsync_ErrorJsonTest()
{
using (var mockHandler = new HttpMessageHandlerMock())
using (var http = new HttpClient(mockHandler))
using (var apiConnection = new TwitterApiConnection("", ""))
{
apiConnection.http = http;
mockHandler.Enqueue(x =>
{
return new HttpResponseMessage(HttpStatusCode.Forbidden)
{
Content = new StringContent("{\"errors\":[{\"code\":187,\"message\":\"Status is a duplicate.\"}]}"),
};
});
var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
var exception = await Assert.ThrowsAsync<TwitterApiException>(() => apiConnection.GetAsync<string>(endpoint, null, endpointName: "/hoge/tetete"))
.ConfigureAwait(false);
// エラーレスポンスの JSON に含まれるエラーコードに基づいてメッセージを出力する
Assert.Equal("DuplicateStatus", exception.Message);
Assert.Equal(TwitterErrorCode.DuplicateStatus, exception.ErrorResponse.Errors[0].Code);
Assert.Equal("Status is a duplicate.", exception.ErrorResponse.Errors[0].Message);
Assert.Equal(0, mockHandler.QueueCount);
}
}
示例13: GetAsync_ErrorStatusTest
public async Task GetAsync_ErrorStatusTest()
{
using (var mockHandler = new HttpMessageHandlerMock())
using (var http = new HttpClient(mockHandler))
using (var apiConnection = new TwitterApiConnection("", ""))
{
apiConnection.http = http;
mockHandler.Enqueue(x =>
{
return new HttpResponseMessage(HttpStatusCode.BadGateway)
{
Content = new StringContent("### Invalid JSON Response ###"),
};
});
var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
var exception = await Assert.ThrowsAsync<TwitterApiException>(() => apiConnection.GetAsync<string>(endpoint, null, endpointName: "/hoge/tetete"))
.ConfigureAwait(false);
// エラーレスポンスの読み込みに失敗した場合はステータスコードをそのままメッセージに使用する
Assert.Equal("BadGateway", exception.Message);
Assert.Null(exception.ErrorResponse);
Assert.Equal(0, mockHandler.QueueCount);
}
}
示例14: GetAsync_UpdateRateLimitTest
public async Task GetAsync_UpdateRateLimitTest()
{
using (var mockHandler = new HttpMessageHandlerMock())
using (var http = new HttpClient(mockHandler))
using (var apiConnection = new TwitterApiConnection("", ""))
{
apiConnection.http = http;
mockHandler.Enqueue(x =>
{
Assert.Equal(HttpMethod.Get, x.Method);
Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json",
x.RequestUri.GetLeftPart(UriPartial.Path));
return new HttpResponseMessage(HttpStatusCode.OK)
{
Headers =
{
{ "X-Rate-Limit-Limit", "150" },
{ "X-Rate-Limit-Remaining", "100" },
{ "X-Rate-Limit-Reset", "1356998400" },
{ "X-Access-Level", "read-write-directmessages" },
},
Content = new StringContent("\"hogehoge\""),
};
});
var apiStatus = new TwitterApiStatus();
MyCommon.TwitterApiInfo = apiStatus;
var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
await apiConnection.GetAsync<string>(endpoint, null, endpointName: "/hoge/tetete")
.ConfigureAwait(false);
Assert.Equal(apiStatus.AccessLevel, TwitterApiAccessLevel.ReadWriteAndDirectMessage);
Assert.Equal(apiStatus.AccessLimit["/hoge/tetete"], new ApiLimit(150, 100, new DateTime(2013, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()));
Assert.Equal(0, mockHandler.QueueCount);
}
}