如何将此curl请求转换为此控制器操作的RSpec测试

时间:2017-10-23 15:54:25

标签: ruby-on-rails ruby curl rspec cors

我使用curl测试a以查看此控制器是否会接收传入请求但现在我想进行RSPEC测试如何将此curl行:curl -v -H "Accept: application/json" -H "Origin: requesting.url.site" -H "Content-Type: application/json" -X POST -d '{"name":"foobar"}' http://arbitrary.site转换为RSPEC Post测试for create?

class API::EventsController < ApplicationController

 skip_before_action :verify_authenticity_token #for this example skip the authenticity_token
 before_action :set_access_control_headers

def preflight
 head 200
end

def set_access_control_headers
 headers['Access-Control-Allow-Origin'] = '*'
 headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
 headers['Access-Control-Allow-Headers'] = 'Content-Type'
end
def create
 registered_application = RegisteredApplication.find_by(url: request.env['HTTP_ORIGIN'])
  if registered_application == nil
    render json: {errors: "Unregistered application"}, status: :unprocessable_entity
  else
    @event = registered_application.events.new(event_params)
    if @event.save
      render json: @event, status: :created
    else
      render json: @event.errors, status: :unprocessable_entity
    end
  end
end

 private
  def event_params
   params.require(:event).permit(:name)
 end
end

1 个答案:

答案 0 :(得分:0)

您应该只将标题添加到通常的rspec控制器后期操作中。

这里已经回答Appending headers to Rspec controller tests