Using Shell Script to test your server
Sometimes, depending of your team or project language, is much easier to use shell script to test your API Server. After some time using this template, i resolved to share. I hope that helps others developers too.
Depends on:
- bash
- curl
Tested on Ubuntu 13.04 64bit.
#!/bin/bash
URL=http://localhost:8080
## Unit-Testable Shell Scripts (http://eradman.com/posts/ut-shell-scripts.html)
typeset -i tests_run=0
function try { this="$1"; }
trap 'printf "$0: exit code $? on line $LINENO\nFAIL: $this\n"; exit 1' ERR
function assert {
let tests_run+=1
[ "$1" = "$2" ] && { echo -n "."; return; }
printf "\nFAIL: $this\n'$1' != '$2'\n"; exit 1
}
## end
###############################################################
try "Example of GET and test for 404 status"
out=$(curl -s -w "%{http_code}" $URL)
assert "404" "$out"
try "Example of POST XML"
# Post xml (from hello.xml file) on /hello
out=$(cat test/hello.xml | curl -s -H "Content-Type: text/xml" -d @- \
-X POST $URL/hello)
assert "Hello World" "$out"
###############################################################
echo
echo "PASS: $tests_run tests run"
Source:
Unit-Testable Shell Scripts http://eradman.com/posts/ut-shell-scripts.html
Written by Roger Leite
Related protips
2 Responses
Gist here: https://gist.github.com/rogerleite/5869576
over 1 year ago
·
Or you can just use
curl -i http://localhost:8080
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Test
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#