Skip to content

Commit 3758bab

Browse files
committed
feat: adds support for http using wget
Implements #14
1 parent 9b75b88 commit 3758bab

File tree

3 files changed

+126
-22
lines changed

3 files changed

+126
-22
lines changed

README.md

+30-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Download the `wait-for` file, either the latest from [`master`](https://raw.gith
4343
With the file locally on your file system, you can directly invoke it.
4444

4545
```
46-
./wait-for host:port [-t timeout] [-- command args]
46+
./wait-for host:port|url [-t timeout] [-- command args]
4747
-q | --quiet Do not output any status messages
4848
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
4949
-- COMMAND ARGS Execute command with args after the test finishes
@@ -69,8 +69,8 @@ Eficode site is up
6969
To wait for database container to become available:
7070

7171

72-
```
73-
version: '2'
72+
```yml
73+
version: '3'
7474

7575
services:
7676
db:
@@ -83,6 +83,30 @@ services:
8383
- db
8484
```
8585
86+
To check if [https://www.eficode.com](https://www.eficode.com) is available over HTTPS:
87+
```
88+
$ ./wait-for https://www.eficode.com -- echo "Eficode is accessible over HTTPS"
89+
Eficode is accessible over HTTPS
90+
```
91+
92+
To wait for your API service to become available:
93+
94+
95+
```yml
96+
version: '3'
97+
98+
services:
99+
api:
100+
image: nginx
101+
102+
tests:
103+
build: .
104+
command: sh -c './wait-for http://api -- echo "The api is up! Let's use it"'
105+
depends_on:
106+
- api
107+
108+
```
109+
86110
## Testing
87111

88112
Ironically testing is done using [bats](https://github.com/sstephenson/bats), which on the other hand is depending on [bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell)).
@@ -100,9 +124,11 @@ Also, please include or update the test cases whenever possible by extending `wa
100124

101125
## Note
102126

103-
Make sure netcat is installed in your Dockerfile before running the command.
127+
Make sure netcat is installed in your Dockerfile before running the command if you test over plain TCP.
104128
```
105129
RUN apt-get -q update && apt-get -qy install netcat
106130
```
107131
https://stackoverflow.com/questions/44663180/docker-why-does-wait-for-always-time-out
108132

133+
If you are connecting over HTTP, then you will need to have wget available.
134+

wait-for

+55-16
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
# SOFTWARE.
2424

25-
set -- "$@" -- "$TIMEOUT" "$QUIET" "$HOST" "$PORT" "$result"
25+
set -- "$@" -- "$TIMEOUT" "$QUIET" "$PROTOCOL" "$HOST" "$PORT" "$result"
2626
TIMEOUT=15
2727
QUIET=0
28+
# The protocol to make the request with, either "tcp" or "http"
29+
PROTOCOL="tcp"
2830

2931
echoerr() {
3032
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
@@ -34,7 +36,7 @@ usage() {
3436
exitcode="$1"
3537
cat << USAGE >&2
3638
Usage:
37-
$cmdname host:port [-t timeout] [-- command args]
39+
$cmdname host:port|url [-t timeout] [-- command args]
3840
-q | --quiet Do not output any status messages
3941
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
4042
-- COMMAND ARGS Execute command with args after the test finishes
@@ -43,25 +45,47 @@ USAGE
4345
}
4446

4547
wait_for() {
46-
if ! command -v nc >/dev/null; then
47-
echoerr 'nc command is missing!'
48-
exit 1
49-
fi
48+
case "$PROTOCOL" in
49+
tcp)
50+
if ! command -v nc >/dev/null; then
51+
echoerr 'nc command is missing!'
52+
exit 1
53+
fi
54+
;;
55+
wget)
56+
if ! command -v wget >/dev/null; then
57+
echoerr 'nc command is missing!'
58+
exit 1
59+
fi
60+
;;
61+
esac
5062

5163
while :; do
52-
nc -z "$HOST" "$PORT" > /dev/null 2>&1
53-
64+
case "$PROTOCOL" in
65+
tcp)
66+
nc -z "$HOST" "$PORT" > /dev/null 2>&1
67+
;;
68+
http)
69+
wget --timeout=1 -q "$HOST" -O /dev/null > /dev/null 2>&1
70+
;;
71+
*)
72+
echoerr "Unknown protocol '$PROTOCOL'"
73+
exit 1
74+
;;
75+
esac
76+
5477
result=$?
78+
5579
if [ $result -eq 0 ] ; then
56-
if [ $# -gt 6 ] ; then
57-
for result in $(seq $(($# - 6))); do
80+
if [ $# -gt 7 ] ; then
81+
for result in $(seq $(($# - 7))); do
5882
result=$1
5983
shift
6084
set -- "$@" "$result"
6185
done
6286

63-
TIMEOUT=$2 QUIET=$3 HOST=$4 PORT=$5 result=$6
64-
shift 6
87+
TIMEOUT=$2 QUIET=$3 PROTOCOL=$4 HOST=$5 PORT=$6 result=$7
88+
shift 7
6589
exec "$@"
6690
fi
6791
exit 0
@@ -80,6 +104,11 @@ wait_for() {
80104

81105
while :; do
82106
case "$1" in
107+
http://*|https://*)
108+
HOST="$1"
109+
PROTOCOL="http"
110+
shift 1
111+
;;
83112
*:* )
84113
HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
85114
PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
@@ -137,9 +166,19 @@ if ! [ "$TIMEOUT" -ge 0 ] 2>/dev/null; then
137166
usage 3
138167
fi
139168

140-
if [ "$HOST" = "" -o "$PORT" = "" ]; then
141-
echoerr "Error: you need to provide a host and port to test."
142-
usage 2
143-
fi
169+
case "$PROTOCOL" in
170+
tcp)
171+
if [ "$HOST" = "" -o "$PORT" = "" ]; then
172+
echoerr "Error: you need to provide a host and port to test."
173+
usage 2
174+
fi
175+
;;
176+
http)
177+
if [ "$HOST" = "" ]; then
178+
echoerr "Error: you need to provide a host to test."
179+
usage 2
180+
fi
181+
;;
182+
esac
144183

145184
wait_for "$@"

wait-for.bats

+41-2
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,54 @@
3333
[ "$output" != "success" ]
3434
}
3535

36-
@test "environment variables should be restored for command invocation" {
36+
@test "environment variable HOST should be restored for command invocation" {
3737
HOST=success run ./wait-for -t 1 google.com:80 -- sh -c 'echo "$HOST"'
3838

3939
[ "$output" = "success" ]
4040
}
4141

42-
@test "unset environment variables should be restored as unset for command invocation" {
42+
@test "unset environment variable HOST should be restored as unset for command invocation" {
4343
run ./wait-for -t 1 google.com:80 -- sh -uc 'echo "$HOST"'
4444

4545
[ "$status" -ne 0 ]
4646
[ "$output" != "google.com" ]
4747
}
48+
49+
@test "environment variable PROTOCOL should be restored for command invocation" {
50+
PROTOCOL=success run ./wait-for -t 1 google.com:80 -- sh -c 'echo "$PROTOCOL"'
51+
52+
[ "$output" = "success" ]
53+
}
54+
55+
@test "unset environment variables PROTOCOL should be restored as unset for command invocation" {
56+
run ./wait-for -t 1 google.com:80 -- sh -uc 'echo "$PROTOCOL"'
57+
58+
[ "$status" -ne 0 ]
59+
[ "$output" != "google.com" ]
60+
}
61+
62+
@test "http://duckduckgo.com should be immediately found" {
63+
run ./wait-for http://duckduckgo.com -- echo 'success'
64+
65+
[ "$output" = "success" ]
66+
}
67+
68+
@test "https://duckduckgo.com should be immediately found" {
69+
run ./wait-for https://duckduckgo.com -- echo 'success'
70+
71+
[ "$output" = "success" ]
72+
}
73+
74+
@test "connection error in HTTP test should not start command" {
75+
run ./wait-for -t 1 http://google.com:8080 -- echo 'success'
76+
77+
[ "$status" -ne 0 ]
78+
[ "$output" != "success" ]
79+
}
80+
81+
@test "not found HTTP status should not start command" {
82+
run ./wait-for -t 1 http://google.com/ping -- echo 'success'
83+
84+
[ "$status" -ne 0 ]
85+
[ "$output" != "success" ]
86+
}

0 commit comments

Comments
 (0)