HTTP POST XML Content from Cucumber

I am trying to send XML content via POST to the controller method ("Parse") ("index") in a simple Rails project. This is not RESTful, since my model name is different, say, β€œcars”. I have the following in a functional test that works:

def test_index ... data_file_path = File.dirname(__FILE__) + '/../../app/views/layouts/index.xml.erb' message = ERB.new( File.read( data_file_path ) ) xml_result = message.result( binding ) doc = REXML::Document.new xml_result @request.env['RAW_POST_DATA'] = xml_result post :index assert_response :success end 

Now I'm trying to use a cucumber (0.4.3) and would like to know how I can simulate a POST request in the When clause. I have only one controller "index" method, and I have the following in config / routes.rb:

 ActionController::Routing::Routes.draw do |map| map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format' end 
  • The webrat inside the cucumber is HTML only and cannot do POST?
  • variable @request is not available from the environment of the cucumber?
  • If I use something like a visit index (assuming it is a Parse controller, index method) in the / step _definitions / car_steps.rb functions, I get the following error:

undefined `index 'method for # (NoMethodError)

Evaluate any suggestions on how to do integration tests with Cucumber for HTTP POST with XML content.

+7
ruby ruby-on-rails cucumber
source share
3 answers

Webrat will not help you here, it is for interacting with the browser, so if you use the API it will not help.

You can use the β€œmessage” in Cucumber, but you need to provide the full path to the action, not just the action. Also, pass a Content-type header so that Rails knows you are passing in XML.

 post("/controller/index", xml_result, {"Content-type" => "text/xml"}) 

On the response side, you can do the following:

 response.should be_success 
+11
source

Patrick Richie's solution also helped me, but I needed to make some minor changes to work with Rails 3.

 post("/controller/index", xml_result, {"CONTENT_TYPE" => "text/xml"}) 

I think this is due to the fact that in v3 Rails is more closely integrated with Rack.

+12
source

Thanks a lot guys, I swore all night. I will add an example with basic authentication and with json if other people than me are looking for it. btw, both application/xml and text/xml , but for json you need application/json .

 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")}) 

and I use them in spec/requests without any webrat or capybara browser files.

+1
source

All Articles