Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!settings.provider.useParamsAuth) {
requestOptions.headers.Authorization = 'Basic ' + (Buffer.from(settings.clientId + ':' + settings.clientSecret, 'utf8')).toString('base64');
}
if (settings.provider.headers) {
Hoek.merge(requestOptions.headers, settings.provider.headers);
}
if (typeof settings.clientSecret === 'object') {
Hoek.merge(requestOptions, settings.clientSecret);
}
// Obtain token
try {
var { res: tokenRes, payload } = await Wreck.post(settings.provider.token, requestOptions);
}
catch (err) {
return h.unauthenticated(Boom.internal('Failed obtaining ' + name + ' access token', err), { credentials });
}
if (tokenRes.statusCode < 200 ||
tokenRes.statusCode > 299) {
return h.unauthenticated(Boom.internal('Failed obtaining ' + name + ' access token', payload), { credentials });
}
try {
payload = internals.parse(payload);
}
catch (err) {
Bounce.rethrow(err, 'system');
it('returns an error on unsupported mime type', async () => {
const server = Hapi.server();
server.route({ method: 'POST', path: '/', handler: (request) => request.payload.key });
await server.start();
const options = {
headers: {
'Content-Type': 'application/unknown',
'Content-Length': '18'
},
payload: '{ "key": "value" }'
};
const err = await expect(Wreck.post(`http://localhost:${server.info.port}/?x=4`, options)).to.reject();
expect(err.output.statusCode).to.equal(415);
await server.stop({ timeout: 1 });
});
it('handles expect 100-continue', async () => {
const server = Hapi.server();
server.route({ method: 'POST', path: '/', handler: (request) => request.payload });
await server.start();
const uri = 'http://localhost:' + server.info.port;
const { res, payload } = await Wreck.post(uri, { payload: { hello: true }, headers: { expect: '100-continue' } });
expect(res.statusCode).to.equal(200);
expect(payload.toString()).to.equal('{"hello":true}');
await server.stop();
});
it('returns a gzip response on a post request when accept-encoding: deflate, gzip is requested', async () => {
const data = '{"test":"true"}';
const server = Hapi.server({ compression: { minBytes: 1 } });
server.route({ method: 'POST', path: '/', handler: (request) => request.payload });
await server.start();
const uri = 'http://localhost:' + server.info.port;
const zipped = await internals.compress('gzip', Buffer.from(data));
const { payload } = await Wreck.post(uri, { headers: { 'accept-encoding': 'deflate, gzip' }, payload: data });
expect(payload.toString()).to.equal(zipped.toString());
await server.stop();
});
server.once('listening', () => {
port = server.address().port;
const form = new FormData();
form.append('file1', Fs.createReadStream('./test/files/file1.txt'));
Wreck.post('http://127.0.0.1:' + port, {
payload: form, headers: form.getHeaders()
}, (err, res, payload) => {
expect(err).to.not.exist();
});
});
),
'seneca-time-client-sent': data.time.client_sent
}
if (defaultHeaders) {
headers = Object.assign(headers, defaultHeaders)
}
var requestOptions = {
json: true,
headers: headers,
timeout: clientOptions.timeout,
payload: JSON.stringify(data.act)
}
var postP = Wreck.post(url, requestOptions)
postP
.then(function(out) {
handle_post(null, out.res, out.payload)
})
.catch(function(err) {
handle_post(err)
})
function handle_post(err, res, payload) {
var response = {
kind: 'res',
res: payload && 'object' === typeof payload ? payload : null,
error: err,
sync: (msg.meta$ || meta).sync
}