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?
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.
It is already answered here Appending headers to Rspec controller tests
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.