Skip to content

Instantly share code, notes, and snippets.

@Emerson
Last active December 8, 2015 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Emerson/919fe12b5d4289106880 to your computer and use it in GitHub Desktop.
Save Emerson/919fe12b5d4289106880 to your computer and use it in GitHub Desktop.
Basic Response and Redirect Assertions in Bash
#!/bin/bash
NC='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
function assert_success() {
request=`curl -s --head $1`
success=`grep "HTTP/1.1 200 OK" <<< "${request}"`
if [ -z "${success}" ]
then
printf "${NC}$1 [${RED}200 ERROR${NC}]\n"
else
printf "${NC}$1 [${GREEN}200 OK${NC}]\n"
fi
}
function assert_redirect() {
request=`curl -s --head $1`
response_code=`grep "HTTP/1.1 301 Moved Permanently" <<< "${request}"`
redirect_location=`grep "Location: $2" <<< "${request}"`
if [ -z "${response_code}" ] || [ -z "${redirect_location}" ]
then
printf "${NC}$1 => $2 [${RED}301 ERROR${NC}]\n"
else
printf "${NC}$1 => $2 [${GREEN}301 OK${NC}]\n"
fi
}
# Usage
# –––––
#
# assert_success "https://www.amazon.com"
# assert_redirect "http://amazon.com" "https://www.amazon.com"
#
@Emerson
Copy link
Author

Emerson commented Dec 8, 2015

usage

Why?

Configuring servers (especially if they are live) can be scary if you're not using Ansible, Chef, or some other provisioning tool. If you have a bunch of complicated http and https settings you need to implement it's worth writing a simple test script that asserts domains and servers are responding like you expect.

How does this help?

The simple functions included in this Gist make it super easy to write a basic smoke test. Just include this Gist in a shell file, add some assertions at the bottom of the file, and run it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment