Skip to content

Instantly share code, notes, and snippets.

@branning
Last active May 16, 2016 18:39
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 branning/d9845050192b03f92a41e61d3262f62e to your computer and use it in GitHub Desktop.
Save branning/d9845050192b03f92a41e61d3262f62e to your computer and use it in GitHub Desktop.
#!/usr/bin/env bats
#
# Bash Automated Testing System (BATS)
# https://github.com/sstephenson/bats
#
# Test curltail, both sourced and executed
@test "Missing argument, sourced" {
. curltail.sh
run curltail
[ "$status" -eq 2 ]
[ "$output" = "curltail requires a URL" ]
}
@test "Missing argument, executed" {
run ./curltail.sh
[ "$status" -eq 2 ]
[ "$output" = "curltail requires a URL" ]
}
@test "Sourcing curltail" {
. curltail.sh
run curltail http://www.google.com >/dev/null
[ "$status" -eq 1 ]
}
@test "Executing curltail" {
run ./curltail.sh http://www.google.com >/dev/null
[ "$status" -eq 0 ]
}
#!/bin/bash
#
# Use curl to tail a file over HTTP, kind of.
#
# Uses range requests (RFC 7233) to get the last N bytes of a resource.
# Change `chunk` below if you want to get more or fewer byte.
# Note that many HTTP 1.1 servers do not support range GET requests,
# so you'll end up with the whole file, after all. *sad trombone*
curltail()
{
local chunk=500 # bytes
url=$1 && [ "$url" ] || {
>&2 echo "${FUNCNAME[0]} requires a URL"
return 1
}
curl -v -X GET -H "range: byte=-$chunk" "$url"
}
# run the function, if we are not being sourced
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
curltail "${1}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment