From caa7adf0a0497fd6ca93dbf63203083c12f0ae8c Mon Sep 17 00:00:00 2001 From: Robin Richtsfeld Date: Mon, 15 Mar 2021 19:53:51 +0100 Subject: [PATCH] Don't disable cache for auth challenge (#1645) * Don't disable cache for auth challenge * Add missing assembly references --- .../NetworkTests/PageAuthenticateTests.cs | 30 +++++++++++++++++-- lib/PuppeteerSharp/NetworkManager.cs | 4 +-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/PuppeteerSharp.Tests/NetworkTests/PageAuthenticateTests.cs b/lib/PuppeteerSharp.Tests/NetworkTests/PageAuthenticateTests.cs index c11533bd0..cbcfe3039 100644 --- a/lib/PuppeteerSharp.Tests/NetworkTests/PageAuthenticateTests.cs +++ b/lib/PuppeteerSharp.Tests/NetworkTests/PageAuthenticateTests.cs @@ -1,4 +1,6 @@ -using System.Net; +using System.Collections.Generic; +using System.Linq; +using System.Net; using System.Threading.Tasks; using Xunit; using Xunit.Abstractions; @@ -64,5 +66,29 @@ await Page.AuthenticateAsync(new Credentials response = await Page.GoToAsync(TestConstants.CrossProcessUrl + "/empty.html"); Assert.Equal(HttpStatusCode.Unauthorized, response.Status); } + + [Fact] + public async Task ShouldNotDisableCaching() + { + Server.SetAuth("/cached/one-style.css", "user4", "pass4"); + Server.SetAuth("/cached/one-style.html", "user4", "pass4"); + + await Page.AuthenticateAsync(new Credentials + { + Username = "user4", + Password = "pass4" + }); + + var responses = new Dictionary(); + Page.Response += (_, e) => responses[e.Response.Url.Split('/').Last()] = e.Response; + + await Page.GoToAsync(TestConstants.ServerUrl + "/cached/one-style.html"); + await Page.ReloadAsync(); + + Assert.Equal(HttpStatusCode.NotModified, responses["one-style.html"].Status); + Assert.False(responses["one-style.html"].FromCache); + Assert.Equal(HttpStatusCode.OK, responses["one-style.css"].Status); + Assert.True(responses["one-style.css"].FromCache); + } } -} \ No newline at end of file +} diff --git a/lib/PuppeteerSharp/NetworkManager.cs b/lib/PuppeteerSharp/NetworkManager.cs index 5ef9d0718..1577ec771 100644 --- a/lib/PuppeteerSharp/NetworkManager.cs +++ b/lib/PuppeteerSharp/NetworkManager.cs @@ -129,7 +129,7 @@ internal Task SetRequestInterceptionAsync(bool value) private Task UpdateProtocolCacheDisabledAsync() => _client.SendAsync("Network.setCacheDisabled", new NetworkSetCacheDisabledRequest { - CacheDisabled = _userCacheDisabled || _protocolRequestInterceptionEnabled + CacheDisabled = _userCacheDisabled || _userRequestInterceptionEnabled }); private async void Client_MessageReceived(object sender, MessageEventArgs e) @@ -376,7 +376,7 @@ private void HandleRequestRedirect(Request request, ResponsePayload responseMess private async Task OnRequestWillBeSentAsync(RequestWillBeSentPayload e) { // Request interception doesn't happen for data URLs with Network Service. - if (_protocolRequestInterceptionEnabled && !e.Request.Url.StartsWith("data:", StringComparison.InvariantCultureIgnoreCase)) + if (_userRequestInterceptionEnabled && !e.Request.Url.StartsWith("data:", StringComparison.InvariantCultureIgnoreCase)) { if (_requestIdToInterceptionId.TryRemove(e.RequestId, out var interceptionId)) {