public async Task Should_work_with_api_receive_endpoint()
            {
                IRequestClient <Request, Response> client = HttpTestHarness.CreateRequestClient <Request, Response>(new Uri(HostAddress, "/api"));

                var request = new Request {
                    Value = "Hello"
                };


                Stopwatch timer;
                Response  result;

                for (var i = 0; i < 5; i++)
                {
                    timer = Stopwatch.StartNew();

                    result = await client.Request(request);

                    timer.Stop();

                    await Console.Out.WriteLineAsync($"Request complete: {timer.ElapsedMilliseconds}ms, Response = {result.ResponseValue}");
                }

                IRequestClient <Request, Response> rootClient = HttpTestHarness.CreateRequestClient <Request, Response>();

                timer  = Stopwatch.StartNew();
                result = await rootClient.Request(request);

                timer.Stop();

                await Console.Out.WriteLineAsync($"Request complete: {timer.ElapsedMilliseconds}ms, Response = {result.ResponseValue}");
            }
예제 #2
0
        public void GivenDebugMode_BundleAUrlShouldServeConcatenatedScripts()
        {
            using (var host = new TestableWebHost("assets", () => httpContext, isAspNetDebuggingEnabled: true))
            {
                host.AddBundleConfiguration(new BundleConfiguration(bundles =>
                    bundles.AddPerSubDirectory<ScriptBundle>("scripts")
                ));
                host.Initialize();

                string bundleUrl;
                using (var http = new HttpTestHarness(host))
                {
                    httpContext = http.Context.Object;
                    Bundles.Reference("scripts/bundle-a");
                    bundleUrl = Bundles.Url("scripts/bundle-a");
                }

                var separator = System.Environment.NewLine + ";" + System.Environment.NewLine;

                var expectedContent = File.ReadAllText("assets\\scripts\\bundle-a\\asset-2.js") + separator +
                                      File.ReadAllText("assets\\scripts\\bundle-a\\asset-1.js");

                Download(host, bundleUrl).ShouldEqual(expectedContent);
            }
        }
예제 #3
0
        public HttpTestFixture(HttpTestHarness harness)
            : base(harness)
        {
            HttpTestHarness = harness;

            HttpTestHarness.OnConfigureHttpBus             += ConfigureHttpBus;
            HttpTestHarness.OnConfigureHttpReceiveEndpoint += ConfigureHttpReceiveEndpoint;
        }
        string DownloadJavaScript(WebHost host, string url)
        {
            using (var http = new HttpTestHarness(host))
            {
                httpContext = http.Context.Object;

                http.Get(url);
                http.Response.VerifySet(r => r.ContentType = "text/javascript");
                return http.ResponseOutputStream.ReadToEnd();
            }
        }
예제 #5
0
        public void WhenRequestFileWithDotExtension_ThenRequestIsResolvedSuccessfully()
        {
            using (var temp = new TempDirectory())
                using (var http = new HttpTestHarness())
                {
                    var filename = Path.Combine(temp, "test.png");
                    var content  = new byte[] { 1, 2, 3 };
                    File.WriteAllBytes(filename, content);

                    http.MapRoute("{*path}", c => new RawFileRequestHandler(c, bundles));
                    http.Server.Setup(s => s.MapPath("~/test.png")).Returns(filename);

                    http.Get("~/test_hash.png");
                    http.Response.Verify(r => r.WriteFile(filename));
                }
        }
예제 #6
0
        public void WhenRequestFileWithDotExtension_ThenRequestIsResolvedSuccessfully()
        {
            using (var temp = new TempDirectory())
            using (var http = new HttpTestHarness())
            {
                var filename = Path.Combine(temp, "test.png");
                var content = new byte[] { 1, 2, 3 };
                File.WriteAllBytes(filename, content);

                http.MapRoute("{*path}", c => new RawFileRequestHandler(c));
                http.Server.Setup(s => s.MapPath("~/test.png")).Returns(filename);

                http.Get("~/test_hash.png");
                http.Response.Verify(r => r.WriteFile(filename));
            }
        }
예제 #7
0
        public void WhenRequestFileThatIsNoReferencedByAsset_ThenDoNotReturnFile()
        {
            using (var temp = new TempDirectory())
                using (var http = new HttpTestHarness())
                {
                    var filename = Path.Combine(temp, "protected.png");
                    var content  = new byte[] { 1, 2, 3 };
                    File.WriteAllBytes(filename, content);

                    http.MapRoute("{*path}", c => new RawFileRequestHandler(c, bundles));
                    http.Server.Setup(s => s.MapPath("~/protected.png")).Returns(filename);

                    http.Get("~/protected_hash.png");

                    http.Response.Verify(r => r.WriteFile(It.IsAny <string>()), Times.Never());
                    http.Response.VerifySet(r => r.StatusCode = 404);
                }
        }
예제 #8
0
        public void WhenRequestFileThatIsNoReferencedByAsset_ThenDoNotReturnFile()
        {
            using (var temp = new TempDirectory())
            using (var http = new HttpTestHarness())
            {
                var filename = Path.Combine(temp, "protected.png");
                var content = new byte[] { 1, 2, 3 };
                File.WriteAllBytes(filename, content);

                http.MapRoute("{*path}", c => new RawFileRequestHandler(c, bundles, null));
                http.Server.Setup(s => s.MapPath("~/protected.png")).Returns(filename);

                http.Get("~/protected_hash.png");

                http.Response.Verify(r => r.WriteFile(It.IsAny<string>()), Times.Never());
                http.Response.VerifySet(r => r.StatusCode = 404);
            }
        }
            public async Task Should_work_with_the_message_request_client_too()
            {
                IRequestClient <Request, Response> client = HttpTestHarness.CreateRequestClient <Request, Response>(HostAddress);

                var request = new Request {
                    Value = "Hello"
                };


                for (var i = 0; i < 5; i++)
                {
                    var timer = Stopwatch.StartNew();


                    var result = await client.Request(request);

                    timer.Stop();

                    await Console.Out.WriteLineAsync($"Request complete: {timer.ElapsedMilliseconds}ms, Response = {result.ResponseValue}");
                }
            }
예제 #10
0
        public void WhenRequestFile_ThenResponseETagHeaderIsSHA1OfContents()
        {
            using (var temp = new TempDirectory())
            using (var http = new HttpTestHarness())
            {
                var filename = Path.Combine(temp, "test.png");
                var content = new byte[] { 1, 2, 3 };
                File.WriteAllBytes(filename, content);

                http.MapRoute("{*path}", c => new RawFileRequestHandler(c));
                http.Server.Setup(s => s.MapPath("~/test.png")).Returns(filename);

                http.Get("~/test_hash.png");

                string expectedETag;
                using (var hasher = SHA1.Create())
                {
                    expectedETag = "\"" + Convert.ToBase64String(hasher.ComputeHash(content)) + "\"";
                }
                http.ResponseCache.Verify(c => c.SetETag(expectedETag));
            }
        }
예제 #11
0
        public void WhenRequestWithETagThatMatchesCurrent_ThenNotModifiedResponseReturned()
        {
            using (var temp = new TempDirectory())
                using (var http = new HttpTestHarness())
                {
                    var filename = Path.Combine(temp, "test.png");
                    var content  = new byte[] { 1, 2, 3 };
                    File.WriteAllBytes(filename, content);

                    http.MapRoute("{*path}", c => new RawFileRequestHandler(c, bundles));
                    http.Server.Setup(s => s.MapPath("~/test.png")).Returns(filename);

                    string eTag;
                    using (var hasher = SHA1.Create())
                    {
                        eTag = "\"" + Convert.ToBase64String(hasher.ComputeHash(content)) + "\"";
                    }

                    http.RequestHeaders["If-None-Match"] = eTag;
                    http.Get("~/test_hash.png");

                    http.Response.VerifySet(r => r.StatusCode = 304);
                }
        }
예제 #12
0
        public void WhenRequestFile_ThenResponseETagHeaderIsSHA1OfContents()
        {
            using (var temp = new TempDirectory())
                using (var http = new HttpTestHarness())
                {
                    var filename = Path.Combine(temp, "test.png");
                    var content  = new byte[] { 1, 2, 3 };
                    File.WriteAllBytes(filename, content);

                    http.MapRoute("{*path}", c => new RawFileRequestHandler(c, bundles));
                    http.Server.Setup(s => s.MapPath("~/test.png")).Returns(filename);


                    http.Get("~/test_hash.png");


                    string expectedETag;
                    using (var hasher = SHA1.Create())
                    {
                        expectedETag = "\"" + Convert.ToBase64String(hasher.ComputeHash(content)) + "\"";
                    }
                    http.ResponseCache.Verify(c => c.SetETag(expectedETag));
                }
        }
예제 #13
0
        public void WhenRequestWithETagThatMatchesCurrent_ThenNotModifiedResponseReturned()
        {
            using (var temp = new TempDirectory())
            using (var http = new HttpTestHarness())
            {
                var filename = Path.Combine(temp, "test.png");
                var content = new byte[] { 1, 2, 3 };
                File.WriteAllBytes(filename, content);

                http.MapRoute("{*path}", c => new RawFileRequestHandler(c));
                http.Server.Setup(s => s.MapPath("~/test.png")).Returns(filename);

                string eTag;
                using (var hasher = SHA1.Create())
                {
                    eTag = "\"" + Convert.ToBase64String(hasher.ComputeHash(content)) + "\"";
                }

                http.RequestHeaders["If-None-Match"] = eTag;
                http.Get("~/test_hash.png");

                http.Response.VerifySet(r => r.StatusCode = 304);
            }
        }
예제 #14
0
        string[] GetPageHtmlResourceUrls(WebHost host, params string[] references)
        {
            using (var http = new HttpTestHarness(host))
            {
                httpContext = http.Context.Object;

                foreach (var reference in references)
                {
                    Bundles.Reference(reference);
                }

                var htmlString =
                    "<html>" +
                    Bundles.RenderScripts().ToHtmlString() +
                    Bundles.RenderStylesheets().ToHtmlString() +
                    Bundles.RenderHtmlTemplates().ToHtmlString() +
                    "</html>";

                var html = XElement.Parse(htmlString);
                var scripts = html.Elements("script").Where((s => s.Attribute("src") != null)).Select(s => s.Attribute("src").Value);
                var links = html.Elements("link").Select(s => s.Attribute("href").Value);
                return links.Concat(scripts).ToArray();
            }
        }
예제 #15
0
        string Download(WebHost host, string url)
        {
            using (var http = new HttpTestHarness(host))
            {
                httpContext = http.Context.Object;

                http.Get(url);
                return http.ResponseOutputStream.ReadToEnd();
            }
        }