예제 #1
0
        public async Task InvalidIfUnmodifiedSinceDateFormatGivesNormalGet(HttpMethod method)
        {
            using var host = await StaticFilesTestServer.Create(app => app.UseFileServer());

            using var server = host.GetTestServer();

            HttpResponseMessage res = await server
                                      .CreateRequest("/SubFolder/extra.xml")
                                      .AddHeader("If-Unmodified-Since", "bad-date")
                                      .SendAsync(method.Method);

            Assert.Equal(HttpStatusCode.OK, res.StatusCode);
        }
예제 #2
0
        public async Task ServerShouldReturnLastModified(HttpMethod method)
        {
            using var host = await StaticFilesTestServer.Create(app => app.UseFileServer());

            using var server = host.GetTestServer();

            HttpResponseMessage response = await server.CreateClient().SendAsync(
                new HttpRequestMessage(method, "http://localhost/SubFolder/extra.xml"));

            Assert.NotNull(response.Content.Headers.LastModified);
            // Verify that DateTimeOffset is UTC
            Assert.Equal(response.Content.Headers.LastModified.Value.Offset, TimeSpan.Zero);
        }
예제 #3
0
        public async Task FutureIfUnmodifiedSinceDateFormatGivesNormalGet(HttpMethod method)
        {
            using var host = await StaticFilesTestServer.Create(app => app.UseFileServer());

            using var server = host.GetTestServer();

            HttpResponseMessage res = await server
                                      .CreateRequest("/SubFolder/extra.xml")
                                      .And(req => req.Headers.IfUnmodifiedSince = DateTimeOffset.Now.AddYears(1))
                                      .SendAsync(method.Method);

            Assert.Equal(HttpStatusCode.OK, res.StatusCode);
        }
예제 #4
0
        public async Task NullArguments()
        {
            // No exception, default provided
            StaticFilesTestServer.Create(app => app.UseDefaultFiles(new DefaultFilesOptions {
                FileProvider = null
            }));

            // PathString(null) is OK.
            var server   = StaticFilesTestServer.Create(app => app.UseDefaultFiles((string)null));
            var response = await server.CreateClient().GetAsync("/");

            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }
예제 #5
0
        public async Task IfMatchShouldBeServedWhenListed(HttpMethod method)
        {
            using var host = await StaticFilesTestServer.Create(app => app.UseFileServer());

            using var server = host.GetTestServer();
            HttpResponseMessage original = await server.CreateClient().GetAsync("http://localhost/SubFolder/extra.xml");

            var req = new HttpRequestMessage(method, "http://localhost/SubFolder/extra.xml");

            req.Headers.Add("If-Match", original.Headers.ETag.ToString());
            HttpResponseMessage resp = await server.CreateClient().SendAsync(req);

            Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
        }
        public async Task NoMatch_PassesThrough(string baseUrl, string baseDir, string requestUrl)
        {
            using (var fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), baseDir)))
            {
                var server = StaticFilesTestServer.Create(app => app.UseStaticFiles(new StaticFileOptions
                {
                    RequestPath  = new PathString(baseUrl),
                    FileProvider = fileProvider
                }));
                var response = await server.CreateRequest(requestUrl).GetAsync();

                Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
            }
        }
예제 #7
0
        public async Task HEADIfRangeWithOldEtagShouldServeFullContent()
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
            var        req    = new HttpRequestMessage(HttpMethod.Head, "http://localhost/SubFolder/ranges.txt");

            req.Headers.Add("If-Range", "\"OldEtag\"");
            req.Headers.Add("Range", "bytes=0-10");
            HttpResponseMessage resp = await server.CreateClient().SendAsync(req);

            Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
            Assert.Null(resp.Content.Headers.ContentRange);
            Assert.Equal(62, resp.Content.Headers.ContentLength);
            Assert.Equal(string.Empty, await resp.Content.ReadAsStringAsync());
        }
예제 #8
0
        public async Task IfNoneMatchAllShouldReturn304ForMatching(HttpMethod method)
        {
            using var host = await StaticFilesTestServer.Create(app => app.UseFileServer());

            using var server = host.GetTestServer();
            HttpResponseMessage resp1 = await server.CreateClient().GetAsync("http://localhost/SubFolder/extra.xml");

            var req2 = new HttpRequestMessage(method, "http://localhost/SubFolder/extra.xml");

            req2.Headers.Add("If-None-Match", "*");
            HttpResponseMessage resp2 = await server.CreateClient().SendAsync(req2);

            Assert.Equal(HttpStatusCode.NotModified, resp2.StatusCode);
        }
