Skip to content

Commit

Permalink
Merge pull request #74 from yujiosaka/fix_response
Browse files Browse the repository at this point in the history
fix response
  • Loading branch information
yujiosaka authored Jan 13, 2018
2 parents 6047b31 + 8698a26 commit bc2820a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/hccrawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
omit,
extend,
each,
reduce,
includes,
some,
endsWith,
Expand Down Expand Up @@ -303,7 +304,11 @@ class HCCrawler extends EventEmitter {
.then(crawler => (
crawler.crawl()
.then(res => {
res.response = pick(res.response, RESPONSE_FIELDS);
res = extend({}, res);
res.response = reduce(RESPONSE_FIELDS, (memo, field) => {
memo[field] = res.response[field]();
return memo;
}, {});
res.options = options;
res.depth = depth;
this.emit(HCCrawler.Events.RequestFinished, res);
Expand Down
10 changes: 8 additions & 2 deletions test/hccrawler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ describe('HCCrawler', () => {
onSuccess = sinon.spy();
sinon.stub(Crawler.prototype, 'crawl').returns(Promise.resolve({
options: {},
response: {
ok: (() => true),
url: (() => 'https://example.com/'),
status: (() => 200),
headers: (() => {}),
},
result: { title: 'Example Domain' },
links: ['http://www.iana.org/domains/example'],
screenshot: null,
Expand Down Expand Up @@ -322,8 +328,8 @@ describe('HCCrawler', () => {
return crawler.onIdle()
.then(() => {
assert.equal(onSuccess.callCount, 2);
assert.equal(Crawler.prototype.crawl.firstCall.thisValue._options.url, URL2);
assert.equal(Crawler.prototype.crawl.secondCall.thisValue._options.url, URL1);
assert.equal(onSuccess.firstCall.args[0].options.url, URL2);
assert.equal(onSuccess.secondCall.args[0].options.url, URL1);
});
});

Expand Down

0 comments on commit bc2820a

Please sign in to comment.