public async Task Body_PostContentLengthX_StreamWithXBytes()
        {
            OwinHttpListener listener = CreateServer(
                env =>
            {
                string[] values;
                var requestHeaders = env.Get <IDictionary <string, string[]> >("owin.RequestHeaders");

                Assert.True(requestHeaders.TryGetValue("Content-length", out values));
                Assert.Single(values);
                Assert.Equal("11", values[0]);

                var requestBody = env.Get <Stream>("owin.RequestBody");
                Assert.NotNull(requestBody);

                var buffer = new MemoryStream();
                requestBody.CopyTo(buffer);
                Assert.Equal(11, buffer.Length);

                return(Task.FromResult(0));
            },
                HttpServerAddress);

            var request = new HttpRequestMessage(HttpMethod.Post, HttpClientAddress);

            request.Content = new StringContent("Hello World");
            await SendRequest(listener, request);
        }
Ejemplo n.º 2
0
        public async Task OwinHttpListenerResponse_101ResponseWithBody_BodyIgnoredByClient()
        {
            OwinHttpListener listener = CreateServer(
                env =>
            {
                env["owin.ResponseStatusCode"] = 101;
                var responseStream             = env.Get <Stream>("owin.ResponseBody");

                var responseHeaders = env.Get <IDictionary <string, string[]> >("owin.ResponseHeaders");
                responseHeaders["content-length"] = new string[] { "10" };

                responseStream.Write(new byte[10], 0, 10);
                return(TaskHelpers.Completed());
            },
                HttpServerAddress);

            using (listener)
            {
                var client = new HttpClient();
                HttpResponseMessage response = await client.GetAsync(HttpClientAddress);

                Assert.Equal(HttpStatusCode.SwitchingProtocols, response.StatusCode);
                Assert.Equal("Switching Protocols", response.ReasonPhrase);
                Assert.Equal(2, response.Headers.Count()); // Date, Server
                Assert.True(response.Headers.Date.HasValue);
                Assert.Equal(1, response.Headers.Server.Count);
                Assert.Equal(0, (await response.Content.ReadAsByteArrayAsync()).Length);
            }
        }
Ejemplo n.º 3
0
        public void Disconnect_ClientDisconnects_EventFires()
        {
            var requestReceived = new ManualResetEvent(false);
            var requestCanceled = new ManualResetEvent(false);

            OwinHttpListener listener = CreateServer(
                env =>
            {
                GetCallCancelled(env).Register(() => requestCanceled.Set());
                requestReceived.Set();
                Assert.True(requestCanceled.WaitOne(1000));
                return(Task.FromResult(0));
            },
                HttpServerAddress);

            using (listener)
            {
                var client      = new HttpClient();
                var requestTask = client.GetAsync(HttpClientAddress);
                Assert.True(requestReceived.WaitOne(1000));
                client.CancelPendingRequests();
                Assert.True(requestCanceled.WaitOne(1000));
                Assert.Throws <AggregateException>(() => requestTask.Result);
            }
        }
Ejemplo n.º 4
0
        private OwinHttpListener CreateServer(AppFunc app, string[] addressParts)
        {
            var wrapper = new OwinHttpListener();

            wrapper.Start(wrapper.Listener, app, CreateAddress(addressParts), null, null);
            return(wrapper);
        }
Ejemplo n.º 5
0
        public async Task EndToEnd_HttpsGetRequest_Success()
        {
            OwinHttpListener listener = CreateServer(
                async env =>
            {
                object obj;
                Assert.True(env.TryGetValue("ssl.LoadClientCertAsync", out obj));
                Assert.NotNull(obj);
                Assert.IsType(typeof(Func <Task>), obj);
                var loadCert = (Func <Task>)obj;
                await loadCert();
                Assert.True(env.TryGetValue("ssl.ClientCertificate", out obj));
                Assert.NotNull(obj);
                Assert.IsType <X509Certificate2>(obj);
            },
                HttpsServerAddress);

            X509Certificate2 clientCert = FindClientCert();

            Assert.NotNull(clientCert);
            HttpResponseMessage response = await SendGetRequest(listener, HttpsClientAddress, clientCert);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(0, response.Content.Headers.ContentLength.Value);
        }
Ejemplo n.º 6
0
        public async Task PathAndPathBase_CorrectlySeperated(string clientString, string serverBasePath,
                                                             string expectedBasePath, string expectedPath, string expectedQuery)
        {
            var fallbackAddress = new string[4];

            HttpServerAddress.CopyTo(fallbackAddress, 0);
            fallbackAddress[3] = "/";
            var serverAddress = new string[4];

            HttpServerAddress.CopyTo(serverAddress, 0);
            serverAddress[3] = serverBasePath;
            clientString     = "http://localhost:8080" + clientString;

            using (var wrapper = new OwinHttpListener())
            {
                wrapper.Start(wrapper.Listener, env =>
                {
                    Assert.Equal(expectedBasePath, (string)env["owin.RequestPathBase"]);
                    Assert.Equal(expectedPath, (string)env["owin.RequestPath"]);
                    Assert.Equal(expectedQuery, (string)env["owin.RequestQueryString"]);
                    return(TaskHelpers.Completed());
                }, CreateAddresses(fallbackAddress, serverAddress), null, null);

                using (var client = new HttpClient())
                {
                    HttpResponseMessage result = await client.GetAsync(clientString);

                    Assert.Equal(HttpStatusCode.OK, result.StatusCode);
                }
            }
        }
