Ejemplo n.º 1
0
        public async Task IfMatchShouldReturn412WhenNotListed(HttpMethod method)
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
            var        req    = new HttpRequestMessage(method, "http://localhost/SubFolder/extra.xml");

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

            Assert.Equal(HttpStatusCode.PreconditionFailed, resp.StatusCode);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        public async Task ServerShouldReturnLastModified(HttpMethod method)
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());

            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);
        }
Ejemplo n.º 8
0
        [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);
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        [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());
        }
Ejemplo n.º 12
0
        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);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
0
        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());
        }
Ejemplo n.º 15
0
        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);
        }
Ejemplo n.º 16
0
        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());
        }
Ejemplo n.º 17
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
            }
        }
Ejemplo n.º 18
0
        public async Task SingleValidRangeShouldServePartialContentSingleByteFile(string range, string expectedRange, int length, string expectedData)
        {
            TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
            var        req    = new HttpRequestMessage(HttpMethod.Get, "http://localhost/SubFolder/SingleByte.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 + "/1", resp.Content.Headers.ContentRange.ToString());
            Assert.Equal(length, resp.Content.Headers.ContentLength);
            Assert.Equal(expectedData, await resp.Content.ReadAsStringAsync());
        }
Ejemplo n.º 19
0
        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 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);
     }
 }
Ejemplo n.º 21
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);
        }
Ejemplo n.º 22
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);
        }
Ejemplo n.º 23
0
        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);
            }
        }
Ejemplo n.º 24
0
        public async Task IfModifiedSinceWithPastDateShouldServePartialContent()
        {
            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.AddHours(-1).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());
        }
Ejemplo n.º 25
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());
        }
Ejemplo n.º 26
0
        public async Task HEADIfRangeWithCurrentEtagShouldReturn200Ok()
        {
            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.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());
        }
Ejemplo n.º 27
0
        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);
            }
        }
Ejemplo n.º 28
0
        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()));
            }
        }
        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.UseDirectoryBrowser(new DirectoryBrowserOptions
                    {
                        RequestPath = new PathString(baseUrl),
                        FileProvider = fileProvider
                    }),
                    services => services.AddDirectoryBrowser());

                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()));
            }
        }
Ejemplo n.º 30
0
        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);
                // the url in the header of `Location: /xxx/xxx` should be encoded
                var expectedURL = UriHelper.BuildRelative(baseUrl, requestUrl + "/", new QueryString(queryString), new FragmentString());
                var actualURL   = response.Headers.GetValues("Location").FirstOrDefault();
                Assert.Equal(expectedURL, actualURL);
                Assert.Empty((await response.Content.ReadAsByteArrayAsync()));
            }
        }