Skip to content

Commit b399fec

Browse files
feat: support axios proxy (#402)
Co-authored-by: Philip Peterson <philip-peterson@users.noreply.github.com>
1 parent 6385e1d commit b399fec

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ See `demo-expect-403` NPM script.
195195

196196
Default expected value is 200.
197197

198+
#### proxy
199+
200+
You can have requests to the resolved URL be passed through a proxy server. To do this, specify both the `--proxy-host` and `--proxy-port` arguments. For example, specifying these as `localhost` and `8000` will pass requests through the proxy server at `localhost:8000`.
201+
202+
```
203+
$ start-test --proxy-host localhost --proxy-port 8000 start :9000 test:e2e
204+
```
205+
206+
Authentication is also supported, as well as custom protocol for the proxy (e.g. `socks5` or `https`), through the `--proxy-user`, `--proxy-password`, and `--proxy-protocol` options.
207+
198208
## `npx` and `yarn`
199209

200210
If you have [npx](https://www.npmjs.com/package/npx) available, you can execute locally installed tools from the shell. For example, if the `package.json` has the following local tools:

src/index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,28 @@ function waitAndRun ({ start, url, runFn, namedArguments }) {
8686
server.on('close', onClose)
8787

8888
debug('starting waitOn %s', url)
89+
90+
let proxy
91+
if (namedArguments.proxyHost) {
92+
if (!namedArguments.proxyPort) {
93+
throw new Error('Proxy host provided but no port provided')
94+
}
95+
proxy = {
96+
host: namedArguments.proxyHost,
97+
port: namedArguments.proxyPort,
98+
protocol: namedArguments.proxyProtocol
99+
}
100+
if (namedArguments.proxyUser) {
101+
if (typeof namedArguments.proxyPassword !== 'string') {
102+
throw new Error('Proxy username provided but no password provided')
103+
}
104+
proxy.auth = {
105+
username: namedArguments.proxyUser,
106+
password: namedArguments.proxyPassword
107+
}
108+
}
109+
}
110+
89111
const options = {
90112
resources: Array.isArray(url) ? url : [url],
91113
interval: waitOnInterval,
@@ -97,7 +119,8 @@ function waitAndRun ({ start, url, runFn, namedArguments }) {
97119
headers: {
98120
Accept: 'text/html, application/json, text/plain, */*'
99121
},
100-
validateStatus
122+
validateStatus,
123+
proxy
101124
}
102125
debug('wait-on options %o', options)
103126

src/utils.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ const arg = require('arg')
66
const debug = require('debug')('start-server-and-test')
77

88
const namedArguments = {
9-
'--expect': Number
9+
'--expect': Number,
10+
11+
'--proxy-host': String,
12+
'--proxy-protocol': String,
13+
'--proxy-port': Number,
14+
'--proxy-user': String,
15+
'--proxy-pass': String
1016
}
1117

1218
/**
@@ -65,6 +71,11 @@ const getNamedArguments = cliArgs => {
6571
debug('initial parsed arguments %o', args)
6672
return {
6773
expect: args['--expect'],
74+
proxyHost: args['--proxy-host'],
75+
proxyUser: args['--proxy-user'],
76+
proxyPassword: args['--proxy-password'],
77+
proxyPort: args['--proxy-port'],
78+
proxyProtocol: args['--proxy-protocol'],
6879
// aliases
6980
'--expected': '--expect'
7081
}

0 commit comments

Comments
 (0)