Ejemplo n.º 7
0
        public async Task OwinHttpListenerResponse_OnFirstWrite_OnSendingHeaders()
        {
            OwinHttpListener listener = CreateServer(
                env =>
            {
                env["owin.ResponseStatusCode"]   = 200;
                env["owin.ResponseReasonPhrase"] = "Custom";
                var responseStream = env.Get <Stream>("owin.ResponseBody");

                var responseHeaders = env.Get <IDictionary <string, string[]> >("owin.ResponseHeaders");

                env.Get <Action <Action <object>, object> >("server.OnSendingHeaders")(state => responseHeaders["custom-header"] = new string[] { "customvalue" }, null);

                responseHeaders["content-length"] = new string[] { "10" };

                responseStream.Write(new byte[10], 0, 10);

                return(TaskHelpers.Completed());
            },
                HttpServerAddress);

            using (listener)
            {
                var client = new HttpClient();
                HttpResponseMessage response = await client.GetAsync(HttpClientAddress);

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal("Custom", response.ReasonPhrase);
                Assert.Equal(3, response.Headers.Count()); // Date, Server
                Assert.True(response.Headers.Date.HasValue);
                Assert.Equal(1, response.Headers.Server.Count);
                Assert.Equal("customvalue", response.Headers.GetValues("custom-header").First());
                Assert.Equal(10, (await response.Content.ReadAsByteArrayAsync()).Length);
            }
        }
Ejemplo n.º 8
0
        public async Task ErrorInWebSocket_Disconnected()
        {
            ManualResetEvent sync     = new ManualResetEvent(false);
            OwinHttpListener listener = CreateServer(env =>
            {
                var accept = (WebSocketAccept)env["websocket.Accept"];
                Assert.NotNull(accept);

                accept(
                    null,
                    wsEnv =>
                {
                    sync.Set();
                    throw new Exception("Application WebSocket error.");
                });

                return(Task.FromResult(0));
            },
                                                     HttpServerAddress);

            using (listener)
            {
                using (var client = new ClientWebSocket())
                {
                    await client.ConnectAsync(new Uri(WsClientAddress), CancellationToken.None);

                    Assert.True(sync.WaitOne(500));
                    await Assert.ThrowsAsync <WebSocketException>(() => client.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None));
                }
            }
        }
Ejemplo n.º 9
0
        public async Task PathAndQueryParsing_CorrectlySeperated(string clientString, string serverBasePath,
                                                                 string expectedBasePath, string expectedPath, string expectedQuery)
        {
            var serverAddress = new string[4];

            HttpServerAddress.CopyTo(serverAddress, 0);
            serverAddress[3] = serverBasePath;
            clientString     = "http://localhost:8080" + clientString;

            OwinHttpListener listener = CreateServer(env =>
            {
                Assert.Equal(expectedBasePath, (string)env["owin.RequestPathBase"]);
                Assert.Equal(expectedPath, (string)env["owin.RequestPath"]);
                Assert.Equal(expectedQuery, (string)env["owin.RequestQueryString"]);
                return(TaskHelpers.Completed());
            }, serverAddress);

            using (listener)
            {
                var client = new HttpClient();
                HttpResponseMessage result = await client.GetAsync(clientString);

                Assert.Equal(HttpStatusCode.OK, result.StatusCode);
            }
        }
Ejemplo n.º 10
0
        public async Task Headers_ReservedHeaders_PassedThrough()
        {
            OwinHttpListener listener = CreateServer(
                env =>
            {
                var responseHeaders = env.Get <IDictionary <string, string[]> >("owin.ResponseHeaders");
                env.Add("owin.ResponseProtocol", "HTTP/1.0");
                responseHeaders.Add("KEEP-alive", new string[] { "TRUE" });
                responseHeaders.Add("content-length", new string[] { "0" });
                responseHeaders.Add("www-Authenticate", new string[] { "Basic", "NTLM" });
                return(TaskHelpers.Completed());
            },
                HttpServerAddress);

            using (listener)
            {
                var client = new HttpClient();
                HttpResponseMessage response = await client.GetAsync(HttpClientAddress);

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal(3, response.Headers.Count()); // Date, Server
                Assert.Equal(0, response.Content.Headers.ContentLength);
                Assert.Equal(2, response.Headers.WwwAuthenticate.Count());

                // The client does not expose KeepAlive
            }
        }
Ejemplo n.º 11
0
        public async Task Headers_OtherReservedHeaders_PassedThrough()
        {
            OwinHttpListener listener = CreateServer(
                env =>
            {
                var responseHeaders = env.Get <IDictionary <string, string[]> >("owin.ResponseHeaders");
                responseHeaders.Add("Transfer-Encoding", new string[] { "ChUnKed" });
                responseHeaders.Add("CONNECTION", new string[] { "ClOsE" });
                return(TaskHelpers.Completed());
            },
                HttpServerAddress);

            using (listener)
            {
                var client = new HttpClient();
                HttpResponseMessage response = await client.GetAsync(HttpClientAddress);

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal(4, response.Headers.Count());                             // Date, Server
                Assert.Equal("chunked", response.Headers.TransferEncoding.ToString()); // Normalized by server
                Assert.True(response.Headers.TransferEncodingChunked.Value);
                Assert.Equal("close", response.Headers.Connection.First());            // Normalized by server
                Assert.True(response.Headers.ConnectionClose.Value);
            }
        }
Ejemplo n.º 12
0
        public void BodyDelegate_ThrowsAsync_ConnectionClosed()
        {
            bool callCancelled = false;

            OwinHttpListener listener = CreateServer(
                env =>
            {
                GetCallCancelled(env).Register(() => callCancelled = true);
                var responseHeaders = env.Get <IDictionary <string, string[]> >("owin.ResponseHeaders");
                responseHeaders.Add("Content-Length", new string[] { "10" });

                var responseStream = env.Get <Stream>("owin.ResponseBody");
                responseStream.WriteByte(0xFF);

                return(TaskHelpers.FromError(new NotImplementedException()));
            },
                HttpServerAddress);

            try
            {
                Assert.Throws <AggregateException>(() => SendGetRequest(listener, HttpClientAddress).Result);
            }
            finally
            {
                Assert.True(callCancelled);
            }
        }
