Skip to content

Commit

Permalink
fix(http-crawler): do not hang on POST without payload (#1546)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Sep 19, 2022
1 parent 19cdf13 commit 8c87390
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/http-crawler/src/internals/http-crawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ export class HttpCrawler<Context extends InternalHttpCrawlingContext<any, any, H
};
}

if (/PATCH|POST|PUT/.test(request.method)) requestOptions.body = request.payload;
if (/PATCH|POST|PUT/.test(request.method)) requestOptions.body = request.payload ?? '';

return requestOptions;
}
Expand Down
25 changes: 25 additions & 0 deletions test/core/crawlers/http_crawler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ router.set('/redirectWithoutCookies', (req, res) => {
res.end();
});

router.set('/echo', (req, res) => {
res.setHeader('content-type', 'text/html');
req.pipe(res);
});

let server: http.Server;
let url: string;

Expand Down Expand Up @@ -231,3 +236,23 @@ test('no empty cookie header', async () => {

expect(results).toStrictEqual([]);
});

test('POST with undefined (empty) payload', async () => {
const results: string[] = [];

const crawler = new HttpCrawler({
handlePageFunction: async ({ body }) => {
results.push(body.toString());
},
});

await crawler.run([
{
url: `${url}/echo`,
payload: undefined,
method: 'POST',
},
]);

expect(results).toStrictEqual(['']);
});

0 comments on commit 8c87390

Please sign in to comment.