Skip to content

Commit 92f597b

Browse files
rgbawesty92
authored andcommitted
Extra HTTP headers can be passed with CreateOptions (#223)
1 parent d2d1731 commit 92f597b

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/CreateOptions.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ export interface CreateOptions {
9393
*/
9494
cookies?: SetCookieOptions[];
9595

96+
/**
97+
* Extra HTTP headers to send when making a request.
98+
*
99+
* @type {[key: string]: string}
100+
* @memberof CreateOptions
101+
*/
102+
extraHTTPHeaders?: { [key: string]: string; };
103+
96104
/**
97105
* Set a callback to receive console messages.
98106
*

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ async function beforeNavigate(options: CreateOptions, client: any): Promise<void
122122
options._navigateFailed = true;
123123
}
124124
});
125+
if (options.extraHTTPHeaders) {
126+
Network.setExtraHTTPHeaders({headers: options.extraHTTPHeaders});
127+
}
125128
if (options.cookies) {
126129
await throwIfCanceledOrFailed(options);
127130
await Network.setCookies({cookies: options.cookies});

test/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ describe('HtmlPdf', () => {
121121
expect(pdf.getRawTextContent()).to.startWith('Cookies:status=Passed!');
122122
});
123123

124+
it('should generate a PDF and send extra http headers', async () => {
125+
const options: HtmlPdf.CreateOptions = {
126+
port,
127+
extraHTTPHeaders: {
128+
'X-Custom-Test-Header1' : 'Passed1!',
129+
'X-Custom-Test-Header2' : 'Passed2!',
130+
},
131+
};
132+
133+
const result = await HtmlPdf.create('http://httpbin.org/headers', options);
134+
const pdf = await getParsedPdf(result.toBuffer());
135+
const rawTextContent = pdf.getRawTextContent();
136+
137+
expect(rawTextContent).to.contain('X-Custom-Test-Header1').and.to.contain('Passed1!');
138+
expect(rawTextContent).to.contain('X-Custom-Test-Header2').and.to.contain('Passed2!');
139+
});
140+
124141
it('should proxy console messages', async () => {
125142
const events: ConsoleAPICalled[] = [];
126143
const options: HtmlPdf.CreateOptions = {

0 commit comments

Comments
 (0)