public async Task Index()
            {
                using (var client = new HttpClient())
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, WebServerUrl);

                    using (var response = await client.SendAsync(request))
                    {
                        Assert.AreEqual(response.StatusCode, HttpStatusCode.OK, "Status Code OK");

                        var html = await response.Content.ReadAsStringAsync();

                        Assert.AreEqual(Resources.Index, html, "Same content index.html");

                        Assert.IsTrue(string.IsNullOrWhiteSpace(response.Headers.Pragma.ToString()), "Pragma empty");
                    }

                    WebServerInstance.Module <StaticFilesModule>().DefaultHeaders.Add(Headers.Pragma, HeaderPragmaValue);

                    request = new HttpRequestMessage(HttpMethod.Get, WebServerUrl);

                    using (var response = await client.SendAsync(request))
                    {
                        Assert.AreEqual(response.StatusCode, HttpStatusCode.OK, "Status Code OK");
                        Assert.AreEqual(HeaderPragmaValue, response.Headers.Pragma.ToString());
                    }
                }
            }
示例#2
0
            public async Task GetWebApiWithCustomHeader_ReturnsNameFromConstructor()
            {
                const string name = nameof(TestControllerWithConstructor);

                WebServerInstance.Module <WebApiModule>().RegisterController((ctx) => new TestControllerWithConstructor(ctx, name));
                using (var client = new HttpClient())
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, WebServerUrl + "name");

                    using (var response = await client.SendAsync(request))
                    {
                        Assert.AreEqual(name, response.Headers.FirstOrDefault(x => x.Key == TestControllerWithConstructor.CustomHeader).Value.FirstOrDefault());
                    }
                }
            }
示例#3
0
            public async Task GetWebApiWithCacheControlDefault_ReturnsValidResponse()
            {
                WebServerInstance.Module <WebApiModule>().RegisterController((ctx) => new TestControllerWithConstructor(ctx));
                using (var client = new HttpClient())
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, WebServerUrl + "name");

                    using (var response = await client.SendAsync(request))
                    {
                        Assert.IsFalse(response.Headers.CacheControl.Public, "Cache is not public");

                        Assert.IsTrue(response.Headers.CacheControl.NoStore);
                        Assert.IsTrue(response.Headers.CacheControl.NoCache);
                        Assert.IsTrue(response.Headers.CacheControl.MustRevalidate);
                    }
                }
            }
        protected async Task ConnectWebSocket()
        {
            var wsUrl = WebServerUrl.Replace("http", "ws") + _url;

            Assert.IsNotNull(WebServerInstance.Module <WebSocketsModule>(), "WebServer has WebSocketsModule");

            Assert.AreEqual(WebServerInstance.Module <WebSocketsModule>().Handlers.Count, 1, "WebSocketModule has one handler");

            if (IgnoreWebConnect)
            {
                Assert.Inconclusive("WebSocket Connect not available");
            }

            var ct = new CancellationTokenSource();

#if NET47
            var clientSocket = new ClientWebSocket();
            await clientSocket.ConnectAsync(new Uri(wsUrl), ct.Token);

            Assert.AreEqual(WebSocketState.Open, clientSocket.State, "Connection is open");

            var message = new ArraySegment <byte>(System.Text.Encoding.Default.GetBytes("HOLA"));
            var buffer  = new ArraySegment <byte>(new byte[5]);

            await clientSocket.SendAsync(message, WebSocketMessageType.Text, true, ct.Token);

            await clientSocket.ReceiveAsync(buffer, ct.Token);

            Assert.AreEqual("HELLO", System.Text.Encoding.UTF8.GetString(buffer.Array).Trim(), "Final message is HELLO");
#else
            var clientSocket = new WebSocket(wsUrl);
            await clientSocket.ConnectAsync(ct.Token);

            clientSocket.OnMessage += (s, e) => { Assert.AreEqual(e.Data, "HELLO"); };

            Assert.AreEqual(WebSocketState.Open, clientSocket.State, "Connection is open");

            var buffer = System.Text.Encoding.UTF8.GetBytes("HOLA");
            await clientSocket.SendAsync(buffer, Opcode.Text, ct.Token);

            await Task.Delay(500, ct.Token);
#endif
        }