public async Task TestGetWebAsync() { // Setup var logger = Log.Logger; var eventBridge = new EventBridge(); var factory = new HttpTunnelHandlerFactory(eventBridge, null, logger); var client = new HttpClientFactory(factory, logger).CreateClient("msft"); var adapter = new MethodHandlerAdapter(factory.YieldReturn()); var chunkServer = new TestChunkServer(100, (method, buffer, type) => { Assert.Equal(MethodNames.Response, method); return(adapter.InvokeAsync(method, buffer, type).Result); }); var server = new HttpTunnelServer(chunkServer.CreateClient(), new Http.Default.HttpClient(new HttpClientFactory(logger), logger), logger); eventBridge.Handler = server; // Act var result = await client.GetAsync("https://www.microsoft.com"); // Assert Assert.NotNull(result); Assert.True(result.IsSuccessStatusCode); Assert.NotNull(result.Content); var payload = await result.Content.ReadAsStringAsync(); Assert.NotNull(payload); Assert.NotNull(result.Headers); Assert.True(result.Headers.Any()); Assert.Contains("<!DOCTYPE html>", payload); }
public async Task TestPostAsync(int requestSize) { // Setup var logger = Log.Logger; var eventBridge = new EventBridge(); var factory = new HttpTunnelHandlerFactory(eventBridge, _serializer, null, logger); var client = new HttpClientFactory(factory, logger).CreateClient("msft"); var adapter = new MethodHandlerAdapter(factory.YieldReturn()); var chunkServer = new TestChunkServer(_serializer, 100000, (method, buffer, type) => { Assert.Equal(MethodNames.Response, method); return(adapter.InvokeAsync(method, buffer, type).Result); }); var uri = new Uri("https://test/test/test?test=test"); var rand = new Random(); var fix = new Fixture(); var requestBuffer = new byte[requestSize]; rand.NextBytes(requestBuffer); var responseBuffer = new byte[10000]; rand.NextBytes(responseBuffer); var request = Mock.Of <IHttpRequest>(r => r.Uri == uri); var response = Mock.Of <IHttpResponse>(r => r.Content == responseBuffer && r.StatusCode == System.Net.HttpStatusCode.OK); var httpclientMock = Mock.Of <IHttpClient>(); Mock.Get(httpclientMock) .Setup(m => m.PostAsync(It.IsAny <IHttpRequest>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(response)); Mock.Get(httpclientMock) .Setup(m => m.NewRequest(It.Is <Uri>(u => u == uri), It.IsAny <string>())) .Returns(request); var server = new HttpTunnelServer(httpclientMock, chunkServer.CreateClient(), _serializer, logger); eventBridge.Handler = server; // Act var result = await client.PostAsync(uri, new ByteArrayContent(requestBuffer)); // Assert Assert.NotNull(result); Assert.Equal(System.Net.HttpStatusCode.OK, result.StatusCode); Assert.NotNull(result.Content); var payload = await result.Content.ReadAsByteArrayAsync(); Assert.Equal(response.Content.Length, payload.Length); Assert.Equal(responseBuffer, payload); Assert.NotNull(result.Headers); Assert.Empty(result.Headers); }
public async Task TestDeleteAsync() { // Setup var logger = Log.Logger; var eventBridge = new EventBridge(); var factory = new HttpTunnelHandlerFactory(eventBridge, null, logger); var client = new HttpClientFactory(factory, logger).CreateClient("msft"); var adapter = new MethodHandlerAdapter(factory.YieldReturn()); var chunkServer = new TestChunkServer(128 * 1024, (method, buffer, type) => { Assert.Equal(MethodNames.Response, method); return(adapter.InvokeAsync(method, buffer, type).Result); }); var uri = new Uri("https://test/test/test?test=test"); var rand = new Random(); var fix = new Fixture(); var request = Mock.Of <IHttpRequest>(r => r.Uri == uri); var response = Mock.Of <IHttpResponse>(r => r.Content == null && r.StatusCode == System.Net.HttpStatusCode.OK); var httpclientMock = Mock.Of <IHttpClient>(); Mock.Get(httpclientMock) .Setup(m => m.DeleteAsync(It.IsAny <IHttpRequest>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(response)); Mock.Get(httpclientMock) .Setup(m => m.NewRequest(It.Is <Uri>(u => u == uri), It.IsAny <string>())) .Returns(request); var server = new HttpTunnelServer(chunkServer.CreateClient(), httpclientMock, logger); eventBridge.Handler = server; // Act var result = await client.DeleteAsync(uri); // Assert Assert.NotNull(result); Assert.Equal(System.Net.HttpStatusCode.OK, result.StatusCode); Assert.Null(result.Content); Assert.NotNull(result.Headers); Assert.Empty(result.Headers); }
/// <summary> /// Create handler /// </summary> /// <param name="outer"></param> public HttpTunnelClientHandler(HttpTunnelHandlerFactory outer) { // 256 KB - Max event message size - leave 4 kb for properties _maxSize = 252 * 1024; _outer = outer; }
/// <inheritdoc/> public HttpTunnelConfigurableFactory(IEventClient client, IWebProxy proxy, IJsonSerializer serializer, IEnumerable <IHttpHandler> handlers, ILogger logger) { _tunnel = new HttpTunnelHandlerFactory(client, serializer, handlers, logger); _fallback = new HttpHandlerFactory(handlers, proxy, logger); }
/// <summary> /// Create handler /// </summary> /// <param name="outer"></param> public HttpTunnelClientHandler(HttpTunnelHandlerFactory outer) { _maxSize = 230 * 1024; // Max event message size _outer = outer; }