Skip to content

Commit

Permalink
Support returning raw resource payload. Closes #193
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Mar 4, 2016
1 parent e3e05e0 commit a21dcc6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
8 changes: 5 additions & 3 deletions lib/oauth.js
Expand Up @@ -401,9 +401,11 @@ internals.Client.prototype._request = function (method, uri, params, oauth, opti
return callback(Boom.internal('Failed obtaining ' + this.provider + ' ' + desc, payload), payload, res.statusCode);
}

payload = internals.parse(payload);
if (payload instanceof Error) {
return callback(Boom.internal('Received invalid payload from ' + this.provider + ' ' + desc + ' endpoint', payload), payload, res.statusCode);
if (!options.raw) {
payload = internals.parse(payload);
if (payload instanceof Error) {
return callback(Boom.internal('Received invalid payload from ' + this.provider + ' ' + desc + ' endpoint', payload), payload, res.statusCode);
}
}

return callback(null, payload, res.statusCode);
Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "bell",
"description": "Third-party login plugin for hapi",
"version": "7.0.0",
"version": "7.1.0",
"repository": "git://github.com/hapijs/bell",
"main": "lib/index.js",
"keywords": [
Expand Down Expand Up @@ -37,7 +37,7 @@
"boom": "3.x.x",
"cryptiles": "3.x.x",
"hoek": "3.x.x",
"joi": "7.x.x",
"joi": "8.x.x",
"wreck": "7.x.x"
},
"peerDependencies": {
Expand Down
64 changes: 63 additions & 1 deletion test/oauth.js
Expand Up @@ -607,7 +607,6 @@ describe('Bell', () => {
});
});


it('passes if isSecure is true when protocol is https (forced)', (done) => {

const mock = new Mock.V1();
Expand Down Expand Up @@ -872,6 +871,69 @@ describe('Bell', () => {
});
});

it('returns raw resource response', { parallel: false }, (done) => {

const mock = new Mock.V1();
mock.start((provider) => {

const server = new Hapi.Server();
server.connection({ host: 'localhost', port: 80 });
server.register(Bell, (err) => {

expect(err).to.not.exist();

server.auth.strategy('custom', 'bell', {
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: 'test',
clientSecret: 'secret',
provider: provider
});

server.route({
method: '*',
path: '/login',
config: {
auth: 'custom',
handler: function (request, reply) {

const client = new Bell.oauth.Client({
name: 'twitter',
provider: provider,
clientId: 'test',
clientSecret: 'secret'
});

const credentials = request.auth.credentials;
client.resource('POST', mock.uri + '/resource', { a: 5 }, { token: credentials.token, secret: credentials.secret, raw: true }, (err, res) => {

expect(err).to.not.exist();
return reply(res);
});
}
}
});

server.inject('/login?next=%2Fhome', (res) => {

const cookie = res.headers['set-cookie'][0].split(';')[0] + ';';
expect(res.headers.location).to.equal(mock.uri + '/auth?oauth_token=1');

mock.server.inject(res.headers.location, (mockRes) => {

expect(mockRes.headers.location).to.equal('http://localhost:80/login?oauth_token=1&oauth_verifier=123');

server.inject({ url: mockRes.headers.location, headers: { cookie: cookie } }, (response) => {

expect(response.result).to.equal('{"a":"5"}');
mock.stop(done);
});
});
});
});
});
});

it('returns resource POST response', { parallel: false }, (done) => {

const mock = new Mock.V1();
Expand Down

0 comments on commit a21dcc6

Please sign in to comment.