Skip to content

Commit

Permalink
fix: fetch api tests were failing in node 18
Browse files Browse the repository at this point in the history
  • Loading branch information
lquixada committed Apr 29, 2023
1 parent 644c419 commit a724da2
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 81 deletions.
15 changes: 8 additions & 7 deletions bin/server
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ app.delete('/request', processRequest)
// This line needs to be at the end, otherwise it will block any method different than GET
app.use(express.static(dir), serveIndex(dir, { icons: true, view: 'details' }))

const ip = '127.0.0.1'
const port = 8000
const url = `http://${ip}:${port}/`
const { argv } = yargs(process.argv.slice(2))

function processRequest (req, res) {
const headers = {}
const isDelete = req.method === 'DELETE'
Expand All @@ -45,18 +50,14 @@ function processFileUrl (args) {

if (fileParamIndex > 0) {
const fileIndex = fileParamIndex + 1
args[fileIndex] = args[fileIndex].replace(/^\.\//, `http://localhost:${port}/`)
args[fileIndex] = args[fileIndex].replace(/^\.\//, url)
}

return args
}

const hostname = '127.0.0.1'
const port = 8000
const { argv } = yargs(process.argv.slice(2))

const server = app.listen(port, hostname, () => {
console.log(`Test server listening at http://localhost:${port}`)
const server = app.listen(port, ip, () => {
console.log(`Test server listening at ${url}`)

if (argv.exec) {
const args = processFileUrl(argv.exec.split(' '))
Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fetch('https://api.github.com/users/lquixada')
.then(res => {
if (res.status >= 400) {
throw new Error("Bad response from server");
}
return res.json();
})
.then(user => {
console.log(user);
})
.catch(err => {
console.error(err);
});
75 changes: 38 additions & 37 deletions test/fetch-api/api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ function addFetchSuite() {
expect(fetch).to.be.a('function');
});
it('should facilitate the making of requests', () => {
return fetch('http://localhost:8000/succeed')
return fetch('http://127.0.0.1:8000/succeed')
.then(function (res) {
console.log(2);
if (res.status >= 400) {
throw new Error('Bad server response');
}
Expand All @@ -23,7 +24,7 @@ function addFetchSuite() {
});
});
it('should catch bad responses', () => {
return fetch('http://localhost:8000/fail')
return fetch('http://127.0.0.1:8000/fail')
.then(function (res) {
if (res.status >= 400) {
throw new Error('Bad server response');
Expand All @@ -35,7 +36,7 @@ function addFetchSuite() {
});
});
it('should resolve promise on 500 error', () => {
return fetch('http://localhost:8000/error')
return fetch('http://127.0.0.1:8000/error')
.then(function (res) {
expect(res.status).to.equal(500);
expect(res.ok).to.equal(false);
Expand All @@ -46,7 +47,7 @@ function addFetchSuite() {
});
});
it('should reject when Request constructor throws', () => {
return fetch('http://localhost:8000/succeed', { method: 'GET', body: 'invalid' })
return fetch('http://127.0.0.1:8000/succeed', { method: 'GET', body: 'invalid' })
.then(function () {
expect.fail('Invalid Request init was accepted');
})
Expand All @@ -55,7 +56,7 @@ function addFetchSuite() {
});
});
it('should send headers', () => {
return fetch('http://localhost:8000/request', {
return fetch('http://127.0.0.1:8000/request', {
headers: {
Accept: 'application/json',
'X-Test': '42'
Expand All @@ -70,7 +71,7 @@ function addFetchSuite() {
});
});
it('with Request as argument', () => {
const request = new Request('http://localhost:8000/request', {
const request = new Request('http://127.0.0.1:8000/request', {
headers: {
Accept: 'application/json',
'X-Test': '42'
Expand All @@ -86,7 +87,7 @@ function addFetchSuite() {
});
});
it('should reuse same Request multiple times', () => {
const request = new Request('http://localhost:8000/request', {
const request = new Request('http://127.0.0.1:8000/request', {
headers: {
Accept: 'application/json',
'X-Test': '42'
Expand Down Expand Up @@ -116,7 +117,7 @@ function addFetchSuite() {
});
});
it('should populate body', () => {
return fetch('http://localhost:8000/succeed')
return fetch('http://127.0.0.1:8000/succeed')
.then(function (res) {
expect(res.status).to.equal(200);
expect(res.ok).to.equal(true);
Expand All @@ -127,13 +128,13 @@ function addFetchSuite() {
});
});
it('should parse headers', () => {
return fetch('http://localhost:8000/request').then(function (res) {
return fetch('http://127.0.0.1:8000/request').then(function (res) {
expect(res.headers.get('Date')).to.equal('Sat, 23 Sep 2017 15:41:16 GMT-0300');
expect(res.headers.get('Content-Type')).to.equal('application/json; charset=utf-8');
});
});
it('should support HTTP GET', () => {
return fetch('http://localhost:8000/request', {
return fetch('http://127.0.0.1:8000/request', {
method: 'get'
})
.then(function (res) {
Expand Down Expand Up @@ -161,7 +162,7 @@ function addFetchSuite() {
}).to.throw(TypeError);
});
it('should support HTTP POST', () => {
return fetch('http://localhost:8000/request', {
return fetch('http://127.0.0.1:8000/request', {
method: 'post',
body: 'name=Hubot'
})
Expand All @@ -174,7 +175,7 @@ function addFetchSuite() {
});
});
it('should support HTTP PUT', () => {
return fetch('http://localhost:8000/request', {
return fetch('http://127.0.0.1:8000/request', {
method: 'put',
body: 'name=Hubot'
})
Expand All @@ -187,7 +188,7 @@ function addFetchSuite() {
});
});
it('should support HTTP PATCH', () => {
return fetch('http://localhost:8000/request', {
return fetch('http://127.0.0.1:8000/request', {
method: 'PATCH',
body: 'name=Hubot'
})
Expand All @@ -200,7 +201,7 @@ function addFetchSuite() {
});
});
it('should support HTTP DELETE', () => {
return fetch('http://localhost:8000/request', {
return fetch('http://127.0.0.1:8000/request', {
method: 'delete'
})
.then(function (res) {
Expand All @@ -217,30 +218,30 @@ function addFetchSuite() {
expect(Request).to.be.a('function');
});
it('should construct an url from string', () => {
const request = new Request('http://localhost:8000/');
expect(request.url).to.equal('http://localhost:8000/');
const request = new Request('http://127.0.0.1:8000/');
expect(request.url).to.equal('http://127.0.0.1:8000/');
});
it('should construct url from object', () => {
const url = {
toString: function () {
return 'http://localhost:8000/';
return 'http://127.0.0.1:8000/';
}
};
const request = new Request(url);
expect(request.url).to.equal('http://localhost:8000/');
expect(request.url).to.equal('http://127.0.0.1:8000/');
});
it('should get GET as the default method', () => {
const request = new Request('http://localhost:8000/');
const request = new Request('http://127.0.0.1:8000/');
expect(request.method).to.equal('GET');
});
it('should set a method', () => {
const request = new Request('http://localhost:8000/', {
const request = new Request('http://127.0.0.1:8000/', {
method: 'post'
});
expect(request.method).to.equal('POST');
});
it('should set headers', () => {
const request = new Request('http://localhost:8000/', {
const request = new Request('http://127.0.0.1:8000/', {
headers: {
accept: 'application/json',
'Content-Type': 'text/plain'
Expand All @@ -250,7 +251,7 @@ function addFetchSuite() {
expect(request.headers.get('content-type')).to.equal('text/plain');
});
it('should set a body', () => {
const request = new Request('http://localhost:8000/', {
const request = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!'
});
Expand All @@ -259,7 +260,7 @@ function addFetchSuite() {
});
});
it.skip('construct with Request', () => {
const request1 = new Request('http://localhost:8000/', {
const request1 = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!',
headers: {
Expand All @@ -271,7 +272,7 @@ function addFetchSuite() {
return request2.text().then(function (data) {
expect(data).to.equal('Hello World!');
expect(request2.method).to.equal('POST');
expect(request2.url).to.equal('http://localhost:8000/');
expect(request2.url).to.equal('http://127.0.0.1:8000/');
expect(request2.headers.get('accept')).to.equal('application/json');
expect(request2.headers.get('content-type')).to.equal('text/plain');
return request1.text().then(function () {
Expand All @@ -282,7 +283,7 @@ function addFetchSuite() {
});
});
it('should construct a Request from another Request', () => {
const request1 = new Request('http://localhost:8000/', {
const request1 = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!',
headers: {
Expand All @@ -297,7 +298,7 @@ function addFetchSuite() {
});
});
it('should construct with Request with overriden headers', () => {
const request1 = new Request('http://localhost:8000/', {
const request1 = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!',
headers: {
Expand All @@ -313,7 +314,7 @@ function addFetchSuite() {
expect(request2.headers.get('x-test')).to.equal('42');
});
it('should construct with Request and override body', () => {
const request1 = new Request('http://localhost:8000/', {
const request1 = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!',
headers: {
Expand All @@ -330,7 +331,7 @@ function addFetchSuite() {
});
});
it('construct with used Request body', () => {
const request1 = new Request('http://localhost:8000/', {
const request1 = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!'
});
Expand All @@ -339,32 +340,32 @@ function addFetchSuite() {
});
});
it('should not have implicit Content-Type', () => {
const req = new Request('http://localhost:8000/');
const req = new Request('http://127.0.0.1:8000/');
expect(req.headers.get('content-type')).to.equal(null);
});
it('POST with blank body should not have implicit Content-Type', () => {
const req = new Request('http://localhost:8000/', {
const req = new Request('http://127.0.0.1:8000/', {
method: 'post'
});
expect(req.headers.get('content-type')).to.equal(null);
});
it('construct with string body sets Content-Type header', () => {
const req = new Request('http://localhost:8000/', {
const req = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!'
});
expect(req.headers.get('content-type')).to.equal('text/plain;charset=UTF-8');
});
it('construct with body and explicit header uses header', () => {
const req = new Request('http://localhost:8000/', {
const req = new Request('http://127.0.0.1:8000/', {
method: 'post',
headers: { 'Content-Type': 'image/png' },
body: 'Hello World!'
});
expect(req.headers.get('content-type')).to.equal('image/png');
});
it('construct with unsupported body type', () => {
const req = new Request('http://localhost:8000/', {
const req = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: {}
});
Expand All @@ -374,7 +375,7 @@ function addFetchSuite() {
});
});
it('construct with null body', () => {
const req = new Request('http://localhost:8000/', {
const req = new Request('http://127.0.0.1:8000/', {
method: 'post'
});
expect(req.headers.get('content-type')).to.equal(null);
Expand All @@ -383,7 +384,7 @@ function addFetchSuite() {
});
});
it('should clone GET request', () => {
const req = new Request('http://localhost:8000/', {
const req = new Request('http://127.0.0.1:8000/', {
headers: { 'content-type': 'text/plain' }
});
const clone = req.clone();
Expand All @@ -394,7 +395,7 @@ function addFetchSuite() {
expect(req.bodyUsed).to.equal(false);
});
it('should clone POST request', () => {
const req = new Request('http://localhost:8000/', {
const req = new Request('http://127.0.0.1:8000/', {
method: 'post',
headers: { 'content-type': 'text/plain' },
body: 'Hello World!'
Expand All @@ -409,7 +410,7 @@ function addFetchSuite() {
});
});
it('clone with used Request body', () => {
const req = new Request('http://localhost:8000/', {
const req = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!'
});
Expand Down
Loading

0 comments on commit a724da2

Please sign in to comment.