t0235-cli-request.sh 1.27 KB
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1
#!/usr/bin/env bash
2 3 4 5 6 7 8 9 10 11 12
#
# Copyright (c) 2015 Jeromy Johnson
# MIT Licensed; see the LICENSE file in this repository.
#

test_description="test http requests made by cli"

. lib/test-lib.sh

test_init_ipfs

Steven Allen's avatar
Steven Allen committed
13 14 15 16
test_expect_success "start nc" '
  rm -f nc_out nc_outp nc_inp && mkfifo nc_inp nc_outp

  nc -k -l 127.0.0.1 5005 <nc_inp >nc_outp &
Łukasz Magiera's avatar
Łukasz Magiera committed
17
  NCPID=$!
Steven Allen's avatar
Steven Allen committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

  exec 6>nc_inp 7<nc_outp

  while ! nc -z 127.0.0.1 5005; do
    go-sleep 100ms
  done
'

test_expect_success "can make http request against nc server" '
  ipfs cat /ipfs/Qmabcdef --api /ip4/127.0.0.1/tcp/5005 &
  IPFSPID=$!

  # handle request
  while read line; do
    if [[ "$line" == "$(echo -e "\r")" ]]; then
      break
    fi
    echo "$line"
  done <&7 >nc_out &&

  echo -e "HTTP/1.1 200 OK\r" >&6 &&
  echo -e "Content-Type: text/plain\r" >&6 &&
  echo -e "Content-Length: 0\r" >&6 &&
  echo -e "\r" >&6 &&
  exec 6<&- &&

  # Wait for IPFS
  wait $IPFSPID
'

test_expect_success "stop nc" '
  kill "$NCPID" && wait "$NCPID" || true
50 51 52
'

test_expect_success "output does not contain multipart info" '
Łukasz Magiera's avatar
Łukasz Magiera committed
53
  test_expect_code 1 grep multipart nc_out
54 55 56
'

test_expect_success "request looks good" '
Łukasz Magiera's avatar
Łukasz Magiera committed
57
  grep "POST /api/v0/cat" nc_out
58 59 60
'

test_expect_success "api flag does not appear in request" '
Łukasz Magiera's avatar
Łukasz Magiera committed
61
  test_expect_code 1 grep "api=/ip4" nc_out
62 63 64
'

test_done