Skip to content

Commit e33e417

Browse files
committed
Add feature test for headers setup in controller specs
1 parent d9e9c92 commit e33e417

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Feature: Headers
2+
3+
We recommend you to switch to request spec instead of controller spec
4+
if you want to set headers in your call.
5+
If you still want to set headers in controller spec, you can use
6+
`request.headers` as mentioned bellow.
7+
8+
Scenario: Set header's value in controller spec
9+
Given a file named "spec/controllers/application_controller_spec.rb" with:
10+
"""ruby
11+
require "rails_helper"
12+
13+
RSpec.describe ApplicationController, :type => :controller do
14+
controller do
15+
def show
16+
if request.headers["Authorization"] == "foo"
17+
head :ok
18+
else
19+
head :forbidden
20+
end
21+
end
22+
end
23+
24+
before do
25+
routes.draw { get "show" => "anonymous#show" }
26+
end
27+
28+
context "valid Authorization header" do
29+
it "returns a 200" do
30+
request.headers["Authorization"] = "foo"
31+
32+
get :show
33+
34+
expect(response).to have_http_status(:ok)
35+
end
36+
end
37+
38+
context "invalid Authorization header" do
39+
it "returns a 403" do
40+
request.headers["Authorization"] = "bar"
41+
42+
get :show
43+
44+
expect(response).to have_http_status(:forbidden)
45+
end
46+
end
47+
end
48+
"""
49+
When I run `rspec spec`
50+
Then the example should pass

0 commit comments

Comments
 (0)