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

cherry-pick(1.15): fix: do not dedup header values (#8998) #9085

Merged
merged 1 commit into from
Sep 22, 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
38 changes: 13 additions & 25 deletions src/utils/multimap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,48 @@
*/

export class MultiMap<K, V> {
private _map: Map<K, Set<V>>;
private _map: Map<K, V[]>;

constructor() {
this._map = new Map<K, Set<V>>();
this._map = new Map<K, V[]>();
}

set(key: K, value: V) {
let set = this._map.get(key);
if (!set) {
set = new Set<V>();
this._map.set(key, set);
let values = this._map.get(key);
if (!values) {
values = [];
this._map.set(key, values);
}
set.add(value);
values.push(value);
}

get(key: K): Set<V> {
return this._map.get(key) || new Set();
get(key: K): V[] {
return this._map.get(key) || [];
}

has(key: K): boolean {
return this._map.has(key);
}

hasValue(key: K, value: V): boolean {
const set = this._map.get(key);
if (!set)
const values = this._map.get(key);
if (!values)
return false;
return set.has(value);
return values.includes(value);
}

get size(): number {
return this._map.size;
}

delete(key: K, value: V): boolean {
const values = this.get(key);
const result = values.delete(value);
if (!values.size)
this._map.delete(key);
return result;
}

deleteAll(key: K) {
this._map.delete(key);
}

keys(): IterableIterator<K> {
return this._map.keys();
}

values(): Iterable<V> {
const result: V[] = [];
for (const key of this.keys())
result.push(...Array.from(this.get(key)!));
result.push(...this.get(key));
return result;
}

Expand Down
8 changes: 6 additions & 2 deletions tests/page/page-route.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,14 @@ it('should pause intercepted fetch request until continue', async ({page, server
expect(status).toBe(200);
});

it('should work with custom referer headers', async ({page, server}) => {
it('should work with custom referer headers', async ({page, server, browserName}) => {
await page.setExtraHTTPHeaders({ 'referer': server.EMPTY_PAGE });
await page.route('**/*', route => {
expect(route.request().headers()['referer']).toBe(server.EMPTY_PAGE);
// See https://github.com/microsoft/playwright/issues/8999
if (browserName === 'chromium')
expect(route.request().headers()['referer']).toBe(server.EMPTY_PAGE + ', ' + server.EMPTY_PAGE);
else
expect(route.request().headers()['referer']).toBe(server.EMPTY_PAGE);
route.continue();
});
const response = await page.goto(server.EMPTY_PAGE);
Expand Down
8 changes: 8 additions & 0 deletions tests/page/page-set-extra-http-headers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,11 @@ it('should throw for non-string header values', async ({page}) => {
const error2 = await page.context().setExtraHTTPHeaders({ 'foo': true }).catch(e => e);
expect(error2.message).toContain('Expected value of header "foo" to be String, but "boolean" is found.');
});

it('should not duplicate referer header', async ({page, server, browserName}) => {
it.fail(browserName === 'chromium', 'Request has referer and Referer');
await page.setExtraHTTPHeaders({ 'referer': server.EMPTY_PAGE });
const response = await page.goto(server.EMPTY_PAGE);
expect(response.ok()).toBe(true);
expect(response.request().headers()['referer']).toBe(server.EMPTY_PAGE);
});