Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the request.body for an Rspec request spec with a GET request

  • rails 5.0.0.1
  • rspec 3.5

I have inherited a code base. I am busy writing integration tests to tie down the app functionality before I consider refactoring.

I have the following lines in a controller concern before_action. It seems to read the request body. The json value here is used to extract an identifier used to authenticate the request.

request.body.rewind
body = request.body.read
json = JSON.parse(body) unless body.empty?

I need to test that the authentication happens correctly. How can I set the request.body for a GET request spec?

like image 647
Apie Avatar asked Nov 22 '16 17:11

Apie


People also ask

What should I test in request specs?

Request specs give you the ability to test what your application does, rather than how it does it. For example, instead of testing that the right template is rendered in a controller spec, with a request spec we can test that the content we are expecting to appear actually appears in the response body.

How do I run an RSpec on a specific file?

To run a single Rspec test file, you can do: rspec spec/models/your_spec. rb to run the tests in the your_spec. rb file.

What are request specs?

Request specs allow you to focus on a single controller action, but unlike controller tests involve the router, the middleware stack, and both rack requests and responses. This adds realism to the test that you are writing, and helps avoid many of the issues that are common in controller specs.


1 Answers

I think you should be able to do this via the request env RAW_POST_DATA

get root_path, {}, 'RAW_POST_DATA' => 'raw json string'
request.raw_post # "raw json string"

See: How to send raw post data in a Rails functional test?

like image 116
hajpoj Avatar answered Oct 25 '22 08:10

hajpoj