Skip to content

Instantly share code, notes, and snippets.

@dandorman
Created December 31, 2019 22:34
Show Gist options
  • Save dandorman/5a70353a0f6902e721f127bf0786f9db to your computer and use it in GitHub Desktop.
Save dandorman/5a70353a0f6902e721f127bf0786f9db to your computer and use it in GitHub Desktop.
A simple test script for a web server's If-Modified-Since behavior
#!/bin/bash
set -e
echo "Setting last_mod to before foo is created..."
last_mod=$(date -u +"%a, %d %b %Y %H:%M:%S GMT")
sleep 2
touch foo
function rm_foo {
rm -f foo
}
trap rm_foo EXIT
echo "Sanity check (should get 200)"
actual=$(curl -si localhost:8000/foo | head -1 | awk '{ print $2 }')
expected="200"
if [ $actual = $expected ]; then
echo ✅
else
echo ❌ $actual
fi
echo "Request with old Last-Modified (should get 200)"
actual=$(curl -siH "If-Modified-Since: $last_mod" localhost:8000/foo | head -1 | awk '{ print $2 }')
expected=200
if [ $actual = $expected ]; then
echo ✅
else
echo ❌ $actual
fi
last_mod=$(curl -si localhost:8000/foo | grep Last-Modified | sed -e 's/Last-Modified: //')
echo "Request with file's Last-Modified (should get 304)"
actual=$(curl -siH "If-Modified-Since: $last_mod" localhost:8000/foo | head -1 | awk '{ print $2 }')
expected=304
if [ $actual = $expected ]; then
echo ✅
else
echo ❌ $actual
fi
echo "Sleeping then updating..."
sleep 2
touch foo
echo "Request updated file with Last-Modified (should get 200)"
actual=$(curl -siH "If-Modified-Since: $last_mod" localhost:8000/foo | head -1 | awk '{ print $2 }')
expected=200
if [ $actual = $expected ]; then
echo ✅
else
echo ❌ $actual
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment