开发者

Rails API functional tests with XML body

开发者 https://www.devze.com 2023-03-07 05:38 出处:网络
I\'ve written functional tests for API endpoints built in Rails using shoulda testing framework. An example looks like the following:

I've written functional tests for API endpoints built in Rails using shoulda testing framework.

An example looks like the following:

  setup do
    authenticated_xml_request('xml-file-name')
    post :new
  end

  should 开发者_开发知识库respond_with :success

authenticated_xml_request is a test helper method that sets @request.env['RAW_POST_DATA'] with XML content.

After upgrading the app from rails 2.3.3 to rails 2.3.8, the functional tests are failing because the XML content received is not merged in the params hash.

I'm setting the request with the correct mime type via @request.accept = "text/xml"

I'm able to inspect the content of the request using request.raw_post but i'd like to keep the current setup working.

Also, running a test from the terminal using cURL or any other library (rest_http) in development mode, the API works perfectly well.

Any tips or help is much appreciated.


Now it's simpler:

post "/api/v1/users.xml", user.to_xml, "CONTENT_TYPE" => 'application/xml'

Note that you have to specify appropriate "CONTENT_TYPE". In other case your request will go as 'application/x-www-form-urlencoded' and xml won't be parsed properly.


I solved the issue by adding a custom patch to rails (test_process.rb file) to convert incoming xml to hash then merge into parameters hash.

on line 439:

parameters ||= {}

parameters.merge!(Hash.from_xml(@request.env['RAW_POST_DATA'])) if @request.env['RAW_POST_DATA'] && @request.env['CONTENT_TYPE']=='application/xml'

0

精彩评论

暂无评论...
验证码 换一张
取 消