Skip to content

Commit 786a53e

Browse files
committed
Add support for basic auth
1 parent 175e75e commit 786a53e

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

bin/test-redirection.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ cli
2929
default: 100,
3030
})
3131
.option('--ignore-query-parameters', 'Ignore query parameters in the final URL.')
32-
.option('-p, --parser [parser]', 'Define which parser should be used: json or csv.')
33-
.option('--csv-delimiter [delimiter]', 'Define the delimiter of the input CSV file, can be a string or a RegExp.')
34-
.option('--replace-host [host]', 'Replace host for both the `from` and `to` parameters.')
32+
.option('-p, --parser [parser]', 'Define which parser should be used: json or csv.', { default: 'json' })
33+
.option('--csv-delimiter [delimiter]', 'Define the delimiter of the input CSV file, can be a string or a RegExp.', { default: ',' })
34+
.option('--replace-host <host>', 'Replace host for both the `from` and `to` parameters.')
3535
.option('-v, --verbose', 'Log all redirections.')
3636
.option('--only-errors', 'Log only errors.')
37+
.option('--user <user>', 'Basic auth user.')
38+
.option('--password <password>', 'Basic auth password.')
3739
.action((configPath, options) => {
3840
const resolvedConfigPath = resolve(process.cwd(), configPath);
3941

src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const OPTIONS = {
1212
replaceHost: '',
1313
verbose: false,
1414
onlyErrors: false,
15+
user: '',
16+
password: '',
1517
};
1618

1719
const DIFF_OPTIONS = {
@@ -30,7 +32,12 @@ const DIFF_OPTIONS = {
3032
*/
3133
async function getFinalRedirect(url, method = 'GET') {
3234
return new Promise((resolve, reject) => {
33-
const cmd = `curl -o /dev/null -sL -k -w "%{url_effective}" -X ${method} -I "${url}"`;
35+
let cmd = `curl -o /dev/null -sL -k -w "%{url_effective}" -X ${method} -I "${url}"`;
36+
37+
if (OPTIONS.user && OPTIONS.password) {
38+
cmd += ` --user ${OPTIONS.user}:${OPTIONS.password}`;
39+
}
40+
3441
exec(cmd, (error, out) => {
3542
if (error) {
3643
if (OPTIONS.verbose) {

0 commit comments

Comments
 (0)