Skip to content

Instantly share code, notes, and snippets.

@harrisj
Created April 26, 2012 16:44
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harrisj/2500883 to your computer and use it in GitHub Desktop.
Save harrisj/2500883 to your computer and use it in GitHub Desktop.
Testing Punctuation on URL Shorteners
require 'net/http'
# These links were taken from my twitter timeline. No endorsement implied.
tiny_urls = {
'bitly' => ['http://bit.ly/3S9c5K', 'http://developer.nytimes.com/'],
'j.mp' => ['http://j.mp/JsML0u', 'http://www.npr.org/2012/04/26/151303230/an-african-trader-and-the-perils-of-business-in-china'],
'tinyurl' => ['http://tinyurl.com/6rq9vob', 'http://politicsoftheplate.com/?p=1283'],
'owly' => ['http://ow.ly/axesj', 'http://threatpost.com/en_us/blogs/opera-12-beta-released-new-security-features-including-do-not-track-support-042612'],
'ht.ly' => ['http://ht.ly/1j7LiX', 'http://rssfeeds.usatoday.com/~r/usatoday-LifeTopStories/~3/8vFV-fFfhAc/1'],
'is.gd' => ['http://is.gd/gk0FoM', 'http://www.wired.com/dangerroom/2012/04/joe-schmoe-drones/'],
'twitter' => ['http://t.co/fV6GSc1x', 'http://met.org/FS1suN'],
'facebook' => ['http://fb.me/1xmJP3kC4', 'http://www.ksn.com/s/YLdNkn88gECowBSM-0Q_KA.cspx#.T5l4FTDa_ws.facebook'],
'tumblr' => ['http://tmblr.co/ZNFL8yKMTybJ', 'http://www.tumblr.com/ZNFL8yKMTybJ'],
'wordpress' => ['http://wp.me/p2jsUG-4b', 'http://ljndawson.wordpress.com/2012/04/25/why-amazon-will-be-the-good-guy/'],
}
APPEND_TESTS = '.,:;)?!'.split(//)
def redirects_to?(small_url, large_url)
response = Net::HTTP.get_response(URI.parse(small_url))
return false unless response.is_a?(Net::HTTPRedirection)
response['location'] == large_url
end
tiny_urls.keys.sort.each do |name|
puts "TESTING #{name}"
short_url, long_url = tiny_urls[name]
if !redirects_to?(short_url, long_url)
puts "ERROR: The short URL #{short_url} IS NOT REDIRECTING TO #{long_url}; FIX THIS BEFORE TESTING THAT SHORTENER"
next
end
APPEND_TESTS.each do |char|
test_url = short_url+char
if redirects_to?(test_url, long_url)
puts " #{test_url} ok"
else
puts " #{test_url} FAIL"
end
end
puts
end
One of my biggest gripes with shortened URLs in tweets is that
they often break when normal sentence punctuation is added to the end.
So, I wrote a test to try various services out and see how they work.
Please suggest more URL shorteners to @harrisj
Why do I care? I have used various twitter clients over the years, and more
than half of them have had bugs in figuring out where the URLs would end on
short URLs. And so, they'll send the trailing punctuation along and it would
break. You might argue this is the fault of the twitter clients, but
Postel's Law says the URL expanders should be lenient in this situation any way.
http://en.wikipedia.org/wiki/Robustness_principle
URL SHORTENING SERVICES: Yes, I can figure out the proper URL by removing the
offending punctuation, less technically-savvy users will just associate your
tiny urls with BROKEN. Why wouldn't you want to avoid that?
TESTING bitly
http://bit.ly/3S9c5K. ok
http://bit.ly/3S9c5K, ok
http://bit.ly/3S9c5K: ok
http://bit.ly/3S9c5K; ok
http://bit.ly/3S9c5K) ok
http://bit.ly/3S9c5K? ok
http://bit.ly/3S9c5K! ok
TESTING facebook
http://fb.me/1xmJP3kC4. FAIL
http://fb.me/1xmJP3kC4, FAIL
http://fb.me/1xmJP3kC4: FAIL
http://fb.me/1xmJP3kC4; FAIL
http://fb.me/1xmJP3kC4) FAIL
http://fb.me/1xmJP3kC4? ok
http://fb.me/1xmJP3kC4! FAIL
TESTING goo.gl
http://goo.gl/WFwhv. ok
http://goo.gl/WFwhv, ok
http://goo.gl/WFwhv: ok
http://goo.gl/WFwhv; ok
http://goo.gl/WFwhv) ok
http://goo.gl/WFwhv? ok
http://goo.gl/WFwhv! FAIL
TESTING ht.ly
http://ht.ly/1j7LiX. ok
http://ht.ly/1j7LiX, ok
http://ht.ly/1j7LiX: ok
http://ht.ly/1j7LiX; ok
http://ht.ly/1j7LiX) ok
http://ht.ly/1j7LiX? ok
http://ht.ly/1j7LiX! ok
TESTING is.gd
http://is.gd/gk0FoM. ok
http://is.gd/gk0FoM, ok
http://is.gd/gk0FoM: ok
http://is.gd/gk0FoM; ok
http://is.gd/gk0FoM) ok
http://is.gd/gk0FoM? ok
http://is.gd/gk0FoM! ok
TESTING j.mp
http://j.mp/JsML0u. ok
http://j.mp/JsML0u, ok
http://j.mp/JsML0u: ok
http://j.mp/JsML0u; ok
http://j.mp/JsML0u) ok
http://j.mp/JsML0u? ok
http://j.mp/JsML0u! ok
TESTING owly
http://ow.ly/axesj. ok
http://ow.ly/axesj, ok
http://ow.ly/axesj: ok
http://ow.ly/axesj; ok
http://ow.ly/axesj) ok
http://ow.ly/axesj? ok
http://ow.ly/axesj! ok
TESTING tinyurl
http://tinyurl.com/6rq9vob. FAIL
http://tinyurl.com/6rq9vob, ok
http://tinyurl.com/6rq9vob: ok
http://tinyurl.com/6rq9vob; ok
http://tinyurl.com/6rq9vob) ok
http://tinyurl.com/6rq9vob? FAIL
http://tinyurl.com/6rq9vob! ok
TESTING tumblr
http://tmblr.co/ZNFL8yKMTybJ. FAIL
http://tmblr.co/ZNFL8yKMTybJ, FAIL
http://tmblr.co/ZNFL8yKMTybJ: FAIL
http://tmblr.co/ZNFL8yKMTybJ; FAIL
http://tmblr.co/ZNFL8yKMTybJ) FAIL
http://tmblr.co/ZNFL8yKMTybJ? ok
http://tmblr.co/ZNFL8yKMTybJ! FAIL
TESTING twitter
http://t.co/fV6GSc1x. FAIL
http://t.co/fV6GSc1x, FAIL
http://t.co/fV6GSc1x: FAIL
http://t.co/fV6GSc1x; FAIL
http://t.co/fV6GSc1x) FAIL
http://t.co/fV6GSc1x? ok
http://t.co/fV6GSc1x! FAIL
TESTING wordpress
http://wp.me/p2jsUG-4b. ok
http://wp.me/p2jsUG-4b, ok
http://wp.me/p2jsUG-4b: ok
http://wp.me/p2jsUG-4b; ok
http://wp.me/p2jsUG-4b) ok
http://wp.me/p2jsUG-4b? ok
http://wp.me/p2jsUG-4b! ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment