예제 #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IOptionsMonitor <TestHarnessOptions> optionsAccessor, IWebHostEnvironment env)
        {
            app.UseWebSockets();
            app.UseStaticFiles();

            TestHarnessOptions options = optionsAccessor.CurrentValue;

            Console.WriteLine($"Chrome from: '{options.ChromePath}'");
            Console.WriteLine($"Files from: '{options.AppPath}'");
            Console.WriteLine($"Using page : '{options.PagePath}'");

            var provider = new FileExtensionContentTypeProvider();

            provider.Mappings [".wasm"] = "application/wasm";

            app.UseStaticFiles(new StaticFileOptions {
                FileProvider          = new PhysicalFileProvider(options.AppPath),
                ServeUnknownFileTypes = true,                 //Cuz .wasm is not a known file type :cry:
                RequestPath           = "",
                ContentTypeProvider   = provider
            });

            var devToolsUrl = options.DevToolsUrl;
            var psi         = new ProcessStartInfo();

            psi.Arguments              = $"--headless --disable-gpu --remote-debugging-port={devToolsUrl.Port} http://{TestHarnessProxy.Endpoint.Authority}/{options.PagePath}";
            psi.UseShellExecute        = false;
            psi.FileName               = options.ChromePath;
            psi.RedirectStandardError  = true;
            psi.RedirectStandardOutput = true;

            app.UseRouter(router => {
                router.MapGet("launch-chrome-and-connect", async context => {
                    Console.WriteLine("New test request");
                    var client = new HttpClient();
                    await LaunchAndServe(psi, context, async(str) => {
                        string res = null;
                        var start  = DateTime.Now;

                        while (res == null)
                        {
                            // Unfortunately it does look like we have to wait
                            // for a bit after getting the response but before
                            // making the list request.  We get an empty result
                            // if we make the request too soon.
                            await Task.Delay(100);

                            res = await client.GetStringAsync(new Uri(new Uri(str), "/json/list"));
                            Console.WriteLine("res is {0}", res);

                            var elapsed = DateTime.Now - start;
                            if (res == null && elapsed.Milliseconds > 2000)
                            {
                                Console.WriteLine($"Unable to get DevTools /json/list response in {elapsed.Seconds} seconds, stopping");
                                return(null);
                            }
                        }

                        var obj = JArray.Parse(res);
                        if (obj == null || obj.Count < 1)
                        {
                            return(null);
                        }

                        var wsURl = obj[0]? ["webSocketDebuggerUrl"]?.Value <string> ();
                        Console.WriteLine(">>> {0}", wsURl);

                        return(wsURl);
                    });
                });
            });

            if (options.NodeApp != null)
            {
                Console.WriteLine($"Doing the nodejs: {options.NodeApp}");
                var nodeFullPath = Path.GetFullPath(options.NodeApp);
                Console.WriteLine(nodeFullPath);

                psi.Arguments = $"--inspect-brk=localhost:0 {nodeFullPath}";
                psi.FileName  = "node";

                app.UseRouter(router => {
                    //Inspector API for using chrome devtools directly
                    router.MapGet("json", SendNodeList);
                    router.MapGet("json/list", SendNodeList);
                    router.MapGet("json/version", SendNodeVersion);
                    router.MapGet("launch-done-and-connect", async context => {
                        await LaunchAndServe(psi, context, null);
                    });
                });
            }
        }
예제 #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IOptionsMonitor <TestHarnessOptions> optionsAccessor, IHostingEnvironment env)
        {
            app.UseWebSockets();
            app.UseStaticFiles();

            TestHarnessOptions options = optionsAccessor.CurrentValue;

            Console.WriteLine($"Chrome from: '{options.ChromePath}'");
            Console.WriteLine($"Files from: '{options.AppPath}'");
            Console.WriteLine($"Using page : '{options.PagePath}'");

            var provider = new FileExtensionContentTypeProvider();

            provider.Mappings [".wasm"] = "application/wasm";

            app.UseStaticFiles(new StaticFileOptions {
                FileProvider          = new PhysicalFileProvider(options.AppPath),
                ServeUnknownFileTypes = true,                 //Cuz .wasm is not a known file type :cry:
                RequestPath           = "",
                ContentTypeProvider   = provider
            });

            var devToolsUrl = options.DevToolsUrl;
            var psi         = new ProcessStartInfo();

            psi.Arguments              = $"--headless --disable-gpu --remote-debugging-port={devToolsUrl.Port} http://localhost:9300/{options.PagePath}";
            psi.UseShellExecute        = false;
            psi.FileName               = options.ChromePath;
            psi.RedirectStandardError  = true;
            psi.RedirectStandardOutput = true;

            app.UseRouter(router => {
                router.MapGet("launch-chrome-and-connect", async context => {
                    Console.WriteLine("New test request");
                    await LaunchAndServe(psi, context, str => {
                        //We wait for it as a signal that chrome finished launching
                        if (!str.StartsWith("DevTools listening on ", StringComparison.Ordinal))
                        {
                            return(null);
                        }

                        var client = new HttpClient();
                        var res    = client.GetStringAsync(new Uri(devToolsUrl, "/json/list")).Result;
                        Console.WriteLine("res is {0}", res);
                        if (res == null)
                        {
                            return(null);
                        }

                        var obj = JArray.Parse(res);
                        if (obj == null || obj.Count < 1)
                        {
                            return(null);
                        }

                        var wsURl = obj[0]? ["webSocketDebuggerUrl"]?.Value <string> ();
                        Console.WriteLine(">>> {0}", wsURl);

                        return(wsURl);
                    });
                });
            });

            if (options.NodeApp != null)
            {
                Console.WriteLine($"Doing the nodejs: {options.NodeApp}");
                var nodeFullPath = Path.GetFullPath(options.NodeApp);
                Console.WriteLine(nodeFullPath);

                psi.Arguments = $"--inspect-brk=localhost:0 {nodeFullPath}";
                psi.FileName  = "node";

                app.UseRouter(router => {
                    //Inspector API for using chrome devtools directly
                    router.MapGet("json", SendNodeList);
                    router.MapGet("json/list", SendNodeList);
                    router.MapGet("json/version", SendNodeVersion);
                    router.MapGet("launch-done-and-connect", async context => {
                        await LaunchAndServe(psi, context, str => {
                            if (str.StartsWith("Debugger listening on", StringComparison.Ordinal))
                            {
                                return(str.Substring(str.IndexOf("ws://", StringComparison.Ordinal)));
                            }
                            return(null);
                        });
                    });
                });
            }
        }