mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat: add initiator to HTTPRequest (#7614)
Co-Authored-By: atersolis <atersolis@atersolis.net>
This commit is contained in:
parent
eebf452d38
commit
a271145b06
@ -341,6 +341,7 @@
|
||||
* [httpRequest.finalizeInterceptions()](#httprequestfinalizeinterceptions)
|
||||
* [httpRequest.frame()](#httprequestframe)
|
||||
* [httpRequest.headers()](#httprequestheaders)
|
||||
* [httpRequest.initiator()](#httprequestinitiator)
|
||||
* [httpRequest.isNavigationRequest()](#httprequestisnavigationrequest)
|
||||
* [httpRequest.method()](#httprequestmethod)
|
||||
* [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.
|
||||
|
||||
#### 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()
|
||||
|
||||
- returns: <[boolean]>
|
||||
|
@ -130,6 +130,7 @@ export class HTTPRequest {
|
||||
private _currentStrategy: InterceptResolutionStrategy;
|
||||
private _currentPriority: number | undefined;
|
||||
private _interceptActions: Array<() => void | PromiseLike<any>>;
|
||||
private _initiator: Protocol.Network.Initiator;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
@ -158,6 +159,7 @@ export class HTTPRequest {
|
||||
this._currentStrategy = 'none';
|
||||
this._currentPriority = undefined;
|
||||
this._interceptActions = [];
|
||||
this._initiator = event.initiator;
|
||||
|
||||
for (const key of Object.keys(event.request.headers))
|
||||
this._headers[key.toLowerCase()] = event.request.headers[key];
|
||||
@ -298,6 +300,13 @@ export class HTTPRequest {
|
||||
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.
|
||||
* @remarks
|
||||
|
2
test/assets/initiator.html
Normal file
2
test/assets/initiator.html
Normal file
@ -0,0 +1,2 @@
|
||||
<iframe src="./frames/frame.html"></iframe>
|
||||
<script src="./initiator.js"></script>
|
8
test/assets/initiator.js
Normal file
8
test/assets/initiator.js
Normal 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);
|
@ -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 () {
|
||||
it('should return |false| for non-cached content', async () => {
|
||||
const { page, server } = getTestState();
|
||||
|
Loading…
Reference in New Issue
Block a user