コード例 #1
0
        public async Task ItAppliesHeaders()
        {
            var headers = new Dictionary <string, string>
            {
                { "X-XSS-Protection", "1; mode=block" },
                { "X-Frame-Options", "SAMEORIGIN" },
            };

            var headerArguments = new string[]
            {
                "X-XSS-Protection: 1; mode=block",
                "x-frame-options: SAMEORIGIN",
            };

            var path = Path.Combine(AppContext.BaseDirectory, "TestAssets", "Mime");

            using (var ds = DotNetServe.Start(path,
                                              output: _output,
                                              headers: headerArguments))
            {
                var resp = await ds.Client.GetWithRetriesAsync("/file.js");

                foreach (var header in headers)
                {
                    var respHeader      = Assert.Single(resp.Headers, h => h.Key == header.Key);
                    var respHeaderValue = Assert.Single(respHeader.Value);
                    Assert.Equal(respHeaderValue, header.Value);
                }
            }
        }
コード例 #2
0
        public async Task ItDoesNotAllowOptionsRequestIfCORSIsNotEnabled()
        {
            using var ds = DotNetServe.Start();
            var result = await ds.Client.SendOptionsWithRetriesAsync(ds.Client.BaseAddress.AbsoluteUri, PrepareCORSHeaders(), output : _output);

            //Making sure we are getting 404 instead of 204 if CORS is not enabled
            Assert.Equal(System.Net.HttpStatusCode.NotFound, result.StatusCode);
        }
コード例 #3
0
ファイル: BrotliTests.cs プロジェクト: zyj0021/dotnet-serve
        public async Task ItCompressesOutput()
        {
            var path = Path.Combine(AppContext.BaseDirectory, "TestAssets", "Mime");

            using var ds = DotNetServe.Start(path, output: _output, useBrotli: true);
            ds.Client.DefaultRequestHeaders.Add("Accept-Encoding", "br, deflate");
            var resp = await ds.Client.GetWithRetriesAsync("file.js");

            Assert.Equal("br", resp.Content.Headers.ContentEncoding.First());
        }
コード例 #4
0
        public async Task ItAllowsOptionsRequestIfCORSIsEnabled()
        {
            using var ds = DotNetServe.Start(enableCors: true, output: _output);
            var result = await ds.Client.SendOptionsWithRetriesAsync(ds.Client.BaseAddress.AbsoluteUri, PrepareCORSHeaders(), output : _output);

            //Making sure we are getting 204 and not 404
            Assert.Equal(System.Net.HttpStatusCode.NoContent, result.StatusCode);

            Assert.Contains("*", result.Headers.First(x => x.Key == "Access-Control-Allow-Origin").Value);
            Assert.Contains("GET", result.Headers.First(x => x.Key == "Access-Control-Allow-Methods").Value);
        }
コード例 #5
0
        public async Task ItCanLoadSimpleRazorPage()
        {
            using (var serve = DotNetServe.Start(
                       directory: Path.Combine(AppContext.BaseDirectory, "TestAssets", "RazorSite"),
                       output: _output,
                       enableRazor: true))
            {
                var result = await serve.Client.GetStringWithRetriesAsync("/", output : _output);

                Assert.Contains("<li>Item 2</li>", result);
            }
        }
コード例 #6
0
        public async Task ItDoesNotServePfxFile()
        {
            var path = Path.Combine(AppContext.BaseDirectory, "TestAssets", "Https", "pfx");

            using var ds = DotNetServe.Start(path,
                                             certPassword: "******",
                                             output: _output,
                                             enableTls: true);
            var resp = await ds.Client.GetWithRetriesAsync("/cert.pfx");

            Assert.Equal(HttpStatusCode.Forbidden, resp.StatusCode);
        }
コード例 #7
0
        public async Task ItDoesNotServePemFiles(string keyFormat)
        {
            var path = Path.Combine(AppContext.BaseDirectory, "TestAssets", "Https", keyFormat);

            using var ds = DotNetServe.Start(path,
                                             output: _output,
                                             enableTls: true);
            var resp1 = await ds.Client.GetWithRetriesAsync("/cert.pem");

            Assert.Equal(HttpStatusCode.Forbidden, resp1.StatusCode);

            var resp2 = await ds.Client.GetWithRetriesAsync("/private.key");

            Assert.Equal(HttpStatusCode.Forbidden, resp2.StatusCode);
        }
コード例 #8
0
        public async Task ItAppliesMimeMappings(string file, string expectedContents, string expectedMime, params string[] mimeMap)
        {
            var path = Path.Combine(AppContext.BaseDirectory, "TestAssets", "Mime");

            using var ds = DotNetServe.Start(path, output: _output, mimeMap: mimeMap);
            var resp = await ds.Client.GetWithRetriesAsync(file);

            if (expectedMime == null)
            {
                Assert.Null(resp.Content.Headers.ContentType);
            }
            else
            {
                Assert.Equal(resp.Content.Headers.ContentType.MediaType, expectedMime);
            }
            var respTxt = await resp.Content.ReadAsStringAsync();

            Assert.Equal(respTxt, expectedContents);
        }
コード例 #9
0
        public static DotNetServe Start(
            string directory         = null,
            int?port                 = default,
            bool enableTls           = false,
            string certPassword      = null,
            string[] mimeMap         = null,
            string[] headers         = null,
            ITestOutputHelper output = null)
        {
            var psi = new ProcessStartInfo
            {
                FileName = DotNetExe.FullPathOrDefault(),
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                ArgumentList           =
                {
                    s_dotnetServe,
                    "--verbose",
                },
                WorkingDirectory = directory ?? AppContext.BaseDirectory,
            };

            if (directory != null)
            {
                psi.ArgumentList.Add("-d");
                psi.ArgumentList.Add(directory);
            }

            if (!port.HasValue)
            {
                port = Interlocked.Increment(ref s_nextPort);
            }

            psi.ArgumentList.Add("-p");
            psi.ArgumentList.Add(port.ToString());

            if (enableTls)
            {
                psi.ArgumentList.Add("--tls");
            }

            if (certPassword != null)
            {
                psi.ArgumentList.Add("--pfx-pwd");
                psi.ArgumentList.Add(certPassword);
            }

            if (mimeMap != null)
            {
                foreach (var mapping in mimeMap)
                {
                    psi.ArgumentList.Add("-m");
                    psi.ArgumentList.Add(mapping);
                }
            }

            if (headers != null)
            {
                foreach (var header in headers)
                {
                    psi.ArgumentList.Add("-h");
                    psi.ArgumentList.Add(header);
                }
            }

            var process = new Process
            {
                EnableRaisingEvents = true,
                StartInfo           = psi,
            };

            var serve = new DotNetServe(process, port.Value, enableTls, output);

            serve.Start();
            return(serve);
        }