summaryrefslogtreecommitdiff
path: root/src/WixToolsetTest.BurnE2E/WebServer/CoreOwinWebServer.cs
blob: 89825813a88a45a99aa70d417dbe44c697ba7f86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolsetTest.BurnE2E
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.FileProviders;
    using Microsoft.Extensions.FileProviders.Physical;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Primitives;

    public class CoreOwinWebServer : IWebServer, IFileProvider
    {
        private Dictionary<string, string> PhysicalPathsByRelativeUrl { get; } = new Dictionary<string, string>();

        private IHost WebHost { get; set; }

        public void AddFiles(Dictionary<string, string> physicalPathsByRelativeUrl)
        {
            foreach (var kvp in physicalPathsByRelativeUrl)
            {
                this.PhysicalPathsByRelativeUrl.Add(kvp.Key, kvp.Value);
            }
        }

        public void Start()
        {
            this.WebHost = Host.CreateDefaultBuilder()
                               .ConfigureWebHostDefaults(webBuilder =>
                               {
                                   // Use localhost instead of * to avoid firewall issues.
                                   webBuilder.UseUrls("http://localhost:9999");
                                   webBuilder.Configure(appBuilder =>
                                   {
                                       appBuilder.UseStaticFiles(new StaticFileOptions
                                       {
                                           FileProvider = this,
                                           RequestPath = "/e2e",
                                           ServeUnknownFileTypes = true,
                                       });
                                   });
                               })
                               .Build();
            this.WebHost.Start();
        }

        public void Dispose()
        {
            var waitTime = TimeSpan.FromSeconds(5);
            this.WebHost?.StopAsync(waitTime).Wait(waitTime);
        }

        public IDirectoryContents GetDirectoryContents(string subpath) => throw new NotImplementedException();

        public IFileInfo GetFileInfo(string subpath)
        {
            if (this.PhysicalPathsByRelativeUrl.TryGetValue(subpath, out var filepath))
            {
                return new PhysicalFileInfo(new FileInfo(filepath));
            }

            return new NotFoundFileInfo(subpath);
        }

        public IChangeToken Watch(string filter) => throw new NotImplementedException();
    }
}