Project

assert-url

0.0
No commit activity in last 3 years
No release in over 3 years
Semantical helpers to test your URLs in Ruby
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
 Project Readme

assert-url

Semantical helpers to test your URLs in Ruby

Usage

Ok, say you're writing a Web app with Ruby (who isn't?) and need to test your routing with Rack::Test.

The old way

def test_some_resource
  get "/some/resource"

  assert last_response.ok?
  assert_equal "http://example.org/some/resource", last_response.url
end

What's wrong with this, you ask? Well, in most cases when you write a test like this, you only care about testing the /some/resource path, but the rest (scheme, port, etc) doesn't really matter. Still we're used to use assert_equal on the whole url.

Now imagine that you need to change the port, scheme or host. Now imagine you have hundreds of tests. It's grep day, my friend.

Enter AssertUrl!

include AssertUrl

def test_some_resource
  get "/some/resource"

  assert last_response.ok?
  assert_path_equal "/some/resource", last_response.url
end

BAM! Now you're only testing what you need. You can change the port, host, even the query and the test will be fine.

AssertUrl provides the following assertions in the form of expected, value:

  • assert_scheme_equal [String or Symbol], [String or URI]
  • assert_host_equal [String], [String or URI]
  • assert_port_equal [Integer], [String or URI]
  • assert_path_equal [String starting with "/"], [String or URI]
  • assert_query_equal [Hash], [String or URI]
  • assert_query_include [Hash], [String or URI]
  • assert_fragment_equal [String], [String or URI]
  • assert_url_equal [String or URI], [String or URI]

But...

Can't I just do something like...?

assert_equal "/some/resource", URI(last_response.url).path

Well, yes! sure you can. In fact, this is very close to how AssertUrl is implemented.

My point here is not to sell you this gem but to make a statement that we need a better habit when testing URLs. And if AssertUrl helps you with it, great!

Installation

gem install assert-url