@cryptotaxi247 / kubo / commits / 1945e44f7

[http_proxy_over_p2p] Add sharness tests for http-proxy-over-p2p

License: MIT Signed-off-by: Chris Boddy <chris@boddy.im>

Chris Boddy committed Sep 29, 2018 at 16:09 UTC 1945e44f7fa786682287b0a775675646d18c9a63
2 files changed +153
core/corehttp/proxy.go
+1
@@ -43,6 +43,7 @@ func ProxyOption() ServeOption {
43
44 s := bufio.NewReader(stream)
45 proxyResponse, err := http.ReadResponse(s, proxyReq)
46 +
47 defer func() { proxyResponse.Body.Close() }()
48 if err != nil {
49 msg := fmt.Sprintf("Failed to send request to stream '%v' to peer '%v'", parsedRequest.name, parsedRequest.target)
test/sharness/t0184-http-proxy-over-p2p.sh new
+152
@@ -0,0 +1,152 @@
1 +#!/usr/bin/env bash
2 +
3 +test_description="Test http proxy over p2p"
4 +
5 +. lib/test-lib.sh
6 +WEB_SERVE_PORT=5099
7 +
8 +function serve_http_once() {
9 + #
10 + # one shot http server (via nc) with static body
11 + #
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 &
16 + REMOTE_SERVER_PID=$!
17 +}
18 +
19 +
20 +function setup_receiver_ipfs() {
21 + #
22 + # setup RECEIVER IPFS daemon
23 + #
24 + IPFS_PATH=$(mktemp -d)
25 + RECEIVER_LOG=$IPFS_PATH/ipfs.log
26 +
27 + ipfs init >> $RECEIVER_LOG 2>&1
28 + ipfs config --json Experimental.Libp2pStreamMounting true >> $RECEIVER_LOG 2>&1
29 + ipfs config --json Addresses.API "\"/ip4/127.0.0.1/tcp/6001\"" >> $RECEIVER_LOG 2>&1
30 + ipfs config --json Addresses.Gateway "\"/ip4/127.0.0.1/tcp/8081\"" >> $RECEIVER_LOG 2>&1
31 + ipfs config --json Addresses.Swarm "[\"/ip4/0.0.0.0/tcp/7001\", \"/ip6/::/tcp/7001\"]" >> $RECEIVER_LOG 2>&1
32 + ipfs daemon >> $RECEIVER_LOG 2>&1 &
33 + RECEIVER_PID=$!
34 + # wait for daemon to start.. maybe?
35 + # ipfs id returns empty string if we don't wait here..
36 + sleep 5
37 + RECEIVER_ID=$(ipfs id -f "<id>")
38 + #
39 + # start a p2p listener on RECIVER to the HTTP server with our content
40 + #
41 + ipfs p2p listen /x/test /ip4/127.0.0.1/tcp/$WEB_SERVE_PORT >> $RECEIVER_LOG 2>&1
42 +}
43 +
44 +
45 +function setup_sender_ipfs() {
46 + #
47 + # setup SENDER IPFS daemon
48 + #
49 + IPFS_PATH=$(mktemp -d)
50 + SENDER_LOG=$IPFS_PATH/ipfs.log
51 + ipfs init >> $SENDER_LOG 2>&1
52 + ipfs config --json Experimental.Libp2pStreamMounting true >> $SENDER_LOG 2>&1
53 + ipfs daemon >> $SENDER_LOG 2>&1 &
54 + SENDER_PID=$!
55 + sleep 5
56 +}
57 +
58 +
59 +function teardown_sender_and_receiver() {
60 + kill -9 $SENDER_PID $RECEIVER_PID > /dev/null 2>&1
61 + sleep 5
62 +}
63 +
64 +function curl_check_response_code() {
65 + local expected_status_code=$1
66 + local path_stub=${2:-http/$RECEIVER_ID/test/index.txt}
67 + local status_code=$(curl -s --write-out %{http_code} --output /dev/null http://localhost:5001/proxy/$path_stub)
68 +
69 + if [[ $status_code -ne $expected_status_code ]];
70 + then
71 + echo "Found status-code "$status_code", expected "$expected_status_code
72 + return 1
73 + fi
74 +
75 + return 0
76 +}
77 +
78 +function curl_send_proxy_request_and_check_response() {
79 + local expected_status_code=$1
80 + local expected_content=$2
81 +
82 + #
83 + # make a request to SENDER_IPFS via the proxy endpoint
84 + #
85 + CONTENT_PATH=$(mktemp)
86 + STATUS_CODE=$(curl -s -o $CONTENT_PATH --write-out %{http_code} http://localhost:5001/proxy/http/$RECEIVER_ID/test/index.txt)
87 +
88 + #
89 + # check status code
90 + #
91 + if [[ $STATUS_CODE -ne $expected_status_code ]];
92 + then
93 + echo "Found status-code "$STATUS_CODE", expected "$expected_status_code
94 + return 1
95 + fi
96 +
97 + #
98 + # check content
99 + #
100 + RESPONSE_CONTENT=$(tail -n 1 $CONTENT_PATH)
101 + if [[ "$RESPONSE_CONTENT" == "$expected_content" ]];
102 + then
103 + return 0
104 + else
105 + echo -e "Found response content:\n'"$RESPONSE_CONTENT"'\nthat differs from expected content:\n'"$expected_content"'"
106 + return 1
107 + fi
108 +}
109 +
110 +
111 +#test_expect_success 'handle proxy http request propogates error response from remote' '
112 +#serve_http_once "SORRY GUYS, I LOST IT" "404 Not Found" &&
113 +#setup_receiver_ipfs &&
114 +#setup_sender_ipfs &&
115 +#curl_send_proxy_request_and_check_response 404 "SORRY GUYS, I LOST IT"
116 +#'
117 +#kill -9 $REMOTE_SERVER_PID
118 +#teardown_sender_and_receiver
119 +
120 +test_expect_success 'handle proxy http request when remote server not available ' '
121 +setup_receiver_ipfs &&
122 +setup_sender_ipfs &&
123 +curl_check_response_code "000"
124 +'
125 +teardown_sender_and_receiver
126 +
127 +test_expect_success 'handle proxy http request ' '
128 +serve_http_once "THE WOODS ARE LOVELY DARK AND DEEP" &&
129 +setup_receiver_ipfs &&
130 +setup_sender_ipfs &&
131 +curl_send_proxy_request_and_check_response 200 "THE WOODS ARE LOVELY DARK AND DEEP"
132 +'
133 +kill -9 $REMOTE_SERVER_PID
134 +teardown_sender_and_receiver
135 +
136 +
137 +
138 +test_expect_success 'handle proxy http request invalid request' '
139 +setup_receiver_ipfs &&
140 +setup_sender_ipfs &&
141 +curl_check_response_code 400 DERPDERPDERP
142 +'
143 +teardown_sender_and_receiver
144 +
145 +test_expect_success 'handle proxy http request unknown proxy peer ' '
146 +setup_receiver_ipfs &&
147 +setup_sender_ipfs &&
148 +curl_check_response_code 400 http/unknown_peer/test/index.txt
149 +'
150 +teardown_sender_and_receiver
151 +
152 +test_done