Esempio n. 1
0
        [ActiveIssue(18188, platforms: TestPlatforms.Windows)] // Indeterminate failure - socket not always fully disconnected.
        public async Task Write_ContentToClosedConnectionAsynchronously_ThrowsHttpListenerException(bool ignoreWriteExceptions)
        {
            const string Text = "Some-String";

            byte[] buffer = Encoding.UTF8.GetBytes(Text);

            using (HttpListenerFactory factory = new HttpListenerFactory())
                using (Socket client = factory.GetConnectedSocket())
                {
                    // Send a header to the HttpListener to give it a context.
                    client.Send(factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
                    HttpListener listener = factory.GetListener();
                    listener.IgnoreWriteExceptions = ignoreWriteExceptions;
                    HttpListenerContext context = await listener.GetContextAsync();

                    // Write the headers to the Socket.
                    await context.Response.OutputStream.WriteAsync(buffer, 0, 1);

                    // Disconnect the Socket from the HttpListener.
                    Helpers.WaitForSocketShutdown(client);

                    // Writing non-header content to a disconnected client should fail, only if IgnoreWriteExceptions is false.
                    if (ignoreWriteExceptions)
                    {
                        await context.Response.OutputStream.WriteAsync(buffer, 0, buffer.Length);
                    }
                    else
                    {
                        await Assert.ThrowsAsync <HttpListenerException>(() => context.Response.OutputStream.WriteAsync(buffer, 0, buffer.Length));
                    }

                    // Closing a response from a closed client if a writing has already failed should not fail.
                    context.Response.Close();
                }
        }
Esempio n. 2
0
        public async Task Write_HeadersToClosedConnectionSynchronously_ThrowsHttpListenerException(bool ignoreWriteExceptions)
        {
            const string Text = "Some-String";

            byte[] buffer = Encoding.UTF8.GetBytes(Text);

            using (HttpListenerFactory factory = new HttpListenerFactory())
                using (Socket client = factory.GetConnectedSocket())
                {
                    // Send a header to the HttpListener to give it a context.
                    client.Send(factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
                    HttpListener listener = factory.GetListener();
                    listener.IgnoreWriteExceptions = ignoreWriteExceptions;
                    HttpListenerContext context = await listener.GetContextAsync();

                    // Disconnect the Socket from the HttpListener.
                    Helpers.WaitForSocketShutdown(client);

                    // Writing to a disconnected client should fail.
                    if (!PlatformDetection.IsWindows && ignoreWriteExceptions)
                    {
                        // Windows sends headers first, followed by content. If headers fail to send, then an exception is always thrown.
                        // However, the managed implementation has already sent the headers by the time we run this test.
                        // This means that if the content fails to send, an exception is only thrown if ignoreWriteExceptions == false.
                        context.Response.OutputStream.Write(buffer, 0, buffer.Length);
                    }
                    else
                    {
                        Assert.Throws <HttpListenerException>(() => context.Response.OutputStream.Write(buffer, 0, buffer.Length));
                    }

                    // Closing a response from a closed client if a writing has already failed should not fail.
                    context.Response.Close();
                }
        }
Esempio n. 3
0
        public async Task CloseResponseEntity_SendToClosedConnection_DoesNotThrow(bool willBlock)
        {
            const string Text = "Some-String";

            byte[] buffer = Encoding.UTF8.GetBytes(Text);

            using (HttpListenerFactory factory = new HttpListenerFactory())
                using (Socket client = factory.GetConnectedSocket())
                {
                    // Send a header to the HttpListener to give it a context.
                    client.Send(factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
                    HttpListener        listener = factory.GetListener();
                    HttpListenerContext context  = await listener.GetContextAsync();

                    // Disconnect the Socket from the HttpListener.
                    Helpers.WaitForSocketShutdown(client);

                    // The non-blocking call can throw or not depending on the timing of the call to
                    // NonBlockingCloseCallback internally.
                    try
                    {
                        context.Response.Close(new byte[] { (byte)'a', (byte)'b' }, willBlock);
                    }
                    catch (HttpListenerException)
                    {
                        Assert.False(willBlock);
                    }
                }
        }
        [ActiveIssue(18188, platforms: TestPlatforms.Windows)] // Indeterminate failure - socket not always fully disconnected.
        public async Task Write_HeadersToClosedConnectionSynchronously_ThrowsHttpListenerException(bool ignoreWriteExceptions)
        {
            const string Text = "Some-String";

            byte[] buffer = Encoding.UTF8.GetBytes(Text);

            using (HttpListenerFactory factory = new HttpListenerFactory())
                using (Socket client = factory.GetConnectedSocket())
                {
                    // Send a header to the HttpListener to give it a context.
                    client.Send(factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
                    HttpListener listener = factory.GetListener();
                    listener.IgnoreWriteExceptions = ignoreWriteExceptions;
                    HttpListenerContext context = await listener.GetContextAsync();

                    // Disconnect the Socket from the HttpListener.
                    client.Close();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    // Writing to, a closed connection should fail.
                    Assert.Throws <HttpListenerException>(() => context.Response.OutputStream.Write(buffer, 0, buffer.Length));

                    // Closing a response from a closed client if a writing has already failed should not fail.
                    context.Response.Close();
                }
        }
Esempio n. 5
0
        public async Task EndPointProperties_GetProperty_ReturnsExpected()
        {
            using (HttpListenerFactory factory = new HttpListenerFactory())
                using (Socket client = factory.GetConnectedSocket())
                {
                    client.Send(factory.GetContent("POST", "Text", headerOnly: false));

                    HttpListener        listener = factory.GetListener();
                    HttpListenerContext context  = await listener.GetContextAsync();

                    HttpListenerRequest request = context.Request;
                    Assert.Equal(client.RemoteEndPoint.ToString(), request.UserHostAddress);

                    Assert.Equal(client.RemoteEndPoint, request.LocalEndPoint);
                    Assert.Same(request.LocalEndPoint, request.LocalEndPoint);

                    Assert.Equal(client.LocalEndPoint, request.RemoteEndPoint);
                    Assert.Same(request.RemoteEndPoint, request.RemoteEndPoint);

                    Assert.Equal(factory.ListeningUrl, request.Url.ToString());
                    Assert.Same(request.Url, request.Url);

                    Assert.Equal($"/{factory.Path}/", request.RawUrl);
                }
        }
Esempio n. 6
0
        public HttpListenerResponseTestBase()
        {
            Factory = new HttpListenerFactory();
            Client  = Factory.GetConnectedSocket();

            // Make sure the tests don't hang if there is nothing to receive.
            Client.ReceiveTimeout = 1000;
        }
Esempio n. 7
0
        private async Task GetRequest(string requestType, string query, string[] headers, Action <Socket, HttpListenerRequest> requestAction, bool sendContent = true, string httpVersion = "1.1")
        {
            using (HttpListenerFactory factory = new HttpListenerFactory())
                using (Socket client = factory.GetConnectedSocket())
                {
                    client.Send(factory.GetContent(httpVersion, requestType, query, sendContent ? "Text" : "", headers, true));

                    HttpListener        listener = factory.GetListener();
                    HttpListenerContext context  = await listener.GetContextAsync();

                    HttpListenerRequest request = context.Request;
                    requestAction(client, request);
                }
        }
Esempio n. 8
0
 public HttpListenerRequestTests()
 {
     Factory = new HttpListenerFactory();
     Client  = Factory.GetConnectedSocket();
 }