Skip to content

Commit ee326b7

Browse files
committed
test: ensure that Puppeteer follows redirects
1 parent 9ef6dfa commit ee326b7

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

test/puppeteer-proxy/factories/createPageProxy.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ test('makes a HTTP request (without proxy)', async (t) => {
5151
});
5252

5353
test('proxies a GET request', async (t) => {
54-
t.plan(2);
54+
t.plan(3);
5555

5656
const requestHandler = sinon.stub().callsFake((incomingRequest, outgoingRequest) => {
5757
outgoingRequest.end('foo');
@@ -74,14 +74,57 @@ test('proxies a GET request', async (t) => {
7474
httpProxyServer.url,
7575
);
7676

77-
const response = await page.goto(httpServer.url);
77+
const response = await page.goto(httpServer.url + '/foo');
7878

7979
t.is((await response.headers())['x-foo'], 'bar');
80+
81+
t.is(await page.url(), httpServer.url + '/foo');
8082
});
8183

8284
t.true(requestHandler.called);
8385
});
8486

87+
test('Puppeteer handles redirects', async (t) => {
88+
t.plan(1);
89+
90+
let requestIndex = 0;
91+
92+
const requestHandler = sinon
93+
.stub()
94+
.callsFake((incomingRequest, outgoingRequest) => {
95+
if (++requestIndex < 3) {
96+
outgoingRequest.writeHead(301, {
97+
location: '/' + requestIndex,
98+
});
99+
outgoingRequest.end();
100+
} else {
101+
outgoingRequest.end(String(requestIndex));
102+
}
103+
});
104+
105+
const httpServer = await createHttpServer(requestHandler);
106+
107+
const httpProxyServer = await createHttpProxyServer();
108+
109+
await createPage(async (page) => {
110+
const pageProxy = createPageProxy({
111+
page,
112+
});
113+
114+
await page.setRequestInterception(true);
115+
116+
proxyRequest(
117+
page,
118+
pageProxy,
119+
httpProxyServer.url,
120+
);
121+
122+
await page.goto(httpServer.url);
123+
});
124+
125+
t.is(requestHandler.callCount, 3);
126+
});
127+
85128
test('handles HTTP errors (unreachable server)', async (t) => {
86129
t.plan(2);
87130

0 commit comments

Comments
 (0)