Skip to content

Instantly share code, notes, and snippets.

@dradford
Last active December 29, 2015 12:38
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 dradford/7671342 to your computer and use it in GitHub Desktop.
Save dradford/7671342 to your computer and use it in GitHub Desktop.
This file is a simple test harness to validate mobile redirects work. Add test cases at the bottom
# Script for testing whether mobile redirects work
#
# It allows you to pass a cookie for a mobile device
# that has chosen to use the desktop version, as per
# this article:
# http://www.cyberciti.biz/faq/setting-up-nginx-to-redirect-mobile-users-to-subdomain/
#
# The cookie is: force_dt_cookie="desktop=true"
#
# usage ./test_mobile_redirects.sh [base uri]
#
# See bottom of file for tests
#common_options="-s --cookie cjar --cookie-jar cjar --location --head -u user:pass"
common_options="-s --cookie cjar --cookie-jar cjar --location --head"
uad='UserAgentString'
uam='Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3'
uri=$1
curl --cookie-jar cjar --output /dev/null "$uri"
test_redirect() {
test_uri=$uri$3
if $1 ; then
redirect=$(curl $common_options -A "$2" $test_uri --cookie force_dt_cookie="desktop=true" | tr -d '\r' | grep Location)
else
redirect=$(curl $common_options -A "$2" $test_uri | tr -d '\r' | grep Location)
fi
if [ -z "$redirect" ] ; then
if [ -z "$4" ] ; then
echo "Passed: No redirect"
else
echo "Failed: Should have redirected $3 --> $4"
fi
else
# set -o xtrace
r_arr=($redirect)
if [ "$(echo ${r_arr[1]} | tr '[A-Z]' '[a-z]')" = "$(echo $uri$4 | tr '[A-Z]' '[a-z]')" ] ; then
echo "Passed: $3 --> $4"
else
echo "Failed: actually redirected $3 --> ${r_arr[1]}"
fi
fi
}
# Tests
# uad = user agent desktop
# uam = user agent mobile
# Usage:
# test_redirect [send cookie?] [uad/uam] [source] [destination]
test_redirect false "$uad" "" ""
test_redirect true "$uam" "" ""
test_redirect false "$uam" "/" "/home"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment