Boti
Boti

Reputation: 3445

htmp post in rails minitest

I would like to post an xml to a controller from minitest.

The way how it works in normal mode is this:

curl -X POST -H "Content-Type: text/xml" -d "@/Users/boti/Rails/clients/kevin/search_server/db/search.xml" localhost:3000/search

I tried by doing this:

test "search with invalid xml" do
  path_to_file = File.join Rails.root.to_s, 'test', 'search_invalid.xml'
  xml = File.read( path_to_file )
  @request.env['RAW_POST_DATA'] = xml
  post "/search/search", xml, {"Content-type" => "text/xml"}

But that way I get this exception:

NoMethodError: undefined method `symbolize_keys' for #<String:0x007fbd7d863188>

Upvotes: 0

Views: 335

Answers (1)

Boti
Boti

Reputation: 3445

I am doing this way in my tests:

path_to_file = File.join Rails.root.to_s, 'test', 'search_invalid_xml.xml'
xml = File.read( path_to_file )
@request.env['RAW_POST_DATA'] = xml
post :search, nil, {"Content-type" => "text/xml"}

Then this way in my controller:

search_doc = Nokogiri::XML.parse request.raw_post

Upvotes: 1

Related Questions