How to use ShouldWorkWithExtraHeadersFromBrowserContext method of Microsoft.Playwright.Tests.PageSetExtraHTTPHeadersTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.PageSetExtraHTTPHeadersTests.ShouldWorkWithExtraHeadersFromBrowserContext

Run Playwright-dotnet automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

PageSetExtraHttpHeadersTests.cs

Source: PageSetExtraHttpHeadersTests.cs Github

copy
1/*
2 * MIT License
3 *
4 * Copyright (c) 2020 Darío Kondratiuk
5 * Modifications copyright (c) Microsoft Corporation.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26using System.Collections.Generic;
27using System.Threading.Tasks;
28using NUnit.Framework;
29
30namespace Microsoft.Playwright.Tests
31{
32    public class PageSetExtraHTTPHeadersTests : PageTestEx
33    {
34        [PlaywrightTest("page-set-extra-http-headers.spec.ts", "should work")]
35        public async Task ShouldWork()
36        {
37            await Page.SetExtraHTTPHeadersAsync(new Dictionary<string, string>
38            {
39                ["Foo"] = "Bar"
40            });
41
42            var headerTask = Server.WaitForRequest("/empty.html", request => request.Headers["Foo"]);
43            await TaskUtils.WhenAll(Page.GotoAsync(Server.EmptyPage), headerTask);
44
45            Assert.AreEqual("Bar", headerTask.Result);
46        }
47
48        [PlaywrightTest("page-set-extra-http-headers.spec.ts", "should work with redirects")]
49        public async Task ShouldWorkWithRedirects()
50        {
51            Server.SetRedirect("/foo.html", "/empty.html");
52            await Page.SetExtraHTTPHeadersAsync(new Dictionary<string, string>
53            {
54                ["Foo"] = "Bar"
55            });
56
57            var headerTask = Server.WaitForRequest("/empty.html", request => request.Headers["Foo"]);
58            await TaskUtils.WhenAll(Page.GotoAsync(Server.Prefix + "/foo.html"), headerTask);
59
60            Assert.AreEqual("Bar", headerTask.Result);
61        }
62
63        [PlaywrightTest("page-set-extra-http-headers.spec.ts", "should work with extra headers from browser context")]
64        public async Task ShouldWorkWithExtraHeadersFromBrowserContext()
65        {
66            await using var context = await Browser.NewContextAsync();
67            await context.SetExtraHTTPHeadersAsync(new Dictionary<string, string>
68            {
69                ["Foo"] = "Bar"
70            });
71            var page = await context.NewPageAsync();
72
73            var headerTask = Server.WaitForRequest("/empty.html", request => request.Headers["Foo"]);
74            await TaskUtils.WhenAll(page.GotoAsync(Server.EmptyPage), headerTask);
75
76            Assert.AreEqual("Bar", headerTask.Result);
77        }
78
79        [PlaywrightTest("page-set-extra-http-headers.spec.ts", "should override extra headers from browser context")]
80        public async Task ShouldOverrideExtraHeadersFromBrowserContext()
81        {
82            await using var context = await Browser.NewContextAsync();
83            await context.SetExtraHTTPHeadersAsync(new Dictionary<string, string>
84            {
85                ["fOo"] = "bAr",
86                ["baR"] = "foO",
87            });
88            var page = await context.NewPageAsync();
89
90            await page.SetExtraHTTPHeadersAsync(new Dictionary<string, string>
91            {
92                ["Foo"] = "Bar"
93            });
94
95            var headerTask = Server.WaitForRequest("/empty.html", request => (request.Headers["Foo"], request.Headers["baR"]));
96            await TaskUtils.WhenAll(page.GotoAsync(Server.EmptyPage), headerTask);
97
98            Assert.AreEqual("Bar", headerTask.Result.Item1);
99            Assert.AreEqual("foO", headerTask.Result.Item2);
100        }
101    }
102}
103
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger ShouldWorkWithExtraHeadersFromBrowserContext code on LambdaTest Cloud Grid

Execute automation tests with ShouldWorkWithExtraHeadersFromBrowserContext on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)