コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }