Ejemplo n.º 1
0
        public void DeliversXmlResponse()
        {
            var port = new AwaitedPort(new TestPort()).Value();

            using (var server =
                       new HttpMock(port,
                                    new FkWire(req =>
            {
                return(new Response.Of(new Body(new XMLCursor(new InputOf("<test/>")))));
            })
                                    ).Value()
                   )
            {
                var response =
                    AsyncContext.Run(() =>
                                     new AspNetCoreWire(
                                         new AspNetCoreClients(),
                                         new TimeSpan(0, 1, 0)
                                         ).Response(
                                         new Get($"http://localhost:{port}?importantQueryParam=importantValue")
                                         )
                                     );

                Assert.True(
                    response.ContainsKey("body")
                    );
            }
        }
Ejemplo n.º 2
0
        public void Returns404()
        {
            var port = new AwaitedPort(new TestPort()).Value();

            using (var server =
                       new HttpMock(port,
                                    new KvpOf <IWire>("path",
                                                      new FkWire()
                                                      )
                                    ).Value()
                   )
            {
                Assert.Equal(
                    404,
                    new Status.Of(
                        new AspNetCoreWire(
                            new AspNetCoreClients(),
                            new TimeSpan(0, 1, 0)
                            ).Response(
                            new Get($"http://localhost:{port}/unknown/path")
                            )
                        ).AsInt()
                    );
            }
        }
Ejemplo n.º 3
0
        public void ListensToAnyPath()
        {
            var port     = new AwaitedPort(new TestPort()).Value();
            var clients  = new AspNetCoreClients();
            var requests = 0;

            using (var server =
                       new HttpMock(port,
                                    new FkWire(req =>
            {
                requests++;
                return(new Response.Of(200, "OK"));
            })
                                    ).Value()
                   )
            {
                Task.WaitAll(
                    new AspNetCoreWire(clients, new TimeSpan(0, 1, 0)).Response(
                        new Get($"http://localhost:{port}")
                        ),
                    new AspNetCoreWire(clients, new TimeSpan(0, 1, 0)).Response(
                        new Get($"http://localhost:{port}/path")
                        ),
                    new AspNetCoreWire(clients, new TimeSpan(0, 1, 0)).Response(
                        new Get($"http://localhost:{port}/another/path")
                        )
                    );
            }
            Assert.Equal(
                3,
                requests
                );
        }
        public void SendsHeaders()
        {
            var port   = new AwaitedPort(new TestPort()).Value();
            var header = "";

            using (var server =
                       new HttpMock(port,
                                    new FkWire(req =>
            {
                header = new FirstOf <string>(new Authorization.Of(req)).Value();
                return(new Response.Of(200, "OK"));
            })
                                    ).Value()
                   )
            {
                new AspNetCoreWire(
                    new AspNetCoreClients(),
                    new TimeSpan(0, 1, 0)
                    ).Response(
                    new Get(
                        new Scheme("http"),
                        new Host("localhost"),
                        new Port(server.Port),
                        new Header("Authorization", "Basic dXNlcjpwYXNzd29yZA==")
                        )
                    ).Wait(30000);
            }
            Assert.Equal(
                "Basic dXNlcjpwYXNzd29yZA==",
                header
                );
        }
        public void EncodesSpecialCharactersInFormBody()
        {
            var    port = new AwaitedPort(new TestPort()).Value();
            string body = "";

            using (var server =
                       new HttpMock(port,
                                    new FkWire(req =>
            {
                body = new TextBody.Of(req).AsString();
            })
                                    ).Value()
                   )
            {
                new Verified(
                    new AspNetCoreWire(
                        new AspNetCoreClients(),
                        new TimeSpan(0, 1, 0)
                        ),
                    new ExpectedStatus(200)
                    ).Response(
                    new Get(
                        new Scheme("http"),
                        new Host("localhost"),
                        new Port(server.Port),
                        new FormParam("key-name", "test&+=xyz"),
                        new BearerTokenAuth("Bearer sdfnhiausihfnksajn")
                        )
                    );
            }
            Assert.Equal(
                "key-name=test%26%2B%3Dxyz",
                body
                );
        }
        public void ReturnsBody()
        {
            var port = new AwaitedPort(new TestPort()).Value();

            using (var server =
                       new HttpMock(port,
                                    new FkWire(
                                        new TextOf("very important content")
                                        )
                                    ).Value()
                   )
            {
                Assert.Equal(
                    "very important content",
                    new TextOf(
                        new Body.Of(
                            new AspNetCoreWire(
                                new AspNetCoreClients(),
                                new TimeSpan(0, 1, 0)
                                ).Response(
                                new Get(
                                    new Scheme("http"),
                                    new Host("localhost"),
                                    new Port(server.Port)
                                    )
                                )
                            )
                        ).AsString()
                    );
            }
        }
        public void SendsBody()
        {
            var port = new AwaitedPort(new TestPort()).Value();
            var body = "";

            using (var server =
                       new HttpMock(port,
                                    new FkWire(req =>
            {
                body =
                    new TextOf(
                        new Body.Of(req)
                        ).AsString();
                return(new Response.Of(200, "OK"));
            })
                                    ).Value()
                   )
            {
                new AspNetCoreWire(
                    new AspNetCoreClients(),
                    new TimeSpan(0, 1, 0)
                    ).Response(
                    new Get(
                        new Scheme("http"),
                        new Host("localhost"),
                        new Port(server.Port),
                        new TextBody("very important content")
                        )
                    ).Wait(30000);
            }
            Assert.Equal(
                "very important content",
                body
                );
        }
Ejemplo n.º 8
0
        public void ForwardsQueryParams()
        {
            var port       = new AwaitedPort(new TestPort()).Value();
            var queryParam = "";

            using (var server =
                       new HttpMock(port,
                                    new FkWire(req =>
            {
                queryParam = new QueryParam.Of(req, "importantQueryParam").AsString();
                return(new Response.Of(200, "OK"));
            })
                                    ).Value()
                   )
            {
                new AspNetCoreWire(
                    new AspNetCoreClients(),
                    new TimeSpan(0, 1, 0)
                    ).Response(
                    new Get($"http://localhost:{port}?importantQueryParam=importantValue")
                    ).Wait(30000);
            }
            Assert.Equal(
                "importantValue",
                queryParam
                );
        }
Ejemplo n.º 9
0
        public void Returns200()
        {
            var port = new AwaitedPort(new TestPort()).Value();

            using (var server =
                       new HttpMock(port,
                                    new FkWire()
                                    ).Value()
                   )
            {
                Assert.Equal(
                    200,
                    new Status.Of(
                        new AspNetCoreWire(
                            new AspNetCoreClients(),
                            new TimeSpan(0, 1, 0)
                            ).Response(
                            new Get(
                                new Scheme("http"),
                                new Host("localhost"),
                                new Port(server.Port),
                                new Path("test/asdf")
                                )
                            )
                        ).AsInt()
                    );
            }
        }
        public void FormBodyWorksWithKestrel()
        {
            var body = "";
            var port = new AwaitedPort(new TestPort()).Value();
            var host = WebHost.CreateDefaultBuilder();

            host.UseUrls($"http://{Environment.MachineName.ToLower()}:{port}");
            host.ConfigureServices(svc =>
            {
                svc.AddSingleton <Action <HttpRequest> >(req => // required to instantiate HtAction from dependecy injection
                {
                    body = new TextOf(req.Body).AsString();
                });
                svc.AddMvc().AddApplicationPart(
                    typeof(HtAction).Assembly
                    ).AddControllersAsServices();
            });
            host.Configure(app =>
            {
                app.UseMvc();
            });

            using (var built = host.Build())
            {
                built.RunAsync();
                try
                {
                    new Verified(
                        new AspNetCoreWire(
                            new AspNetCoreClients(),
                            new TimeSpan(0, 1, 0)
                            ),
                        new ExpectedStatus(200)
                        ).Response(
                        new Post(
                            new Scheme("http"),
                            new Host("localhost"),
                            new Port(port),
                            new Path("action"),
                            new FormParams(
                                new MapOf(
                                    "key-1-name", "this is a test",
                                    "key-2-name", "test&+=xyz"
                                    )
                                )
                            )
                        );
                }
                finally
                {
                    built.StopAsync();
                }
            }

            Assert.Equal(
                "key-1-name=this+is+a+test&key-2-name=test%26%2B%3Dxyz",
                body
                );
        }
Ejemplo n.º 11
0
        public void RoutesToPath()
        {
            var port    = new AwaitedPort(new TestPort()).Value();
            var clients = new AspNetCoreClients();
            var result  = 0;

            using (var server =
                       new HttpMock(port,
                                    new KvpOf <IWire>("",
                                                      new FkWire(req =>
            {
                result += 1;
                return(new Response.Of(200, "OK"));
            })
                                                      ),
                                    new KvpOf <IWire>("path",
                                                      new FkWire(req =>
            {
                result += 2;
                return(new Response.Of(200, "OK"));
            })
                                                      ),
                                    new KvpOf <IWire>("another/path",
                                                      new FkWire(req =>
            {
                result += 4;
                return(new Response.Of(200, "OK"));
            })
                                                      )
                                    ).Value()
                   )
            {
                Task.WaitAll(
                    new AspNetCoreWire(clients, new TimeSpan(0, 1, 0)).Response(
                        new Get($"http://localhost:{port}")
                        ),
                    new AspNetCoreWire(clients, new TimeSpan(0, 1, 0)).Response(
                        new Get($"http://localhost:{port}/path")
                        ),
                    new AspNetCoreWire(clients, new TimeSpan(0, 1, 0)).Response(
                        new Get($"http://localhost:{port}/another/path")
                        )
                    );
            }
            Assert.Equal(
                7,
                result
                );
        }
Ejemplo n.º 12
0
        public void TransmitsRawZipFile()
        {
            var    port   = new AwaitedPort(new TestPort()).Value();
            IInput result = new DeadInput();

            using (var server =
                       new MockServer(
                           port,
                           "{}",
                           (req, res, prm) =>
                           result =
                               new InputOf(
                                   new BytesOf(
                                       new InputOf(req.InputStream)
                                       ).AsBytes()
                                   ),
                           "localhost"
                           )
                   )
            {
                new AspNetCoreWire(
                    new AspNetCoreClients()
                    ).Response(
                    new Post($"http://localhost:{port}/",
                             new Body(
                                 new ResourceOf(
                                     "Assets/test.zip",
                                     this.GetType().Assembly
                                     )
                                 )
                             )
                    ).Wait(30000);
            }
            Assert.Equal(
                "this is a test", // content of test.txt in Assets/test.zip
                new TextOf(
                    new UnzippedFile(result, "test.txt")
                    ).AsString()
                );
        }
Ejemplo n.º 13
0
        public void TransmitsZipFile()
        {
            var port = new AwaitedPort(new TestPort()).Value();

            using (var server =
                       new HttpMock(port,
                                    new FkWire(req =>
                                               new Response.Of(
                                                   new Status(200),
                                                   new Reason("OK"),
                                                   new Body(
                                                       new ResourceOf(
                                                           "Assets/test.zip",
                                                           this.GetType().Assembly
                                                           ),
                                                       "application/zip"
                                                       )
                                                   )
                                               )
                                    ).Value()
                   )
            {
                Assert.Equal(
                    "this is a test", // content of test.txt in Assets/test.zip
                    new TextOf(
                        new UnzippedFile(
                            new Body.Of(
                                new AspNetCoreWire(
                                    new AspNetCoreClients()
                                    ).Response(
                                    new Get($"http://localhost:{port}/")
                                    )
                                ),
                            "test.txt"
                            )
                        ).AsString()
                    );
            }
        }
        public void ReturnsMultipleHeaderValues(string expected)
        {
            var port = new AwaitedPort(new TestPort()).Value();

            using (var server =
                       new HttpMock(port,
                                    new FkWire(req =>
            {
                return
                (new Response.Of(200, "OK",
                                 new ManyOf <IKvp>(
                                     new KvpOf("Allow", "DELETE"),
                                     new KvpOf("Allow", "GET"),
                                     new KvpOf("Allow", "POST"),
                                     new KvpOf("Allow", "PUT")
                                     )
                                 ));
            })
                                    ).Value()
                   )
            {
                Assert.Contains(
                    expected,
                    new Header.Of(
                        new AspNetCoreWire(
                            new AspNetCoreClients(),
                            new TimeSpan(0, 1, 0)
                            ).Response(
                            new Get(
                                new Scheme("http"),
                                new Host("localhost"),
                                new Port(server.Port)
                                )
                            ),
                        "Allow"
                        )
                    );
            }
        }
        public void ReturnsHeaders()
        {
            var port = new AwaitedPort(new TestPort()).Value();

            using (var server =
                       new HttpMock(port,
                                    new FkWire(req =>
            {
                return
                (new Response.Of(200, "OK",
                                 new ManyOf <IKvp>(
                                     new KvpOf("Allow", "GET")
                                     )
                                 ));
            })
                                    ).Value()
                   )
            {
                Assert.Equal(
                    "GET",
                    new FirstOf <string>(
                        new Header.Of(
                            new AspNetCoreWire(
                                new AspNetCoreClients(),
                                new TimeSpan(0, 1, 0)
                                ).Response(
                                new Get(
                                    new Scheme("http"),
                                    new Host("localhost"),
                                    new Port(server.Port)
                                    )
                                ),
                            "Allow"
                            )
                        ).Value()
                    );
            }
        }
Ejemplo n.º 16
0
        public void TransmitsLongText()
        {
            var port = new AwaitedPort(new TestPort()).Value();

            using (var server =
                       new HttpMock(port,
                                    new FkWire(req =>
                                               new Response.Of(
                                                   new Status(200),
                                                   new Reason("OK"),
                                                   new TextBody(
                                                       new TextOf(
                                                           new ResourceOf(
                                                               "Assets/lorem-ipsum.txt",
                                                               this.GetType().Assembly
                                                               )
                                                           )
                                                       )
                                                   )
                                               )
                                    ).Value()
                   )
            {
                Assert.Equal(
                    3499,
                    new TextOf(
                        new Body.Of(
                            new AspNetCoreWire(
                                new AspNetCoreClients()
                                ).Response(
                                new Get($"http://localhost:{port}/")
                                )
                            )
                        ).AsString().Replace("\r", "").Replace("\n", "").Length
                    );
            }
        }
        public void SendsMultipleHeaderValues(string expected)
        {
            var port = new AwaitedPort(new TestPort()).Value();
            IEnumerable <string> headers = new ManyOf <string>();

            using (var server =
                       new HttpMock(port,
                                    new FkWire(req =>
            {
                headers = new Accept.Of(req);
                return(new Response.Of(200, "OK"));
            })
                                    ).Value()
                   )
            {
                new AspNetCoreWire(
                    new AspNetCoreClients(),
                    new TimeSpan(0, 1, 0)
                    ).Response(
                    new Get(
                        new Scheme("http"),
                        new Host("localhost"),
                        new Port(server.Port),
                        new Headers(
                            new KvpOf("Accept", "application/json"),
                            new KvpOf("Accept", "application/xml"),
                            new KvpOf("Accept", "text/plain")
                            )
                        )
                    ).Wait(30000);
            }
            Assert.Contains(
                expected,
                new Yaapii.Atoms.Text.Joined(", ", headers).AsString()
                );
        }
        public void FormBodyDoesNotAddDuplicateHeaders()
        {
            var contentTypeHeaders = new List <string>();
            var port = new AwaitedPort(new TestPort()).Value();
            var host = WebHost.CreateDefaultBuilder();

            host.UseUrls($"http://{Environment.MachineName.ToLower()}:{port}");
            host.ConfigureServices(svc =>
            {
                svc.AddSingleton <Action <HttpRequest> >(req => // required to instantiate HtAction from dependecy injection
                {
                    foreach (var header in req.Headers["Content-Type"])
                    {
                        contentTypeHeaders.AddRange(
                            header.Split(", ")
                            );
                    }
                });
                svc.AddMvc().AddApplicationPart(
                    typeof(HtAction).Assembly
                    ).AddControllersAsServices();
            });
            host.Configure(app =>
            {
                app.UseMvc();
            });

            using (var built = host.Build())
            {
                built.RunAsync();
                try
                {
                    new Verified(
                        new AspNetCoreWire(
                            new AspNetCoreClients(),
                            new TimeSpan(0, 1, 0)
                            ),
                        new ExpectedStatus(200)
                        ).Response(
                        new Post(
                            new Scheme("http"),
                            new Host("localhost"),
                            new Port(port),
                            new Path("action"),
                            new FormParams(
                                new MapOf(
                                    "key-1-name", "this is a test",
                                    "key-2-name", "test&+=xyz"
                                    )
                                )
                            )
                        );
                }
                finally
                {
                    built.StopAsync();
                }
            }

            Assert.Single(
                contentTypeHeaders
                );
        }