Ejemplo n.º 13
0
        public async Task Environment_EmptyGetRequest_RequiredKeysPresentAndCorrect()
        {
            OwinHttpListener listener = CreateServer(
                env =>
            {
                object ignored;
                Assert.True(env.TryGetValue("owin.RequestMethod", out ignored));
                Assert.Equal("GET", env["owin.RequestMethod"]);

                Assert.True(env.TryGetValue("owin.RequestPath", out ignored));
                Assert.Equal("/SubPath", env["owin.RequestPath"]);

                Assert.True(env.TryGetValue("owin.RequestPathBase", out ignored));
                Assert.Equal("/BaseAddress", env["owin.RequestPathBase"]);

                Assert.True(env.TryGetValue("owin.RequestProtocol", out ignored));
                Assert.Equal("HTTP/1.1", env["owin.RequestProtocol"]);

                Assert.True(env.TryGetValue("owin.RequestQueryString", out ignored));
                Assert.Equal("QueryString", env["owin.RequestQueryString"]);

                Assert.True(env.TryGetValue("owin.RequestScheme", out ignored));
                Assert.Equal("http", env["owin.RequestScheme"]);

                Assert.True(env.TryGetValue("owin.Version", out ignored));
                Assert.Equal("1.0", env["owin.Version"]);

                return(TaskHelpers.Completed());
            },
                HttpServerAddress);

            await SendGetRequest(listener, HttpClientAddress + "SubPath?QueryString");
        }
Ejemplo n.º 14
0
        public async Task Body_PostChunkedX_StreamWithXBytes()
        {
            OwinHttpListener listener = CreateServer(
                env =>
            {
                string[] values;
                var requestHeaders = env.Get <IDictionary <string, string[]> >("owin.RequestHeaders");

                Assert.True(requestHeaders.TryGetValue("Transfer-Encoding", out values));
                Assert.Equal(1, values.Length);
                Assert.Equal("chunked", values[0]);

                var requestBody = env.Get <Stream>("owin.RequestBody");
                Assert.NotNull(requestBody);

                var buffer = new MemoryStream();
                requestBody.CopyTo(buffer);
                Assert.Equal(11, buffer.Length);

                return(TaskHelpers.Completed());
            },
                HttpServerAddress);

            var request = new HttpRequestMessage(HttpMethod.Post, HttpClientAddress);

            request.Headers.TransferEncodingChunked = true;
            request.Content = new StringContent("Hello World");
            await SendRequest(listener, request);
        }
Ejemplo n.º 15
0
        public void BodyDelegate_ThrowsSync_ConnectionClosed()
        {
            bool             callCancelled = false;
            OwinHttpListener listener      = CreateServer(
                env =>
            {
                GetCallCancelled(env).Register(() => callCancelled = true);
                var responseHeaders = env.Get <IDictionary <string, string[]> >("owin.ResponseHeaders");
                responseHeaders.Add("Content-Length", new string[] { "10" });

                var responseStream = env.Get <Stream>("owin.ResponseBody");
                responseStream.WriteByte(0xFF);

                throw new NotImplementedException();
            },
                HttpServerAddress);

            try
            {
                // TODO: XUnit 2.0 adds support for Assert.Throws<...>(async () => await myTask);
                // that way we can specify the correct exception type.
                Assert.Throws <AggregateException>(() => SendGetRequest(listener, HttpClientAddress).Result);
            }
            finally
            {
                Assert.True(callCancelled);
            }
        }
Ejemplo n.º 16
0
        public async Task Body_PostEchoRequest_Success()
        {
            bool callCancelled = false;

            OwinHttpListener listener = CreateServer(
                async env =>
            {
                GetCallCancelled(env).Register(() => callCancelled = true);
                var requestHeaders  = env.Get <IDictionary <string, string[]> >("owin.RequestHeaders");
                var responseHeaders = env.Get <IDictionary <string, string[]> >("owin.ResponseHeaders");
                responseHeaders.Add("Content-Length", requestHeaders["Content-Length"]);

                var requestStream  = env.Get <Stream>("owin.RequestBody");
                var responseStream = env.Get <Stream>("owin.ResponseBody");

                var buffer = new MemoryStream();
                await requestStream.CopyToAsync(buffer, 1024);
                buffer.Seek(0, SeekOrigin.Begin);
                await buffer.CopyToAsync(responseStream, 1024);
            },
                HttpServerAddress);

            using (listener)
            {
                var    client              = new HttpClient();
                string dataString          = "Hello World";
                HttpResponseMessage result = await client.PostAsync(HttpClientAddress, new StringContent(dataString));

                result.EnsureSuccessStatusCode();
                Assert.Equal(dataString.Length, result.Content.Headers.ContentLength.Value);
                Assert.Equal(dataString, await result.Content.ReadAsStringAsync());
                Assert.False(callCancelled);
            }
        }
Ejemplo n.º 17
0
        public async Task Headers_CustomHeaders_PassedThrough()
        {
            OwinHttpListener listener = CreateServer(
                env =>
            {
                var responseHeaders = env.Get <IDictionary <string, string[]> >("owin.ResponseHeaders");
                responseHeaders.Add("Custom1", new string[] { "value1a", "value1b" });
                responseHeaders.Add("Custom2", new string[] { "value2a, value2b" });
                responseHeaders.Add("Custom3", new string[] { "value3a, value3b", "value3c" });
                return(TaskHelpers.Completed());
            },
                HttpServerAddress);

            using (listener)
            {
                var client = new HttpClient();
                HttpResponseMessage response = await client.GetAsync(HttpClientAddress);

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal(6, response.Headers.Count()); // Date, Chunked, Server

                Assert.Equal(2, response.Headers.GetValues("Custom1").Count());
                Assert.Equal("value1a", response.Headers.GetValues("Custom1").First());
                Assert.Equal("value1b", response.Headers.GetValues("Custom1").Skip(1).First());
                Assert.Equal(1, response.Headers.GetValues("Custom2").Count());
                Assert.Equal("value2a, value2b", response.Headers.GetValues("Custom2").First());
                Assert.Equal(2, response.Headers.GetValues("Custom3").Count());
                Assert.Equal("value3a, value3b", response.Headers.GetValues("Custom3").First());
                Assert.Equal("value3c", response.Headers.GetValues("Custom3").Skip(1).First());
            }
        }
