feat: add initiator to HTTPRequest (#7614)

Co-Authored-By: atersolis <atersolis@atersolis.net>
This commit is contained in:
Jan Scheffler 2021-09-29 17:14:21 +02:00 committed by GitHub
parent eebf452d38
commit a271145b06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 0 deletions

View File

@ -341,6 +341,7 @@
* [httpRequest.finalizeInterceptions()](#httprequestfinalizeinterceptions) * [httpRequest.finalizeInterceptions()](#httprequestfinalizeinterceptions)
* [httpRequest.frame()](#httprequestframe) * [httpRequest.frame()](#httprequestframe)
* [httpRequest.headers()](#httprequestheaders) * [httpRequest.headers()](#httprequestheaders)
* [httpRequest.initiator()](#httprequestinitiator)
* [httpRequest.isNavigationRequest()](#httprequestisnavigationrequest) * [httpRequest.isNavigationRequest()](#httprequestisnavigationrequest)
* [httpRequest.method()](#httprequestmethod) * [httpRequest.method()](#httprequestmethod)
* [httpRequest.postData()](#httprequestpostdata) * [httpRequest.postData()](#httprequestpostdata)
@ -4792,6 +4793,14 @@ When in Cooperative Mode, awaits pending interception handlers and then decides
- returns: <[Object]> An object with HTTP headers associated with the request. All header names are lower-case. - returns: <[Object]> An object with HTTP headers associated with the request. All header names are lower-case.
#### httpRequest.initiator()
- returns: <[Object]> An object describing the initiator of the request
- `type` <[string]> Type of this initiator. Possible values: `parser`, `script`, `preload`, `SignedExchange` and `other`.
- `stack` <?[Object]> JavaScript stack trace for the initiator, set for `script` only.
- `url` <?[string]> Initiator URL, set for `parser`, `script` and `SignedExchange` type.
- `lineNumber` <?[number]> 0 based initiator line number, set for `parser` and `script`.
#### httpRequest.isNavigationRequest() #### httpRequest.isNavigationRequest()
- returns: <[boolean]> - returns: <[boolean]>

View File

@ -130,6 +130,7 @@ export class HTTPRequest {
private _currentStrategy: InterceptResolutionStrategy; private _currentStrategy: InterceptResolutionStrategy;
private _currentPriority: number | undefined; private _currentPriority: number | undefined;
private _interceptActions: Array<() => void | PromiseLike<any>>; private _interceptActions: Array<() => void | PromiseLike<any>>;
private _initiator: Protocol.Network.Initiator;
/** /**
* @internal * @internal
@ -158,6 +159,7 @@ export class HTTPRequest {
this._currentStrategy = 'none'; this._currentStrategy = 'none';
this._currentPriority = undefined; this._currentPriority = undefined;
this._interceptActions = []; this._interceptActions = [];
this._initiator = event.initiator;
for (const key of Object.keys(event.request.headers)) for (const key of Object.keys(event.request.headers))
this._headers[key.toLowerCase()] = event.request.headers[key]; this._headers[key.toLowerCase()] = event.request.headers[key];
@ -298,6 +300,13 @@ export class HTTPRequest {
return this._isNavigationRequest; return this._isNavigationRequest;
} }
/**
* @returns the initiator of the request.
*/
initiator(): Protocol.Network.Initiator {
return this._initiator;
}
/** /**
* A `redirectChain` is a chain of requests initiated to fetch a resource. * A `redirectChain` is a chain of requests initiated to fetch a resource.
* @remarks * @remarks

View File

@ -0,0 +1,2 @@
<iframe src="./frames/frame.html"></iframe>
<script src="./initiator.js"></script>

8
test/assets/initiator.js Normal file
View File

@ -0,0 +1,8 @@
const script = document.createElement('script');
script.src = './injectedfile.js';
document.body.appendChild(script);
const style = document.createElement('link');
style.rel = 'stylesheet';
style.href = './injectedstyle.css';
document.head.appendChild(style);

View File

@ -137,6 +137,48 @@ describe('network', function () {
}); });
}); });
describeFailsFirefox('Request.initiator', () => {
it('shoud return the initiator', async () => {
const { page, server } = getTestState();
const initiators = new Map();
page.on('request', (request) =>
initiators.set(request.url().split('/').pop(), request.initiator())
);
await page.goto(server.PREFIX + '/initiator.html');
expect(initiators.get('initiator.html').type).toBe('other');
expect(initiators.get('initiator.js').type).toBe('parser');
expect(initiators.get('initiator.js').url).toBe(
server.PREFIX + '/initiator.html'
);
expect(initiators.get('frame.html').type).toBe('parser');
expect(initiators.get('frame.html').url).toBe(
server.PREFIX + '/initiator.html'
);
expect(initiators.get('script.js').type).toBe('parser');
expect(initiators.get('script.js').url).toBe(
server.PREFIX + '/frames/frame.html'
);
expect(initiators.get('style.css').type).toBe('parser');
expect(initiators.get('style.css').url).toBe(
server.PREFIX + '/frames/frame.html'
);
expect(initiators.get('initiator.js').type).toBe('parser');
expect(initiators.get('injectedfile.js').type).toBe('script');
expect(initiators.get('injectedfile.js').stack.callFrames[0].url).toBe(
server.PREFIX + '/initiator.js'
);
expect(initiators.get('injectedstyle.css').type).toBe('script');
expect(initiators.get('injectedstyle.css').stack.callFrames[0].url).toBe(
server.PREFIX + '/initiator.js'
);
expect(initiators.get('initiator.js').url).toBe(
server.PREFIX + '/initiator.html'
);
});
});
describeFailsFirefox('Response.fromCache', function () { describeFailsFirefox('Response.fromCache', function () {
it('should return |false| for non-cached content', async () => { it('should return |false| for non-cached content', async () => {
const { page, server } = getTestState(); const { page, server } = getTestState();