예제 #9
0
        public async Task MultipleValidRangesShouldServeFullContent(string ranges)
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
            var        req    = new HttpRequestMessage(HttpMethod.Get, "http://localhost/SubFolder/ranges.txt");

            req.Headers.Add("Range", "bytes=" + ranges);
            HttpResponseMessage resp = await server.CreateClient().SendAsync(req);

            Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
            Assert.Equal("text/plain", resp.Content.Headers.ContentType.ToString());
            Assert.Null(resp.Content.Headers.ContentRange);
            Assert.Equal(62, resp.Content.Headers.ContentLength);
            Assert.Equal("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", await resp.Content.ReadAsStringAsync());
        }
예제 #10
0
        private async Task PostDirectory_PassesThrough(string baseUrl, string baseDir, string requestUrl)
        {
            using (var fileProvider = new PhysicalFileProvider(Path.Combine(AppContext.BaseDirectory, baseDir)))
            {
                var server = StaticFilesTestServer.Create(app => app.UseDefaultFiles(new DefaultFilesOptions
                {
                    RequestPath  = new PathString(baseUrl),
                    FileProvider = fileProvider
                }));
                var response = await server.CreateRequest(requestUrl).GetAsync();

                Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); // Passed through
            }
        }
예제 #11
0
        public async Task IfNoneMatchShouldBeIgnoredForNonTwoHundredAnd304Responses(HttpMethod method)
        {
            using var host = await StaticFilesTestServer.Create(app => app.UseFileServer());

            using var server = host.GetTestServer();
            HttpResponseMessage resp1 = await server.CreateClient().GetAsync("http://localhost/SubFolder/extra.xml");

            var req2 = new HttpRequestMessage(method, "http://localhost/SubFolder/extra.xml");

            req2.Headers.Add("If-None-Match", resp1.Headers.ETag.ToString());
            HttpResponseMessage resp2 = await server.CreateClient().SendAsync(req2);

            Assert.Equal(HttpStatusCode.NotFound, resp2.StatusCode);
        }
예제 #12
0
        public async Task SingleValidRangeShouldServePartialContent(string range, string expectedRange, int length, string expectedData)
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
            var        req    = new HttpRequestMessage(HttpMethod.Get, "http://localhost/SubFolder/ranges.txt");

            req.Headers.Add("Range", "bytes=" + range);
            HttpResponseMessage resp = await server.CreateClient().SendAsync(req);

            Assert.Equal(HttpStatusCode.PartialContent, resp.StatusCode);
            Assert.NotNull(resp.Content.Headers.ContentRange);
            Assert.Equal("bytes " + expectedRange + "/62", resp.Content.Headers.ContentRange.ToString());
            Assert.Equal(length, resp.Content.Headers.ContentLength);
            Assert.Equal(expectedData, await resp.Content.ReadAsStringAsync());
        }
예제 #13
0
        [InlineData("2-2,0-0")] // SHOULD send in the requested order.
        public async Task HEADMultipleValidRangesShouldServeFullContent(string range)
        {
            using var host = await StaticFilesTestServer.Create(app => app.UseFileServer());

            using var server = host.GetTestServer();
            var req = new HttpRequestMessage(HttpMethod.Head, "http://localhost/SubFolder/ranges.txt");

            req.Headers.Add("Range", "bytes=" + range);
            HttpResponseMessage resp = await server.CreateClient().SendAsync(req);

            Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
            Assert.Equal("text/plain", resp.Content.Headers.ContentType.ToString());
            Assert.Equal(string.Empty, await resp.Content.ReadAsStringAsync());
        }
        private async Task PassesThrough(string method, string baseUrl, string baseDir, string requestUrl)
        {
            using (var fileProvider = new PhysicalFileProvider(Path.Combine(AppContext.BaseDirectory, baseDir)))
            {
                var server = StaticFilesTestServer.Create(app => app.UseStaticFiles(new StaticFileOptions
                {
                    RequestPath  = new PathString(baseUrl),
                    FileProvider = fileProvider
                }));
                var response = await server.CreateRequest(requestUrl).SendAsync(method);

                Assert.Null(response.Content.Headers.LastModified);
                Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
            }
        }