Ejemplo n.º 18
0
        public async Task OwinHttpListenerResponse_HeadRequestWithContentLength_Success()
        {
            OwinHttpListener listener = CreateServer(env =>
            {
                var responseHeaders = env.Get <IDictionary <string, string[]> >("owin.ResponseHeaders");
                responseHeaders["Content-Length"] = new string[] { "10" };
                return(TaskHelpers.Completed());
            }, HttpServerAddress);

            using (listener)
            {
                var client = new HttpClient();
                HttpRequestMessage  request  = new HttpRequestMessage(HttpMethod.Head, HttpClientAddress);
                HttpResponseMessage response = await client.SendAsync(request);

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal("OK", response.ReasonPhrase);
                Assert.Equal(2, response.Headers.Count());
                Assert.False(response.Headers.TransferEncodingChunked.HasValue);
                Assert.True(response.Headers.Date.HasValue);
                Assert.Equal(1, response.Headers.Server.Count);
                Assert.Equal(1, response.Content.Headers.Count()); // Content-Length
                Assert.Equal(10, response.Content.Headers.ContentLength);
                Assert.Equal(string.Empty, await response.Content.ReadAsStringAsync());
            }
        }
Ejemplo n.º 19
0
        private static void FixCrashBug(OwinHttpListener owinHttpListener)
        {
            var        httpListener = owinHttpListener.Listener;
            var        currentOutstandingAccepts  = 0;
            var        currentOutstandingRequests = 0;
            var        owinHttpListenerType       = owinHttpListener.GetType();
            var        owinHttpListenerOffloadStartNextRequestMethodInfo = owinHttpListenerType.GetMethod("OffloadStartNextRequest", DefaultBindingFlags);
            var        owinHttpListenerOffloadStartNextRequest           = (Action)Delegate.CreateDelegate(typeof(Action), owinHttpListener, owinHttpListenerOffloadStartNextRequestMethodInfo);
            var        owinHttpListenerCanAcceptMoreRequestsPropertyInfo = owinHttpListener.GetType().GetProperty("CanAcceptMoreRequests", BindingFlags.Instance | BindingFlags.NonPublic) ?? throw new MissingMemberException("CanAcceptMoreRequests");
            var        owinHttpListenerProcessRequestAsyncMethodInfo     = owinHttpListener.GetType().GetMethod("ProcessRequestAsync", BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(HttpListenerContext) }, null);
            var        owinHttpListenerProcessRequestAsync = (Func <HttpListenerContext, Task>)Delegate.CreateDelegate(typeof(Func <HttpListenerContext, Task>), owinHttpListener, owinHttpListenerProcessRequestAsyncMethodInfo ?? throw new MissingMethodException("ProcessRequestAsync"));
            var        logHelperLogExceptionMethodInfo     = LogHelperType.GetMethod("LogException", BindingFlags.NonPublic | BindingFlags.Static);
            var        logHelperLogException  = (Action <LoggerType, string, Exception>)Delegate.CreateDelegate(typeof(Action <LoggerType, string, Exception>), null, logHelperLogExceptionMethodInfo ?? throw new MissingMethodException("LogException"));
            LoggerType owinHttpListenerLogger = null;

            var newStartNextRequestAsync = new Action(async() =>
            {
                if (owinHttpListenerLogger == null)
                {
                    lock (LockObj)
                    {
                        owinHttpListenerLogger = (LoggerType)owinHttpListenerType
                                                 .GetField("_logger", DefaultBindingFlags)
                                                 ?.GetValue(owinHttpListener);
                    }
                }

                while (httpListener.IsListening && (bool)owinHttpListenerCanAcceptMoreRequestsPropertyInfo.GetValue(owinHttpListener))
                {
                    Interlocked.Increment(ref currentOutstandingAccepts);
                    HttpListenerContext context;
                    try
                    {
                        context = await httpListener.GetContextAsync();
                        if (IsEmptyPayloadAndContentLength(context))
                        {
                            Interlocked.Decrement(ref currentOutstandingAccepts);
                            owinHttpListenerOffloadStartNextRequest();
                            continue;
                        }
                    }
                    catch (Exception ex)
                    {
                        Interlocked.Decrement(ref currentOutstandingAccepts);
                        logHelperLogException(owinHttpListenerLogger, "Accept", ex);
                        continue;
                    }

                    Interlocked.Decrement(ref currentOutstandingAccepts);
                    Interlocked.Increment(ref currentOutstandingRequests);
                    owinHttpListenerOffloadStartNextRequest();
                    await owinHttpListenerProcessRequestAsync(context);
                }
            });

            owinHttpListenerType
            .GetField("_startNextRequestAsync", DefaultBindingFlags)
            ?.SetValue(owinHttpListener, newStartNextRequestAsync);
        }
Ejemplo n.º 20
0
        public async Task AppDelegate_ThrowsSync_500Error()
        {
            OwinHttpListener    listener = CreateServer(_notImplemented, HttpServerAddress);
            HttpResponseMessage response = await SendGetRequest(listener, HttpClientAddress);

            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            Assert.Equal(0, response.Content.Headers.ContentLength.Value);
        }
Ejemplo n.º 21
0
 private async Task SendGetRequest(OwinHttpListener listener, string address)
 {
     using (listener)
     {
         var    client = new HttpClient();
         string result = await client.GetStringAsync(address);
     }
 }
