Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add initiator to HTTPRequest #7614

Merged
merged 3 commits into from Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/api.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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]>
Expand Down
9 changes: 9 additions & 0 deletions src/common/HTTPRequest.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/assets/initiator.html
@@ -0,0 +1,2 @@
<iframe src="./frames/frame.html"></iframe>
<script src="./initiator.js"></script>
8 changes: 8 additions & 0 deletions test/assets/initiator.js
@@ -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);
42 changes: 42 additions & 0 deletions test/network.spec.ts
Expand Up @@ -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();
Expand Down