public async Task GetAsync_TestDefaultResponse() { var messageHandler = FakeResponseHandler.Create(HttpStatusCode.OK, endpoint, null); var client = new RestfulClient(endpoint, messageHandler); var testObject = await client.GetAsync <TestObject>(endpoint); Assert.AreEqual(default(TestObject), testObject); }
public async Task GetAsync_TestEmptyResponse() { var messageHandler = FakeResponseHandler.Create(HttpStatusCode.OK, endpoint); var client = new RestfulClient(endpoint, messageHandler); var testObject = await client.GetAsync <Empty>(endpoint); Assert.IsTrue(testObject.IsSuccessStatusCode); Assert.AreEqual("OK", testObject.ReasonPhrase); Assert.AreEqual(HttpStatusCode.OK, testObject.StatusCode); }
public async Task GetAsync_TestRequestAndResponse() { var messageHandler = FakeResponseHandler.Create(HttpStatusCode.OK, endpoint, payload); var client = new RestfulClient(endpoint, messageHandler); var testObject = await client.GetAsync <TestObject>(endpoint); Assert.IsNotNull(testObject); Assert.AreEqual(1, messageHandler.Requests.Count); var request = messageHandler.Requests.FirstOrDefault(); Assert.AreEqual(HttpMethod.Get, request.Method); Assert.AreEqual(new Uri(endpoint), request.RequestUri); Assert.IsNull(request.Content); }
public async Task GetAsync_TestRequestFailure() { var messageHandler = FakeResponseHandler.Create(HttpStatusCode.NotFound, endpoint); var client = new RestfulClient(endpoint, messageHandler); try { await client.GetAsync <TestObject>(endpoint); } catch (RestfulClientException ex) { Assert.AreEqual(HttpStatusCode.NotFound, ex.StatusCode); Assert.IsNull(ex.ResponseContent); Assert.IsTrue(ex.Message.Contains("RestfulClient received unsuccessful response")); } }
public async Task GetAsync() { //arrange var rest = Substitute.For<IRestClient>(); var client = new RestfulClient(rest); IEnumerable<Ad> ads = new List<Ad>(); rest.GetAsync(Arg.Any<HttpConfiguration>(), Arg.Any<Func<HttpResponseMessage, IEnumerable<Ad>>>()) .Returns(Task.FromResult(ads)); //act var ret = await client.GetAsync(new List<long> { 1 }); //assert rest.Received(1).GetAsync( Arg.Is<HttpConfiguration>(x => x.Route == "api/ad"), Arg.Any<Func<HttpResponseMessage, IEnumerable<Ad>>>()); }
public async Task GetAsync_TestAdditionalRequestHeaders() { var messageHandler = FakeResponseHandler.Create(HttpStatusCode.OK, endpoint, payload); var client = new RestfulClient(endpoint, messageHandler); var headers = new Dictionary <string, string>() { { testHeader.Key, testHeader.Value } }; var testObject = await client.GetAsync <TestObject>(endpoint, headers); var request = messageHandler.Requests.FirstOrDefault(); Assert.IsTrue(request.Headers.Contains(testHeader.Key)); var testHeaderValues = request.Headers.GetValues(testHeader.Key); Assert.AreEqual(1, testHeaderValues.Count()); Assert.AreEqual(testHeader.Value, testHeaderValues.FirstOrDefault()); }