Ejemplo n.º 22
0
        public AutoTuneMiddleware(AppFunc next, OwinHttpListener server)
        {
            _next   = next;
            _server = server;
            _server.SetRequestProcessingLimits((int)_currentMaxAccepts, _currentMaxRequests);

            _timer = new Timer(TimerFired, null, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(0.1));
        }
Ejemplo n.º 23
0
        public async Task EndToEnd_GetRequest_Success()
        {
            OwinHttpListener    listener = CreateServer(env => TaskHelpers.Completed(), HttpServerAddress);
            HttpResponseMessage response = await SendGetRequest(listener, HttpClientAddress);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(0, response.Content.Headers.ContentLength.Value);
        }
Ejemplo n.º 24
0
        public void OwinHttpListener_HttpsCreatedStartedStoppedDisposed_Success()
        {
            OwinHttpListener listener = CreateServer(_notImplemented, HttpsServerAddress);

            using (listener)
            {
                listener.Stop();
            }
        }
Ejemplo n.º 25
0
        private async Task SendRequest(OwinHttpListener listener, HttpRequestMessage request)
        {
            using (listener)
            {
                var client = new HttpClient();
                HttpResponseMessage result = await client.SendAsync(request);

                result.EnsureSuccessStatusCode();
            }
        }
Ejemplo n.º 26
0
        public async Task SubProtocol_SelectLastSubProtocol_Success()
        {
            OwinHttpListener listener = CreateServer(env =>
            {
                var accept = (WebSocketAccept)env["websocket.Accept"];
                Assert.NotNull(accept);

                var requestHeaders  = env.Get <IDictionary <string, string[]> >("owin.RequestHeaders");
                var responseHeaders = env.Get <IDictionary <string, string[]> >("owin.ResponseHeaders");

                // Select the last sub-protocol from the client.
                string subProtocol = requestHeaders["Sec-WebSocket-Protocol"].Last().Split(',').Last().Trim();

                responseHeaders["Sec-WebSocket-Protocol"] = new string[] { subProtocol + "A" };

                accept(
                    new Dictionary <string, object>()
                {
                    { "websocket.SubProtocol", subProtocol }
                },
                    async wsEnv =>
                {
                    var sendAsync    = wsEnv.Get <WebSocketSendAsync>("websocket.SendAsync");
                    var receiveAsync = wsEnv.Get <WebSocketReceiveAsync>("websocket.ReceiveAsync");
                    var closeAsync   = wsEnv.Get <WebSocketCloseAsync>("websocket.CloseAsync");

                    var buffer = new ArraySegment <byte>(new byte[100]);
                    Tuple <int, bool, int> serverReceive = await receiveAsync(buffer, CancellationToken.None);
                    // Assume close received
                    await closeAsync((int)WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None);
                });

                return(TaskHelpers.Completed());
            },
                                                     HttpServerAddress);

            using (listener)
            {
                using (var client = new ClientWebSocket())
                {
                    client.Options.AddSubProtocol("protocol1");
                    client.Options.AddSubProtocol("protocol2");

                    await client.ConnectAsync(new Uri(WsClientAddress), CancellationToken.None);

                    await client.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None);

                    var receiveBody = new byte[100];
                    WebSocketReceiveResult readResult = await client.ReceiveAsync(new ArraySegment <byte>(receiveBody), CancellationToken.None);

                    Assert.Equal(WebSocketMessageType.Close, readResult.MessageType);
                    Assert.Equal("protocol2", client.SubProtocol);
                }
            }
        }
Ejemplo n.º 27
0
        public void Ctor_PathMissingEndSlash_Added()
        {
            OwinHttpListener listener = CreateServer(_notImplemented, new string[]
            {
                "http", "localhost", "8080", "/BadPathDoesntEndInSlash"
            });

            using (listener)
            {
                listener.Stop();
            }
        }
Ejemplo n.º 28
0
        public async Task EndToEnd_EchoData_Success()
        {
            ManualResetEvent echoComplete = new ManualResetEvent(false);
            OwinHttpListener listener     = CreateServer(env =>
            {
                var accept = (WebSocketAccept)env["websocket.Accept"];
                Assert.NotNull(accept);

                accept(
                    null,
                    async wsEnv =>
                {
                    var sendAsync    = wsEnv.Get <WebSocketSendAsync>("websocket.SendAsync");
                    var receiveAsync = wsEnv.Get <WebSocketReceiveAsync>("websocket.ReceiveAsync");
                    var closeAsync   = wsEnv.Get <WebSocketCloseAsync>("websocket.CloseAsync");

                    var buffer = new ArraySegment <byte>(new byte[100]);
                    Tuple <int, bool, int> serverReceive = await receiveAsync(buffer, CancellationToken.None);
                    await sendAsync(new ArraySegment <byte>(buffer.Array, 0, serverReceive.Item3),
                                    serverReceive.Item1, serverReceive.Item2, CancellationToken.None);
                    Assert.True(echoComplete.WaitOne(100), "Echo incomplete");
                    await closeAsync((int)WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None);
                });

                return(Task.FromResult(0));
            },
                                                         HttpServerAddress);

            using (listener)
            {
                using (var client = new ClientWebSocket())
                {
                    await client.ConnectAsync(new Uri(WsClientAddress), CancellationToken.None);

                    byte[] sendBody = Encoding.UTF8.GetBytes("Hello World");
                    await client.SendAsync(new ArraySegment <byte>(sendBody), WebSocketMessageType.Text, true, CancellationToken.None);

                    var receiveBody = new byte[100];
                    WebSocketReceiveResult readResult = await client.ReceiveAsync(new ArraySegment <byte>(receiveBody), CancellationToken.None);

                    echoComplete.Set();

                    Assert.Equal(WebSocketMessageType.Text, readResult.MessageType);
                    Assert.True(readResult.EndOfMessage);
                    Assert.Equal(sendBody.Length, readResult.Count);
                    Assert.Equal("Hello World", Encoding.UTF8.GetString(receiveBody, 0, readResult.Count));
                }
            }
        }
