Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseDefaultFiles();
            app.UseDeveloperExceptionPage();

            var options = new PreCompressedStaticFileOptions();

            options.CompressionTypes.Add <Brotli>();
            options.CompressionTypes.Add <Gzip>();
            app.UsePreCompressedStaticFiles(options);
        }
        public async Task Should_use_the_file_provider_of_StaticFileOptions_if_it_is_provided()
        {
            // Arrange
            var fileInfo = Substitute.For <IFileInfo>();

            fileInfo.Exists.Returns(true);
            fileInfo.IsDirectory.Returns(false);
            fileInfo.Length.Returns(12);
            fileInfo.LastModified.Returns(new DateTimeOffset(2018, 12, 16, 13, 36, 0, new TimeSpan()));
            fileInfo.CreateReadStream().Returns(new MemoryStream(Encoding.UTF8.GetBytes("fileprovider")));

            var mockFileProvider = Substitute.For <IFileProvider>();

            mockFileProvider.GetFileInfo("/i_only_exist_in_mociFileProvider.html").Returns(fileInfo);

            var staticFileOptions = new PreCompressedStaticFileOptions()
            {
                FileProvider = mockFileProvider
            };

            var builder = new WebHostBuilder()
                          .Configure(app =>
            {
                app.UsePreCompressedStaticFiles(staticFileOptions);
                app.Use(next =>
                {
                    return(async context =>
                    {
                        // this test should never call the next middleware
                        // set status code to 999 to detect a test failure
                        context.Response.StatusCode = 999;
                    });
                });
            }).UseWebRoot(Path.Combine(Environment.CurrentDirectory, "wwwroot"));
            var server = new TestServer(builder);

            // Act
            var client = server.CreateClient();

            client.DefaultRequestHeaders.Add("Accept-Encoding", "br");
            var response = await client.GetAsync("/i_only_exist_in_mociFileProvider.html");

            var content = await response.Content.ReadAsStringAsync();

            // Assert
            response.StatusCode.Should().Be(200);
            content.Should().Be("fileprovider");
            response.Content.Headers.TryGetValues("Content-Type", out IEnumerable <string> contentTypeValues);
            contentTypeValues.Single().Should().Be("text/html");
        }
        public async Task Should_use_custom_compression_type_when_defined()
        {
            // Arrange

            var options = new PreCompressedStaticFileOptions();

            options.CompressionTypes.Add <TestCompressionType>();

            var builder = new WebHostBuilder()
                          .Configure(app =>
            {
                app.UsePreCompressedStaticFiles(options);
                app.Use(next =>
                {
                    return(async context =>
                    {
                        // this test should never call the next middleware
                        // set status code to 999 to detect a test failure
                        context.Response.StatusCode = 999;
                    });
                });
            }).UseWebRoot(Path.Combine(Environment.CurrentDirectory, "wwwroot"));
            var server = new TestServer(builder);

            // Act
            var client = server.CreateClient();

            client.DefaultRequestHeaders.Add("Accept-Encoding", "test");
            var response = await client.GetAsync("/i_also_exist_compressed.html");

            var content = await response.Content.ReadAsStringAsync();

            // Assert
            response.StatusCode.Should().Be(200);
            content.Should().Be("test");
            response.Content.Headers.TryGetValues("Content-Type", out IEnumerable <string> contentTypeValues);
            contentTypeValues.Single().Should().Be("text/html");
        }