예제 #15
0
        public async Task HEADSingleInvalidRangeIgnored(string range)
        {
            using var host = await StaticFilesTestServer.Create(app => app.UseFileServer());

            using var server = host.GetTestServer();
            var req = new HttpRequestMessage(HttpMethod.Head, "http://localhost/SubFolder/ranges.txt");

            req.Headers.TryAddWithoutValidation("Range", "bytes=" + range);
            HttpResponseMessage resp = await server.CreateClient().SendAsync(req);

            Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
            Assert.Null(resp.Content.Headers.ContentRange);
            Assert.Equal(62, resp.Content.Headers.ContentLength);
            Assert.Equal(string.Empty, await resp.Content.ReadAsStringAsync());
        }
예제 #16
0
        public async Task IfModifiedSinceWithCurrentDateShouldReturn304()
        {
            using var host = await StaticFilesTestServer.Create(app => app.UseFileServer());

            using var server = host.GetTestServer();
            HttpResponseMessage original = await server.CreateClient().GetAsync("http://localhost/SubFolder/ranges.txt");

            var req = new HttpRequestMessage(HttpMethod.Get, "http://localhost/SubFolder/ranges.txt");

            req.Headers.Add("If-Modified-Since", original.Content.Headers.LastModified.Value.ToString("r"));
            req.Headers.Add("Range", "bytes=0-10");
            HttpResponseMessage resp = await server.CreateClient().SendAsync(req);

            Assert.Equal(HttpStatusCode.NotModified, resp.StatusCode);
        }
예제 #17
0
        public async Task IfUnmodifiedSinceDateLessThanLastModifiedShouldReturn412(HttpMethod method)
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());

            HttpResponseMessage res1 = await server
                                       .CreateRequest("/SubFolder/extra.xml")
                                       .SendAsync(method.Method);

            HttpResponseMessage res2 = await server
                                       .CreateRequest("/SubFolder/extra.xml")
                                       .And(req => req.Headers.IfUnmodifiedSince = DateTimeOffset.MinValue)
                                       .SendAsync(method.Method);

            Assert.Equal(HttpStatusCode.PreconditionFailed, res2.StatusCode);
        }
예제 #18
0
        public async Task MatchingBothConditionsReturnsNotModified(HttpMethod method)
        {
            TestServer          server = StaticFilesTestServer.Create(app => app.UseFileServer());
            HttpResponseMessage resp1  = await server
                                         .CreateRequest("/SubFolder/extra.xml")
                                         .SendAsync(method.Method);

            HttpResponseMessage resp2 = await server
                                        .CreateRequest("/SubFolder/extra.xml")
                                        .AddHeader("If-None-Match", resp1.Headers.ETag.ToString())
                                        .And(req => req.Headers.IfModifiedSince = resp1.Content.Headers.LastModified)
                                        .SendAsync(method.Method);

            Assert.Equal(HttpStatusCode.NotModified, resp2.StatusCode);
        }
예제 #19
0
        public async Task IfModifiedSinceDateLessThanLastModifiedShouldReturn200()
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());

            HttpResponseMessage res1 = await server
                                       .CreateRequest("/SubFolder/extra.xml")
                                       .GetAsync();

            HttpResponseMessage res2 = await server
                                       .CreateRequest("/SubFolder/extra.xml")
                                       .And(req => req.Headers.IfModifiedSince = DateTimeOffset.MinValue)
                                       .GetAsync();

            Assert.Equal(HttpStatusCode.OK, res2.StatusCode);
        }
