diff --git a/lib/NetworkManager.js b/lib/NetworkManager.js index dce1a662d20..fd3e5ced279 100644 --- a/lib/NetworkManager.js +++ b/lib/NetworkManager.js @@ -92,6 +92,13 @@ class NetworkManager extends EventEmitter { * @param {!Object} event */ _onRequestWillBeSent(event) { + if (event.redirectResponse) { + let request = this._idToRequest.get(event.requestId); + let response = new Response(request, event.redirectResponse); + request._response = response; + this.emit(NetworkManager.Events.Response, response); + this.emit(NetworkManager.Events.RequestFinished, request); + } let request = new Request(event.request); this._idToRequest.set(event.requestId, request); this.emit(NetworkManager.Events.Request, request); diff --git a/test/StaticServer.js b/test/StaticServer.js index de8f9c16e4f..6edaa0f71cb 100644 --- a/test/StaticServer.js +++ b/test/StaticServer.js @@ -54,6 +54,17 @@ class StaticServer { this._routes.set(path, handler); } + /** + * @param {string} fromPath + * @param {string} toPath + */ + setRedirect(from, to) { + this.setRoute(from, (req, res) => { + res.writeHead(302, { location: to }); + res.end(); + }); + } + /** * @param {string} path * @return {!Promise} diff --git a/test/test.js b/test/test.js index e03150c2994..8017e1f55ba 100644 --- a/test/test.js +++ b/test/test.js @@ -547,6 +547,24 @@ describe('Puppeteer', function() { await page.navigate(EMPTY_PAGE); expect(events).toEqual(['request', 'response', 'requestfinished']); })); + it('should support redirects', SX(async function() { + let events = []; + page.on('request', request => events.push(`${request.method} ${request.url}`)); + page.on('response', response => events.push(`${response.status} ${response.url}`)); + page.on('requestfinished', request => events.push(`DONE ${request.url}`)); + page.on('requestfailed', request => events.push(`FAIL ${request.url}`)); + staticServer.setRedirect('/foo.html', '/empty.html'); + const FOO_URL = STATIC_PREFIX + '/foo.html'; + await page.navigate(FOO_URL); + expect(events).toEqual([ + `GET ${FOO_URL}`, + `302 ${FOO_URL}`, + `DONE ${FOO_URL}`, + `GET ${EMPTY_PAGE}`, + `200 ${EMPTY_PAGE}`, + `DONE ${EMPTY_PAGE}` + ]); + })); }); });