forked from mendhak/docker-http-https-echo
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests.sh
More file actions
executable file
·192 lines (153 loc) · 5.49 KB
/
tests.sh
File metadata and controls
executable file
·192 lines (153 loc) · 5.49 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env bash
set -euo pipefail
function message {
echo ""
echo "---------------------------------------------------------------"
echo $1
echo "---------------------------------------------------------------"
}
RESTORE=$(echo -en '\033[0m')
RED=$(echo -en '\033[01;31m')
GREEN=$(echo -en '\033[01;32m')
function failed {
echo ${RED}✗$1${RESTORE}
}
function passed {
echo ${GREEN}✓$1${RESTORE}
}
if ! [ -x "$(command -v jq)" ]; then
message "JQ not installed. Installing..."
sudo apt -y install jq
fi
message " Build image "
docker build -t mendhak/http-https-echo:latest .
mkdir -p testarea
pushd testarea
message " Cleaning up from previous test run "
docker ps -q --filter "name=http-echo-tests" | grep -q . && docker stop http-echo-tests
message " Start container normally "
docker run -d --rm --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo
sleep 5
message " Make http(s) request, and test the path, method and header. "
REQUEST=$(curl -s -k -X PUT -H "Arbitrary:Header" -d aaa=bbb https://localhost:8443/hello-world)
if [ $(echo $REQUEST | jq -r '.path') == '/hello-world' ] && \
[ $(echo $REQUEST | jq -r '.method') == 'PUT' ] && \
[ $(echo $REQUEST | jq -r '.headers.arbitrary') == 'Header' ]
then
passed "HTTPS request passed."
else
failed "HTTPS request failed."
echo $REQUEST | jq
exit 1
fi
REQUEST=$(curl -s -X PUT -H "Arbitrary:Header" -d aaa=bbb http://localhost:8080/hello-world)
if [ $(echo $REQUEST | jq -r '.path') == '/hello-world' ] && \
[ $(echo $REQUEST | jq -r '.method') == 'PUT' ] && \
[ $(echo $REQUEST | jq -r '.headers.arbitrary') == 'Header' ]
then
passed "HTTP request passed."
else
failed "HTTP request failed."
echo $REQUEST | jq
exit 1
fi
message " Make JSON request, and test that json is in the output. "
REQUEST=$(curl -s -X POST -H "Content-Type: application/json" -d '{"a":"b"}' http://localhost:8080/)
if [ $(echo $REQUEST | jq -r '.json.a') == 'b' ]
then
passed "JSON test passed."
else
failed "JSON test failed."
echo $REQUEST | jq
exit 1
fi
message " Stop containers "
docker stop http-echo-tests
message " Start container with different internal ports "
docker run -d --rm -e HTTP_PORT=8888 -e HTTPS_PORT=9999 --name http-echo-tests -p 8080:8888 -p 8443:9999 -t mendhak/http-https-echo
sleep 5
message " Make http(s) request, and test the path, method and header. "
REQUEST=$(curl -s -k -X PUT -H "Arbitrary:Header" -d aaa=bbb https://localhost:8443/hello-world)
if [ $(echo $REQUEST | jq -r '.path') == '/hello-world' ] && \
[ $(echo $REQUEST | jq -r '.method') == 'PUT' ] && \
[ $(echo $REQUEST | jq -r '.headers.arbitrary') == 'Header' ]
then
passed "HTTPS request passed."
else
failed "HTTPS request failed."
echo $REQUEST | jq
exit 1
fi
REQUEST=$(curl -s -X PUT -H "Arbitrary:Header" -d aaa=bbb http://localhost:8080/hello-world)
if [ $(echo $REQUEST | jq -r '.path') == '/hello-world' ] && \
[ $(echo $REQUEST | jq -r '.method') == 'PUT' ] && \
[ $(echo $REQUEST | jq -r '.headers.arbitrary') == 'Header' ]
then
passed "HTTP request passed."
else
failed "HTTP request failed."
echo $REQUEST | jq
exit 1
fi
message " Stop containers "
docker stop http-echo-tests
message " Start container with JWT_HEADER "
docker run -d --rm -e JWT_HEADER=Authentication --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo
sleep 5
REQUEST=$(curl -s -k -H "Authentication: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" https://localhost:8443/ )
if [ $(echo $REQUEST | jq -r '.jwt.header.typ') == 'JWT' ] && \
[ $(echo $REQUEST | jq -r '.jwt.header.alg') == 'HS256' ] && \
[ $(echo $REQUEST | jq -r '.jwt.payload.sub') == '1234567890' ]
then
passed "JWT request passed."
else
failed "JWT request failed."
echo $REQUEST | jq
exit 1
fi
message " Stop containers "
docker stop http-echo-tests
message " Start container with LOG_IGNORE_PATH "
docker run -d --rm -e LOG_IGNORE_PATH=/ping --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo
sleep 5
curl -s -k -X POST -d "banana" https://localhost:8443/ping > /dev/null
if [ $(docker logs http-echo-tests | wc -l) == 1 ] && \
! [ $(docker logs http-echo-tests | grep banana) ]
then
passed "LOG_IGNORE_PATH ignored the /ping path"
else
failed "LOG_IGNORE_PATH failed"
docker logs http-echo-tests
exit 1
fi
message " Stop containers "
docker stop http-echo-tests
message " Start container with LOG_WITHOUT_NEWLINE "
docker run -d --rm -e LOG_WITHOUT_NEWLINE=1 --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo
sleep 5
curl -s -k -X POST -d "tiramisu" https://localhost:8443/ > /dev/null
if [ $(docker logs http-echo-tests | wc -l) == 3 ] && \
[ $(docker logs http-echo-tests | grep tiramisu) ]
then
passed "LOG_WITHOUT_NEWLINE logged output in single line"
else
failed "LOG_WITHOUT_NEWLINE failed"
docker logs http-echo-tests
exit 1
fi
message " Stop containers "
docker stop http-echo-tests
message " Check that container is running as a NON ROOT USER by default"
docker run -d --name http-echo-tests --rm mendhak/http-https-echo
WHOAMI=$(docker exec http-echo-tests whoami)
if [ "$WHOAMI" == "node" ]
then
passed "Running as non root user"
else
failed "Running as root user"
exit 1
fi
message " Stop containers "
docker stop http-echo-tests
popd
rm -rf testarea