Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrate testing OPTIONS requests for CORS #110

Open
eliotsykes opened this issue Dec 31, 2015 · 0 comments
Open

Demonstrate testing OPTIONS requests for CORS #110

eliotsykes opened this issue Dec 31, 2015 · 0 comments

Comments

@eliotsykes
Copy link
Owner

Notes:

diff --git a/spec/api/events_api_spec.rb b/spec/api/events_api_spec.rb
index 7f1c1d0..3f4f2ef 100644
--- a/spec/api/events_api_spec.rb
+++ b/spec/api/events_api_spec.rb
@@ -1,15 +1,32 @@
 require 'rails_helper'

 RSpec.describe '/api/events' do
+  
+  context 'OPTIONS#preflight' do
+    it 'responds with CORS headers' do
+      options '/api/events/'
+      
+      expect(response).to have_http_status(:ok)
+      expect(response.headers['Access-Control-Allow-Headers']).to eq 'Content-Type'
+      # More like this expect(response.headers[...]).to eq ...
+      expect(response.body).to eq ''
+    end
+  end
+  
   context 'POST#create' do

     context 'with correct paramaters' do
-      xit 'creates a new event for the registered application' do
+      it 'creates a new event for the registered application' do
           user = create(:user)
           application = create(:application)
           params = { name: "the name of the event" }
+          headers = {
+            'Content-Type' => 'application/json',
+            'Accept' => 'application/json',
+            'Origin' => application.url
+          }

-          post '/api/events/', params.to_json
+          post '/api/events/', params.to_json, headers
           json = JSON.parse(response.body)

           expect(json).to eq("")
diff --git a/spec/controllers/api/events_controller_spec.rb b/spec/controllers/api/events_controller_spec.rb
index 345168b..cfb9d5f 100644
--- a/spec/controllers/api/events_controller_spec.rb
+++ b/spec/controllers/api/events_controller_spec.rb
@@ -3,7 +3,7 @@ require 'rails_helper'
 RSpec.describe API::EventsController, type: :controller do
   describe "OPTIONS#preflight" do
     it "returns status 200" do
-      get :preflight
+      process :preflight, 'OPTIONS'
       expect(response).to have_http_status(200)
       expect(response.headers["Access-Control-Allow-Headers"]).to eq("Content-Type")
     end
diff --git a/spec/support/request_helper.rb b/spec/support/request_helper.rb
new file mode 100644
index 0000000..d63928b
--- /dev/null
+++ b/spec/support/request_helper.rb
@@ -0,0 +1,14 @@
+module RequestHelper
+  # Add support for testing `options` requests in RSpec.
+  # See: https://github.com/rspec/rspec-rails/issues/925
+  def options(*args)
+    reset! unless integration_session
+    integration_session.__send__(:process, :options, *args).tap do
+      copy_session_variables!
+    end
+  end
+end
+
+RSpec.configure do |config|
+  config.include RequestHelper, type: :request
+end
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant