Setting up cucumber environment for testing a remote restful API

252 views
Skip to first unread message

Larry Siden

unread,
Jul 18, 2010, 6:03:50 PM7/18/10
to
I want to use Cucumber to test a remote, restful PHP app.  I just want to send simple GET requests and one POST request (aka curl) for logging on.  I don't really need to emulate a browser and navagate pages.  I just want to check the HTTP status, save the session cookie, and parse the JSON that gets returned.

What is the easiest way to accomplish this for cucumber?  Do I use Webrat, Mechanize, Selenium, some combination of them, or something else?  I am reading the online docs, but not sure what each gem does that distinguishes it for my purposes.

I used Cucumber to test a restful API on my own localhost with Rack-Test and it worked great, but this time I think the setup needs to be different and I'm not sure where to start.

Elton Okada

unread,
Jul 18, 2010, 11:06:26 PM7/18/10
to web...@googlegroups.com
take a look at fakeweb http://fakeweb.rubyforge.org/, i guess is what you need
regards
Elton Okada

--
You received this message because you are subscribed to the Google Groups "webrat" group.
To post to this group, send email to web...@googlegroups.com.
To unsubscribe from this group, send email to webrat+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webrat?hl=en.

Matt Wynne

unread,
Jul 19, 2010, 4:46:42 AM7/19/10
to web...@googlegroups.com

On 18 Jul 2010, at 23:03, Larry Siden wrote:

> I want to use Cucumber to test a remote, restful PHP app. I just want to send simple GET requests and one POST request (aka curl) for logging on. I don't really need to emulate a browser and navagate pages. I just want to check the HTTP status, save the session cookie, and parse the JSON that gets returned.

Someone on this list a few weeks ago mentioned using a combination of aruba (a gem containing cucumber step definitions for testing command-line applications) and curl. This would mean you'd end up with steps like this:

Given there is a widget with the following details:
| name | Matt |
| make | British |
When I run "curl http://mytestapp.com/widgets.php"
Then it should pass with:
"""
[{"name": "Matt", "make": "British"}]
"""

What do you think? Would that work for you?


>
> What is the easiest way to accomplish this for cucumber? Do I use Webrat, Mechanize, Selenium, some combination of them, or something else? I am reading the online docs, but not sure what each gem does that distinguishes it for my purposes.
>
> I used Cucumber to test a restful API on my own localhost with Rack-Test and it worked great, but this time I think the setup needs to be different and I'm not sure where to start.
>

> --
> You received this message because you are subscribed to the Google Groups "webrat" group.
> To post to this group, send email to web...@googlegroups.com.
> To unsubscribe from this group, send email to webrat+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/webrat?hl=en.

cheers,
Matt

http://blog.mattwynne.net
+44(0)7974 430184

aslak hellesoy

unread,
Jul 19, 2010, 6:07:57 AM7/19/10
to web...@googlegroups.com
On Mon, Jul 19, 2010 at 5:06 AM, Elton Okada <elton...@gmail.com> wrote:
> take a look at fakeweb http://fakeweb.rubyforge.org/, i guess is what you
> need

I think you misunderstood what Larry needs. He needs to issue real
HTTP requests, not stub them out.
I'd try typhoeus or httparty
(http://ruby-toolbox.com/categories/http_clients.html)

It would be nice if someone wrote a REST abstraction API with various
pluggable drivers (rack-test and typhoeus/httparty). -Kind of like
Capybara, but without the DOM stuff - just focus on simple REST. Then
we could slap an aruba-like set of step defs around it.

Still nobody volunteering to create the restafarian gem for Cucumber?

Aslak

Larry Siden

unread,
Jul 19, 2010, 1:50:46 PM7/19/10
to web...@googlegroups.com
Fakeweb looks interesting as a mock, but it's not what I need.  I want to test a real website with a REST-ful API.

Larry Siden,
Westside Consulting LLC
westside-consulting.com
734-926-9614

Larry Siden

unread,
Jul 19, 2010, 2:23:32 PM7/19/10
to web...@googlegroups.com, aslak.h...@gmail.com
That's a  great resource Aslak.  Thank you!

It's frustrating that Webrat::Mechanize won't work.  After a call to #visit, response_body is an empty string ("").  That's wierd, b/c tracing through the Webrat and Mechanize code it looks like response_body should be something more structured than a "plain old" String!  

Mechanize and Webrat each have logging, but I can't figure out how to enable them.

Larry Siden,
Westside Consulting LLC
westside-consulting.com
734-926-9614



Steve Byrne

unread,
Jul 19, 2010, 2:29:28 PM7/19/10
to web...@googlegroups.com
Here's how I enable logging in mechanize:

require 'logger'

agent = Mechanize.new { |a| a.log = Logger.new("mech.log") }

Larry Siden

unread,
Jul 19, 2010, 2:56:34 PM7/19/10
to web...@googlegroups.com
Thanks!  That's brilliant!  Where do people learn this stuff.  From reading the code?

Larry Siden

unread,
Jul 19, 2010, 3:01:11 PM7/19/10
to web...@googlegroups.com
Okay, I see what's going on now:

D, [2010-07-19T14:53:33.685619 #13070] DEBUG -- : saved cookie: PHPSESSID=9hvn58jicc85latd6ibvivtue1
I, [2010-07-19T14:53:33.686797 #13070]  INFO -- : status: 302

So it successfully created a session.  It's just not following the redirect.  Also, how do I get the cookie into Mechanize::CookieJar (or is it Webrat::CookieJar? - too hungry to search right now).  Is there any way for me to confirm whether the cookie has been saved?

Larry Siden,
Westside Consulting LLC
westside-consulting.com
734-926-9614



Reply all
Reply to author
Forward
0 new messages