Permalink
Browse files

added capability to store default curl options when calling resty fun…

…ction
  • Loading branch information...
1 parent 12bffc4 commit 76d1b6c25db1587d41d93292e225155e7ed1eb2e @micha committed Mar 20, 2011
Showing with 56 additions and 9 deletions.
  1. +14 −0 README.md
  2. +6 −1 resty
  3. +3 −4 test/runtests
  4. +14 −0 test/tests
  5. +19 −4 test/tests.out
View
@@ -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
--------------------------------------------
View
7 resty
@@ -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)
@@ -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
}
View
@@ -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 )&
@@ -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 )&
View
@@ -1,3 +1,6 @@
+
+resty 127.0.0.1:9090 2> /dev/null
+
# TEST 1: basic GET request
GET /simple.html
@@ -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
+
View
@@ -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
@@ -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
<
@@ -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
@@ -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
@@ -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
<
@@ -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.