Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb5185a
commit 107756f
Showing
6 changed files
with
212 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,105 @@ | ||
import test from 'ava'; | ||
import delay from 'delay'; | ||
import {createServer} from './helpers/server'; | ||
import got from '..'; | ||
|
||
let s; | ||
|
||
test.before('setup', async () => { | ||
s = await createServer(); | ||
const echoHeaders = (req, res) => { | ||
res.statusCode = 200; | ||
res.write(JSON.stringify(req.headers)); | ||
res.end(); | ||
}; | ||
s.on('/', echoHeaders); | ||
await s.listen(s.port); | ||
}); | ||
|
||
test('beforeRequest receives normalized options', async t => { | ||
await got( | ||
s.url, | ||
{ | ||
json: true, | ||
hooks: { | ||
beforeRequest: [ | ||
options => { | ||
t.is(options.path, '/'); | ||
t.is(options.hostname, 'localhost'); | ||
} | ||
] | ||
} | ||
} | ||
); | ||
}); | ||
|
||
test('beforeRequest allows modifications', async t => { | ||
const res = await got( | ||
s.url, | ||
{ | ||
json: true, | ||
hooks: { | ||
beforeRequest: [ | ||
options => { | ||
options.headers.foo = 'bar'; | ||
} | ||
] | ||
} | ||
} | ||
); | ||
t.is(res.body.foo, 'bar'); | ||
}); | ||
|
||
test('beforeRequest awaits async function', async t => { | ||
const res = await got( | ||
s.url, | ||
{ | ||
json: true, | ||
hooks: { | ||
beforeRequest: [ | ||
async options => { | ||
await delay(100); | ||
options.headers.foo = 'bar'; | ||
} | ||
] | ||
} | ||
} | ||
); | ||
t.is(res.body.foo, 'bar'); | ||
}); | ||
|
||
test('beforeRequest rejects when beforeRequest throws', async t => { | ||
await t.throws( | ||
() => got(s.url, { | ||
hooks: { | ||
beforeRequest: [ | ||
() => { | ||
throw new Error('oops'); | ||
} | ||
] | ||
} | ||
}), | ||
{ | ||
instanceOf: Error, | ||
message: 'oops' | ||
} | ||
); | ||
}); | ||
|
||
test('beforeRequest rejects when beforeRequest rejects', async t => { | ||
await t.throws( | ||
() => got(s.url, { | ||
hooks: { | ||
beforeRequest: [() => Promise.reject(new Error('oops'))] | ||
} | ||
}), | ||
{ | ||
instanceOf: Error, | ||
message: 'oops' | ||
} | ||
); | ||
}); | ||
|
||
test.after('cleanup', async () => { | ||
await s.close(); | ||
}); |