Reputation: 40920
I should be kind to my web service consumers and serve them some nice examples, even though it's no fun maintaining a big xml request test. Are there better ways to be a good WS provider?
I have no html. The app accepts both XML and JSON, so to ensure the validity of the API examples(both xml and json), I'd like to prove their OK in an integration suite.
In your answer, I'd like to see some examples, not "try cucumber/webrat/capybara" only. It's hard to find howto without html. Thanks for helping out!
Upvotes: 5
Views: 1832
Reputation: 14195
Since you don't need the fancy features of webrat/capybara for executing javascript or dealing with arbitrary html, it makes sense to just use the basic integration test support from rails.
I'd store the API examples in some form that can be easily transformed to either XML or JSON, then use this file in the integration test so that you get both format types tested while only maintaining one representation of the test requests. You can also write a task to generate API examples for the documentation from this.
The full response body from any API call in your tests will be stored in @response.body, and you can parse/verify that however you please.
Upvotes: 2
Reputation: 40920
I had this standalone script, enabling me to send xml request, but requiring a server:
require 'rubygems'
require 'net/http'
require 'json'
url = URI.parse('http://localhost:3030/myresource.xml')
request = Net::HTTP::Post.new(url.path)
request.content_type="text/xml"
request.basic_auth('user', 'secret')
request.body = "<?xml version='1.0' encoding='UTF-8'?><somedata><name>Test Name 1</name><description>Some data for testing</description></somedata>"
response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
puts response
finally I was able to do this without starting up a server, using rspec 2. Putting this in a spec file under spec/requests
enables me to do it in my app without webrat or capybara.
for XML
post("/myresource.xml",
some_xml_string,
{"CONTENT_TYPE" => "text/xml",
"HTTP_AUTHORIZATION" => ActionController::HttpAuthentication::Basic.encode_credentials("user", "secret")})
and JSON
post("/myresource.json",
some_json_string,
{"CONTENT_TYPE" => "application/json",
"HTTP_AUTHORIZATION" => ActionController::HttpAuthentication::Basic.encode_credentials("user", "secret")})
Now I guess I can build the some_xml_string
from a remote resource like my documentation xml or json file (some http:// resource), for instance. Yes, it's more to maintain and the test will be fragile. I'll have to think more about this... Changing APIs used by external people isn't something to be taken upon lightly, always a lot of trade-offs. Better suggests are welcome!
Upvotes: 1