Last active
January 25, 2017 17:12
-
-
Save fertapric/6d33bc936b980a0d2faa83a6fc5624a4 to your computer and use it in GitHub Desktop.
Poor man's test suite in Bash
This file contains 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 | |
function setup { | |
[ -z "$FILES_HOST"] && FILES_HOST=localhost | |
} | |
function teardown { | |
echo "REMOVE ALL THE FILES FROM BUCKET" | |
} | |
function test_healthcheck_endpoint { | |
response=$(curl --verbose $FILES_HOST/check 2>&1) | |
assert_contains "$response" "< HTTP/1.1 200 OK" && \ | |
assert_contains "$response" "Content-Type: application/json" && \ | |
assert_contains "$response" '{ "service": "files", "status": "healthy" }' | |
} | |
function test_upload_endpoint_rejects_get_requests { | |
response=$(request GET) | |
assert_contains "$response" "< HTTP/1.1 404 Not Found" | |
} | |
function test_upload_endpoint_rejects_put_requests { | |
response=$(request PUT) | |
assert_contains "$response" "< HTTP/1.1 404 Not Found" | |
} | |
function test_upload_endpoint_rejects_delete_requests { | |
response=$(request DELETE) | |
assert_contains "$response" "< HTTP/1.1 404 Not Found" | |
} | |
function test_upload_endpoint_rejects_requests_without_content_type { | |
response=$(request POST "") | |
assert_contains "$response" "< HTTP/1.1 415 Unsupported Media Type" | |
} | |
function test_upload_endpoint_rejects_requests_with_unsupported_content_type { | |
response=$(request POST application/json) | |
assert_contains "$response" "< HTTP/1.1 415 Unsupported Media Type" | |
} | |
function test_rejects_multipart_formdata_requests { | |
echo "YES" | |
} | |
function test_uploads_image_png_files { | |
filepath=${BASH_SOURCE%/*}/fixtures/test_4x4.png | |
response=$(request POST image/png --data-binary @"$filepath") | |
assert_file_uploaded_equal $(get_file_url "$response") $filepath | |
} | |
function test_resizes_image_png_files { | |
resized_filepath=${BASH_SOURCE%/*}/fixtures/test_2x2.png | |
filepath=${BASH_SOURCE%/*}/fixtures/test_4x4.png | |
response=$(request POST image/png --data-binary @"$filepath") | |
assert_file_uploaded_equal "$(get_file_url "$response")?w=2" $resized_filepath | |
} | |
function test_uploads_image_jpeg_files { | |
echo "YES" | |
} | |
function test_resizes_image_jpeg_files { | |
echo "YES" | |
} | |
function test_uploads_image_gif_files { | |
echo "YES" | |
} | |
function test_resizes_image_gif_files { | |
echo "YES" | |
} | |
function test_resizing_parameters { | |
echo "YES" | |
} | |
function test_resizing_with_non_compatible_image_files { | |
echo "YES" | |
} | |
function test_uploads_text_plain_files { | |
filepath=${BASH_SOURCE%/*}/fixtures/test.txt | |
response=$(request POST text/plain --data-binary @"$filepath") | |
assert_file_uploaded_equal $(get_file_url "$response") $filepath | |
} | |
function test_ignores_resizing_parameters_with_text_plain_files { | |
filepath=${BASH_SOURCE%/*}/fixtures/test.txt | |
response=$(request POST text/plain --data-binary @"$filepath") | |
assert_file_uploaded_equal "$(get_file_url "$response")?w=20" $filepath | |
} | |
function request { | |
method=$1; shift | |
content_type=$1; shift | |
curl --verbose --request $method --header "Content-Type: $content_type" $@ $FILES_HOST 2>&1 | |
} | |
function get_file_url { | |
echo "$1" | grep "X-File-URL" | cut -d " " -f 3 | tr -d '\r' | |
} | |
function assert_file_uploaded_equal { | |
file_url=$1 | |
file_url_with_params=${file_url%\?*} | |
filename=${file_url_with_params##*/} | |
curl -o /tmp/$filename $file_url 2>&1 | |
cmp /tmp/$filename $2 | |
} | |
function assert_contains { | |
echo "$1" | grep "$2" | |
} | |
function run_tests { | |
SECONDS=0 | |
failures_count=0 | |
test_count=0 | |
tests=$(grep -E "function test_" $0 | grep -v grep | cut -d " " -f 2) | |
setup > /dev/null 2>&1 | |
for test in $tests | |
do | |
test_count=$((test_count+1)) | |
test_output=$(eval $test) | |
if [ $? -eq 0 ] | |
then | |
printf "\33[0;32m.\33[0m" | |
else | |
failures_count=$((failures_count+1)) | |
printf "\n\33[0;31mE\n\nTest $test failed\n\nOutput:\n\n$test_output\33[0m\n" | |
fi | |
done | |
teardown > /dev/null 2>&1 | |
echo | |
echo | |
echo "Finished in ${SECONDS}s" | |
echo | |
echo "$test_count tests, $failures_count failures" | |
exit $failures_count | |
} | |
run_tests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment