Setting Content-Type header for RSpec and Rails-API

后端 未结 6 1725
遇见更好的自我
遇见更好的自我 2021-02-19 10:41

I\'m using the rails-api gem to build a web service and want to test my API with RSpec. Every request I make, regardless of the HTTP method has the CONTENT_TYPE hea

相关标签:
6条回答
  • 2021-02-19 11:08

    This worked for me Rails 4.0.3 and Rspec 2.14.1 if anyone is looking for more recent versions.

    put '/projects/1.json', {name: 'Updated Project 1'}, {
      'HTTP_ACCEPT' => 'application/json',
      'CONTENT_TYPE' => 'application/json'
    }
    

    and

    wrap_parameters Project, format: :json
    
    0 讨论(0)
  • 2021-02-19 11:16

    If you are using Rails 4 (and rspec ~3.7) and don't want to use the inline syntax:

    request.headers["CONTENT_TYPE"] = "application/json"
    
    0 讨论(0)
  • 2021-02-19 11:26

    Using the new Rails v5.0.x API only settings I found that this problem with rails defaulting everything to "application/x-www-form-urlencoded" is still in issue for testing with RSpec-Rails Requests

    Here is what I did to fix the problem:

    Create support file at ./spec/support/json_requests.rb

    Edit it to be something like this to override the behavior for all of your API only JSON requests:

    module JsonRequests
      def get(*args)
        super(*json_args(*args))
      end
    
      def post(*args)
        super(*json_args(*args))
      end
    
      def update(*args)
        super(*json_args(*args))
      end
    
      def patch(*args)
        super(*json_args(*args))
      end
    
      def put(*args)
        super(*json_args(*args))
      end
    
      def delete(*args)
        super(*json_args(*args))
      end
    
      def json_args(path, params = {}, headers = {})
        [path, params.to_json, headers.merge('CONTENT_TYPE' => 'application/json')]
      end
    end
    
    RSpec.configure do |config|
      config.include JsonRequests, type: :request
    end
    

    Keep in mind that this will override all Specs within ./spec/requests so if you need to use "application/x-www-form-urlencoded" you could also include this module manually as needed in your Describe 'something' do block.

    0 讨论(0)
  • 2021-02-19 11:27

    Rails 5 no hacks:

    put(:update,
        params: {project_id: 1},
        body: {name: 'Updated Project 1'}.to_json,
        as: :json)
    

    This sets the content_type correctly. In the controller params will hold both params and body.

    0 讨论(0)
  • 2021-02-19 11:27

    Rails 5

    headers = { 'CONTENT_TYPE' => 'application/json' }
    params = { user_type: 'tester' } 
    

    and after that request like

    post '/api/v1/users/test', params.to_json, headers
    

    and also remove .to_json from request route

    0 讨论(0)
  • 2021-02-19 11:30

    A lot of frustration and variations and that's what worked for me. Rails 3.2.12 Rspec 2.10

     @request.env["HTTP_ACCEPT"] = "application/json"
     @request.env["CONTENT_TYPE"] = "application/json"
     put :update, :id => 1, "email" => "bing@test.com"
    

    wrap_parameters seems to be working declared this way

    wrap_parameters User, format: :json
    

    being used for User model

    0 讨论(0)
提交回复
热议问题