#1 open
Chris Kampmeier

API changes to support request bodies

Reported by Chris Kampmeier | January 7th, 2009 @ 09:52 PM | in 1.2.0

The last feature to add to FakeWeb before you could completely stub out a RESTful web service is the ability to respond to PUTs and POSTs differently depending on the content of their request bodies.

I already extended FakeWeb to support HTTP methods, which got us here:

FakeWeb.register_uri(:get, "http://example.com", :string => "response")

It'd be natural to just add a third parameter before the options hash:

FakeWeb.register_uri(:post, "http://example.com/users", "user[login]=chrisk", :string => "User chrisk created!")

Something that's attractive to me about Ruby, though, is the way that keys in options hashes make method calls self-documenting. In practice, having many parameters (4, here) often strikes me as a code smell of a method that needs refactoring. So something more like this would be easier to understand, at a glance:

FakeWeb.register_uri(:post, "http://example.com/users", :body => "user[login]=chrisk", :string => "User chrisk created!")

However, we're currently using the third argument for only the response, not the request -- it might more accurately be called response_options. And that'll be parsed as one options hash with two keys. So that gets confusing.

One solution would be to get all RSpecky:

FakeWeb.register(:post, "http://example.com/users").with_body("user[login]=chrisk").and_respond(:string => "User chrisk created!")

Something like that would be kind of nice, actually. That'd require some major architectural changes to FakeWeb, though.

Anybody have thoughts or suggestions? I wanted to get this out in the open instead of just thinking about it by myself.

Comments and changes to this ticket

  • Chris Kampmeier

    Chris Kampmeier January 7th, 2009 @ 10:07 PM

    • Milestone set to 1.2.0
    • State changed from “new” to “open”
  • Leandro Pedroni

    Leandro Pedroni March 2nd, 2010 @ 05:03 AM

    I'm starting now to use fakeweb to stub S3 behaviour when dealing with plugins... It'd be cool to have access to the request body and the matched regexp from the url.

    I think passing a block to the register call (sinatra-y) could be interesting

    FakeWeb.register(:post, "http://example.com/users") do |body, header|
      # this fake method could parse a multipart body...
      params = parse(body)
      # this generates the response
      "User #{params[:login]} created"

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

A Ruby tool for faking responses to Net::HTTP requests