-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelpers.js
More file actions
107 lines (95 loc) · 3.29 KB
/
Copy pathhelpers.js
File metadata and controls
107 lines (95 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2025-Present Datadog, Inc.
*/
/**
* Shared URL helpers for k6 load tests.
* k6 doesn't have native URL constructor, so we implement manual URL parsing.
*/
// Configuration from environment
export const BASE_URL = __ENV.TARGET_URL || 'http://traefik:80';
export const DEPLOYMENT_HOST_URL = __ENV.DEPLOYMENT_HOST_URL || 'http://localhost:8080';
/**
* Rewrite external URLs to internal Docker network URLs.
* OAuth redirects use the configured external hostname (e.g., localhost:8080)
* but k6 inside Docker needs to use the internal hostname (traefik:80).
*/
export function rewriteUrl(url) {
if (url && url.startsWith(DEPLOYMENT_HOST_URL)) {
return url.replace(DEPLOYMENT_HOST_URL, BASE_URL);
}
return url;
}
/**
* Extract origin (protocol + host) from a URL string.
*/
export function getUrlOrigin(url) {
const match = url.match(/^(https?:\/\/[^\/]+)/);
return match ? match[1] : '';
}
/**
* Get the directory path from a URL (everything up to the last /).
*/
export function getUrlDirectory(url) {
const origin = getUrlOrigin(url);
const path = url.slice(origin.length);
const lastSlash = path.lastIndexOf('/');
return origin + (lastSlash >= 0 ? path.slice(0, lastSlash + 1) : '/');
}
/**
* Resolve a relative URL against a base URL.
*/
export function resolveUrl(baseUrl, relativeUrl) {
if (relativeUrl.startsWith('http')) {
return relativeUrl;
}
if (relativeUrl.startsWith('/')) {
return getUrlOrigin(baseUrl) + relativeUrl;
}
return getUrlDirectory(baseUrl) + relativeUrl;
}
/**
* Extract access_token from URL query string or fragment.
* Handles URLs like: http://example.com/callback?access_token=xxx
* or: http://example.com/callback#access_token=xxx
*
* NOTE: k6's goja runtime lacks native URL/URLSearchParams APIs,
* so we parse manually instead of using `new URL(url).searchParams`.
*/
export function extractTokenFromUrl(url) {
try {
// Split URL into base and the part after ? or #
// Handle both query string (?access_token=) and fragment (#access_token=)
let searchStr = '';
const queryIndex = url.indexOf('?');
const hashIndex = url.indexOf('#');
if (queryIndex !== -1) {
const endIndex = hashIndex > queryIndex ? hashIndex : url.length;
searchStr = url.substring(queryIndex + 1, endIndex);
} else if (hashIndex !== -1) {
searchStr = url.substring(hashIndex + 1);
}
if (searchStr) {
const params = {};
searchStr.split('&').forEach(pair => {
const eqIndex = pair.indexOf('=');
if (eqIndex !== -1) {
const key = decodeURIComponent(pair.substring(0, eqIndex));
const value = decodeURIComponent(pair.substring(eqIndex + 1));
params[key] = value;
}
});
if (params.access_token) {
return { success: true, accessToken: params.access_token };
}
}
} catch (e) {
console.warn('Failed to extract token from URL:', url, e);
}
return { success: false, error: 'Could not extract token from URL' };
}
/**
* Maximum number of redirects to follow to prevent infinite loops.
*/
export const MAX_REDIRECTS = 10;