Skip to content

Commit

Permalink
Make it optional passing res@request to callback with 'passResToCallb…
Browse files Browse the repository at this point in the history
…ack' option
  • Loading branch information
wangtao committed Nov 26, 2013
1 parent 9e16912 commit 18ea0cb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
8 changes: 5 additions & 3 deletions fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
, 'timeout': null
, 'scope': null
, 'redirectUri': null
, 'passResToCallback': null
}
, readOnlyCalls = {
'admin.getallocation': true
Expand Down Expand Up @@ -295,7 +296,8 @@

if(isOAuthRequest && response && response.statusCode === 200 &&
response.headers && /.*text\/plain.*/.test(response.headers['content-type'])) {
cb(parseOAuthApiResponse(body), response);
var parsedBody = parseOAuthApiResponse(body)
options('passResToCallback') ? cb(parsedBody, response) : cb(parsedBody)
} else {
var json;
try {
Expand All @@ -309,7 +311,7 @@
Error: ex
}};
}
cb(json, response);
options('passResToCallback') ? cb(json, response) : cb(json);
}
});
};
Expand Down Expand Up @@ -467,7 +469,7 @@
if(!data || data.error) {
originalCallback(new FacebookApiException(data));
} else {
originalCallback(null, data, res);
options('passResToCallback') ? originalCallback(null, data, res) : originalCallback(null, data);
}
};
}
Expand Down
44 changes: 30 additions & 14 deletions test/api/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,7 @@ describe('FB.api', function () {
result.should.have.property('username', 'zuck');
done();
});
});

it('should have content-length header as 172', function (done) {
FB.api('4', function (result, response) {
response.should.have.header('content-length', '172')
done();
})
})

});
});

describe("FB.api('/4', cb)", function () {
Expand Down Expand Up @@ -126,18 +118,42 @@ describe('FB.api', function () {
});

describe("FB.api('4', cb)", function () {
it("should expose response object to callback with node style", function (done) {
beforeEach(function () {
nock('https://graph.facebook.com:443')
.get('/4')
.reply(200, "{\"id\":\"4\",\"name\":\"Mark Zuckerberg\",\"first_name\":\"Mark\",\"last_name\":\"Zuckerberg\",\"link\":\"http:\\/\\/www.facebook.com\\/zuck\",\"username\":\"zuck\",\"gender\":\"male\",\"locale\":\"en_US\"}", { 'access-control-allow-origin': '*',
'content-type': 'text/javascript; charset=UTF-8',
'content-length': '172' });
});

it("should not expose response object when passResToCallback is falsy", function (done) {
FB.api('4', function(data, res) {
should.strictEqual(res, undefined);
done();
});
});

it("should support passResToCallback option", function () {
FB.options({passResToCallback: true});
should.equal(FB.options('passResToCallback'), true);
});

it("should expose response object when passResToCallback is true", function (done) {
FB.options({passResToCallback: true});

FB.api('4', function(data, res) {
res.should.have.header('content-length', '172');
done();
});
});

it("should expose response object to node-style callback as well", function (done) {
FB.options({passResToCallback: true});
FB.napi('4', function(err, data, res) {
res.should.have.status(200)
done()
})
})
res.should.have.status(200);
done();
});
});
})
});
});

0 comments on commit 18ea0cb

Please sign in to comment.