| 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 dns resolution of api endpoint 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 | test_init_ipfs |
| 17 | |
| 18 | test_expect_success "start nc" ' |
| 19 | rm -f nc_out nc_outp nc_inp && mkfifo nc_inp nc_outp |
| 20 | |
| 21 | socat PIPE:nc_inp!!PIPE:nc_outp tcp-listen:5006,fork,max-children=1,bind=127.0.0.1 & |
| 22 | NCPID=$! |
| 23 | |
| 24 | exec 6>nc_inp 7<nc_outp |
| 25 | |
| 26 | socat /dev/null tcp:127.0.01:5006,retry=10 |
| 27 | ' |
| 28 | |
| 29 | test_expect_success "can make http request against dns resolved nc server" ' |
| 30 | ipfs cat /ipfs/Qmabcdef --api /dns4/localhost/tcp/5006 & |
| 31 | IPFSPID=$! |
| 32 | |
| 33 | # handle request for /api/v0/version |
| 34 | while read line; do |
| 35 | if [[ "$line" == "$(echo -e "\r")" ]]; then |
| 36 | break |
| 37 | fi |
| 38 | echo "$line" |
| 39 | done <&7 >nc_out && |
| 40 | |
| 41 | echo -e "HTTP/1.1 200 OK\r" >&6 && |
| 42 | echo -e "Content-Type: application/json\r" >&6 && |
| 43 | echo -e "Content-Length: 21\r" >&6 && |
| 44 | echo -e "\r" >&6 && |
| 45 | echo -e "{\"Version\":\"0.23.0\"}\r" >&6 && |
| 46 | echo -e "\r" >&6 && |
| 47 | |
| 48 | # handle request for /api/v0/cat |
| 49 | while read line; do |
| 50 | if [[ "$line" == "$(echo -e "\r")" ]]; then |
| 51 | break |
| 52 | fi |
| 53 | echo "$line" |
| 54 | done <&7 >nc_out && |
| 55 | |
| 56 | echo -e "HTTP/1.1 200 OK\r" >&6 && |
| 57 | echo -e "Content-Type: text/plain\r" >&6 && |
| 58 | echo -e "Content-Length: 0\r" >&6 && |
| 59 | echo -e "\r" >&6 && |
| 60 | exec 6<&- && |
| 61 | |
| 62 | # Wait for IPFS |
| 63 | wait $IPFSPID |
| 64 | ' |
| 65 | |
| 66 | test_expect_success "stop nc" ' |
| 67 | kill "$NCPID" && wait "$NCPID" || true |
| 68 | ' |
| 69 | |
| 70 | test_expect_success "request was received by local nc server" ' |
| 71 | grep "POST /api/v0/cat" nc_out |
| 72 | ' |
| 73 | |
| 74 | test_done |