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

fix: do not dedup header values #8998

Merged
merged 3 commits into from
Sep 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 14 additions & 25 deletions src/utils/multimap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,49 @@
*/

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);
// console.log('set ' + key + ' => ' + values);
}

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: 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);
});