public async Task SameETagShouldBeReturnedAgain()
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());

            HttpResponseMessage response1 = await server.CreateClient().GetAsync("http://localhost/SubFolder/extra.xml");

            HttpResponseMessage response2 = await server.CreateClient().GetAsync("http://localhost/SubFolder/extra.xml");

            Assert.Equal(response2.Headers.ETag, response1.Headers.ETag);
        }
        public async Task IfMatchShouldReturn412WhenNotListed()
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
            var        req    = new HttpRequestMessage(HttpMethod.Get, "http://localhost/SubFolder/extra.xml");

            req.Headers.Add("If-Match", "\"fake\"");
            HttpResponseMessage resp = await server.CreateClient().SendAsync(req);

            Assert.Equal(HttpStatusCode.PreconditionFailed, resp.StatusCode);
        }
        public async Task IfMatchShouldBeServedForAstrisk()
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
            var        req    = new HttpRequestMessage(HttpMethod.Get, "http://localhost/SubFolder/extra.xml");

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

            Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
        }
        public async Task ServerShouldReturnLastModified()
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());

            HttpResponseMessage response = await server.CreateClient().GetAsync("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);
        }
        public async Task SingleValidRangeShouldServeRequestedRangeNotSatisfiableEmptyFile(string range)
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
            var        req    = new HttpRequestMessage(HttpMethod.Get, "http://localhost/SubFolder/Empty.txt");

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

            Assert.Equal(HttpStatusCode.RequestedRangeNotSatisfiable, resp.StatusCode);
        }
        public async Task IfMatchShouldBeIgnoredForUnsupportedMethods(HttpMethod method)
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
            var        req    = new HttpRequestMessage(method, "http://localhost/SubFolder/extra.xml");

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

            Assert.Equal(HttpStatusCode.NotFound, resp.StatusCode);
        }
        public async Task InvalidIfUnmodifiedSinceDateFormatGivesNormalGet(HttpMethod method)
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());

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

            Assert.Equal(HttpStatusCode.OK, res.StatusCode);
        }
        [InlineData("1000-1001")] // Out of range
        public async Task HEADSingleNotSatisfiableRangeReturnsOk(string range)
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
            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);
        }
Exemple #9
0
        public async Task ServerShouldReturnETag()
        {
            using var host = await StaticFilesTestServer.Create(app => app.UseFileServer());

            using var server = host.GetTestServer();

            HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/SubFolder/extra.xml");

            Assert.NotNull(response.Headers.ETag);
            Assert.NotNull(response.Headers.ETag.Tag);
        }
        public async Task FutureIfUnmodifiedSinceDateFormatGivesNormalGet(HttpMethod method)
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());

            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);
        }
        public async Task IfMatchShouldBeServedWhenListed(HttpMethod method)
        {
            TestServer          server   = StaticFilesTestServer.Create(app => app.UseFileServer());
            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 IfNoneMatchShouldBeIgnoredForNonTwoHundredAnd304Responses(HttpMethod method)
        {
            TestServer          server = StaticFilesTestServer.Create(app => app.UseFileServer());
            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);
        }
        public async Task IfNoneMatchAllShouldReturn304ForMatching(HttpMethod method)
        {
            TestServer          server = StaticFilesTestServer.Create(app => app.UseFileServer());
            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);
        }
        [InlineData("2-2,0-0")] // SHOULD send in the requested order.
        public async Task HEADMultipleValidRangesShouldServeFullContent(string range)
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
            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());
        }
        public async Task IfModifiedSinceWithCurrentDateShouldReturn304()
        {
            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-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);
        }
Exemple #16
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);
        }
        [InlineData("-0")]        // Suffix range must be non-zero
        public async Task SingleNotSatisfiableRange(string range)
        {
            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.TryAddWithoutValidation("Range", "bytes=" + range);
            HttpResponseMessage resp = await server.CreateClient().SendAsync(req);

            Assert.Equal(HttpStatusCode.RequestedRangeNotSatisfiable, resp.StatusCode);
            Assert.Equal("bytes */62", resp.Content.Headers.ContentRange.ToString());
        }
Exemple #18
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);
        }
        public async Task SingleInvalidRangeIgnored(string range)
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
            var        req    = new HttpRequestMessage(HttpMethod.Get, "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("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", await resp.Content.ReadAsStringAsync());
        }
        public async Task HEADSingleValidRangeShouldReturnOk(string range)
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
            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.Null(resp.Content.Headers.ContentRange);
            Assert.Equal(62, resp.Content.Headers.ContentLength);
            Assert.Equal(string.Empty, await resp.Content.ReadAsStringAsync());
        }
        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);
            }
        }
Exemple #22
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
            }
        }
        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());
        }
        public async Task IfRangeWithOldEtagShouldServeFullContent()
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
            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());
        }
        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);
            }
        }
        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);
        }
        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());
        }
        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);
        }
        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);
        }
Exemple #30
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);
            }
        }