public async void FailureTest() { mock.Setup(c => c.PostAsync(It.IsAny <string>(), It.IsAny <IDictionary <string, string> >())) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(@" <rsp stat=""fail""> <err code=""112"" msg=""Method "rtm.test.ech" not found""/> </rsp> ") })); var httpClient = mock.Object; var rawClient = new MilkRawClient(context, signatureGenerator, httpClient); var param = new Dictionary <string, string>(); try { await rawClient.Invoke("rtm.test.echo", new Dictionary <string, string>()); throw new Exception("not failed."); } catch (MilkFailureException ex) { Assert.Equal("112", ex.Code); Assert.Equal("Method \"rtm.test.ech\" not found", ex.Msg); } }
public async Task SuccessTest() { mock.Setup(c => c.PostAsync(It.IsAny <string>(), It.IsAny <IDictionary <string, string> >())) .Callback <string, IDictionary <string, string> >((_, parameters) => { Assert.Equal("rtm.test.echo", parameters["method"]); Assert.Equal("bar", parameters["foo"]); Assert.Equal("api-key", parameters["api_key"]); Assert.Equal("test-token", parameters["auth_token"]); Assert.Equal("signature", parameters["api_sig"]); }) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(@" <rsp stat=""ok""> <method>rtm.test.echo</method> <foo>bar</foo> </rsp> ") })); var httpClient = mock.Object; context.AuthToken = new MilkAuthToken("test-token", MilkPerms.Delete); var rawClient = new MilkRawClient(context, signatureGenerator, httpClient); var rawXml = await rawClient.Invoke("rtm.test.echo", new Dictionary <string, string> { { "foo", "bar" } }); rawXml.Is(@" <rsp stat=""ok""> <method>rtm.test.echo</method> <foo>bar</foo> </rsp> "); mock.Verify(h => h.PostAsync("https://api.rememberthemilk.com/services/rest/", It.IsAny <IDictionary <string, string> >())); }
public async void HttpErrorOccurs() { mock.Setup(c => c.PostAsync(It.IsAny <string>(), It.IsAny <IDictionary <string, string> >())) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.ServiceUnavailable))); var httpClient = mock.Object; var rawClient = new MilkRawClient(context, signatureGenerator, httpClient); var param = new Dictionary <string, string>(); var occured = false; try { await rawClient.Invoke("rtm.test.echo", new Dictionary <string, string>()); } catch (MilkHttpException ex) { occured = true; ex.StatusCode.Is(HttpStatusCode.ServiceUnavailable); } Assert.True(occured); }