public async Task GetAccessTokenAsync_ErrorResponseTest() { using var mockHandler = new HttpMessageHandlerMock(); using var http = new HttpClient(mockHandler); var bitly = new BitlyApi(http); mockHandler.Enqueue(x => { return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{\"status_code\": \"500\", \"status_txt\": \"MISSING_ARG_USERNAME\"}"), }); }); await Assert.ThrowsAsync <WebApiException>(() => bitly.GetAccessTokenAsync("hogehoge", "tetete")) .ConfigureAwait(false); Assert.Equal(0, mockHandler.QueueCount); }
public async Task GetAccessTokenAsync_Test() { using (var mockHandler = new HttpMessageHandlerMock()) using (var http = new HttpClient(mockHandler)) { var bitly = new BitlyApi(http); mockHandler.Enqueue(async x => { Assert.Equal(HttpMethod.Post, x.Method); Assert.Equal("https://api-ssl.bitly.com/oauth/access_token", x.RequestUri.GetLeftPart(UriPartial.Path)); Assert.Equal("Basic", x.Headers.Authorization.Scheme); Assert.Equal(ApplicationSettings.BitlyClientId + ":" + ApplicationSettings.BitlyClientSecret, Encoding.UTF8.GetString(Convert.FromBase64String(x.Headers.Authorization.Parameter))); var body = await x.Content.ReadAsStringAsync() .ConfigureAwait(false); var query = HttpUtility.ParseQueryString(body); Assert.Equal("password", query["grant_type"]); Assert.Equal("hogehoge", query["username"]); Assert.Equal("tetete", query["password"]); return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{\"access_token\": \"abcdefg\"}"), }); }); var result = await bitly.GetAccessTokenAsync("hogehoge", "tetete") .ConfigureAwait(false); Assert.Equal("abcdefg", result); Assert.Equal(0, mockHandler.QueueCount); } }