コード例 #1
0
        public async Task CanSetExtraHTTPHeaders()
        {
            using (var browser = new ChromiumWebBrowser("about:blank", automaticallyCreateBrowser: false))
            {
                await browser.CreateBrowserAsync();

                RequestWillBeSentEventArgs requestWillBeSentEventArgs = null;
                using (var devToolsClient = browser.GetDevToolsClient())
                {
                    var extraHeaders = new Headers();
                    extraHeaders.SetCommaSeparatedValues("TEST", "0");
                    extraHeaders.AppendCommaSeparatedValues("test", " 1 ", "\" 2 \"");
                    extraHeaders.AppendCommaSeparatedValues("Test", " 2,5 ");

                    await devToolsClient.Network.SetExtraHTTPHeadersAsync(extraHeaders);

                    devToolsClient.Network.RequestWillBeSent += (sender, args) =>
                    {
                        if (requestWillBeSentEventArgs == null)
                        {
                            requestWillBeSentEventArgs = args;
                        }
                    };

                    // enable events
                    await devToolsClient.Network.EnableAsync();

                    await browser.LoadUrlAsync("www.google.com");
                }

                Assert.NotNull(requestWillBeSentEventArgs);
                Assert.NotEmpty(requestWillBeSentEventArgs.RequestId);
                Assert.NotEqual(0, requestWillBeSentEventArgs.Timestamp);
                Assert.NotEqual(0, requestWillBeSentEventArgs.WallTime);
                Assert.NotNull(requestWillBeSentEventArgs.Request);
                Assert.True(requestWillBeSentEventArgs.Request.Headers.TryGetValues("TeSt", out var values));
                Assert.Collection(values,
                                  v => Assert.Equal("0", v),
                                  v => Assert.Equal("1", v),
                                  v => Assert.Equal(" 2 ", v),
                                  v => Assert.Equal(" 2,5 ", v)
                                  );
            }
        }
コード例 #2
0
        public async Task CanRegisterMultipleEventHandlers()
        {
            using (var browser = new ChromiumWebBrowser("about:blank", automaticallyCreateBrowser: false))
            {
                await browser.CreateBrowserAsync();

                using (var devToolsClient = browser.GetDevToolsClient())
                {
                    DevToolsEventArgs devToolsEventArgs = null;
                    EventHandler <DevToolsEventArgs> devToolsEventHandler = (sender, args) =>
                    {
                        if (devToolsEventArgs == null)
                        {
                            devToolsEventArgs = args;
                        }
                    };
                    devToolsClient.DevToolsEvent += devToolsEventHandler;

                    RequestWillBeSentEventArgs requestWillBeSentEventArgs1 = null;
                    EventHandler <RequestWillBeSentEventArgs> requestWillBeSentEventHandler1 = (sender, args) =>
                    {
                        if (requestWillBeSentEventArgs1 == null)
                        {
                            requestWillBeSentEventArgs1 = args;
                        }
                    };
                    devToolsClient.Network.RequestWillBeSent += requestWillBeSentEventHandler1;

                    RequestWillBeSentEventArgs requestWillBeSentEventArgs2 = null;
                    EventHandler <RequestWillBeSentEventArgs> requestWillBeSentEventHandler2 = (sender, args) =>
                    {
                        if (requestWillBeSentEventArgs2 == null)
                        {
                            requestWillBeSentEventArgs2 = args;
                        }
                    };
                    devToolsClient.Network.RequestWillBeSent += requestWillBeSentEventHandler2;

                    // enable events
                    await devToolsClient.Network.EnableAsync();

                    await browser.LoadUrlAsync("www.google.com");

                    Assert.NotNull(devToolsEventArgs);
                    Assert.NotNull(requestWillBeSentEventArgs1);
                    Assert.NotNull(requestWillBeSentEventArgs2);

                    Assert.Equal(requestWillBeSentEventArgs1.RequestId, requestWillBeSentEventArgs2.RequestId);

                    // remove second event handler
                    devToolsClient.Network.RequestWillBeSent -= requestWillBeSentEventHandler2;
                    devToolsEventArgs           = null;
                    requestWillBeSentEventArgs1 = null;
                    requestWillBeSentEventArgs2 = null;

                    await browser.LoadUrlAsync("www.google.com");

                    Assert.NotNull(devToolsEventArgs);
                    Assert.NotNull(requestWillBeSentEventArgs1);
                    Assert.Null(requestWillBeSentEventArgs2);
                }
            }
        }