jcnnghm
jcnnghm

Reputation: 7432

Best Way to Test Rails REST XML API?

I want to test the REST api on my Rails site. What is the easiest/best way to do this with the rails testing framework? I'm only doing the standard resourceful stuff, so I am wondering in particular, since this is so bog standard, if there is any automagical way to test this stuff.

Upvotes: 12

Views: 7299

Answers (6)

Krishna Prasad Varma
Krishna Prasad Varma

Reputation: 4740

You can try curl

use --form-string to pass form data to server

(1)

curl --form-string "book_key=BOOK1234" --form-string "author=Gandhi"  -X PUT 'http://localhost:3000/api/show_all_books_of_a_particular_author?params1=value1&param2=value2'

In the controller you will get params['book_key']=BOOK1234 and params["author"]="Gandhi"

use -F "[email protected];type=application/msword;"

(2)

curl -F "document=@my_experiments_with_truth.pdf;type=application/pdf;" --form-string "author=Gandhi" --form-string "[email protected]"  -X PUT 'http://localhost:3000/api/submit_a_pdf_book?params1=value1&param2=value2'

In the controller you will get params['email]="[email protected]" and params["author"]="Gandhi" and params["document"] = "File(object)" . This works only if test.doc is in current directory . Dont forget to pass mime-type as the server may take it as "application octet-stream" and need to write to code to handle this seperately.

Upvotes: 1

muirbot
muirbot

Reputation: 2081

I rolled my own solution to this and thought it would be helpful. I wrote a module that uses the json, curb, and addressable gems to send GET, PUT, POST, and DELETE requests to localhost:3000. It can request either XML (as the original question asked for) or json. It returns the response body as a Hash. It is mostly a wrapper around the curb gem, which I think has horrendous syntax.

Note that I am automatically loading my api_key. This can be disabled by passing :api_key => false or broken using api_key => "wrong". You might want to leave this out or modify it to fit your authentication scheme.

Here's the module:

module ApiTesting
  # requres the json, curb, and addressable gems

  require "addressable/uri"

  def api_call(path, verb, query_hash={}, options={})
    options.reverse_merge! :api_key => "abc1234", :format => "xml"
    query_hash.reverse_merge!({:api_key => options["api_key"]}) if options[:api_key]
    query = to_query_string(query_hash)
    full_path = "http://localhost:3000/#{path}.#{options[:format]}?#{query}"
    response = case verb
      when :get
        Curl::Easy.perform(full_path)
      when :post
        Curl::Easy.http_post("http://localhost:3000/#{path}.#{options[:format]}", query)
      when :put
        Curl::Easy.http_put(full_path, nil)
      when :delete
        Curl::Easy.http_delete(full_path)
    end
    case options[:format]
      when "xml"
        Hash.from_xml(response.body_str)
      when "json"
        JSON.parse(response.body_str)
    end
  end

  private

  def to_query_string(val)
    uri = Addressable::URI.new
    uri.query_values = val
    uri.query
  end

end

And here are some simple examples: Requesting resource attributes with GET:

    api_call("calls/41", :get)

Creating resources with POST:

    api_call("people", :post, {:person => {:first => "Robert", :last => "Smith" } })

Updating resources with PUT:

    api_call("people/21", :put, {:person => { :first => "Bob" } })

Deleting resources with DELETE:

    api_call("calls/41", :delete)

Turning off automatic insertion of api_key:

    api_call("calls/41", :get, {}, {:api_key => false})

Use the wrong api_key:

    api_call("calls/41", :get, {}, {:api_key => "wrong"})

Use as json (default is xml):

    api_call("calls/41", :get, {}, {:format => "json"})

Upvotes: 5

We use RESTClient a Firefox add-on to visit and test REST services.

https://addons.mozilla.org/en-US/firefox/addon/9780

We have been using this in my team for a couple of months and I don't think we could do our work without it. It's very easy to get up and running and friendly to use.

If you get the latest version from Sourceforge there is even Oauth Support for it, something I haven't found in any other REST client.

http://sourceforge.net/projects/restclient/develop

One, of many, advantages of using a Firefox add-on is that it's cross plattform. We use the same tool (RESTclient) for all members of our team even though we use different OS's (Mac, Linux, Windows).

Upvotes: 1

jluebbert
jluebbert

Reputation: 260

This is not automated but it is really great for seeing what your API is doing.

http://hurl.r09.railsrumble.com/

Upvotes: 2

Ariejan
Ariejan

Reputation: 11079

I would recommend using Cucumber. Cucumber emulates a browser and you can verify the results it gets. This works fine for XML requests as well as JSON and plain old HTML.

Upvotes: 2

Dasharatham Bitla
Dasharatham Bitla

Reputation:

If you are looking at testing the APIs that you have created manually - you may want to try this! Seems to work well!

REST Client - Simply Test REST APIs

You cannot do the automated testing with this though!

Upvotes: 0

Related Questions