Implement native Fetch API host support (Window.fetch, Request/Response/Headers, body mixin)#1120
Implement native Fetch API host support (Window.fetch, Request/Response/Headers, body mixin)#1120Copilot wants to merge 11 commits into
Conversation
Agent-Logs-Url: https://github.com/HtmlUnit/htmlunit/sessions/1a078496-eb07-45bb-9da2-369d21fb4fb9 Co-authored-by: rbri <2544132+rbri@users.noreply.github.com>
|
@copilot move the test for headers / request / response into separate classes. Test all details |
Agent-Logs-Url: https://github.com/HtmlUnit/htmlunit/sessions/570614f2-3973-4f96-b008-ce4e7a5fcc8d Co-authored-by: rbri <2544132+rbri@users.noreply.github.com>
Done in commit 2e26233. I split the coverage into separate classes ( Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot requesttest method constructorWithInit shows no results when running with real browsers |
Agent-Logs-Url: https://github.com/HtmlUnit/htmlunit/sessions/875f96e8-5378-44e3-a297-e99670811f3d Co-authored-by: rbri <2544132+rbri@users.noreply.github.com>
Fixed in commit cfd8e45. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot looks like the methods have problem with the order e.g. bodyMethods the log output for various methods in RequestTest appears in different order when running with real browsers. Fix that by synchonizing this in some way or spilt the tests |
Agent-Logs-Url: https://github.com/HtmlUnit/htmlunit/sessions/41ff744f-bdad-4216-8db7-9e9c17e67552 Co-authored-by: rbri <2544132+rbri@users.noreply.github.com>
Fixed in commit 8a1b118. I synchronized Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot this ordering problem is real for many methods, check the whole RequestTest class and make sure the log ordering is stable |
Agent-Logs-Url: https://github.com/HtmlUnit/htmlunit/sessions/0dd452a0-f05e-464b-934c-dc260b71a140 Co-authored-by: rbri <2544132+rbri@users.noreply.github.com>
Fixed in commit fc4cc7b. I reviewed RequestTest for async ordering issues and stabilized constructorWithInit by moving the async Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot in invalidConstructionCases() the first new call does not fail in real browsers |
Agent-Logs-Url: https://github.com/HtmlUnit/htmlunit/sessions/639df19b-aefe-4c95-bd99-42085be15473 Co-authored-by: rbri <2544132+rbri@users.noreply.github.com>
Fixed in commit 289454d. I changed the first invalidConstructionCases input to Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot still get ok for the first |
Agent-Logs-Url: https://github.com/HtmlUnit/htmlunit/sessions/5b8733d9-0d4e-498e-95b2-dfde04c19a32 Co-authored-by: rbri <2544132+rbri@users.noreply.github.com>
Fixed in commit 48c2765. I changed the first case to Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot check hearderTest and responsetest and make the same improvements you did for the requestTest |
|
@copilot org.opentest4j.AssertionFailedError: expected: <200§true§0§error§301§/a§302§> but was: <> |
Agent-Logs-Url: https://github.com/HtmlUnit/htmlunit/sessions/eb4c0531-5e18-4a8b-ba4f-99434cfa82bf Co-authored-by: rbri <2544132+rbri@users.noreply.github.com>
Done in commit 014577e. I reviewed both tests and applied the same ordering stabilization to Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Agent-Logs-Url: https://github.com/HtmlUnit/htmlunit/sessions/d72cd26b-ab7c-41f5-a64b-2d3a325c7c72 Co-authored-by: rbri <2544132+rbri@users.noreply.github.com>
Fixed in commit 488f702. I updated Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot staticMethods still fails with org.opentest4j.AssertionFailedError: expected: <200§true§0§error§301§/a§302§> but was: <> |
Agent-Logs-Url: https://github.com/HtmlUnit/htmlunit/sessions/e52f7995-87a3-4e2d-a477-2b1e4278ee35 Co-authored-by: rbri <2544132+rbri@users.noreply.github.com>
Fixed in commit cbb91aa. I kept the browser-safe Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|



Original prompt
Goal
Implement full support for the JavaScript Fetch API in HtmlUnit, matching the patterns and conventions already used in the codebase (e.g.
XMLHttpRequest,WebSocket,Promise,EventTarget).Scope
New classes to create (under
src/main/java/org/htmlunit/javascript/host/)fetch/Request.java— JSRequestobject (@JsxClass)fetch/Response.java— JSResponseobject (@JsxClass)fetch/Headers.java— JSHeadersobject (@JsxClass)fetch/FetchBodyMixin.java(or base class) — shared body-consumption methods:text(),json(),arrayBuffer(),blob(),formData(), all returningPromiseWindow.javachanges — add@JsxFunction fetch(...)that returns aPromise<Response>Behaviour to implement
fetch(input, init?)Requestobject asinputinitdictionary with:method,headers,body,mode,credentials,cache,redirect,referrer,integrity,keepalive,signalPromisethat resolves with aResponseWebClient/WebRequestinternally (likeXMLHttpRequestdoes)TypeError(rejects the promise) for non-HTTP(S)/blob/data schemes when appropriateAbortSignal/AbortControllerintegration if already present, otherwise leave a stubHeadersHeaders, a plain object, or a sequence of[name, value]pairsappend(name, value),delete(name),get(name),getSetCookie(),has(name),set(name, value)entries(),keys(),values(),forEach(callback)@@iterator(same asentries())Requestnew Request(input, init?)url,method,headers,body,bodyUsed,mode,credentials,cache,redirect,referrer,referrerPolicy,integrity,keepalive,signalclone(), plus all body-consumption methods (text(),json(),arrayBuffer(),blob(),formData())Responsenew Response(body?, init?)— body can be string,Blob,ArrayBuffer,FormData,URLSearchParams,ReadableStream, or nullResponse.ok(),Response.error(),Response.redirect(url, status?)ok,status,statusText,headers,url,redirected,type,body,bodyUsedclone(), plus all body-consumption methods (text(),json(),arrayBuffer(),blob(),formData())Implementation notes
@JsxClassimplementations:@JsxClass,@JsxConstructor,@JsxFunction,@JsxGetter,@JsxStaticFunctionannotationsJavaScriptConfiguration(look at howXMLHttpRequest,WebSocket,URLare registered — there is typically a list of all host classes)JavaScriptEngine.reportRuntimeError(...)/JavaScriptEngine.typeError(...)for error throwingPromiseobjects where required (look at how existing code returns promises, e.g. search forNativePromiseor howasyncresults are returned in the JS engine)fetch(), reuseWebClient.loadWebResponse(WebRequest)(same asXMLHttpRequest)Responsebody consumption lazy — the response body bytes are read once and cachedXMLHttpRequestjava.net.http.HttpClient— use HtmlUnit's ownWebClientinfrastructureTest suite to create
Create
src/test/java/org/htmlunit/javascript/host/fetch/FetchTest.java(extendingWebDriverTestCase) with comprehensive tests covering:Basic fetch
fetchexists as a global functionResponseresponse.okistruefor 2xx,falsefor 4xx/5xxresponse.statusandresponse.statusTextresponse.urlreflects the final URL (after redirects)response.typeis"basic"for same-originHeaders
new Headers()— empty constructornew Headers({...})— from objectnew Headers([['k','v']])— from sequenceheaders.get(),headers.has(),headers.set(),headers.append(),headers.delete()headers.forEach(),headers.entries(),headers.keys(),headers.values()Request
new Request(url)— basic constructornew Request(url, { method, headers, body })request.url,request.method,request.headersrequest.bodyUsedstartsfalse, becomestrueafter consumptionrequest.clone()Response
new Response()— emptynew Response(body)— string bodyresponse.text()— returns Promise resolving to stringresponse.json()— returns Promise resolving to pa...This pull request was created from Copilot chat.