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();
            }
        }
예제 #2
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));
                }
        }
예제 #3
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));
            }
        }
예제 #4
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);
                }
        }
예제 #5
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);
            }
        }
예제 #6
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));
            }
        }
예제 #7
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);
                }
        }
예제 #8
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));
                }
        }
예제 #9
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);
            }
        }
예제 #10
0
        string Download(WebHost host, string url)
        {
            using (var http = new HttpTestHarness(host))
            {
                httpContext = http.Context.Object;

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