Exemple #1
0
        public async Task TestEmptyGetRequest()
        {
            using (var server = new HttpHost(TestPort))
            {
                const string path = "/test/path?query=value";

                await server.OpenAsync(request =>
                {
                    Assert.Equal(path, request.RequestUri.PathAndQuery);

                    Assert.Equal(HttpMethod.Get, request.Method);

                    return(Task.FromResult(new HttpResponseMessage {
                        StatusCode = HttpStatusCode.Found
                    }));
                });

                using (var client = new HttpClient())
                {
                    var request  = new HttpRequestMessage(HttpMethod.Get, LocalHost + path);
                    var response = await client.SendAsync(request);

                    Assert.Equal(HttpStatusCode.Found, response.StatusCode);
                }
            }
        }
Exemple #2
0
 static async Task Test()
 {
     await _server.OpenAsync(request =>
     {
         return(Task.FromResult(new HttpResponseMessage {
             StatusCode = HttpStatusCode.OK, Content = new StringContent(request.RequestUri.Query)
         }));
     });
 }
Exemple #3
0
        public async Task TestPostRequestWithCustomeHeadersAndBody()
        {
            using (var server = new HttpHost(TestPort))
            {
                const string path    = "/test/path?query=value";
                var          headers = new Dictionary <string, string>
                {
                    { "custom-header-1", "custom-value-1" }, { "content-custom", "content-value" }
                };
                const string contentType     = "suave/test";
                const string content         = "string content";
                var          responseContent = new byte[] { 1, 2, 3 };

                await server.OpenAsync(async request =>
                {
                    Assert.Equal(path, request.RequestUri.PathAndQuery);

                    Assert.Equal(HttpMethod.Post, request.Method);

                    Assert.True(headers.All(pair => request.Headers.First(s => s.Key == pair.Key).Value.First() == pair.Value));

                    Assert.Equal(contentType, request.Content.Headers.ContentType.ToString());

                    Assert.Equal(content, await request.Content.ReadAsStringAsync());

                    var response = new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.Accepted,
                        Content    = new ByteArrayContent(responseContent)
                    };
                    response.Headers.Add("server-custom", "server-value");
                    return(response);
                });

                using (var client = new HttpClient())
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, LocalHost + path);
                    headers.Aggregate(request.Headers, (a, b) =>
                    {
                        a.Add(b.Key, b.Value);
                        return(a);
                    });
                    request.Content = new StringContent(content);
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

                    var response = await client.SendAsync(request);

                    Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);

                    Assert.Equal(responseContent, await response.Content.ReadAsByteArrayAsync());

                    Assert.Equal("server-value", response.Headers.First(s => s.Key == "server-custom").Value.First());
                }
            }
        }
        private async Task EnsureServerOpen()
        {
            // On startup all WebHook listeners will call into this function.
            // We only need to create the server once - it is shared by all.
            if (_httpHost == null)
            {
                _httpHost = new HttpHost(_port);
                await _httpHost.OpenAsync(OnRequest);

                _trace.Verbose(string.Format("Opened HTTP server on port {0}", _port));
            }
        }
        public async Task TestPostRequestWithCustomeHeadersAndBody()
        {
            using (var server = new HttpHost(TestPort))
            {
                const string path = "/test/path?query=value";
                var headers = new Dictionary<string, string>
                    { { "custom-header-1", "custom-value-1" }, { "content-custom", "content-value" } };
                const string contentType = "suave/test";
                const string content = "string content";
                var responseContent = new byte[] { 1, 2, 3 };

                await server.OpenAsync(async request =>
                {
                    Assert.Equal(path, request.RequestUri.PathAndQuery);

                    Assert.Equal(HttpMethod.Post, request.Method);

                    Assert.True(headers.All(pair => request.Headers.First(s => s.Key == pair.Key).Value.First() == pair.Value));

                    Assert.Equal(contentType, request.Content.Headers.ContentType.ToString());

                    Assert.Equal(content, await request.Content.ReadAsStringAsync());

                    var response =  new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.Accepted,
                        Content = new ByteArrayContent(responseContent)
                    };
                    response.Headers.Add("server-custom", "server-value");
                    return response;
                });

                using (var client = new HttpClient())
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, LocalHost + path);
                    headers.Aggregate(request.Headers, (a, b) =>
                    {
                        a.Add(b.Key, b.Value);
                        return a;
                    });
                    request.Content = new StringContent(content);
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

                    var response = await client.SendAsync(request);
                    
                    Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);

                    Assert.Equal(responseContent, await response.Content.ReadAsByteArrayAsync());

                    Assert.Equal("server-value", response.Headers.First(s => s.Key == "server-custom").Value.First());
                }
            }
        }
        public async Task TestEmptyGetRequest()
        {
            using (var server = new HttpHost(TestPort))
            {
                const string path = "/test/path?query=value";

                await server.OpenAsync(request =>
                {
                    Assert.Equal(path, request.RequestUri.PathAndQuery);

                    Assert.Equal(HttpMethod.Get, request.Method);

                    return Task.FromResult(new HttpResponseMessage { StatusCode = HttpStatusCode.Found });
                });

                using (var client = new HttpClient())
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, LocalHost + path);
                    var response = await client.SendAsync(request);
                    Assert.Equal(HttpStatusCode.Found, response.StatusCode);
                }
            }
        }
Exemple #7
0
        public async Task IsServerRunningPositiveTest()
        {
            var port = GetAvailablePort();

            using (var httpHost = new HttpHost(port))
            {
                await httpHost.OpenAsync(r => {
                    var url = r.RequestUri;
                    HttpResponseMessage response;

                    if (url.AbsolutePath == "/")
                    {
                        response = new HttpResponseMessage(HttpStatusCode.OK);
                    }
                    else if (url.AbsolutePath.Equals("/admin/host/status", StringComparison.OrdinalIgnoreCase))
                    {
                        response = new HttpResponseMessage
                        {
                            Content = new StringContent(JsonConvert.SerializeObject(new HostStatus()), Encoding.UTF8, "application/json")
                        };
                    }
                    else
                    {
                        response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                    }

                    return(Task.FromResult(response));
                });

                var uri    = new Uri($"http://localhost:{port}");
                var result = await uri.IsServerRunningAsync();

                result.Should().BeTrue(because: "Server is running");

                httpHost.Close();
            }
        }
        private async Task EnsureServerOpen()
        {
            // On startup all WebHook listeners will call into this function.
            // We only need to create the server once - it is shared by all.
            if (_httpHost == null)
            {
                _httpHost = new HttpHost(_port);
                await _httpHost.OpenAsync(OnRequest);

                _trace.Verbose(string.Format("Opened HTTP server on port {0}", _port));
            }
        }