Ejemplo n.º 29
0
        public async Task EndToEnd_SingleThreadedTwoGetRequests_Success()
        {
            OwinHttpListener listener = CreateServer(env => TaskHelpers.Completed(), HttpServerAddress);

            using (listener)
            {
                var    client = new HttpClient();
                string result = await client.GetStringAsync(HttpClientAddress);

                Assert.Equal(string.Empty, result);
                result = await client.GetStringAsync(HttpClientAddress);

                Assert.Equal(string.Empty, result);
            }
        }
Ejemplo n.º 30
0
        public async Task EndToEnd_HttpsGetRequestNoClientCert_Success()
        {
            OwinHttpListener listener = CreateServer(
                env =>
            {
                object obj;
                Assert.False(env.TryGetValue("owin.ClientCertificate", out obj));
                return(TaskHelpers.Completed());
            },
                HttpsServerAddress);

            HttpResponseMessage response = await SendGetRequest(listener, HttpsClientAddress, null);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(0, response.Content.Headers.ContentLength.Value);
        }
Ejemplo n.º 31
0
 private Task<HttpResponseMessage> SendGetRequest(OwinHttpListener listener, string address)
 {
     return SendGetRequest(listener, address, null);
 }
        public async Task PathAndPathBase_CorrectlySeperated(string clientString, string serverBasePath,
            string expectedBasePath, string expectedPath, string expectedQuery)
        {
            var fallbackAddress = new string[4];
            HttpServerAddress.CopyTo(fallbackAddress, 0);
            fallbackAddress[3] = "/";
            var serverAddress = new string[4];
            HttpServerAddress.CopyTo(serverAddress, 0);
            serverAddress[3] = serverBasePath;
            clientString = "http://localhost:8080" + clientString;

            using (var wrapper = new OwinHttpListener())
            {
                wrapper.Start(wrapper.Listener, env =>
                {
                    Assert.Equal(expectedBasePath, (string)env["owin.RequestPathBase"]);
                    Assert.Equal(expectedPath, (string)env["owin.RequestPath"]);
                    Assert.Equal(expectedQuery, (string)env["owin.RequestQueryString"]);
                    return TaskHelpers.Completed();
                }, CreateAddresses(fallbackAddress, serverAddress), null);

                using (var client = new HttpClient())
                {
                    HttpResponseMessage result = await client.GetAsync(clientString);
                    Assert.Equal(HttpStatusCode.OK, result.StatusCode);
                }
            }
        }
 private Task<HttpResponseMessage> SendGetRequest(OwinHttpListener listener, string address)
 {
     return SendGetRequest(listener, address, ClientCertificateOption.Automatic);
 }
        private async Task<HttpResponseMessage> SendGetRequest(OwinHttpListener listener, string address, ClientCertificateOption certOptions)
        {
            using (listener)
            {
                var handler = new WebRequestHandler();

                // Ignore server cert errors.
                handler.ServerCertificateValidationCallback = (a, b, c, d) => true;
                handler.ClientCertificateOptions = certOptions;

                var client = new HttpClient(handler);
                return await client.GetAsync(address);
            }
        }
 private bool PropertiesTrySetValue(string key, object value)
 {
     switch (key.Length)
     {
         case 12:
             if (string.Equals(key, "owin.Version", StringComparison.Ordinal))
             {
                 OwinVersion = (string)value;
                 return true;
             }
             if (string.Equals(key, "host.AppName", StringComparison.Ordinal))
             {
                 HostAppName = (string)value;
                 return true;
             }
             if (string.Equals(key, "host.AppMode", StringComparison.Ordinal))
             {
                 HostAppMode = (string)value;
                 return true;
             }
            break;
         case 18:
             if (string.Equals(key, "owin.CallCancelled", StringComparison.Ordinal))
             {
                 CallCancelled = (CancellationToken)value;
                 return true;
             }
             if (string.Equals(key, "owin.RequestMethod", StringComparison.Ordinal))
             {
                 RequestMethod = (string)value;
                 return true;
             }
             if (string.Equals(key, "owin.RequestScheme", StringComparison.Ordinal))
             {
                 RequestScheme = (string)value;
                 return true;
             }
            break;
         case 20:
             if (string.Equals(key, "owin.RequestProtocol", StringComparison.Ordinal))
             {
                 RequestProtocol = (string)value;
                 return true;
             }
             if (string.Equals(key, "owin.RequestPathBase", StringComparison.Ordinal))
             {
                 RequestPathBase = (string)value;
                 return true;
             }
             if (string.Equals(key, "owin.ResponseHeaders", StringComparison.Ordinal))
             {
                 ResponseHeaders = (IDictionary<string, string[]>)value;
                 return true;
             }
            break;
         case 16:
             if (string.Equals(key, "owin.RequestPath", StringComparison.Ordinal))
             {
                 RequestPath = (string)value;
                 return true;
             }
             if (string.Equals(key, "owin.RequestBody", StringComparison.Ordinal))
             {
                 RequestBody = (Stream)value;
                 return true;
             }
             if (string.Equals(key, "host.TraceOutput", StringComparison.Ordinal))
             {
                 HostTraceOutput = (TextWriter)value;
                 return true;
             }
             if (string.Equals(key, "server.LocalPort", StringComparison.Ordinal))
             {
                 ServerLocalPort = (string)value;
                 return true;
             }
             if (string.Equals(key, "websocket.Accept", StringComparison.Ordinal))
             {
                 WebSocketAccept = (WebSocketAccept)value;
                 return true;
             }
            break;
         case 23:
             if (string.Equals(key, "owin.RequestQueryString", StringComparison.Ordinal))
             {
                 RequestQueryString = (string)value;
                 return true;
             }
             if (string.Equals(key, "owin.ResponseStatusCode", StringComparison.Ordinal))
             {
                 ResponseStatusCode = (int)value;
                 return true;
             }
             if (string.Equals(key, "server.OnSendingHeaders", StringComparison.Ordinal))
             {
                 OnSendingHeaders = (Action<Action<object>, object>)value;
                 return true;
             }
             if (string.Equals(key, "ssl.LoadClientCertAsync", StringComparison.Ordinal))
             {
                 LoadClientCert = (Func<Task>)value;
                 return true;
             }
            break;
         case 19:
             if (string.Equals(key, "owin.RequestHeaders", StringComparison.Ordinal))
             {
                 RequestHeaders = (IDictionary<string, string[]>)value;
                 return true;
             }
             if (string.Equals(key, "host.OnAppDisposing", StringComparison.Ordinal))
             {
                 OnAppDisposing = (CancellationToken)value;
                 return true;
             }
             if (string.Equals(key, "server.Capabilities", StringComparison.Ordinal))
             {
                 ServerCapabilities = (IDictionary<string, object>)value;
                 return true;
             }
            break;
         case 25:
             if (string.Equals(key, "owin.ResponseReasonPhrase", StringComparison.Ordinal))
             {
                 ResponseReasonPhrase = (string)value;
                 return true;
             }
            break;
         case 17:
             if (string.Equals(key, "owin.ResponseBody", StringComparison.Ordinal))
             {
                 ResponseBody = (Stream)value;
                 return true;
             }
             if (string.Equals(key, "server.RemotePort", StringComparison.Ordinal))
             {
                 ServerRemotePort = (string)value;
                 return true;
             }
            break;
         case 11:
             if (string.Equals(key, "server.User", StringComparison.Ordinal))
             {
                 ServerUser = (IPrincipal)value;
                 return true;
             }
            break;
         case 22:
             if (string.Equals(key, "server.RemoteIpAddress", StringComparison.Ordinal))
             {
                 ServerRemoteIpAddress = (string)value;
                 return true;
             }
            break;
         case 21:
             if (string.Equals(key, "server.LocalIpAddress", StringComparison.Ordinal))
             {
                 ServerLocalIpAddress = (string)value;
                 return true;
             }
             if (string.Equals(key, "ssl.ClientCertificate", StringComparison.Ordinal))
             {
                 ClientCert = (X509Certificate)value;
                 return true;
             }
             if (string.Equals(key, "Mono.Net.HttpListener", StringComparison.Ordinal))
             {
                 Listener = (Mono.Net.HttpListener)value;
                 return true;
             }
            break;
         case 14:
             if (string.Equals(key, "server.IsLocal", StringComparison.Ordinal))
             {
                 ServerIsLocal = (bool)value;
                 return true;
             }
            break;
         case 27:
             if (string.Equals(key, "ssl.ClientCertificateErrors", StringComparison.Ordinal))
             {
                 ClientCertErrors = (Exception)value;
                 return true;
             }
            break;
         case 28:
             if (string.Equals(key, "Mono.Net.HttpListenerContext", StringComparison.Ordinal))
             {
                 RequestContext = (HttpListenerContext)value;
                 return true;
             }
            break;
         case 49:
             if (string.Equals(key, "Microsoft.Owin.Host.HttpListener.OwinHttpListener", StringComparison.Ordinal))
             {
                 OwinHttpListener = (OwinHttpListener)value;
                 return true;
             }
            break;
     }
     return false;
 }
 private bool PropertiesTryRemove(string key)
 {
     switch (key.Length)
     {
         case 12:
             if (((_flag0 & 0x1u) != 0) && string.Equals(key, "owin.Version", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x1u;
                 _OwinVersion = default(string);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x8000u) != 0) && string.Equals(key, "host.AppName", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x8000u;
                 _HostAppName = default(string);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x10000u) != 0) && string.Equals(key, "host.AppMode", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x10000u;
                 _HostAppMode = default(string);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
            break;
         case 18:
             if (((_flag0 & 0x2u) != 0) && string.Equals(key, "owin.CallCancelled", StringComparison.Ordinal))
             {
                 _initFlag0 &= ~0x2u;
                 _flag0 &= ~0x2u;
                 _CallCancelled = default(CancellationToken);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x8u) != 0) && string.Equals(key, "owin.RequestMethod", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x8u;
                 _RequestMethod = default(string);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x10u) != 0) && string.Equals(key, "owin.RequestScheme", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x10u;
                 _RequestScheme = default(string);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
            break;
         case 20:
             if (((_flag0 & 0x4u) != 0) && string.Equals(key, "owin.RequestProtocol", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x4u;
                 _RequestProtocol = default(string);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x20u) != 0) && string.Equals(key, "owin.RequestPathBase", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x20u;
                 _RequestPathBase = default(string);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x1000u) != 0) && string.Equals(key, "owin.ResponseHeaders", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x1000u;
                 _ResponseHeaders = default(IDictionary<string, string[]>);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
            break;
         case 16:
             if (((_flag0 & 0x40u) != 0) && string.Equals(key, "owin.RequestPath", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x40u;
                 _RequestPath = default(string);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x200u) != 0) && string.Equals(key, "owin.RequestBody", StringComparison.Ordinal))
             {
                 _initFlag0 &= ~0x200u;
                 _flag0 &= ~0x200u;
                 _RequestBody = default(Stream);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x4000u) != 0) && string.Equals(key, "host.TraceOutput", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x4000u;
                 _HostTraceOutput = default(TextWriter);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x1000000u) != 0) && string.Equals(key, "server.LocalPort", StringComparison.Ordinal))
             {
                 _initFlag0 &= ~0x1000000u;
                 _flag0 &= ~0x1000000u;
                 _ServerLocalPort = default(string);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x20000000u) != 0) && string.Equals(key, "websocket.Accept", StringComparison.Ordinal))
             {
                 _initFlag0 &= ~0x20000000u;
                 _flag0 &= ~0x20000000u;
                 _WebSocketAccept = default(WebSocketAccept);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
            break;
         case 23:
             if (((_flag0 & 0x80u) != 0) && string.Equals(key, "owin.RequestQueryString", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x80u;
                 _RequestQueryString = default(string);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x400u) != 0) && string.Equals(key, "owin.ResponseStatusCode", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x400u;
                 _ResponseStatusCode = default(int);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x80000u) != 0) && string.Equals(key, "server.OnSendingHeaders", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x80000u;
                 _OnSendingHeaders = default(Action<Action<object>, object>);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x10000000u) != 0) && string.Equals(key, "ssl.LoadClientCertAsync", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x10000000u;
                 _LoadClientCert = default(Func<Task>);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
            break;
         case 19:
             if (((_flag0 & 0x100u) != 0) && string.Equals(key, "owin.RequestHeaders", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x100u;
                 _RequestHeaders = default(IDictionary<string, string[]>);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x20000u) != 0) && string.Equals(key, "host.OnAppDisposing", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x20000u;
                 _OnAppDisposing = default(CancellationToken);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x100000u) != 0) && string.Equals(key, "server.Capabilities", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x100000u;
                 _ServerCapabilities = default(IDictionary<string, object>);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
            break;
         case 25:
             if (((_flag0 & 0x800u) != 0) && string.Equals(key, "owin.ResponseReasonPhrase", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x800u;
                 _ResponseReasonPhrase = default(string);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
            break;
         case 17:
             if (((_flag0 & 0x2000u) != 0) && string.Equals(key, "owin.ResponseBody", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x2000u;
                 _ResponseBody = default(Stream);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x400000u) != 0) && string.Equals(key, "server.RemotePort", StringComparison.Ordinal))
             {
                 _initFlag0 &= ~0x400000u;
                 _flag0 &= ~0x400000u;
                 _ServerRemotePort = default(string);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
            break;
         case 11:
             if (((_flag0 & 0x40000u) != 0) && string.Equals(key, "server.User", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x40000u;
                 _ServerUser = default(IPrincipal);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
            break;
         case 22:
             if (((_flag0 & 0x200000u) != 0) && string.Equals(key, "server.RemoteIpAddress", StringComparison.Ordinal))
             {
                 _initFlag0 &= ~0x200000u;
                 _flag0 &= ~0x200000u;
                 _ServerRemoteIpAddress = default(string);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
            break;
         case 21:
             if (((_flag0 & 0x800000u) != 0) && string.Equals(key, "server.LocalIpAddress", StringComparison.Ordinal))
             {
                 _initFlag0 &= ~0x800000u;
                 _flag0 &= ~0x800000u;
                 _ServerLocalIpAddress = default(string);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x4000000u) != 0) && string.Equals(key, "ssl.ClientCertificate", StringComparison.Ordinal))
             {
                 _initFlag0 &= ~0x4000000u;
                 _flag0 &= ~0x4000000u;
                 _ClientCert = default(X509Certificate);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
             if (((_flag0 & 0x80000000u) != 0) && string.Equals(key, "Mono.Net.HttpListener", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x80000000u;
                 _Listener = default(Mono.Net.HttpListener);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
            break;
         case 14:
             if (((_flag0 & 0x2000000u) != 0) && string.Equals(key, "server.IsLocal", StringComparison.Ordinal))
             {
                 _initFlag0 &= ~0x2000000u;
                 _flag0 &= ~0x2000000u;
                 _ServerIsLocal = default(bool);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
            break;
         case 27:
             if (((_flag0 & 0x8000000u) != 0) && string.Equals(key, "ssl.ClientCertificateErrors", StringComparison.Ordinal))
             {
                 _initFlag0 &= ~0x8000000u;
                 _flag0 &= ~0x8000000u;
                 _ClientCertErrors = default(Exception);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
            break;
         case 28:
             if (((_flag0 & 0x40000000u) != 0) && string.Equals(key, "Mono.Net.HttpListenerContext", StringComparison.Ordinal))
             {
                 _flag0 &= ~0x40000000u;
                 _RequestContext = default(HttpListenerContext);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
            break;
         case 49:
             if (((_flag1 & 0x1u) != 0) && string.Equals(key, "Microsoft.Owin.Host.HttpListener.OwinHttpListener", StringComparison.Ordinal))
             {
                 _flag1 &= ~0x1u;
                 _OwinHttpListener = default(OwinHttpListener);
                 // This can return true incorrectly for values that delayed initialization may determine are not actually present.
                 return true;
             }
            break;
     }
     return false;
 }
 private OwinHttpListener CreateServer(Func<IDictionary<string, object>, Task> app, string[] addressParts)
 {
     var wrapper = new OwinHttpListener();
     wrapper.Start(wrapper.Listener, app, CreateAddress(addressParts), null);
     return wrapper;
 }
 private async Task SendGetRequest(OwinHttpListener listener, string address)
 {
     using (listener)
     {
         var client = new HttpClient();
         string result = await client.GetStringAsync(address);
     }
 }
 private async Task SendRequest(OwinHttpListener listener, HttpRequestMessage request)
 {
     using (listener)
     {
         var client = new HttpClient();
         HttpResponseMessage result = await client.SendAsync(request);
         result.EnsureSuccessStatusCode();
     }
 }
Ejemplo n.º 40
0
 private OwinHttpListener CreateServer(AppFunc app, string[] addressParts)
 {
     var wrapper = new OwinHttpListener();
     wrapper.Start(wrapper.Listener, app, CreateAddress(addressParts), null, null);
     return wrapper;
 }