public static Func<object[], TestHttpMessageHandler> RunRequest(this IRequestBuilder builder, string methodName, string returnContent = null) { var factory = builder.BuildRestResultFuncForMethod(methodName); var testHttpMessageHandler = new TestHttpMessageHandler(); if (returnContent != null) { testHttpMessageHandler.Content = new StringContent(returnContent); } return paramList => { var task = (Task)factory(new HttpClient(testHttpMessageHandler) { BaseAddress = new Uri("http://api/") }, paramList); task.Wait(); return testHttpMessageHandler; }; }
public async Task ValueTypesArentValidButTheyWorkAnyway() { var handler = new TestHttpMessageHandler("true"); var fixture = RestService.For <IBrokenWebApi>(new HttpClient(handler) { BaseAddress = new Uri("http://nowhere.com") }); var result = await fixture.PostAValue("Does this work?"); Assert.True(result); }
public void HttpClientShouldNotPrefixEmptyAbsolutePathToTheRequestUri() { var fixture = new RequestBuilderImplementation(typeof(IDummyHttpApi)); var factory = fixture.BuildRestResultFuncForMethod("FetchSomeStuff"); var testHttpMessageHandler = new TestHttpMessageHandler(); var task = (Task)factory(new HttpClient(testHttpMessageHandler) { BaseAddress = new Uri("http://api/") }, new object[] { 42 }); task.Wait(); Assert.Equal("http://api/foo/bar/42", testHttpMessageHandler.RequestMessage.RequestUri.ToString()); }
public static Func <object[], HttpRequestMessage> BuildRequestFactoryForMethod(this IRequestBuilder builder, string methodName) { var factory = builder.BuildRestResultFuncForMethod(methodName); var testHttpMessageHandler = new TestHttpMessageHandler(); return(paramList => { var task = (Task)factory(new HttpClient(testHttpMessageHandler) { BaseAddress = new Uri("http://api/") }, paramList); task.Wait(); return testHttpMessageHandler.RequestMessage; }); }
public static Func <object[], TestHttpMessageHandler> RunRequest(this IRequestBuilder builder, string methodName, string returnContent = null) { var factory = builder.BuildRestResultFuncForMethod(methodName); var testHttpMessageHandler = new TestHttpMessageHandler(); if (returnContent != null) { testHttpMessageHandler.Content = new StringContent(returnContent); } return(paramList => { var task = (Task)factory(new HttpClient(testHttpMessageHandler) { BaseAddress = new Uri("http://api/") }, paramList); task.Wait(); return testHttpMessageHandler; }); }
public void HttpContentTest() { var fixture = new RequestBuilderImplementation(typeof(IHttpContentApi)); var factory = fixture.BuildRestResultFuncForMethod("PostFileUpload"); var testHttpMessageHandler = new TestHttpMessageHandler(); var retContent = new StreamContent(new MemoryStream()); testHttpMessageHandler.Content = retContent; var mpc = new MultipartContent("foosubtype"); var task = (Task <HttpContent>)factory(new HttpClient(testHttpMessageHandler) { BaseAddress = new Uri("http://api/") }, new object[] { mpc }); task.Wait(); Assert.Equal(testHttpMessageHandler.RequestMessage.Content, mpc); Assert.Equal(retContent, task.Result); }
public void ReadStringContentWithMetadata() { var fixture = new RequestBuilderImplementation(typeof(IDummyHttpApi)); var factory = fixture.BuildRestResultFuncForMethod("FetchSomeStringWithMetadata"); var testHttpMessageHandler = new TestHttpMessageHandler(); var task = (Task <ApiResponse <string> >)factory(new HttpClient(testHttpMessageHandler) { BaseAddress = new Uri("http://api/") }, new object[] { 42 }); task.Wait(); Assert.NotNull(task.Result.Headers); Assert.True(task.Result.IsSuccessStatusCode); Assert.NotNull(task.Result.ReasonPhrase); Assert.False(task.Result.StatusCode == default(HttpStatusCode)); Assert.NotNull(task.Result.Version); Assert.Equal("test", task.Result.Content); }
public static Func<object[], HttpRequestMessage> BuildRequestFactoryForMethod(this IRequestBuilder builder, string methodName) { var factory = builder.BuildRestResultFuncForMethod(methodName); var testHttpMessageHandler = new TestHttpMessageHandler(); return paramList => { var task = (Task)factory(new HttpClient(testHttpMessageHandler) { BaseAddress = new Uri("http://api/")}, paramList); task.Wait(); return testHttpMessageHandler.RequestMessage; }; }
public void HttpClientShouldNotPrefixEmptyAbsolutePathToTheRequestUri() { var fixture = new RequestBuilderImplementation(typeof(IDummyHttpApi)); var factory = fixture.BuildRestResultFuncForMethod("FetchSomeStuff"); var testHttpMessageHandler = new TestHttpMessageHandler(); var task = (Task)factory(new HttpClient(testHttpMessageHandler) { BaseAddress = new Uri("http://api/") }, new object[] { 42 }); task.Wait(); Assert.AreEqual("http://api/foo/bar/42", testHttpMessageHandler.RequestMessage.RequestUri.ToString()); }
public void HttpContentTest() { var fixture = new RequestBuilderImplementation(typeof(IHttpContentApi)); var factory = fixture.BuildRestResultFuncForMethod("PostFileUpload"); var testHttpMessageHandler = new TestHttpMessageHandler(); var retContent = new StreamContent(new MemoryStream()); testHttpMessageHandler.Content = retContent; var mpc = new MultipartContent("foosubtype"); var task = (Task<HttpContent>)factory(new HttpClient(testHttpMessageHandler) { BaseAddress = new Uri("http://api/") }, new object[] { mpc }); task.Wait(); Assert.AreEqual(testHttpMessageHandler.RequestMessage.Content, mpc); Assert.AreEqual(retContent, task.Result); }