diff --git a/CHANGELOG.md b/CHANGELOG.md index 701fc5c..dfcd5a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 1.3.2 + +Released on Friday, November 13 2026. + +- Fixed `fetch`, `XMLHttpRequest` and `sendBeacon` sending the `Accept` header of a navigation (`text/html,...`), so that servers choosing by it answered with their web page instead of the data; it is `*/*` unless the script sets one + # 1.3.1 Released on Sunday, November 8 2026. diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index fc410a8..b21507a 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -6,6 +6,7 @@ AngleSharp.Io contains code written by (in order of first pull request / commit) * [Florian Rappl](https://github.com/FlorianRappl) * [Joel Verhagen](https://github.com/joelverhagen) +* [Michael Thyregod](https://github.com/michaelthyregod) Without these awesome people AngleSharp.Io could not exist. Thanks to everyone for your contributions! :beers: diff --git a/src/AngleSharp.Io.Docs/package.json b/src/AngleSharp.Io.Docs/package.json index 1a7cc90..79ffdb0 100644 --- a/src/AngleSharp.Io.Docs/package.json +++ b/src/AngleSharp.Io.Docs/package.json @@ -1,6 +1,6 @@ { "name": "@anglesharp/io", - "version": "1.3.0", + "version": "1.3.2", "preview": true, "description": "The doclet for the AngleSharp.Io documentation.", "keywords": [ diff --git a/src/AngleSharp.Io.Tests/Beacon/BeaconTests.cs b/src/AngleSharp.Io.Tests/Beacon/BeaconTests.cs index 655dec7..8fb8e1b 100644 --- a/src/AngleSharp.Io.Tests/Beacon/BeaconTests.cs +++ b/src/AngleSharp.Io.Tests/Beacon/BeaconTests.cs @@ -291,6 +291,18 @@ public async Task SameOriginBeaconsAreNotPreflighted() Assert.AreEqual("https://beacon.example", server.Requests[0].Headers[HeaderNames.Origin]); } + [Test] + public async Task BeaconsAcceptAnyType() + { + var server = new CorsServer(); + var navigator = await server.OpenNavigatorAsync("https://beacon.example/").ConfigureAwait(false); + + Assert.IsTrue(navigator.SendBeacon("/collect", "data")); + await WaitForAsync(() => server.Requests.Count == 1).ConfigureAwait(false); + + Assert.AreEqual("*/*", server.Requests[0].Headers[HeaderNames.Accept]); + } + [Test] public async Task PreflightIsSentWithoutCredentialsAndTheBeaconWithThem() { diff --git a/src/AngleSharp.Io.Tests/Network/FetchTests.cs b/src/AngleSharp.Io.Tests/Network/FetchTests.cs index dd2bb7f..e2c3456 100644 --- a/src/AngleSharp.Io.Tests/Network/FetchTests.cs +++ b/src/AngleSharp.Io.Tests/Network/FetchTests.cs @@ -58,6 +58,38 @@ public async Task FetchShouldApplyMethodHeadersAndBody() } } + [Test] + public async Task FetchShouldAcceptAnyTypeByDefault() + { + var loader = new TestDocumentLoader(request => + new TestDownload(Task.FromResult(CreateResponse("https://example.org/api", "{}")))); + + var window = await OpenWindowAsync(loader, "https://example.org/base/page").ConfigureAwait(false); + await window.FetchAsync("/api").ConfigureAwait(false); + + Assert.AreEqual("*/*", loader.LastRequest.Headers[HeaderNames.Accept]); + } + + [Test] + public async Task FetchShouldSendTheAcceptHeaderOfTheScript() + { + var loader = new TestDocumentLoader(request => + new TestDownload(Task.FromResult(CreateResponse("https://example.org/api", "{}")))); + + var window = await OpenWindowAsync(loader, "https://example.org/base/page").ConfigureAwait(false); + var options = new FetchOptions + { + Headers = new Dictionary + { + ["accept"] = "application/json" + }, + }; + + await window.FetchAsync("/api", options).ConfigureAwait(false); + + Assert.AreEqual("application/json", loader.LastRequest.Headers[HeaderNames.Accept]); + } + [Test] public async Task FetchShouldSetMultipartContentTypeForFormDataWhenMissing() { diff --git a/src/AngleSharp.Io.Tests/Network/XmlHttpRequesterTests.cs b/src/AngleSharp.Io.Tests/Network/XmlHttpRequesterTests.cs index 01e0eb3..bf7aea0 100644 --- a/src/AngleSharp.Io.Tests/Network/XmlHttpRequesterTests.cs +++ b/src/AngleSharp.Io.Tests/Network/XmlHttpRequesterTests.cs @@ -46,6 +46,31 @@ public async Task OpenAndSendShouldCreateRequestWithHeadersAndBody() } } + [TestCase(null, "*/*")] + [TestCase("application/json", "application/json")] + public async Task SendShouldAcceptAnyTypeUnlessSetByTheScript(String accept, String expected) + { + var loader = new TestDocumentLoader(request => + { + var response = CreateResponse("https://api.example.test/endpoint", "{}", HttpStatusCode.OK); + return new TestDownload(request.Target, Task.FromResult(response)); + }); + + var xhr = await CreateRequesterAsync(loader, "https://origin.example.test/").ConfigureAwait(false); + xhr.Open("GET", "https://api.example.test/endpoint"); + + if (accept != null) + { + xhr.SetRequestHeader("Accept", accept); + } + + xhr.Send(); + + await WaitForReadyStateAsync(xhr, RequesterState.Done).ConfigureAwait(false); + + Assert.AreEqual(expected, loader.LastRequest.Headers[HeaderNames.Accept]); + } + [Test] public async Task SendWithFormDataShouldSetMultipartContentTypeWhenMissing() { diff --git a/src/AngleSharp.Io/Dom/BeaconSender.cs b/src/AngleSharp.Io/Dom/BeaconSender.cs index 087ff11..cf68215 100644 --- a/src/AngleSharp.Io/Dom/BeaconSender.cs +++ b/src/AngleSharp.Io/Dom/BeaconSender.cs @@ -301,6 +301,7 @@ public DocumentRequest CreateRequest() Referer = Referer, }; + request.Headers[HeaderNames.Accept] = "*/*"; request.Headers[HeaderNames.Origin] = Origin; if (ContentType is not null) diff --git a/src/AngleSharp.Io/Dom/WindowExtensions.cs b/src/AngleSharp.Io/Dom/WindowExtensions.cs index 172bf7d..66b8b73 100644 --- a/src/AngleSharp.Io/Dom/WindowExtensions.cs +++ b/src/AngleSharp.Io/Dom/WindowExtensions.cs @@ -272,6 +272,10 @@ private static DocumentRequest CreateRequest(IWindow window, Url url, FetchOptio Referer = window.Document?.DocumentUri, }; + // not the documents a navigation asks for: servers answering by Accept would give + // their web page instead of the data (fetch takes any type unless the script says so) + request.Headers[HeaderNames.Accept] = "*/*"; + if (requestOptions.Headers != null) { foreach (var pair in requestOptions.Headers.Pairs) diff --git a/src/AngleSharp.Io/Dom/XmlHttpRequest.cs b/src/AngleSharp.Io/Dom/XmlHttpRequest.cs index c08c792..6e2d9c6 100644 --- a/src/AngleSharp.Io/Dom/XmlHttpRequest.cs +++ b/src/AngleSharp.Io/Dom/XmlHttpRequest.cs @@ -269,6 +269,9 @@ public void Send(Object body = null) Referer = _window.Document.DocumentUri, }; + // any type, not the documents of a navigation, unless set by the script + request.Headers[HeaderNames.Accept] = "*/*"; + foreach (var header in _headers) { request.Headers[header.Key] = header.Value; diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 77dac41..c40b5de 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ Providers additional requesters and IO helpers for AngleSharp. AngleSharp.Io - 1.3.0 + 1.3.2 latest true true