-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathtest.sh
executable file
·146 lines (118 loc) · 5.79 KB
/
test.sh
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
#!/bin/bash
set -e
CONTAINER=${CONTAINER}
if [ -z "${CONTAINER}" ]; then
echo "You must specify a build container with \${CONTAINER} to test (see my README.md)"
exit 1
fi
HEADERS_FILE="./.headers"
SOME_RESP_HEADER="SOME-RESPONSE-HEADER"
pushd "gwy"
# Test building the gateway.
docker run --rm -v="$PWD":/defs "$CONTAINER" -f /defs/test/test.proto -i /defs -s Message
# And make sure that we can build the test gateway too.
docker build -t $CONTAINER-test-gateway gen/grpc-gateway/
# Now run the test container with a prefix in the background
docker run -p=8080:80 -e 'MESSAGE_PROXY_API-PREFIX=/api/' -e 'MESSAGE_RESPONSE-HEADERS_'${SOME_RESP_HEADER}'=some-value' $CONTAINER-test-gateway &
# Give it a few to start accepting requests
sleep 5
# Now use curl to make sure we get an expected status.
# From https://superuser.com/a/442395
status=`curl -i -s -o $HEADERS_FILE -w "%{http_code}" localhost:8080/api/messages`
# For now, we expect a 503 service unavailable, since we don't have a grpc service
# running. In the future, if this was a real backend we should get a 200. However,
# here we can use the 503 to indicate that the gateway tried to send the request
# downstream.
echo ""
if [ "$status" -ne "503" ]; then
kill $!
echo >&2 "[Fail] - Received expected response from gateway when no backend service running"
echo >&2 "Invalid status: '$status' with /api/messages http request"
exit 1
fi
if ! grep -qi "$SOME_RESP_HEADER" "$HEADERS_FILE"; then
kill $!
echo >&2 "[Fail] - Received expected response from gateway when no backend service running"
echo >&2 "header $SOME_RESP_HEADER was not found in response"
rm $HEADERS_FILE
exit 1
fi
echo "[Pass] - Received expected response from gateway when no backend service running"
rm $HEADERS_FILE
# If we call an endpoint that does not exist (say just messages), we should
# get a 404, since there's no handler for that endpoint.
status=`curl -s -o /dev/null -w "%{http_code}" localhost:8080/messages`
echo ""
if [ "$status" -ne "404" ]; then
kill $!
echo >&2 "[Fail] - Received expected response from gateway when grpc method does not exist"
echo >&2 "Invalid status: '$status' with /messages http request"
exit 1
fi
echo "[Pass] - Received expected response from gateway when grpc method does not exist"
# UnboundUnary should not work
# Unbound methods require the request payload as request body (curl --data 'payload')
status=`curl -s -o /dev/null -w "%{http_code}" --data '{}' localhost:8080/api/Messages.Message/UnboundUnary`
echo ""
if [ "$status" -ne "404" ]; then
kill $!
echo >&2 "[Fail] - Received expected response from gateway when expected payload not passed in the http request body"
echo >&2 "Invalid status: '$status' with /api/Messages.Message/UnboundUnary http request"
exit 1
fi
echo "[Pass] - Received expected response from gateway when expected payload not passed in the http request body"
kill $!
# Test building the gateway with unbound methods.
docker run --rm -v="$PWD":/defs "$CONTAINER" -f test/test.proto -i . -s Message --generate-unbound-methods
# And make sure that we can build the test gateway too.
docker build -t $CONTAINER-test-gateway gen/grpc-gateway/
# Now run the test container with a prefix in the background
docker run -p=8080:80 -e 'MESSAGE_PROXY_API-PREFIX=/api/' -e 'MESSAGE_RESPONSE-HEADERS_'${SOME_RESP_HEADER}'=some-value' $CONTAINER-test-gateway &
# Give it a few to start accepting requests
sleep 5
# Now use curl to make sure we get an expected status.
# From https://superuser.com/a/442395
status=`curl -i -s -o $HEADERS_FILE -w "%{http_code}" localhost:8080/api/messages`
# For now, we expect a 503 service unavailable, since we don't have a grpc service
# running. In the future, if this was a real backend we should get a 200. However,
# here we can use the 503 to indicate that the gateway tried to send the request
# downstream.
echo ""
if [ "$status" -ne "503" ]; then
kill $!
echo >&2 "[Fail] - Received expected response from gateway when no backend service running, and unbound methods are generated"
echo >&2 "Invalid status: '$status' with /api/messages http request"
exit 1
fi
echo "[Pass] - Received expected response from gateway when no backend service running, and unbound methods are generated"
# UnboundUnary should work
# Unbound methods require the request payload as request body (curl --data 'payload')
status=`curl -i -s -o $HEADERS_FILE -w "%{http_code}" --data '{}' localhost:8080/api/Messages.Message/UnboundUnary`
echo ""
if [ "$status" -ne "503" ]; then
kill $!
echo >&2 "[Fail] - Received expected response from gateway when no backend service running and calling an unbound method, and unbound methods are generated"
echo >&2 "Invalid status: '$status' with /api/Messages.Message/UnboundUnary http request"
exit 1
fi
if ! grep -qi "$SOME_RESP_HEADER" "$HEADERS_FILE"; then
kill $!
echo >&2 "[Fail] - Received expected response from gateway when no backend service running and calling an unbound method, and unbound methods are generated"
echo >&2 "header $SOME_RESP_HEADER was not found in response"
rm $HEADERS_FILE
exit 1
fi
rm $HEADERS_FILE
echo "[Pass] - Received expected response from gateway when no backend service running and calling an unbound method, and unbound methods are generated"
# If we call an endpoint that does not exist (say just messages), we should
# get a 404, since there's no handler for that endpoint.
status=`curl -s -o /dev/null -w "%{http_code}" localhost:8080/messages`
echo ""
if [ "$status" -ne "404" ]; then
kill $!
echo >&2 "[Fail] - Received expected response from gateway when grpc method does not exist, and unbound methods are generated"
echo >&2 "Invalid status: '$status' with /messages http request"
exit 1
fi
echo "[Pass] - Received expected response from gateway when grpc method does not exist, and unbound methods are generated"
kill $!