Skip to content

Commit

Permalink
added capability to store default curl options when calling resty fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
micha committed Mar 20, 2011
1 parent 12bffc4 commit 76d1b6c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,20 @@ to make a GET request to `/Something?foo=bar&baz=baf` you would do:
This sends the name/value pairs specified with the `-d` options as a query
string in the URL.

Default Curl Options
--------------------

Sometimes you want to send some options to curl for every request. It
would be tedious to have to repeat these options constantly. To tell
resty to always add certain curl options you can specify those options
when you call resty to set the URI base. For example:

resty example.com:8080 -H "Accept: application/json" -u user:pass

Every subsequent request will have the `-H "Accept:..."` and `-u user:...`
options automatically added. Each time resty is called this option list
is reset.

Per-Host/Per-Method Curl Configuration Files
--------------------------------------------

Expand Down
7 changes: 6 additions & 1 deletion resty
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ function resty() {

case "$method" in
HEAD|OPTIONS|GET|DELETE|POST|PUT|TRACE)
for i in ${!_resty_opt_*};do
curlopt[$((j+${i#_resty_opt_}-1))]="${!i}"
done
dat=$( ( [ "$hasdata" = "yes" ] \
&& ( ( [ -n "$1" ] && [ "${1#-}" = "$1" ] && echo "$1") \
|| [ ! -t 0 ] && echo "@-") ) || echo)
Expand Down Expand Up @@ -90,12 +93,14 @@ function resty() {
fi
;;
http://*|https://*)
unset ${!_resty_opt_*}
for i in ${!curlopt[*]};do export _resty_opt_$i="${curlopt[$i]}";done
echo "$method" |grep '\*' >/dev/null || method="${method}*"
(echo "$method" |tee "${_resty_nohistory:-$host}") |cat 1>&2 \
&& _resty_host="$method"
;;
*)
resty "http://$method"
resty "http://$method" "${curlopt[@]}"
;;
esac
}
Expand Down
7 changes: 3 additions & 4 deletions test/runtests
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/usr/bin/env bash

TEST=$1
PORT=${3:-9090}

. ../resty -W 127.0.0.1:$PORT
. ../resty -W

# run tests to check that the correct request is being made
( j=1
while true; do
gnetcat -l -w 1 -p $PORT -e './lilhttpd -I' localhost || break
gnetcat -l -w 1 -p 9090 -e './lilhttpd -I' localhost || break
grep '^# TEST [0-9][0-9]*' $TEST |head -$j |tail -1 |sed 's/^#/# [REQ]/'
j=$((j+1))
done )&
Expand All @@ -20,7 +19,7 @@ wait
# run tests again, this time checking actual output
( j=1
while true; do
gnetcat -l -w 1 -p $PORT -e ./lilhttpd localhost || break
gnetcat -l -w 1 -p 9090 -e ./lilhttpd localhost || break
grep '^# TEST [0-9][0-9]*' $TEST |head -$j |tail -1 |sed 's/^#/# [OUT]/'
j=$((j+1))
done )&
Expand Down
14 changes: 14 additions & 0 deletions test/tests
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

resty 127.0.0.1:9090 2> /dev/null

# TEST 1: basic GET request
GET /simple.html

Expand Down Expand Up @@ -33,3 +36,14 @@ POST /simple.html -u "user:secret" -H "Accept: application/json" < simple.json

# TEST 12: json pretty-print formatting
GET /simple.json | ../pp 2>/dev/null

resty 127.0.0.1:9090 -H "Accept: application/json" -u user:pass 2> /dev/null

# TEST 13: make sure the default options are correctly set
GET /simple.json

resty 127.0.0.1:9090 2> /dev/null

# TEST 14: make sure the default options are correctly unset
GET /simple.json

23 changes: 19 additions & 4 deletions test/tests.out
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ here is some data
HTTP/1.0 200 OK
Cache-Control: private
Content-Type: text/plain
Server: lilhttpd 1.0
Server: lilhttpd/1.0
Connection: close
Content-Length: 168
# [REQ] TEST 7: Raw response
Expand All @@ -61,7 +61,7 @@ Accept: */*
< HTTP/1.0 200 OK
< Cache-Control: private
< Content-Type: text/plain
< Server: lilhttpd 1.0
< Server: lilhttpd/1.0
< Connection: close
< Content-Length: 164
<
Expand Down Expand Up @@ -95,6 +95,17 @@ Content-Length: 66
Content-Type: application/x-www-form-urlencoded
{"foo":[1,2,3],"bar":{"dog":"woof","cat":"meow","fish":"banana"}}
# [REQ] TEST 12: json pretty-print formatting
# [REQ] TEST 13: make sure the default options are correctly set
GET /simple.json HTTP/1.1
Authorization: Basic dXNlcjpwYXNz
User-Agent: curl/7.21.2 (x86_64-apple-darwin10.4.0) libcurl/7.21.2 OpenSSL/1.0.0d zlib/1.2.5 libidn/1.19
Host: 127.0.0.1:9090
Accept: application/json
# [REQ] TEST 14: make sure the default options are correctly unset
GET /simple.json HTTP/1.1
User-Agent: curl/7.21.2 (x86_64-apple-darwin10.4.0) libcurl/7.21.2 OpenSSL/1.0.0d zlib/1.2.5 libidn/1.19
Host: 127.0.0.1:9090
Accept: */*
# [OUT] TEST 1: basic GET request
[1]fork me on github

Expand Down Expand Up @@ -254,7 +265,7 @@ http://github.com/micha/resty
HTTP/1.0 200 OK
Cache-Control: private
Content-Type: text/html; charset=us-ascii
Server: lilhttpd 1.0
Server: lilhttpd/1.0
Connection: close
Content-Length: 2340
# [OUT] TEST 7: Raw response
Expand Down Expand Up @@ -353,7 +364,7 @@ Content-Length: 2340
< HTTP/1.0 200 OK
< Cache-Control: private
< Content-Type: text/html; charset=us-ascii
< Server: lilhttpd 1.0
< Server: lilhttpd/1.0
< Connection: close
< Content-Length: 2340
<
Expand Down Expand Up @@ -496,3 +507,7 @@ http://github.com/micha/resty
]
}

# [OUT] TEST 13: make sure the default options are correctly set
{"foo":[1,2,3],"bar":{"dog":"woof","cat":"meow","fish":"banana"}}
# [OUT] TEST 14: make sure the default options are correctly unset
{"foo":[1,2,3],"bar":{"dog":"woof","cat":"meow","fish":"banana"}}

0 comments on commit 76d1b6c

Please sign in to comment.