public async Task ProxyWebSocketsSmokeTest() { const string supportedSubProtocol = "myproto2"; const string otherSubProtocol = "myproto1"; const string message1Content = "TEST MESSAGE 1"; const string message2Content = "TEST MSG 2"; const string message3Content = "TEST MESSAGE 3"; const string closeStatusDescription = "My Status1"; using (var server = new WebHostBuilder() .UseKestrel() .Configure(app => app.UseWebSockets().Run(async ctx => { var socket = await ctx.WebSockets.AcceptWebSocketAsync(supportedSubProtocol); var message1 = await ReceiveTextMessage(socket); var message2 = await ReceiveTextMessage(socket); Assert.Equal(message1Content, message1); Assert.Equal(message2Content, message2); await socket.SendAsync(new ArraySegment <byte>(Encoding.UTF8.GetBytes(message3Content)), WebSocketMessageType.Text, true, CancellationToken.None); await socket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, closeStatusDescription, CancellationToken.None); })).Start("http://localhost:4001")) using (var proxy = new WebHostBuilder() .UseKestrel() .ConfigureServices(services => services.AddProxy(options => { options.GetProxyOptions = request => Task.FromResult(ProxyOptions.FromUri(new Uri($"http://localhost:4001"))); })) .Configure(app => app.UseWebSockets().RunProxy()) .Start("http://localhost:4002")) using (var client = new ClientWebSocket()) { client.Options.AddSubProtocol(otherSubProtocol); client.Options.AddSubProtocol(supportedSubProtocol); await client.ConnectAsync(new Uri("ws://localhost:4002"), CancellationToken.None); Assert.Equal(supportedSubProtocol, client.SubProtocol); await client.SendAsync(new ArraySegment <byte>(Encoding.UTF8.GetBytes(message1Content)), WebSocketMessageType.Text, true, CancellationToken.None); await client.SendAsync(new ArraySegment <byte>(Encoding.UTF8.GetBytes(message2Content)), WebSocketMessageType.Text, true, CancellationToken.None); var message3 = await ReceiveTextMessage(client); Assert.Equal(message3Content, message3); var result = await client.ReceiveAsync(new ArraySegment <byte>(new byte[4096]), CancellationToken.None); Assert.Equal(WebSocketMessageType.Close, result.MessageType); Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus); Assert.Equal(closeStatusDescription, result.CloseStatusDescription); await client.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, closeStatusDescription, CancellationToken.None); } }
public async Task PassthroughRequestsWithoutBodyWithResponseHeaders(string MethodType, int Port) { var builder = new WebHostBuilder() .ConfigureServices(services => services.AddProxy(options => { options.GetProxyOptions = request => Task.FromResult(ProxyOptions.FromUri(new Uri($"http://localhost:{Port}"))); options.MessageHandler = new TestMessageHandler { Sender = req => { IEnumerable <string> hostValue; req.Headers.TryGetValues("Host", out hostValue); Assert.Equal("localhost:" + Port, hostValue.Single()); Assert.Equal("http://localhost:" + Port + "/", req.RequestUri.ToString()); Assert.Equal(new HttpMethod(MethodType), req.Method); var response = new HttpResponseMessage(HttpStatusCode.Created); response.Headers.Add("testHeader", "testHeaderValue"); response.Content = new StringContent("Response Body"); return(response); } }; })) .Configure(app => app.RunProxy()); var server = new TestServer(builder); var requestMessage = new HttpRequestMessage(new HttpMethod(MethodType), ""); var responseMessage = await server.CreateClient().SendAsync(requestMessage); Assert.Equal(HttpStatusCode.Created, responseMessage.StatusCode); var responseContent = responseMessage.Content.ReadAsStringAsync(); Assert.True(responseContent.Wait(3000) && !responseContent.IsFaulted); Assert.Equal("Response Body", responseContent.Result); IEnumerable <string> testHeaderValue; responseMessage.Headers.TryGetValues("testHeader", out testHeaderValue); Assert.Equal("testHeaderValue", testHeaderValue.Single()); }