Skip to content

Commit

Permalink
Add missing IgnoreHttpsErrors tests (hardkoded#1954)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok authored May 2, 2022
1 parent 2bf806c commit aa56515
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 55 deletions.
50 changes: 25 additions & 25 deletions docfx_project/examples/Page.Request.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# How to log network requests
_Contributors: [Meir Blachman](https://github.com/meir017)_

## Problem

You need to monitor the outgoing network requests.

## Solution

Use `Page.Request` event to monitor network requests.


```cs
using var browser = await Puppeteer.LaunchAsync(new () { Headless = true });
var page = await browser.NewPageAsync();
page.Request += (sender, e) =>
{
Console.WriteLine($"Request: {e.Request.Method} {e.Request.Url}");
foreach (var header in e.Request.Headers)
{
Console.WriteLine($"{header.Key}: {header.Value}");
}
};
await page.GoToAsync("https://example.com");
```
# How to log network requests
_Contributors: [Meir Blachman](https://github.com/meir017)_

## Problem

You need to monitor the outgoing network requests.

## Solution

Use `Page.Request` event to monitor network requests.


```cs
using var browser = await Puppeteer.LaunchAsync(new () { Headless = true });
var page = await browser.NewPageAsync();
page.Request += (sender, e) =>
{
Console.WriteLine($"Request: {e.Request.Method} {e.Request.Url}");
foreach (var header in e.Request.Headers)
{
Console.WriteLine($"{header.Key}: {header.Value}");
}
};
await page.GoToAsync("https://example.com");
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections.Features;
using Microsoft.AspNetCore.Http;
using Xunit;
using Xunit.Abstractions;
using PuppeteerSharp.Helpers;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;

namespace PuppeteerSharp.Tests.IgnoreHttpsErrorsTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class IgnoreHttpsErrorsTests : PuppeteerPageBaseTest
{
public IgnoreHttpsErrorsTests(ITestOutputHelper output) : base(output)
{
DefaultOptions = TestConstants.DefaultBrowserOptions();
DefaultOptions.IgnoreHTTPSErrors = true;
}

[PuppeteerTest("ignorehttpserrors.spec.ts", "ignoreHTTPSErrors", "should work")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldWork()
{
var response = await Page.GoToAsync($"{TestConstants.HttpsPrefix}/empty.html");
Assert.True(response.Ok);
}

[PuppeteerTest("ignorehttpserrors.spec.ts", "ignoreHTTPSErrors", "should work with request interception")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldWorkWithRequestInterception()
{
await Page.SetRequestInterceptionAsync(true);
Page.Request += async (_, e) => await e.Request.ContinueAsync();
var response = await Page.GoToAsync(TestConstants.EmptyPage);
Assert.Equal(HttpStatusCode.OK, response.Status);
}

[PuppeteerTest("ignorehttpserrors.spec.ts", "ignoreHTTPSErrors", "should work with mixed content")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldWorkWithMixedContent()
{
HttpsServer.SetRoute("/mixedcontent.html", async (context) =>
{
await context.Response.WriteAsync($"<iframe src='{TestConstants.EmptyPage}'></iframe>");
});
await Page.GoToAsync(TestConstants.HttpsPrefix + "/mixedcontent.html", new NavigationOptions
{
WaitUntil = new[] { WaitUntilNavigation.Load }
});
Assert.Equal(2, Page.Frames.Length);
// Make sure blocked iframe has functional execution context
// @see https://github.com/GoogleChrome/puppeteer/issues/2709
Assert.Equal(3, await Page.MainFrame.EvaluateExpressionAsync<int>("1 + 2"));
Assert.Equal(5, await Page.FirstChildFrame().EvaluateExpressionAsync<int>("2 + 3"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
Expand Down Expand Up @@ -42,6 +42,14 @@ await Task.WhenAll(
TestUtils.CurateProtocol(response.SecurityDetails.Protocol));
}

[PuppeteerTest("ignorehttpserrors.spec.ts", "Response.securityDetails", "should be |null| for non-secure requests")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldBeNullForNonSecureRequests()
{
var response = await Page.GoToAsync(TestConstants.EmptyPage);
Assert.Null(response.SecurityDetails);
}

[PuppeteerTest("ignorehttpserrors.spec.ts", "Response.securityDetails", "Network redirects should report SecurityDetails")]
[SkipBrowserFact(skipFirefox: true)]
public async Task NetworkRedirectsShouldReportSecurityDetails()
Expand All @@ -68,34 +76,5 @@ await Task.WhenAll(
TestUtils.CurateProtocol(requestTask.Result.ToString()),
TestUtils.CurateProtocol(response.SecurityDetails.Protocol));
}

[PuppeteerTest("ignorehttpserrors.spec.ts", "Response.securityDetails", "should work with request interception")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldWorkWithRequestInterception()
{
await Page.SetRequestInterceptionAsync(true);
Page.Request += async (_, e) => await e.Request.ContinueAsync();
var response = await Page.GoToAsync(TestConstants.EmptyPage);
Assert.Equal(HttpStatusCode.OK, response.Status);
}

[PuppeteerTest("ignorehttpserrors.spec.ts", "Response.securityDetails", "should work with mixed content")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldWorkWithMixedContent()
{
HttpsServer.SetRoute("/mixedcontent.html", async (context) =>
{
await context.Response.WriteAsync($"<iframe src='{TestConstants.EmptyPage}'></iframe>");
});
await Page.GoToAsync(TestConstants.HttpsPrefix + "/mixedcontent.html", new NavigationOptions
{
WaitUntil = new[] { WaitUntilNavigation.Load }
});
Assert.Equal(2, Page.Frames.Length);
// Make sure blocked iframe has functional execution context
// @see https://github.com/GoogleChrome/puppeteer/issues/2709
Assert.Equal(3, await Page.MainFrame.EvaluateExpressionAsync<int>("1 + 2"));
Assert.Equal(5, await Page.FirstChildFrame().EvaluateExpressionAsync<int>("2 + 3"));
}
}
}

0 comments on commit aa56515

Please sign in to comment.