Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/injected/src/injectedScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,10 @@ export class InjectedScript {
return 'Not an input[type=file] element';

const files = payloads.map(file => {
const bytes = Uint8Array.from(atob(file.buffer), c => c.charCodeAt(0));
const binary = atob(file.buffer);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; ++i)
bytes[i] = binary.charCodeAt(i);
return new File([bytes], file.name, { type: file.mimeType, lastModified: file.lastModifiedMs });
});
const dt = new DataTransfer();
Expand Down
13 changes: 11 additions & 2 deletions packages/isomorphic/urlMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { LRUCache } from './lruCache';
import { isString } from './stringUtils';

export function isHttpUrl(url: string, base?: string): boolean {
Expand Down Expand Up @@ -177,11 +178,19 @@ export function urlMatchesEqual(match1: URLMatch, match2: URLMatch) {
return match1 === match2;
}

const globRegexCache = new LRUCache<string, RegExp>(1000);

export function urlMatches(baseURL: string | undefined, urlString: string, match: URLMatch | undefined, webSocketUrl?: boolean): boolean {
if (match === undefined || match === '')
return true;
if (isString(match))
match = new RegExp(resolveGlobToRegexPattern(baseURL, match, webSocketUrl));
if (isString(match)) {
const glob = match;
const key = JSON.stringify([baseURL, glob, !!webSocketUrl]);
match = globRegexCache.getOrCompute(key, () => ({
value: new RegExp(resolveGlobToRegexPattern(baseURL, glob, webSocketUrl)),
size: 1,
}));
}
if (isRegExp(match)) {
match.lastIndex = 0;
return match.test(urlString);
Expand Down
29 changes: 28 additions & 1 deletion tests/page/interception.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ it('RO-152 should intercept after a service worker', {
}
});

it('RO-153 should work with glob', async () => {
it('RO-153 should work with glob', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/44186' },
}, async () => {
function globToRegex(glob: string): RegExp {
return new RegExp(globToRegexPattern(glob));
}
Expand Down Expand Up @@ -194,6 +196,31 @@ it('RO-153 should work with glob', async () => {
expect(urlMatches(undefined, `${prefix}:blank`, `${prefix}:*`)).toBeTruthy();
expect(urlMatches(undefined, `not${prefix}:blank`, `${prefix}:*`)).toBeFalsy();
}

const originalRegExp = globalThis.RegExp;
let compilations = 0;
const matches: boolean[] = [];
try {
globalThis.RegExp = new Proxy(originalRegExp, {
construct(target, args) {
++compilations;
return Reflect.construct(target, args);
}
});
for (const host of ['first.test', 'second.test']) {
for (const webSocketUrl of [false, true]) {
const baseURL = `http://${host}/cached/`;
const url = `${webSocketUrl ? 'ws' : 'http'}://${host}/cached/file`;
matches.push(urlMatches(baseURL, url, './*', webSocketUrl));
matches.push(!urlMatches(baseURL, url + '/nested', './*', webSocketUrl));
matches.push(urlMatches(baseURL, url, './*', webSocketUrl));
}
}
} finally {
globalThis.RegExp = originalRegExp;
}
expect(matches).toEqual(Array(12).fill(true));
expect(compilations).toBe(4);
});

it('RO-154 should throw on unbalanced glob braces', async () => {
Expand Down
31 changes: 25 additions & 6 deletions tests/page/page-set-input-files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,34 @@ test('IF-013 should work @smoke', async ({ page, asset }) => {
expect(await page.$eval('input', input => input.files[0].name)).toBe('file-to-upload.txt');
});

test('IF-014 should set from memory', async ({ page }) => {
await page.setContent(`<input type=file>`);
await page.setInputFiles('input', {
test('IF-014 should set from memory', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/44186' },
}, async ({ page }) => {
await page.setContent(`<input type=file multiple>`);
const binary = Buffer.from(Array.from({ length: 256 }, (_, i) => i));
const payloads = [{
name: 'test.txt',
mimeType: 'text/plain',
buffer: Buffer.from('this is a test')
});
expect(await page.$eval('input', input => input.files.length)).toBe(1);
expect(await page.$eval('input', input => input.files[0].name)).toBe('test.txt');
}, {
name: 'binary.bin',
mimeType: 'application/octet-stream',
buffer: binary
}, {
name: 'empty.txt',
mimeType: 'text/plain',
buffer: Buffer.alloc(0)
}];
await page.setInputFiles('input', payloads);
expect(await page.$eval('input', input => Promise.all([...input.files].map(async file => ({
name: file.name,
mimeType: file.type,
bytes: Array.from(new Uint8Array(await file.arrayBuffer())),
}))))).toEqual(payloads.map(file => ({
name: file.name,
mimeType: file.mimeType,
bytes: Array.from(file.buffer),
})));
});

test('IF-015 should work with CSP', async ({ page, server, asset }) => {
Expand Down
Loading