Reputation: 603
Old Rails 4.2.10 line:
post :show, "Some XML as String"
to Rails 5.1.4
post :show, params: { ??? }
So what is the key value pair I add here?
Edit: So currently in the tests its written like this:
let(:logout_request_xml) { "<soap-env:Envelope xmlns:soap-env='http://schemas.xmlsoap.org/soap/envelope/'><soap-env:Body>...more stuff...</soap-env:Body></soap-env:Envelope>" }
...
post :show, logout_request_xml
Upvotes: 0
Views: 1059
Reputation: 796
None of the above answers worked for me in Rails 5.2.2.
I was using code like this @request.env['RAW_POST_DATA'] = json
in 5.1 in some cases where I needed a special type of serialization and I converted all of those to:
json = { "some" => "JSON" }.to_json
post :show, body: json
Upvotes: 2
Reputation: 91
I've been able to POST raw data using Rails 5.1.4 by setting it directly in the request for a controller spec
@request.env['RAW_POST_DATA'] = '<test>some raw xml</test>'
post :show
In the controller this can then be read via the request.body.read
> request.body.read
=> "<test>some raw xml</test>"
Note that this will not work when moving to Rails 5.2. In that case the request body will be empty as the underlying behavior has changed. The best way I've found to test this scenario is to use Request Specs instead of Controller Specs.
Controller specs - A controller spec is an RSpec wrapper for a Rails functional test. It allows you to simulate a single http request in each example, and then specify expected outcomes
Request specs - Request specs provide a thin wrapper around Rails' integration tests, and are designed to drive behavior through the full stack, including routing (provided by Rails) and without stubbing (that's up to you).
Here is an example posting that same data via a Request Spec:
post items_path, env: {'RAW_POST_DATA' => "<test>some raw xml</test>"}
In the controller
> request.body.read
=> "<test>some raw xml</test>"
Upvotes: 3
Reputation: 44370
Here is how you can pass raw body in the minitest:
post :show, as: :xml, headers: { 'RAW_POST_DATA': 'Some XML as String' }
You can't use params
because it's not suppose to be used in this way, also you need to add as: :xml
becuase you send raw body with application/x-www-form-urlencoded
content-type.
In the controller request.raw_body
Upvotes: 1