master
sh 87 lines 1.9 KB
Raw
1 #!/usr/bin/env bash
2 #
3 # Copyright (c) 2015 Jeromy Johnson
4 # MIT Licensed; see the LICENSE file in this repository.
5 #
6
7 test_description="test http requests made by cli"
8
9 . lib/test-lib.sh
10
11 if ! test_have_prereq SOCAT; then
12 skip_all="skipping '$test_description': socat is not available"
13 test_done
14 fi
15
16
17 test_init_ipfs
18
19 test_expect_success "start nc" '
20 rm -f nc_out nc_outp nc_inp && mkfifo nc_inp nc_outp
21
22 socat PIPE:nc_inp!!PIPE:nc_outp tcp-listen:5005,fork,max-children=1,bind=127.0.0.1 &
23 NCPID=$!
24
25 exec 6>nc_inp 7<nc_outp
26
27 socat /dev/null tcp:127.0.01:5005,retry=10
28 '
29
30 test_expect_success "can make http request against nc server" '
31 ipfs cat /ipfs/Qmabcdef --api /dns4/localhost/tcp/5005 &
32 IPFSPID=$!
33
34 # handle request for /api/v0/version
35 while read line; do
36 if [[ "$line" == "$(echo -e "\r")" ]]; then
37 break
38 fi
39 echo "$line"
40 done <&7 >nc_out &&
41
42 echo -e "HTTP/1.1 200 OK\r" >&6 &&
43 echo -e "Content-Type: application/json\r" >&6 &&
44 echo -e "Content-Length: 21\r" >&6 &&
45 echo -e "\r" >&6 &&
46 echo -e "{\"Version\":\"0.23.0\"}\r" >&6 &&
47 echo -e "\r" >&6 &&
48
49 # handle request for /api/v0/cat
50 while read line; do
51 if [[ "$line" == "$(echo -e "\r")" ]]; then
52 break
53 fi
54 echo "$line"
55 done <&7 >nc_out &&
56
57 echo -e "HTTP/1.1 200 OK\r" >&6 &&
58 echo -e "Content-Type: text/plain\r" >&6 &&
59 echo -e "Content-Length: 0\r" >&6 &&
60 echo -e "\r" >&6 &&
61 exec 6<&- &&
62
63 # Wait for IPFS
64 wait $IPFSPID
65 '
66
67 test_expect_success "stop nc" '
68 kill "$NCPID" && wait "$NCPID" || true
69 '
70
71 test_expect_success "output does not contain multipart info" '
72 test_expect_code 1 grep multipart nc_out
73 '
74
75 test_expect_success "request looks good" '
76 grep "POST /api/v0/cat" nc_out
77 '
78
79 test_expect_success "api flag does not appear in request" '
80 test_expect_code 1 grep "api=/ip4" nc_out
81 '
82
83 test_expect_success "host has dns name not ip address" '
84 grep "Host: localhost:5005" nc_out
85 '
86
87 test_done