public async void GetString() { var httpHandler = new MockHttpHandler(x => new HttpApiResponse(body: x.Body)); var client = HttpApiClient<IApi>.Create("http://localhost", httpHandler: httpHandler); var result = await client.GetString("foo"); Assert.AreEqual("foo", result); }
public async void PathForApiType() { var api = new HttpApi<IPathForApi>(); var endpoint = api.Endpoints.Single().Value; var httpHandler = new MockHttpHandler(); await endpoint.Call(httpHandler, "http://localhost", new Dictionary<string, object> { ["key"] = "to" }); Assert.AreEqual("http://localhost/path/to/api", httpHandler.Request.Url.ToString()); }
public async void Method() { var httpHandler = new MockHttpHandler(); var responseHandler = new MockResponseHandler<object>(); var endpoint = new HttpApiEndpoint("path/to/api", HttpMethod.Get, new Dictionary<string, IHttpArgumentHandler>(), responseHandler, Enumerable.Empty<HttpHeader>()); await endpoint.Call(httpHandler, "http://localhost", new Dictionary<string, object>()); Assert.AreEqual(HttpMethod.Get, httpHandler.Request.Method); }
public async void HeadersProvider() { var httpHandler = new MockHttpHandler(); var responseHandler = new MockResponseHandler<object>(); var headersProvider = new MockApiInstrumenter(); headersProvider.Headers.Add(new HttpHeader("key", "value")); var endpoint = new HttpApiEndpoint("path/to/api", HttpMethod.Get, new Dictionary<string, IHttpArgumentHandler>(), responseHandler, Enumerable.Empty<HttpHeader>()); await endpoint.Call(httpHandler, "http://localhost", new Dictionary<string, object>(), headersProvider.InstrumentCall); var header = httpHandler.Request.Headers.Single(); Assert.AreEqual("key", header.Name); Assert.AreEqual("value", header.Values.Single()); }
public async void ArgumentProvider() { var httpHandler = new MockHttpHandler(); var responseHandler = new MockResponseHandler<string>(x => x.Headers.Single(y => y.Name == "name").Values.Single()); var endpoint = new HttpApiEndpoint( "path/to/api", HttpMethod.Get, new Dictionary<string, IHttpArgumentHandler> { { "name", new HttpHeaderArgumentHandler(DefaultTypeConverter.Create()) } }, responseHandler, Enumerable.Empty<HttpHeader>()); var response = await endpoint.Call(httpHandler, "http://localhost", new Dictionary<string, object> { ["name"] = "value" }); Assert.AreEqual("value", response); }
public async Task InterfaceHandlerForPropertyGet() { var httpHandler = new MockHttpHandler(x => new HttpApiResponse(body: x.Body)); var accessToken = "foo"; var client = HttpApiClient<IInterfaceHandlerApi>.Create("http://localhost", httpHandler: httpHandler, interfaceHandler: invocation => { switch (invocation.Method.Name) { case "get_AccessToken": return Task.FromResult((object)accessToken); } return invocation.Proceed(); }); Assert.AreEqual(accessToken, client.AccessToken); var result = await client.GetString("bar"); Assert.AreEqual("bar", result); }
public async void QueryWithSubstitution() { var api = new HttpApi<IQueryWithSubstitution>(); var endpoint = api.Endpoints.Single().Value; var httpHandler = new MockHttpHandler(); await endpoint.Call(httpHandler, "http://localhost", new Dictionary<string, object> { ["key"] = "bar" }); Assert.AreEqual("http://localhost/path?foo=bar", httpHandler.Request.Url.ToString()); }
public async void Header() { var api = new HttpApi<IHeaderApi>(); var endpoint = api.Endpoints.Single().Value; var httpHandler = new MockHttpHandler(); await endpoint.Call(httpHandler, "http://localhost", new Dictionary<string, object>()); var header = httpHandler.Request.Headers.Single(x => x.Name == "User-Agent"); Assert.AreEqual("TestUserAgent", header.Values.Single()); }
public async void JsonResponse() { var api = new HttpApi<IJsonResponse>(); var endpoint = api.Endpoints.Single().Value; var httpHandler = new MockHttpHandler(new HttpApiResponse(body: new JsonHttpBody("foo"))); var result = (string)await endpoint.Call(httpHandler, "http://localhost", new Dictionary<string, object>()); Assert.AreEqual("foo", result); }
public async Task ComposedJsonBody() { var api = new HttpApi<IComposedJsonBody>(); var endpoint = api.Endpoints.Single().Value; var httpHandler = new MockHttpHandler(); await endpoint.Call(httpHandler, "http://localhost", new Dictionary<string, object> { ["value1"] = 5, ["value2"] = "foo" }); var body = (JsonHttpBody)httpHandler.Request.Body; var jsonObject = (JObject)body.Json; Assert.AreEqual(5, (int)jsonObject["value1"]); Assert.AreEqual("foo", (string)jsonObject["value2"]); }
public async Task DirectJsonBody() { var api = new HttpApi<IDirectJsonBody>(); var endpoint = api.Endpoints.Single().Value; var httpHandler = new MockHttpHandler(); await endpoint.Call(httpHandler, "http://localhost", new Dictionary<string, object> { ["number"] = 5 }); var body = (JsonHttpBody)httpHandler.Request.Body; var value = (JValue)body.Json; Assert.AreEqual(5, (int)value); }
public async void ReturnTypeConverter() { var api = new HttpApi<IReturnTypeConverter>(); var endpoint = api.Endpoints.Single().Value; var httpHandler = new MockHttpHandler(); var result = await endpoint.Call(httpHandler, "http://localhost", new Dictionary<string, object> { ["number"] = 5 }); Assert.AreEqual("foo", result); }
public async void ParameterLevelTypeConverter() { var api = new HttpApi<IParameterLevelTypeConverter>(); var endpoint = api.Endpoints.Single().Value; var httpHandler = new MockHttpHandler(); await endpoint.Call(httpHandler, "http://localhost", new Dictionary<string, object> { ["number"] = 5 }); Assert.AreEqual("http://localhost/foo", httpHandler.Request.Url.ToString()); }
public async void MultipelIdsCommaSeparated() { var api = new HttpApi<IMultipleIdsCommaSeparated>(); var endpoint = api.Endpoints.Single().Value; var httpHandler = new MockHttpHandler(); await endpoint.Call(httpHandler, "http://localhost", new Dictionary<string, object> { ["ids"] = new[] { 1, 3 } }); Assert.AreEqual("http://localhost?ids=1,3", httpHandler.Request.Url.ToString()); }
public async void MultipleQueryArguments() { var api = new HttpApi<IMultipeQueryArguments>(); var endpoint = api.Endpoints.Single().Value; var httpHandler = new MockHttpHandler(); await endpoint.Call(httpHandler, "http://localhost", new Dictionary<string, object> { ["ids"] = new[] { 1, 3 } }); Assert.AreEqual("http://localhost?ids=1&ids=3", httpHandler.Request.Url.ToString()); await endpoint.Call(httpHandler, "http://localhost", new Dictionary<string, object> { ["firstName"] = "John" }); Assert.AreEqual("http://localhost?firstName=John", httpHandler.Request.Url.ToString()); }