[http_proxy_over_p2p] multipart request proxy test
Added a test for the case where the request to be proxied is a http post multipart-form request. License: MIT Signed-off-by: Chris Boddy <chris@boddy.im>
Chris Boddy committed
Oct 11, 2018 at 09:53 UTC
8676b2ebf1d0ba4ac3abcde18ba16b8d34138129
1 file changed
+64
-10
test/sharness/t0184-http-proxy-over-p2p.sh
+64
-10
@@ -12,7 +12,8 @@ function serve_http_once() {
12
local body=$1
13
local status_code=${2:-"200 OK"}
14
local length=$(expr 1 + ${#body})
15
- echo -e "HTTP/1.1 $status_code\nContent-length: $length\n\n$body" | nc -l $WEB_SERVE_PORT &
15
+ REMOTE_SERVER_LOG=$(mktemp)
16
+ echo -e "HTTP/1.1 $status_code\nContent-length: $length\n\n$body" | nc -l $WEB_SERVE_PORT > $REMOTE_SERVER_LOG &
17
REMOTE_SERVER_PID=$!
18
}
19
@@ -21,7 +22,7 @@ function setup_receiver_ipfs() {
22
#
23
# setup RECEIVER IPFS daemon
24
#
24
- IPFS_PATH=$(mktemp -d)
25
+ local IPFS_PATH=$(mktemp -d)
26
RECEIVER_LOG=$IPFS_PATH/ipfs.log
27
28
ipfs init >> $RECEIVER_LOG 2>&1
@@ -46,7 +47,7 @@ function setup_sender_ipfs() {
47
#
48
# setup SENDER IPFS daemon
49
#
49
- IPFS_PATH=$(mktemp -d)
50
+ local IPFS_PATH=$(mktemp -d)
51
SENDER_LOG=$IPFS_PATH/ipfs.log
52
ipfs init >> $SENDER_LOG 2>&1
53
ipfs config --json Experimental.Libp2pStreamMounting true >> $SENDER_LOG 2>&1
@@ -115,38 +116,91 @@ function curl_send_proxy_request_and_check_response() {
116
fi
117
}
118
119
+function curl_send_multipart_form_request() {
120
+ local expected_status_code=$1
121
+ local FILE_PATH=$(mktemp)
122
+ FILE_CONTENT="curl will send a multipart-form POST request when sending a file which is handy"
123
+ echo $FILE_CONTENT > $FILE_PATH
124
+ #
125
+ # send multipart form request
126
+ #
127
+ STATUS_CODE=$(curl -s -F file=@$FILE_PATH http://localhost:5001/proxy/http/$RECEIVER_ID/test/index.txt)
128
+ #
129
+ # check status code
130
+ #
131
+ if [[ $STATUS_CODE -ne $expected_status_code ]];
132
+ then
133
+ echo -e "Found status-code "$STATUS_CODE", expected "$expected_status_code
134
+ return 1
135
+ fi
136
+ #
137
+ # check request method
138
+ #
139
+ if ! grep "POST /index.txt" $REMOTE_SERVER_LOG > /dev/null;
140
+ then
141
+ echo "Remote server request method/resource path was incorrect"
142
+ return 1
143
+ fi
144
+ #
145
+ # check content received
146
+ #
147
+ if ! grep "$FILE_CONTENT" $REMOTE_SERVER_LOG > /dev/null;
148
+ then
149
+ echo "form-data-content was not correct"
150
+ return 1
151
+ fi
152
+ #
153
+ # check request is multipart-form
154
+ #
155
+ if ! grep "Content-Type: multipart/form-data;" $REMOTE_SERVER_LOG > /dev/null;
156
+ then
157
+ echo "Request content-type was not multipart/form-data"
158
+ return 1
159
+ fi
160
+ return 0
161
+}
162
163
+teardown_sender_and_receiver
164
test_expect_success 'handle proxy http request propogates error response from remote' '
165
serve_http_once "SORRY GUYS, I LOST IT" "404 Not Found" &&
121
-setup_sender_and_receiver_ipfs &&
122
-curl_send_proxy_request_and_check_response 404 "SORRY GUYS, I LOST IT"
166
+ setup_sender_and_receiver_ipfs &&
167
+ curl_send_proxy_request_and_check_response 404 "SORRY GUYS, I LOST IT"
168
'
169
teardown_sender_and_receiver
170
teardown_remote_server
171
172
test_expect_success 'handle proxy http request sends bad-gateway when remote server not available ' '
173
setup_sender_and_receiver_ipfs &&
129
-curl_send_proxy_request_and_check_response 502 ""
174
+ curl_send_proxy_request_and_check_response 502 ""
175
'
176
teardown_sender_and_receiver
177
178
test_expect_success 'handle proxy http request ' '
179
serve_http_once "THE WOODS ARE LOVELY DARK AND DEEP" &&
135
-setup_sender_and_receiver_ipfs &&
136
-curl_send_proxy_request_and_check_response 200 "THE WOODS ARE LOVELY DARK AND DEEP"
180
+ setup_sender_and_receiver_ipfs &&
181
+ curl_send_proxy_request_and_check_response 200 "THE WOODS ARE LOVELY DARK AND DEEP"
182
'
183
teardown_sender_and_receiver
184
185
test_expect_success 'handle proxy http request invalid request' '
186
setup_sender_and_receiver_ipfs &&
142
-curl_check_response_code 400 DERPDERPDERP
187
+ curl_check_response_code 400 DERPDERPDERP
188
'
189
teardown_sender_and_receiver
190
191
test_expect_success 'handle proxy http request unknown proxy peer ' '
192
setup_sender_and_receiver_ipfs &&
148
-curl_check_response_code 502 unknown_peer/test/index.txt
193
+ curl_check_response_code 502 unknown_peer/test/index.txt
194
'
195
teardown_sender_and_receiver
196
197
+test_expect_success 'handle multipart/form-data http request' '
198
+serve_http_once "OK" &&
199
+setup_sender_and_receiver_ipfs &&
200
+curl_send_multipart_form_request
201
+'
202
+teardown_sender_and_receiver
203
+teardown_remote_server
204
+
205
+
206
test_done