From 1ba2b8540dc52791e70c2f2c076f50e135af3b75 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Tue, 4 Sep 2018 20:39:59 +0100 Subject: [PATCH] feat(network): introduce Response.remoteAddress() (#3192) Closes #2171. --- docs/api.md | 6 ++++++ lib/NetworkManager.js | 11 +++++++++++ test/network.spec.js | 6 ++++++ test/test.js | 2 ++ 4 files changed, 25 insertions(+) diff --git a/docs/api.md b/docs/api.md index e0762c2b3a0..f39f6375777 100644 --- a/docs/api.md +++ b/docs/api.md @@ -265,6 +265,7 @@ * [response.headers()](#responseheaders) * [response.json()](#responsejson) * [response.ok()](#responseok) + * [response.remoteAddress()](#responseremoteaddress) * [response.request()](#responserequest) * [response.securityDetails()](#responsesecuritydetails) * [response.status()](#responsestatus) @@ -3072,6 +3073,11 @@ This method will throw if the response body is not parsable via `JSON.parse`. Contains a boolean stating whether the response was successful (status in the range 200-299) or not. +#### response.remoteAddress() +- returns: <[Object]> + - `ip` <[string]> the IP address of the remote server + - `port` <[number]> the port used to connect to the remote server + #### response.request() - returns: <[Request]> A matching [Request] object. diff --git a/lib/NetworkManager.js b/lib/NetworkManager.js index 7211cb9b773..5e392c5531d 100644 --- a/lib/NetworkManager.js +++ b/lib/NetworkManager.js @@ -522,6 +522,10 @@ class Response { this._bodyLoadedPromiseFulfill = fulfill; }); + this._remoteAddress = { + ip: responsePayload.remoteIPAddress, + port: responsePayload.remotePort, + }; this._status = responsePayload.status; this._url = request.url(); this._fromDiskCache = !!responsePayload.fromDiskCache; @@ -532,6 +536,13 @@ class Response { this._securityDetails = responsePayload.securityDetails ? new SecurityDetails(responsePayload.securityDetails) : null; } + /** + * @return {{ip: string, port: number}} + */ + remoteAddress() { + return this._remoteAddress; + } + /** * @return {string} */ diff --git a/test/network.spec.js b/test/network.spec.js index a856a9c9ae2..6268dcc37be 100644 --- a/test/network.spec.js +++ b/test/network.spec.js @@ -56,6 +56,10 @@ module.exports.addTests = function({testRunner, expect}) { expect(responses[0].fromCache()).toBe(false); expect(responses[0].fromServiceWorker()).toBe(false); expect(responses[0].request()).toBeTruthy(); + const remoteAddress = responses[0].remoteAddress(); + // Either IPv6 or IPv4, depending on environment. + expect(remoteAddress.ip === '[::1]' || remoteAddress.ip === '127.0.0.1').toBe(true); + expect(remoteAddress.port).toBe(server.PORT); }); it('Response.fromCache()', async({page, server}) => { @@ -196,6 +200,7 @@ module.exports.addTests = function({testRunner, expect}) { const redirectChain = response.request().redirectChain(); expect(redirectChain.length).toBe(1); expect(redirectChain[0].url()).toContain('/foo.html'); + expect(redirectChain[0].response().remoteAddress().port).toBe(server.PORT); }); }); @@ -250,6 +255,7 @@ module.exports.addTests = function({testRunner, expect}) { }); const response = await page.goto(server.EMPTY_PAGE); expect(response.ok()).toBe(true); + expect(response.remoteAddress().port).toBe(server.PORT); }); xit('should work when POST is redirected with 302', async({page, server}) => { server.setRedirect('/rredirect', '/empty.html'); diff --git a/test/test.js b/test/test.js index 79afd73e973..4124693ebb0 100644 --- a/test/test.js +++ b/test/test.js @@ -77,6 +77,7 @@ beforeAll(async state => { const port = 8907 + state.parallelIndex * 2; state.server = await SimpleServer.create(assetsPath, port); state.server.enableHTTPCache(cachedPath); + state.server.PORT = port; state.server.PREFIX = `http://localhost:${port}`; state.server.CROSS_PROCESS_PREFIX = `http://127.0.0.1:${port}`; state.server.EMPTY_PAGE = `http://localhost:${port}/empty.html`; @@ -84,6 +85,7 @@ beforeAll(async state => { const httpsPort = port + 1; state.httpsServer = await SimpleServer.createHTTPS(assetsPath, httpsPort); state.httpsServer.enableHTTPCache(cachedPath); + state.httpsServer.PORT = httpsPort; state.httpsServer.PREFIX = `https://localhost:${httpsPort}`; state.httpsServer.CROSS_PROCESS_PREFIX = `https://127.0.0.1:${httpsPort}`; state.httpsServer.EMPTY_PAGE = `https://localhost:${httpsPort}/empty.html`;