-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: use standard fetch API when using node >=18 #17269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CamilleLetavernier
wants to merge
1
commit into
master
Choose a base branch
from
issues/12775
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| /******************************************************************************** | ||
| * Copyright (C) 2025 EclipseSource GmbH. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Eclipse Public License v. 2.0 which is available at | ||
| * http://www.eclipse.org/legal/epl-2.0. | ||
| * | ||
| * This Source Code may also be made available under the following Secondary | ||
| * Licenses when the conditions for such availability set forth in the Eclipse | ||
| * Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
| * with the GNU Classpath Exception which is available at | ||
| * https://www.gnu.org/software/classpath/license.html. | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 | ||
| ********************************************************************************/ | ||
|
|
||
| import { expect } from 'chai'; | ||
| import * as http from 'http'; | ||
| import { NodeRequestService } from './node-request-service'; | ||
| import { CancellationToken, RequestContext } from './common-request-service'; | ||
|
|
||
| describe('NodeRequestService', () => { | ||
| let service: NodeRequestService; | ||
| let server: http.Server; | ||
| let serverPort: number; | ||
|
|
||
| before(done => { | ||
| service = new NodeRequestService(); | ||
| server = http.createServer((req, res) => { | ||
| if (req.url === '/json') { | ||
| res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
| res.end(JSON.stringify({ message: 'hello' })); | ||
| } else if (req.url === '/text') { | ||
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
| res.end('hello world'); | ||
| } else if (req.url === '/echo-headers') { | ||
| res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
| res.end(JSON.stringify(req.headers)); | ||
| } else if (req.url === '/echo-method') { | ||
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
| res.end(req.method); | ||
| } else if (req.url === '/echo-body') { | ||
| const chunks: Buffer[] = []; | ||
| req.on('data', chunk => chunks.push(chunk)); | ||
| req.on('end', () => { | ||
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
| res.end(Buffer.concat(chunks).toString()); | ||
| }); | ||
| } else if (req.url === '/status/404') { | ||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('not found'); | ||
| } else if (req.url === '/status/500') { | ||
| res.writeHead(500, { 'Content-Type': 'text/plain' }); | ||
| res.end('server error'); | ||
| } else if (req.url === '/slow') { | ||
| setTimeout(() => { | ||
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
| res.end('slow response'); | ||
| }, 5000); | ||
| } else if (req.url === '/empty') { | ||
| res.writeHead(204); | ||
| res.end(); | ||
| } else { | ||
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
| res.end('ok'); | ||
| } | ||
| }); | ||
| server.listen(0, () => { | ||
| const addr = server.address(); | ||
| if (addr && typeof addr === 'object') { | ||
| serverPort = addr.port; | ||
| } | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| after(done => { | ||
| server.close(done); | ||
| }); | ||
|
|
||
| function url(path: string): string { | ||
| return `http://localhost:${serverPort}${path}`; | ||
| } | ||
|
|
||
| it('should perform a basic GET request', async () => { | ||
| const result = await service.request({ url: url('/text') }); | ||
| expect(result.res.statusCode).to.equal(200); | ||
| expect(RequestContext.asText(result)).to.equal('hello world'); | ||
| }); | ||
|
|
||
| it('should parse JSON responses', async () => { | ||
| const result = await service.request({ url: url('/json') }); | ||
| expect(result.res.statusCode).to.equal(200); | ||
| const json = RequestContext.asJson<{ message: string }>(result); | ||
| expect(json.message).to.equal('hello'); | ||
| }); | ||
|
|
||
| it('should handle non-200 status codes', async () => { | ||
| const result = await service.request({ url: url('/status/404') }); | ||
| expect(result.res.statusCode).to.equal(404); | ||
| }); | ||
|
|
||
| it('should handle 500 status codes', async () => { | ||
| const result = await service.request({ url: url('/status/500') }); | ||
| expect(result.res.statusCode).to.equal(500); | ||
| }); | ||
|
|
||
| it('should handle 204 No Content', async () => { | ||
| const result = await service.request({ url: url('/empty') }); | ||
| expect(result.res.statusCode).to.equal(204); | ||
| expect(RequestContext.asText(result)).to.equal(''); | ||
| }); | ||
|
|
||
| it('should send custom headers', async () => { | ||
| const result = await service.request({ | ||
| url: url('/echo-headers'), | ||
| headers: { 'X-Custom': 'test-value' } | ||
| }); | ||
| const headers = RequestContext.asJson<Record<string, string>>(result); | ||
| expect(headers['x-custom']).to.equal('test-value'); | ||
| }); | ||
|
|
||
| it('should use specified HTTP method', async () => { | ||
| const result = await service.request({ | ||
| url: url('/echo-method'), | ||
| type: 'POST' | ||
| }); | ||
| expect(RequestContext.asText(result)).to.equal('POST'); | ||
| }); | ||
|
|
||
| it('should send request body', async () => { | ||
| const result = await service.request({ | ||
| url: url('/echo-body'), | ||
| type: 'POST', | ||
| data: 'test body content' | ||
| }); | ||
| expect(RequestContext.asText(result)).to.equal('test body content'); | ||
| }); | ||
|
|
||
| it('should timeout when request takes too long', async () => { | ||
| try { | ||
| await service.request({ | ||
| url: url('/slow'), | ||
| timeout: 100 | ||
| }); | ||
| expect.fail('Should have thrown a timeout error'); | ||
| } catch (e) { | ||
| expect(e).to.be.an.instanceOf(Error); | ||
| } | ||
| }); | ||
|
|
||
| it('should abort on cancellation', async () => { | ||
| const listeners: Array<() => void> = []; | ||
| const token: CancellationToken = { | ||
| isCancellationRequested: false, | ||
| onCancellationRequested: (listener: () => void) => { | ||
| listeners.push(listener); | ||
| } | ||
| }; | ||
|
|
||
| const promise = service.request({ | ||
| url: url('/slow'), | ||
| }, token); | ||
|
|
||
| // Trigger cancellation shortly after the request starts | ||
| setTimeout(() => { | ||
| listeners.forEach(l => l()); | ||
| }, 50); | ||
|
|
||
| try { | ||
| await promise; | ||
| expect.fail('Should have thrown an abort error'); | ||
| } catch (e) { | ||
| expect(e).to.be.an.instanceOf(Error); | ||
| } | ||
| }); | ||
|
|
||
| it('should include response headers', async () => { | ||
| const result = await service.request({ url: url('/json') }); | ||
| expect(result.res.headers['content-type']).to.equal('application/json'); | ||
| }); | ||
|
|
||
| it('should set the url on the result', async () => { | ||
| const requestUrl = url('/text'); | ||
| const result = await service.request({ url: requestUrl }); | ||
| expect(result.url).to.equal(requestUrl); | ||
| }); | ||
|
|
||
| describe('configure', () => { | ||
| it('should configure proxy settings', async () => { | ||
| const configuredService = new NodeRequestService(); | ||
| await configuredService.configure({ | ||
| proxyUrl: 'http://proxy.example.com:8080', | ||
| strictSSL: false, | ||
| proxyAuthorization: 'Basic dGVzdDp0ZXN0' | ||
| }); | ||
| // Just verify configuration doesn't throw | ||
| }); | ||
|
|
||
| it('should inject Proxy-Authorization header when authorization is configured', async () => { | ||
| const configuredService = new NodeRequestService(); | ||
| await configuredService.configure({ | ||
| proxyAuthorization: 'Basic dGVzdDp0ZXN0' | ||
| }); | ||
| const result = await configuredService.request({ url: url('/echo-headers') }); | ||
| const headers = RequestContext.asJson<Record<string, string>>(result); | ||
| expect(headers['proxy-authorization']).to.equal('Basic dGVzdDp0ZXN0'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('resolveProxy', () => { | ||
| it('should return undefined by default', async () => { | ||
| const proxy = await service.resolveProxy('https://example.com'); | ||
| expect(proxy).to.be.undefined; | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The copyright year should be 2026, not 2025.