|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using System.Net; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using PuppeteerSharp.Tests.Attributes; |
| 6 | +using PuppeteerSharp.Nunit; |
| 7 | +using NUnit.Framework; |
| 8 | + |
| 9 | +namespace PuppeteerSharp.Tests.RequestInterceptionExperimentalTests; |
| 10 | + |
| 11 | +public class RequestRespondTests : PuppeteerPageBaseTest |
| 12 | +{ |
| 13 | + [PuppeteerTest("requestinterception-experimental.spec.ts", "Request.respond", "should work")] |
| 14 | + [Skip(SkipAttribute.Targets.Firefox)] |
| 15 | + public async Task ShouldWork() |
| 16 | + { |
| 17 | + await Page.SetRequestInterceptionAsync(true); |
| 18 | + Page.AddRequestInterceptor(request => request.RespondAsync(new ResponseData |
| 19 | + { |
| 20 | + Status = HttpStatusCode.Created, |
| 21 | + Headers = new Dictionary<string, object> { ["foo"] = "bar" }, |
| 22 | + Body = "Yo, page!" |
| 23 | + }, 0)); |
| 24 | + |
| 25 | + var response = await Page.GoToAsync(TestConstants.EmptyPage); |
| 26 | + Assert.AreEqual(HttpStatusCode.Created, response.Status); |
| 27 | + Assert.AreEqual("bar", response.Headers["foo"]); |
| 28 | + Assert.AreEqual("Yo, page!", await Page.EvaluateExpressionAsync<string>("document.body.textContent")); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// In puppeteer this method is called ShouldWorkWithStatusCode422. |
| 33 | + /// I found that status 422 is not available in all .NET runtimes (see https://github.com/dotnet/core/blob/4c4642d548074b3fbfd425541a968aadd75fea99/release-notes/2.1/Preview/api-diff/preview2/2.1-preview2_System.Net.md) |
| 34 | + /// As the goal here is testing HTTP codes that are not in Chromium (see https://cs.chromium.org/chromium/src/net/http/http_status_code_list.h?sq=package:chromium&g=0) we will use code 426: Upgrade Required |
| 35 | + /// </summary> |
| 36 | + [PuppeteerTest("requestinterception-experimental.spec.ts", "Request.respond", "should work with status code 422")] |
| 37 | + [Skip(SkipAttribute.Targets.Firefox)] |
| 38 | + public async Task ShouldWorkReturnStatusPhrases() |
| 39 | + { |
| 40 | + await Page.SetRequestInterceptionAsync(true); |
| 41 | + Page.AddRequestInterceptor(request => request.RespondAsync(new ResponseData |
| 42 | + { |
| 43 | + Status = HttpStatusCode.UpgradeRequired, Body = "Yo, page!" |
| 44 | + }, 0)); |
| 45 | + |
| 46 | + var response = await Page.GoToAsync(TestConstants.EmptyPage); |
| 47 | + Assert.AreEqual(HttpStatusCode.UpgradeRequired, response.Status); |
| 48 | + Assert.AreEqual("Upgrade Required", response.StatusText); |
| 49 | + Assert.AreEqual("Yo, page!", await Page.EvaluateExpressionAsync<string>("document.body.textContent")); |
| 50 | + } |
| 51 | + |
| 52 | + [PuppeteerTest("requestinterception-experimental.spec.ts", "Request.respond", "should redirect")] |
| 53 | + [Skip(SkipAttribute.Targets.Firefox)] |
| 54 | + public async Task ShouldRedirect() |
| 55 | + { |
| 56 | + await Page.SetRequestInterceptionAsync(true); |
| 57 | + |
| 58 | + Page.AddRequestInterceptor(request => |
| 59 | + { |
| 60 | + if (!request.Url.Contains("rrredirect")) |
| 61 | + { |
| 62 | + return request.ContinueAsync(new Payload(), 0); |
| 63 | + } |
| 64 | + |
| 65 | + return request.RespondAsync(new ResponseData |
| 66 | + { |
| 67 | + Status = HttpStatusCode.Redirect, |
| 68 | + Headers = new Dictionary<string, object> { ["location"] = TestConstants.EmptyPage } |
| 69 | + }, 0); |
| 70 | + }); |
| 71 | + |
| 72 | + var response = await Page.GoToAsync(TestConstants.ServerUrl + "/rrredirect"); |
| 73 | + |
| 74 | + Assert.That(response.Request.RedirectChain, Has.Exactly(1).Items); |
| 75 | + Assert.AreEqual(TestConstants.ServerUrl + "/rrredirect", response.Request.RedirectChain[0].Url); |
| 76 | + Assert.AreEqual(TestConstants.EmptyPage, response.Url); |
| 77 | + } |
| 78 | + |
| 79 | + [PuppeteerTest("requestinterception-experimental.spec.ts", "Request.respond", "should allow mocking binary responses")] |
| 80 | + [Skip(SkipAttribute.Targets.Firefox)] |
| 81 | + public async Task ShouldAllowMockingBinaryResponses() |
| 82 | + { |
| 83 | + await Page.SetRequestInterceptionAsync(true); |
| 84 | + Page.AddRequestInterceptor(request => |
| 85 | + { |
| 86 | + var imageData = File.ReadAllBytes("./Assets/pptr.png"); |
| 87 | + return request.RespondAsync(new ResponseData { ContentType = "image/png", BodyData = imageData }, 0); |
| 88 | + }); |
| 89 | + |
| 90 | + await Page.EvaluateFunctionAsync(@"PREFIX => |
| 91 | + { |
| 92 | + const img = document.createElement('img'); |
| 93 | + img.src = PREFIX + '/does-not-exist.png'; |
| 94 | + document.body.appendChild(img); |
| 95 | + return new Promise(fulfill => img.onload = fulfill); |
| 96 | + }", TestConstants.ServerUrl); |
| 97 | + var img = await Page.QuerySelectorAsync("img"); |
| 98 | + Assert.True(ScreenshotHelper.PixelMatch("mock-binary-response.png", await img.ScreenshotDataAsync())); |
| 99 | + } |
| 100 | + |
| 101 | + [PuppeteerTest("requestinterception-experimental.spec.ts", "Request.respond", |
| 102 | + "should stringify intercepted request response headers")] |
| 103 | + [Skip(SkipAttribute.Targets.Firefox)] |
| 104 | + public async Task ShouldStringifyInterceptedRequestResponseHeaders() |
| 105 | + { |
| 106 | + await Page.SetRequestInterceptionAsync(true); |
| 107 | + Page.AddRequestInterceptor(request => request.RespondAsync(new ResponseData |
| 108 | + { |
| 109 | + Status = HttpStatusCode.OK, |
| 110 | + Headers = new Dictionary<string, object> { ["foo"] = true }, |
| 111 | + Body = "Yo, page!" |
| 112 | + }, 0)); |
| 113 | + |
| 114 | + var response = await Page.GoToAsync(TestConstants.EmptyPage); |
| 115 | + Assert.AreEqual(HttpStatusCode.OK, response.Status); |
| 116 | + Assert.AreEqual("True", response.Headers["foo"]); |
| 117 | + Assert.AreEqual("Yo, page!", await Page.EvaluateExpressionAsync<string>("document.body.textContent")); |
| 118 | + } |
| 119 | + |
| 120 | + [Skip(SkipAttribute.Targets.Firefox)] |
| 121 | + public async Task ShouldAllowMultipleInterceptedRequestResponseHeaders() |
| 122 | + { |
| 123 | + await Page.SetRequestInterceptionAsync(true); |
| 124 | + Page.AddRequestInterceptor(request => |
| 125 | + { |
| 126 | + return request.RespondAsync(new ResponseData |
| 127 | + { |
| 128 | + Status = HttpStatusCode.OK, |
| 129 | + Headers = new Dictionary<string, object> |
| 130 | + { |
| 131 | + ["foo"] = new [] { true, false }, |
| 132 | + ["Set-Cookie"] = new [] { "sessionId=abcdef", "specialId=123456" } |
| 133 | + }, |
| 134 | + Body = "Yo, page!" |
| 135 | + }, 0); |
| 136 | + }); |
| 137 | + |
| 138 | + var response = await Page.GoToAsync(TestConstants.EmptyPage); |
| 139 | + var cookies = await Page.GetCookiesAsync(TestConstants.EmptyPage); |
| 140 | + |
| 141 | + Assert.AreEqual(HttpStatusCode.OK, response.Status); |
| 142 | + Assert.AreEqual("True\nFalse", response.Headers["foo"]); |
| 143 | + Assert.AreEqual("Yo, page!", await Page.EvaluateExpressionAsync<string>("document.body.textContent")); |
| 144 | + Assert.AreEqual("specialId", cookies[0].Name); |
| 145 | + Assert.AreEqual("123456", cookies[0].Value); |
| 146 | + Assert.AreEqual("sessionId", cookies[1].Name); |
| 147 | + Assert.AreEqual("abcdef", cookies[1].Value); |
| 148 | + } |
| 149 | +} |
0 commit comments