简体   繁体   English

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

[英]How would I turn this curl request into an RSpec test for this controller action

I used curl to test a to see if this controller would receive incoming requests but now I want to make a RSPEC test how would I turn this curl line: curl -v -H "Accept: application/json" -H "Origin: requesting.url.site" -H "Content-Type: application/json" -X POST -d '{"name":"foobar"}' http://arbitrary.site into a RSPEC Post test for create?我使用 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 测试以进行创建?

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

You should just append headers to the usual rspec controller post action.您应该将标题附加到通常的 rspec 控制器发布操作。

It is already answered here Appending headers to Rspec controller tests已在此处回答将标头附加到 Rspec 控制器测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM