2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-08 17:22:34 +03:00

feat(fetch): add fetch, Request, Response env config variables for the adapter; (#7003)

* feat(fetch): add fetch, Request, Response env config variables for the adapter;

* feat(fetch): fixed design issue for environments without fetch API globals;
This commit is contained in:
Dmitriy Mozgovoy
2025-08-30 22:02:24 +03:00
committed by GitHub
parent a9f47afbf3
commit c959ff2901
7 changed files with 340 additions and 192 deletions
+85 -4
View File
@@ -399,14 +399,95 @@ describe('supports fetch with nodejs', function () {
const FormData = (await import('form-data')).default; // Node FormData
const form = new FormData();
form.append('foo', 'bar');
server = await startHTTPServer((req, res) => {
const contentType = req.headers['content-type'];
assert.match(contentType, /^multipart\/form-data; boundary=/i);
res.end('OK');
});
await fetchAxios.post('/form', form);
});
});
});
});
describe('env config', () => {
it('should respect env fetch API configuration', async() => {
const { data, headers } = await fetchAxios.get('/', {
env: {
fetch() {
return {
headers: {
foo: '1'
},
text: async () => 'test'
}
}
}
});
assert.strictEqual(headers.get('foo'), '1');
assert.strictEqual(data, 'test');
});
it('should be able to request with lack of Request object', async() => {
const form = new FormData();
form.append('x', '1');
const { data, headers } = await fetchAxios.post('/', form, {
onUploadProgress() {
// dummy listener to activate streaming
},
env: {
Request: null,
fetch() {
return {
headers: {
foo: '1'
},
text: async () => 'test'
}
}
}
});
assert.strictEqual(headers.get('foo'), '1');
assert.strictEqual(data, 'test');
});
it('should be able to handle response with lack of Response object', async() => {
const { data, headers } = await fetchAxios.get('/', {
onDownloadProgress() {
// dummy listener to activate streaming
},
env: {
Request: null,
Response: null,
fetch() {
return {
headers: {
foo: '1'
},
text: async () => 'test'
}
}
}
});
assert.strictEqual(headers.get('foo'), '1');
assert.strictEqual(data, 'test');
});
it('should fallback to the global on undefined env value', async() => {
server = await startHTTPServer((req, res) => res.end('OK'));
const { data } = await fetchAxios.get('/', {
env: {
fetch: undefined
}
});
assert.strictEqual(data, 'OK');
});
});
});