Example #1
0
        public async Task CanSetCustomServiceProvider()
        {
            var server = TestServer.Create(app =>
            {
                app.Run(context =>
                {
                    context.ApplicationServices = new ServiceCollection()
                                                  .AddTransient <TestService>()
                                                  .BuildServiceProvider();

                    context.RequestServices = new ServiceCollection()
                                              .AddTransient <TestService>()
                                              .BuildServiceProvider();

                    var s1 = context.ApplicationServices.GetRequiredService <TestService>();
                    var s2 = context.RequestServices.GetRequiredService <TestService>();

                    return(context.Response.WriteAsync("Success"));
                });
            });
            string result = await server.CreateClient().GetStringAsync("/path");

            Assert.Equal("Success", result);
        }
Example #2
0
 public void CreateWithDelegate()
 {
     // Arrange
     // Act & Assert (Does not throw)
     TestServer.Create(app => { });
 }
Example #3
0
        public async Task WebSocketWorks()
        {
            // Arrange
            RequestDelegate appDelegate = async ctx =>
            {
                if (ctx.WebSockets.IsWebSocketRequest)
                {
                    var websocket = await ctx.WebSockets.AcceptWebSocketAsync();

                    var receiveArray = new byte[1024];
                    while (true)
                    {
                        var receiveResult = await websocket.ReceiveAsync(new System.ArraySegment <byte>(receiveArray), CancellationToken.None);

                        if (receiveResult.MessageType == WebSocketMessageType.Close)
                        {
                            await websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal Closure", CancellationToken.None);

                            break;
                        }
                        else
                        {
                            var sendBuffer = new System.ArraySegment <byte>(receiveArray, 0, receiveResult.Count);
                            await websocket.SendAsync(sendBuffer, receiveResult.MessageType, receiveResult.EndOfMessage, CancellationToken.None);
                        }
                    }
                }
            };
            var server = TestServer.Create(app =>
            {
                app.Run(appDelegate);
            });

            // Act
            var client       = server.CreateWebSocketClient();
            var clientSocket = await client.ConnectAsync(new System.Uri("http://localhost"), CancellationToken.None);

            var hello = Encoding.UTF8.GetBytes("hello");
            await clientSocket.SendAsync(new System.ArraySegment <byte>(hello), WebSocketMessageType.Text, true, CancellationToken.None);

            var world = Encoding.UTF8.GetBytes("world!");
            await clientSocket.SendAsync(new System.ArraySegment <byte>(world), WebSocketMessageType.Binary, true, CancellationToken.None);

            await clientSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Normal Closure", CancellationToken.None);

            // Assert
            Assert.Equal(WebSocketState.CloseSent, clientSocket.State);

            var buffer = new byte[1024];
            var result = await clientSocket.ReceiveAsync(new System.ArraySegment <byte>(buffer), CancellationToken.None);

            Assert.Equal(hello.Length, result.Count);
            Assert.True(hello.SequenceEqual(buffer.Take(hello.Length)));
            Assert.Equal(WebSocketMessageType.Text, result.MessageType);

            result = await clientSocket.ReceiveAsync(new System.ArraySegment <byte>(buffer), CancellationToken.None);

            Assert.Equal(world.Length, result.Count);
            Assert.True(world.SequenceEqual(buffer.Take(world.Length)));
            Assert.Equal(WebSocketMessageType.Binary, result.MessageType);

            result = await clientSocket.ReceiveAsync(new System.ArraySegment <byte>(buffer), CancellationToken.None);

            Assert.Equal(WebSocketMessageType.Close, result.MessageType);
            Assert.Equal(WebSocketState.Closed, clientSocket.State);

            clientSocket.Dispose();
        }
Example #4
0
 public TestClientTests()
 {
     _server = TestServer.Create(app => app.Run(ctx => Task.FromResult(0)));
 }
Example #5
0
        public TestClientTests()
        {
            _services = HostingServices.Create().BuildServiceProvider();

            _server = TestServer.Create(_services, app => app.Run(ctx => Task.FromResult(0)));
        }