Skip to content

Commit d6fc902

Browse files
committed
Add basic container and curl request tests to match features
1 parent ed1dd46 commit d6fc902

1 file changed

Lines changed: 157 additions & 0 deletions

File tree

tests.sh

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#!/usr/bin/env bash
2+
3+
function message {
4+
echo ""
5+
echo "---------------------------------------------------------------"
6+
echo $1
7+
echo "---------------------------------------------------------------"
8+
}
9+
10+
RESTORE=$(echo -en '\033[0m')
11+
RED=$(echo -en '\033[01;31m')
12+
GREEN=$(echo -en '\033[01;32m')
13+
YELLOW=$(echo -en '\033[00;33m')
14+
BLUE=$(echo -en '\033[00;34m')
15+
MAGENTA=$(echo -en '\033[00;35m')
16+
PURPLE=$(echo -en '\033[00;35m')
17+
CYAN=$(echo -en '\033[00;36m')
18+
LIGHTGRAY=$(echo -en '\033[00;37m')
19+
LRED=$(echo -en '\033[01;31m')
20+
LGREEN=$(echo -en '\033[01;32m')
21+
LYELLOW=$(echo -en '\033[01;33m')
22+
LBLUE=$(echo -en '\033[01;34m')
23+
LMAGENTA=$(echo -en '\033[01;35m')
24+
LPURPLE=$(echo -en '\033[01;35m')
25+
LCYAN=$(echo -en '\033[01;36m')
26+
WHITE=$(echo -en '\033[01;37m')
27+
28+
29+
30+
function failed {
31+
echo ${RED}$1${RESTORE}
32+
}
33+
34+
function passed {
35+
echo ${GREEN}$1${RESTORE}
36+
}
37+
38+
if ! [ -x "$(command -v jq)" ]; then
39+
message "JQ not installed. Installing..."
40+
sudo apt -y install jq
41+
fi
42+
43+
44+
message " Build image "
45+
docker build -t mendhak/http-https-echo:latest .
46+
47+
mkdir -p testarea
48+
pushd testarea
49+
50+
message " Cleaning up from previous test run "
51+
docker ps -q --filter "name=http-echo-tests" | grep -q . && docker stop http-echo-tests
52+
53+
message " Start container "
54+
docker run -d --rm --name http-echo-tests -p 8080:80 -p 8443:443 -t mendhak/http-https-echo
55+
sleep 5
56+
57+
58+
59+
60+
message " Make http(s) request, and test the path, method and header. "
61+
REQUEST=$(curl -s -k -X PUT -H "Arbitrary:Header" -d aaa=bbb https://localhost:8443/hello-world)
62+
if [ $(echo $REQUEST | jq -r '.path') == '/hello-world' ] && \
63+
[ $(echo $REQUEST | jq -r '.method') == 'PUT' ] && \
64+
[ $(echo $REQUEST | jq -r '.headers.arbitrary') == 'Header' ]
65+
then
66+
passed "HTTPS request passed."
67+
else
68+
failed "HTTPS request failed."
69+
echo $REQUEST | jq
70+
exit 1
71+
fi
72+
73+
REQUEST=$(curl -s -X PUT -H "Arbitrary:Header" -d aaa=bbb http://localhost:8080/hello-world)
74+
if [ $(echo $REQUEST | jq -r '.path') == '/hello-world' ] && \
75+
[ $(echo $REQUEST | jq -r '.method') == 'PUT' ] && \
76+
[ $(echo $REQUEST | jq -r '.headers.arbitrary') == 'Header' ]
77+
then
78+
passed "HTTP request passed."
79+
else
80+
failed "HTTP request failed."
81+
echo $REQUEST | jq
82+
exit 1
83+
fi
84+
85+
message " Make JSON request, and test that json is in the output. "
86+
REQUEST=$(curl -s -X POST -H "Content-Type: application/json" -d '{"a":"b"}' http://localhost:8080/)
87+
if [ $(echo $REQUEST | jq -r '.json.a') == 'b' ]
88+
then
89+
passed "JSON test passed."
90+
else
91+
failed "JSON test failed."
92+
echo $REQUEST | jq
93+
exit 1
94+
fi
95+
96+
message " Stop containers "
97+
docker stop http-echo-tests
98+
99+
message " Start container with different internal ports "
100+
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
101+
sleep 5
102+
103+
message " Make http(s) request, and test the path, method and header. "
104+
REQUEST=$(curl -s -k -X PUT -H "Arbitrary:Header" -d aaa=bbb https://localhost:8443/hello-world)
105+
if [ $(echo $REQUEST | jq -r '.path') == '/hello-world' ] && \
106+
[ $(echo $REQUEST | jq -r '.method') == 'PUT' ] && \
107+
[ $(echo $REQUEST | jq -r '.headers.arbitrary') == 'Header' ]
108+
then
109+
passed "HTTPS request passed."
110+
else
111+
failed "HTTPS request failed."
112+
echo $REQUEST | jq
113+
exit 1
114+
fi
115+
116+
REQUEST=$(curl -s -X PUT -H "Arbitrary:Header" -d aaa=bbb http://localhost:8080/hello-world)
117+
if [ $(echo $REQUEST | jq -r '.path') == '/hello-world' ] && \
118+
[ $(echo $REQUEST | jq -r '.method') == 'PUT' ] && \
119+
[ $(echo $REQUEST | jq -r '.headers.arbitrary') == 'Header' ]
120+
then
121+
passed "HTTP request passed."
122+
else
123+
failed "HTTP request failed."
124+
echo $REQUEST | jq
125+
exit 1
126+
fi
127+
128+
129+
message " Stop containers "
130+
docker stop http-echo-tests
131+
132+
133+
message " Start container with JWT_HEADER "
134+
docker run -d --rm -e JWT_HEADER=Authentication --name http-echo-tests -p 8080:80 -p 8443:443 -t mendhak/http-https-echo
135+
sleep 5
136+
137+
REQUEST=$(curl -s -k -H "Authentication: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" https://localhost:8443/ )
138+
if [ $(echo $REQUEST | jq -r '.jwt.header.typ') == 'JWT' ] && \
139+
[ $(echo $REQUEST | jq -r '.jwt.header.alg') == 'HS256' ] && \
140+
[ $(echo $REQUEST | jq -r '.jwt.payload.sub') == '1234567890' ]
141+
then
142+
passed "JWT request passed."
143+
else
144+
failed "JWT request failed."
145+
echo $REQUEST | jq
146+
exit 1
147+
fi
148+
149+
message " Stop containers "
150+
docker stop http-echo-tests
151+
152+
153+
154+
155+
156+
popd
157+
rm -rf testarea

0 commit comments

Comments
 (0)