public void Add_InvalidPrefixAlreadyStarted_ThrowsHttpListenerExceptionOnAdd(string uriPrefix) { using (var factory = new HttpListenerFactory()) { HttpListener listener = factory.GetListener(); Assert.Single(listener.Prefixes); Assert.Throws <HttpListenerException>(() => listener.Prefixes.Add(uriPrefix)); } }
public void Add_PrefixAlreadyRegisteredAndStarted_ThrowsHttpListenerException(string hostname) { using (var factory = new HttpListenerFactory(hostname)) { HttpListener listener = factory.GetListener(); string uriPrefix = Assert.Single(listener.Prefixes); Assert.Throws <HttpListenerException>(() => listener.Prefixes.Add(uriPrefix)); Assert.Throws <HttpListenerException>(() => listener.Prefixes.Add(uriPrefix + "/sub_path/")); } }
public void ListenerRestart_BeginGetContext_Success() { using (HttpListenerFactory factory = new HttpListenerFactory()) { HttpListener listener = factory.GetListener(); listener.BeginGetContext((f) => { }, null); listener.Stop(); listener.Start(); listener.BeginGetContext((f) => { }, null); } }
public async Task ListenerRestart_Success(bool sync) { const string Content = "ListenerRestart_GetContext_Success"; using (HttpListenerFactory factory = new HttpListenerFactory()) using (HttpClient client = new HttpClient()) { HttpListener listener = factory.GetListener(); _output.WriteLine("Connecting to {0}", factory.ListeningUrl); Task <string> clientTask = client.GetStringAsync(factory.ListeningUrl); HttpListenerContext context = sync ? listener.GetContext() : listener.EndGetContext(listener.BeginGetContext(ar => { }, null)); HttpListenerResponse response = context.Response; response.OutputStream.Write(Encoding.UTF8.GetBytes(Content)); response.OutputStream.Close(); string body = await clientTask; Assert.Equal(Content, body); // Stop and start listener again. listener.Stop(); try { // This may fail if something else took our port while restarting. listener.Start(); } catch (HttpListenerException e) { _output.WriteLine(e.Message); // Skip test if we lost race and we are unable to bind on same port again. throw new SkipTestException("Unable to restart listener"); } _output.WriteLine("Connecting to {0} after restart", factory.ListeningUrl); // Repeat request to be sure listener is working. clientTask = client.GetStringAsync(factory.ListeningUrl); context = sync ? listener.GetContext() : listener.EndGetContext(listener.BeginGetContext(ar => { }, null)); response = context.Response; response.OutputStream.Write(Encoding.UTF8.GetBytes(Content)); response.OutputStream.Close(); body = await clientTask; Assert.Equal(Content, body); } }
public void Add_PrefixAlreadyRegisteredAndNotStarted_ThrowsHttpListenerException(string hostname) { using (var factory = new HttpListenerFactory(hostname)) { string uriPrefix = Assert.Single(factory.GetListener().Prefixes); var listener = new HttpListener(); listener.Prefixes.Add(uriPrefix); Assert.Throws <HttpListenerException>(() => listener.Start()); } }
public async Task GetContext_StopIsCalled_GetContextUnblocked() { using var listenerFactory = new HttpListenerFactory(); var listener = listenerFactory.GetListener(); listener.Start(); var listenerTask = Task.Run(() => Assert.Throws <HttpListenerException>(() => listener.GetContext())); await Task.Delay(1000); // Wait for listenerTask to call GetContext. listener.Stop(); listener.Close(); await listenerTask.WaitAsync(TimeSpan.FromSeconds(10)); }
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); } }
public void Add_PrefixAlreadyRegisteredWithDifferentPathAndNotStarted_Works(string hostname) { using (var factory = new HttpListenerFactory(hostname)) { HttpListener listener = factory.GetListener(); string uriPrefix = Assert.Single(listener.Prefixes); listener.Prefixes.Add(uriPrefix + "sub_path/"); Assert.Equal(2, listener.Prefixes.Count); listener.Start(); Assert.True(listener.IsListening); } }
public void Remove_PrefixExistsStarted_ReturnsTrue() { using (var factory = new HttpListenerFactory()) { HttpListener listener = factory.GetListener(); string uriPrefix = Assert.Single(listener.Prefixes); Assert.True(listener.Prefixes.Remove(uriPrefix)); Assert.False(listener.Prefixes.Contains(uriPrefix)); Assert.Equal(0, listener.Prefixes.Count); // Even though the listener has no prefixes, it should still be listening. Assert.True(listener.IsListening); } }
public void EndGetContext_AlreadyCalled_ThrowsInvalidOperationException() { using (var listenerFactory = new HttpListenerFactory()) using (var client = new HttpClient()) { HttpListener listener = listenerFactory.GetListener(); listener.Start(); Task <string> clientTask = client.GetStringAsync(listenerFactory.ListeningUrl); IAsyncResult beginGetContextResult = listener.BeginGetContext(null, null); listener.EndGetContext(beginGetContextResult); Assert.Throws <InvalidOperationException>(() => listener.EndGetContext(beginGetContextResult)); } }
public void Add_SamePortDifferentPathDifferentListenerNotStarted_Works(string host) { using (var factory1 = new HttpListenerFactory(host, path: string.Empty)) { HttpListener listener1 = factory1.GetListener(); using (var listener2 = new HttpListener()) { string prefixWithSubPath = $"{factory1.ListeningUrl}sub_path/"; listener2.Prefixes.Add(prefixWithSubPath); Assert.Equal(prefixWithSubPath, Assert.Single(listener2.Prefixes)); listener2.Start(); Assert.True(listener2.IsListening); } } }
public async Task Remove_PrefixExistsStarted_ReturnsTrue() { using (var factory = new HttpListenerFactory()) { HttpListener listener = factory.GetListener(); string uriPrefix = Assert.Single(listener.Prefixes); Assert.True(listener.Prefixes.Remove(uriPrefix)); Assert.False(listener.Prefixes.Contains(uriPrefix)); Assert.Equal(0, listener.Prefixes.Count); // Trying to connect to the HttpListener should now fail. using (var client = new HttpClient()) { await Assert.ThrowsAsync <HttpRequestException>(() => client.GetStringAsync(factory.ListeningUrl)); } } }
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.Equal(client.LocalEndPoint, request.RemoteEndPoint); Assert.Equal(factory.ListeningUrl, request.Url.ToString()); Assert.Equal($"/{factory.Path}/", request.RawUrl); } }
public async Task ReceiveAsync_ReadBuffer_WithWindowsAuthScheme_Success() { HttpListenerFactory factory = new HttpListenerFactory(authenticationSchemes: AuthenticationSchemes.IntegratedWindowsAuthentication); var uriBuilder = new UriBuilder(factory.ListeningUrl) { Scheme = "ws" }; Task <HttpListenerContext> serverContextTask = factory.GetListener().GetContextAsync(); ClientWebSocket client = new ClientWebSocket(); client.Options.Credentials = CredentialCache.DefaultCredentials; Task clientConnectTask = client.ConnectAsync(uriBuilder.Uri, CancellationToken.None); if (clientConnectTask == await Task.WhenAny(serverContextTask, clientConnectTask)) { await clientConnectTask; Assert.True(false, "Client should not have completed prior to server sending response"); } HttpListenerContext context = await serverContextTask; HttpListenerWebSocketContext wsContext = await context.AcceptWebSocketAsync(null); await clientConnectTask; const string Text = "Hello Web Socket"; byte[] sentBytes = Encoding.ASCII.GetBytes(Text); await client.SendAsync(new ArraySegment <byte>(sentBytes), WebSocketMessageType.Text, true, new CancellationToken()); byte[] receivedBytes = new byte[sentBytes.Length]; WebSocketReceiveResult result = await ReceiveAllAsync(wsContext.WebSocket, receivedBytes.Length, receivedBytes); Assert.Equal(WebSocketMessageType.Text, result.MessageType); Assert.True(result.EndOfMessage); Assert.Null(result.CloseStatus); Assert.Null(result.CloseStatusDescription); Assert.Equal(Text, Encoding.ASCII.GetString(receivedBytes)); }
[ActiveIssue(18188, platforms: TestPlatforms.Windows)] // Indeterminate failure - socket not always fully disconnected. public async Task Write_ContentToClosedConnectionSynchronously_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. context.Response.OutputStream.Write(buffer, 0, 1); // Disconnect the Socket from the HttpListener. client.Close(); GC.Collect(); GC.WaitForPendingFinalizers(); // Writing non-header content to a disconnected client should fail, only if IgnoreWriteExceptions is false. if (ignoreWriteExceptions) { 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(); } }
public HttpListenerWebSocketTests() { Factory = new HttpListenerFactory(); Listener = Factory.GetListener(); Client = new ClientWebSocket(); }
public HttpResponseStreamTests() { _factory = new HttpListenerFactory(); _listener = _factory.GetListener(); _helper = new GetContextHelper(_listener, _factory.ListeningUrl); }
public HttpListenerAuthenticationTests() { _factory = new HttpListenerFactory(); _listener = _factory.GetListener(); }
public SimpleHttpTests(ITestOutputHelper output) { _factory = new HttpListenerFactory(); _listener = _factory.GetListener(); _output = output; }
public AuthenticationTests() { _factory = new HttpListenerFactory(); _listener = _factory.GetListener(); _url = _factory.ListeningUrl; }
public SimpleHttpTests() { _factory = new HttpListenerFactory(); _listener = _factory.GetListener(); }
public WebSocketTests() { _factory = new HttpListenerFactory(); _listener = _factory.GetListener(); _url = _factory.ListeningUrl; }