From 8676b2ebf1d0ba4ac3abcde18ba16b8d34138129 Mon Sep 17 00:00:00 2001 From: Chris Boddy Date: Thu, 11 Oct 2018 09:53:10 +0100 Subject: [PATCH] [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 --- test/sharness/t0184-http-proxy-over-p2p.sh | 74 +++++++++++++++++++--- 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/test/sharness/t0184-http-proxy-over-p2p.sh b/test/sharness/t0184-http-proxy-over-p2p.sh index 198d80d0c..382141a5f 100755 --- a/test/sharness/t0184-http-proxy-over-p2p.sh +++ b/test/sharness/t0184-http-proxy-over-p2p.sh @@ -12,7 +12,8 @@ function serve_http_once() { local body=$1 local status_code=${2:-"200 OK"} local length=$(expr 1 + ${#body}) - echo -e "HTTP/1.1 $status_code\nContent-length: $length\n\n$body" | nc -l $WEB_SERVE_PORT & + REMOTE_SERVER_LOG=$(mktemp) + echo -e "HTTP/1.1 $status_code\nContent-length: $length\n\n$body" | nc -l $WEB_SERVE_PORT > $REMOTE_SERVER_LOG & REMOTE_SERVER_PID=$! } @@ -21,7 +22,7 @@ function setup_receiver_ipfs() { # # setup RECEIVER IPFS daemon # - IPFS_PATH=$(mktemp -d) + local IPFS_PATH=$(mktemp -d) RECEIVER_LOG=$IPFS_PATH/ipfs.log ipfs init >> $RECEIVER_LOG 2>&1 @@ -46,7 +47,7 @@ function setup_sender_ipfs() { # # setup SENDER IPFS daemon # - IPFS_PATH=$(mktemp -d) + local IPFS_PATH=$(mktemp -d) SENDER_LOG=$IPFS_PATH/ipfs.log ipfs init >> $SENDER_LOG 2>&1 ipfs config --json Experimental.Libp2pStreamMounting true >> $SENDER_LOG 2>&1 @@ -115,38 +116,91 @@ function curl_send_proxy_request_and_check_response() { fi } +function curl_send_multipart_form_request() { + local expected_status_code=$1 + local FILE_PATH=$(mktemp) + FILE_CONTENT="curl will send a multipart-form POST request when sending a file which is handy" + echo $FILE_CONTENT > $FILE_PATH + # + # send multipart form request + # + STATUS_CODE=$(curl -s -F file=@$FILE_PATH http://localhost:5001/proxy/http/$RECEIVER_ID/test/index.txt) + # + # check status code + # + if [[ $STATUS_CODE -ne $expected_status_code ]]; + then + echo -e "Found status-code "$STATUS_CODE", expected "$expected_status_code + return 1 + fi + # + # check request method + # + if ! grep "POST /index.txt" $REMOTE_SERVER_LOG > /dev/null; + then + echo "Remote server request method/resource path was incorrect" + return 1 + fi + # + # check content received + # + if ! grep "$FILE_CONTENT" $REMOTE_SERVER_LOG > /dev/null; + then + echo "form-data-content was not correct" + return 1 + fi + # + # check request is multipart-form + # + if ! grep "Content-Type: multipart/form-data;" $REMOTE_SERVER_LOG > /dev/null; + then + echo "Request content-type was not multipart/form-data" + return 1 + fi + return 0 +} +teardown_sender_and_receiver test_expect_success 'handle proxy http request propogates error response from remote' ' serve_http_once "SORRY GUYS, I LOST IT" "404 Not Found" && -setup_sender_and_receiver_ipfs && -curl_send_proxy_request_and_check_response 404 "SORRY GUYS, I LOST IT" + setup_sender_and_receiver_ipfs && + curl_send_proxy_request_and_check_response 404 "SORRY GUYS, I LOST IT" ' teardown_sender_and_receiver teardown_remote_server test_expect_success 'handle proxy http request sends bad-gateway when remote server not available ' ' setup_sender_and_receiver_ipfs && -curl_send_proxy_request_and_check_response 502 "" + curl_send_proxy_request_and_check_response 502 "" ' teardown_sender_and_receiver test_expect_success 'handle proxy http request ' ' serve_http_once "THE WOODS ARE LOVELY DARK AND DEEP" && -setup_sender_and_receiver_ipfs && -curl_send_proxy_request_and_check_response 200 "THE WOODS ARE LOVELY DARK AND DEEP" + setup_sender_and_receiver_ipfs && + curl_send_proxy_request_and_check_response 200 "THE WOODS ARE LOVELY DARK AND DEEP" ' teardown_sender_and_receiver test_expect_success 'handle proxy http request invalid request' ' setup_sender_and_receiver_ipfs && -curl_check_response_code 400 DERPDERPDERP + curl_check_response_code 400 DERPDERPDERP ' teardown_sender_and_receiver test_expect_success 'handle proxy http request unknown proxy peer ' ' setup_sender_and_receiver_ipfs && -curl_check_response_code 502 unknown_peer/test/index.txt + curl_check_response_code 502 unknown_peer/test/index.txt ' teardown_sender_and_receiver +test_expect_success 'handle multipart/form-data http request' ' +serve_http_once "OK" && +setup_sender_and_receiver_ipfs && +curl_send_multipart_form_request +' +teardown_sender_and_receiver +teardown_remote_server + + test_done