Skip to content

Commit 868680d

Browse files
authored
Merge pull request #36 from sushobhit-lt/DOT-2633
fix URL trim and https scheme
2 parents e5123ca + 841487d commit 868680d

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/lib/schemaValidation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ajv.addFormat('web-url', {
1212
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
1313
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
1414
'(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
15-
return urlPattern.test(url);
15+
return urlPattern.test(url.trim());
1616
}
1717
});
1818
addErrors(ajv);

src/lib/screenshot.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { chromium, firefox, webkit, Browser } from "@playwright/test"
22
import { Context, WebStaticConfig } from "../types.js"
3-
import { delDir } from "./utils.js"
3+
import { delDir, ensureHttps } from "./utils.js"
44

55
const BROWSER_CHROME = 'chrome';
66
const BROWSER_SAFARI = 'safari';
@@ -47,6 +47,10 @@ export async function captureScreenshots(ctx: Context, screenshots: WebStaticCon
4747
let screenshotId = screenshot.name.toLowerCase().replace(/\s/g, '-');
4848

4949
const page = await context.newPage();
50+
if(screenshot.url){
51+
screenshot.url = screenshot.url.trim();
52+
screenshot.url = ensureHttps(screenshot.url)
53+
}
5054
await page.goto(screenshot.url, pageOptions);
5155
await page.waitForTimeout(screenshot.waitForTimeout || 0)
5256

src/lib/utils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
import fs from 'fs';
22

3+
const HTTP_SCHEME = 'https:';
4+
const HTTP_SCHEME_PREFIX = 'https://';
5+
const WWW = 'www.';
6+
37
export function delDir(dir: string): void {
48
if (fs.existsSync(dir)) {
59
fs.rmSync(dir, { recursive: true });
610
}
11+
}
12+
13+
// Function to ensure URL uses HTTPS scheme
14+
export function ensureHttps(urlString: string) {
15+
try {
16+
if (urlString && urlString.startsWith(WWW)) {
17+
urlString = HTTP_SCHEME_PREFIX + urlString
18+
}
19+
let url = new URL(urlString);
20+
if (url.protocol !== HTTP_SCHEME) {
21+
url.protocol = HTTP_SCHEME;
22+
}
23+
return url.toString();
24+
} catch (error) {
25+
console.error('Invalid URL: '+urlString, error);
26+
return null;
27+
}
728
}

0 commit comments

Comments
 (0)