brighter.

shiny shimmery sparkles.

  1. Search
  2. About
  3. Subscribe
  4. Archive
  5. Random

brighter.

shiny shimmery sparkles.

Newer
Older
  • FakeWeb with Regular Expression support for registering URIs

    I have recently started using a gem called FakeWeb to fake http requests in my specs. It’s awesome - FakeWeb allowed me to ensure that I had stubbed out all external API requests (on a rather API-dependent application) with one line of code:

    # prevent specs from trying to hit external api's
    require 'fakeweb'
    FakeWeb.allow_net_connect = false

    However, I ran into an issue with it on another project that needed to block all requests except those going to a specific service. The issue was that each request had a different query string, and soon our spec helper was full of lines like this:

    FakeWeb.register_uri("http://www.external.service.com/api/some/script?crazy=true&param=foo&list=0&whatever=3", :string => "foo") FakeWeb.register_uri("http://www.external.service.com/api/some/script?crazy=true&param=foo&list=0&whatever=4&additional=etc", :string => "foo") FakeWeb.register_uri("http://www.external.service.com/api/some/script?crazy=true&param=foo&list=0&whatever=5&additional=bar", :string => "foo") FakeWeb.register_uri("http://www.external.service.com/api/some/script?crazy=true&param=foo&list=0&whatever=6", :string => "foo")

    The query params differed depending on the request issued. Where we really got into trouble was with escaped URIs - these are harder for mere humans to read, and we missed differences in the longer ones quite a few times.

    FakeWeb.register_uri("http://example.com/?a=%09%0D&b=%09%0D&ca=%09dog%4D&d=%26%0D&g=%09%0D&b=%09%0D&ca=%09dog%4D&d=%26%0D") FakeWeb.register_uri("http://example.com/?a=%09%0D&b=%09%0D&ca=%09dog%4D&d=%26%0D&g=%09%0D&b=%09%0E&ca=%09dog%4D&d=%26%0D") FakeWeb.register_uri("http://example.com/?bc=%09%0D&b=%09%0D&ca=%09dog%4D&d=%26%0D&g=%09%0D&b=%09%0D&ca=%09dog%4D&d=%26%0D")

    This prompted me to ask, “why not pass a regexp to FakeWeb’s register_uri method?” That way, we could pass something like /example.com\/?a=(.*)$/ for example and not have to worry about the query parameters changing… and thus have less clutter in our spec helper.

    FakeWeb didn’t support this (it allowed you to pass URI and String objects) but it wasn’t too hard to add it. You can check out my fork here:

    FakeWeb with Regular Expression support for registering URIs

    I hope that other people find this useful. Oh yeah - please let me know on github if you find a bug in the code, and of course, feel free to fork it too!

    (originally posted on brighter.net)

    Posted on October 12, 2009

  • psi
  • benjaminstein
  • jexe
  • pith
  • clientsfromhell
  • timocratic

Field Notes Theme. Designed by Manasto Jones. Powered by Tumblr.