Last active
December 8, 2015 20:43
-
-
Save Emerson/919fe12b5d4289106880 to your computer and use it in GitHub Desktop.
Basic Response and Redirect Assertions in Bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
andhttps
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.