예제 #20
0
        public async Task HEADIfRangeWithOldDateShouldServeFullContent()
        {
            TestServer          server   = StaticFilesTestServer.Create(app => app.UseFileServer());
            HttpResponseMessage original = await server.CreateClient().GetAsync("http://localhost/SubFolder/ranges.txt");

            var req = new HttpRequestMessage(HttpMethod.Head, "http://localhost/SubFolder/ranges.txt");

            req.Headers.Add("If-Range", original.Content.Headers.LastModified.Value.Subtract(TimeSpan.FromDays(1)).ToString("r"));
            req.Headers.Add("Range", "bytes=0-10");
            HttpResponseMessage resp = await server.CreateClient().SendAsync(req);

            Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
            Assert.Null(resp.Content.Headers.ContentRange);
            Assert.Equal(62, resp.Content.Headers.ContentLength);
            Assert.Equal(string.Empty, await resp.Content.ReadAsStringAsync());
        }
        private async Task NearMatch_RedirectAddSlash(string baseUrl, string baseDir, string requestUrl, string queryString)
        {
            using (var fileProvider = new PhysicalFileProvider(Path.Combine(AppContext.BaseDirectory, baseDir)))
            {
                var server = StaticFilesTestServer.Create(app => app.UseDefaultFiles(new DefaultFilesOptions
                {
                    RequestPath  = new PathString(baseUrl),
                    FileProvider = fileProvider
                }));
                var response = await server.CreateRequest(requestUrl + queryString).GetAsync();

                Assert.Equal(HttpStatusCode.Moved, response.StatusCode);
                Assert.Equal(requestUrl + "/" + queryString, response.Headers.GetValues("Location").FirstOrDefault());
                Assert.Empty((await response.Content.ReadAsByteArrayAsync()));
            }
        }
예제 #22
0
        public async Task IfRangeWithOldEtagShouldServeFullContent()
        {
            using var host = await StaticFilesTestServer.Create(app => app.UseFileServer());

            using var server = host.GetTestServer();
            var req = new HttpRequestMessage(HttpMethod.Get, "http://localhost/SubFolder/ranges.txt");

            req.Headers.Add("If-Range", "\"OldEtag\"");
            req.Headers.Add("Range", "bytes=0-10");
            HttpResponseMessage resp = await server.CreateClient().SendAsync(req);

            Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
            Assert.Null(resp.Content.Headers.ContentRange);
            Assert.Equal(62, resp.Content.Headers.ContentLength);
            Assert.Equal("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", await resp.Content.ReadAsStringAsync());
        }
예제 #23
0
        private async Task NoMatch_PassesThrough(string baseUrl, string baseDir, string requestUrl)
        {
            using (var fileProvider = new PhysicalFileProvider(Path.Combine(AppContext.BaseDirectory, baseDir)))
            {
                var server = StaticFilesTestServer.Create(
                    app => app.UseDirectoryBrowser(new DirectoryBrowserOptions
                {
                    RequestPath  = new PathString(baseUrl),
                    FileProvider = fileProvider
                }),
                    services => services.AddDirectoryBrowser());
                var response = await server.CreateRequest(requestUrl).GetAsync();

                Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
            }
        }
예제 #24
0
        public async Task IfRangeWithCurrentDateShouldServePartialContent()
        {
            TestServer          server   = StaticFilesTestServer.Create(app => app.UseFileServer());
            HttpResponseMessage original = await server.CreateClient().GetAsync("http://localhost/SubFolder/ranges.txt");

            var req = new HttpRequestMessage(HttpMethod.Get, "http://localhost/SubFolder/ranges.txt");

            req.Headers.Add("If-Range", original.Content.Headers.LastModified.Value.ToString("r"));
            req.Headers.Add("Range", "bytes=0-10");
            HttpResponseMessage resp = await server.CreateClient().SendAsync(req);

            Assert.Equal(HttpStatusCode.PartialContent, resp.StatusCode);
            Assert.Equal("bytes 0-10/62", resp.Content.Headers.ContentRange.ToString());
            Assert.Equal(11, resp.Content.Headers.ContentLength);
            Assert.Equal("0123456789a", await resp.Content.ReadAsStringAsync());
        }
예제 #25
0
        public async Task IfModifiedSinceDateGreaterThanLastModifiedShouldReturn304(HttpMethod method)
        {
            using var host = await StaticFilesTestServer.Create(app => app.UseFileServer());

            using var server = host.GetTestServer();

            HttpResponseMessage res1 = await server
                                       .CreateRequest("/SubFolder/extra.xml")
                                       .SendAsync(method.Method);

            HttpResponseMessage res2 = await server
                                       .CreateRequest("/SubFolder/extra.xml")
                                       .And(req => req.Headers.IfModifiedSince = DateTimeOffset.Now)
                                       .SendAsync(method.Method);

            Assert.Equal(HttpStatusCode.NotModified, res2.StatusCode);
        }
        public async Task FoundFile_LastModifiedTrimsSeconds()
        {
            using (var fileProvider = new PhysicalFileProvider(AppContext.BaseDirectory))
            {
                var server = StaticFilesTestServer.Create(app => app.UseStaticFiles(new StaticFileOptions
                {
                    FileProvider = fileProvider
                }));
                var fileInfo = fileProvider.GetFileInfo("TestDocument.txt");
                var response = await server.CreateRequest("TestDocument.txt").GetAsync();

                var last    = fileInfo.LastModified;
                var trimmed = new DateTimeOffset(last.Year, last.Month, last.Day, last.Hour, last.Minute, last.Second, last.Offset).ToUniversalTime();

                Assert.Equal(response.Content.Headers.LastModified.Value, trimmed);
            }
        }
        public async Task FoundFile_Served(string baseUrl, string baseDir, string requestUrl)
        {
            using (var fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), baseDir)))
            {
                var server = StaticFilesTestServer.Create(app => app.UseStaticFiles(new StaticFileOptions
                {
                    RequestPath  = new PathString(baseUrl),
                    FileProvider = fileProvider
                }));
                var response = await server.CreateRequest(requestUrl).GetAsync();

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal("text/plain", response.Content.Headers.ContentType.ToString());
                Assert.True(response.Content.Headers.ContentLength > 0);
                Assert.Equal(response.Content.Headers.ContentLength, (await response.Content.ReadAsByteArrayAsync()).Length);
            }
        }
        public async Task HeadFile_HeadersButNotBodyServed(string baseUrl, string baseDir, string requestUrl)
        {
            using (var fileProvider = new PhysicalFileProvider(Path.Combine(AppContext.BaseDirectory, baseDir)))
            {
                var server = StaticFilesTestServer.Create(app => app.UseStaticFiles(new StaticFileOptions
                {
                    RequestPath  = new PathString(baseUrl),
                    FileProvider = fileProvider
                }));
                var fileInfo = fileProvider.GetFileInfo(Path.GetFileName(requestUrl));
                var response = await server.CreateRequest(requestUrl).SendAsync("HEAD");

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal("text/plain", response.Content.Headers.ContentType.ToString());
                Assert.True(response.Content.Headers.ContentLength == fileInfo.Length);
                Assert.Empty((await response.Content.ReadAsByteArrayAsync()));
            }
        }
예제 #29
0
        private async Task FoundDirectory_Served(string baseUrl, string baseDir, string requestUrl)
        {
            using (var fileProvider = new PhysicalFileProvider(Path.Combine(AppContext.BaseDirectory, baseDir)))
            {
                var server = StaticFilesTestServer.Create(
                    app => app.UseDirectoryBrowser(new DirectoryBrowserOptions
                {
                    RequestPath  = new PathString(baseUrl),
                    FileProvider = fileProvider
                }),
                    services => services.AddDirectoryBrowser());
                var response = await server.CreateRequest(requestUrl).GetAsync();

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal("text/html; charset=utf-8", response.Content.Headers.ContentType.ToString());
                Assert.True(response.Content.Headers.ContentLength > 0);
                Assert.Equal(response.Content.Headers.ContentLength, (await response.Content.ReadAsByteArrayAsync()).Length);
            }
        }
예제 #30
0
        public async Task HEADIfRangeWithCurrentEtagShouldReturn200Ok()
        {
            using var host = await StaticFilesTestServer.Create(app => app.UseFileServer());

            using var server = host.GetTestServer();
            HttpResponseMessage original = await server.CreateClient().GetAsync("http://localhost/SubFolder/ranges.txt");

            var req = new HttpRequestMessage(HttpMethod.Head, "http://localhost/SubFolder/ranges.txt");

            req.Headers.Add("If-Range", original.Headers.ETag.ToString());
            req.Headers.Add("Range", "bytes=0-10");
            HttpResponseMessage resp = await server.CreateClient().SendAsync(req);

            Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
            Assert.Equal(original.Headers.ETag, resp.Headers.ETag);
            Assert.Null(resp.Content.Headers.ContentRange);
            Assert.Equal(62, resp.Content.Headers.ContentLength);
            Assert.Equal(string.Empty, await resp.Content.ReadAsStringAsync());
        }