Skip to content

Commit 2aa51ea

Browse files
committed
use let construct instead of an ivar for the request object
1 parent bda2b58 commit 2aa51ea

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

spec/lib/secure_headers_spec.rb

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,47 @@ module SecureHeaders
1616

1717
before(:each) do
1818
reset_config
19-
@request = Rack::Request.new("HTTP_X_FORWARDED_SSL" => "on")
19+
2020
end
2121

22+
let(:request) { Rack::Request.new("HTTP_X_FORWARDED_SSL" => "on") }
23+
2224
it "raises a NotYetConfiguredError if default has not been set" do
2325
expect do
24-
SecureHeaders.header_hash_for(@request)
26+
SecureHeaders.header_hash_for(request)
2527
end.to raise_error(Configuration::NotYetConfiguredError)
2628
end
2729

2830
it "raises a NotYetConfiguredError if trying to opt-out of unconfigured headers" do
2931
expect do
30-
SecureHeaders.opt_out_of_header(@request, CSP::CONFIG_KEY)
32+
SecureHeaders.opt_out_of_header(request, CSP::CONFIG_KEY)
3133
end.to raise_error(Configuration::NotYetConfiguredError)
3234
end
3335

3436
describe "#header_hash_for" do
3537
it "allows you to opt out of individual headers" do
3638
Configuration.default
37-
SecureHeaders.opt_out_of_header(@request, CSP::CONFIG_KEY)
38-
SecureHeaders.opt_out_of_header(@request, XContentTypeOptions::CONFIG_KEY)
39-
hash = SecureHeaders.header_hash_for(@request)
39+
SecureHeaders.opt_out_of_header(request, CSP::CONFIG_KEY)
40+
SecureHeaders.opt_out_of_header(request, XContentTypeOptions::CONFIG_KEY)
41+
hash = SecureHeaders.header_hash_for(request)
4042
expect(hash['Content-Security-Policy-Report-Only']).to be_nil
4143
expect(hash['Content-Security-Policy']).to be_nil
4244
expect(hash['X-Content-Type-Options']).to be_nil
4345
end
4446

4547
it "allows you to opt out entirely" do
4648
Configuration.default
47-
SecureHeaders.opt_out_of_all_protection(@request)
48-
hash = SecureHeaders.header_hash_for(@request)
49+
SecureHeaders.opt_out_of_all_protection(request)
50+
hash = SecureHeaders.header_hash_for(request)
4951
ALL_HEADER_CLASSES.each do |klass|
5052
expect(hash[klass::CONFIG_KEY]).to be_nil
5153
end
5254
end
5355

5456
it "allows you to override X-Frame-Options settings" do
5557
Configuration.default
56-
SecureHeaders.override_x_frame_options(@request, XFrameOptions::DENY)
57-
hash = SecureHeaders.header_hash_for(@request)
58+
SecureHeaders.override_x_frame_options(request, XFrameOptions::DENY)
59+
hash = SecureHeaders.header_hash_for(request)
5860
expect(hash[XFrameOptions::HEADER_NAME]).to eq(XFrameOptions::DENY)
5961
end
6062

@@ -64,17 +66,17 @@ module SecureHeaders
6466
config.csp = OPT_OUT
6567
end
6668

67-
SecureHeaders.override_x_frame_options(@request, XFrameOptions::SAMEORIGIN)
68-
SecureHeaders.override_content_security_policy_directives(@request, default_src: %w(https:), script_src: %w('self'))
69+
SecureHeaders.override_x_frame_options(request, XFrameOptions::SAMEORIGIN)
70+
SecureHeaders.override_content_security_policy_directives(request, default_src: %w(https:), script_src: %w('self'))
6971

70-
hash = SecureHeaders.header_hash_for(@request)
72+
hash = SecureHeaders.header_hash_for(request)
7173
expect(hash[CSP::HEADER_NAME]).to eq("default-src https:; script-src 'self'")
7274
expect(hash[XFrameOptions::HEADER_NAME]).to eq(XFrameOptions::SAMEORIGIN)
7375
end
7476

7577
it "produces a hash of headers with default config" do
7678
Configuration.default
77-
hash = SecureHeaders.header_hash_for(@request)
79+
hash = SecureHeaders.header_hash_for(request)
7880
expect_default_values(hash)
7981
end
8082

@@ -104,8 +106,8 @@ module SecureHeaders
104106
}
105107
end
106108

107-
SecureHeaders.append_content_security_policy_directives(@request, script_src: %w(anothercdn.com))
108-
hash = SecureHeaders.header_hash_for(@request)
109+
SecureHeaders.append_content_security_policy_directives(request, script_src: %w(anothercdn.com))
110+
hash = SecureHeaders.header_hash_for(request)
109111
expect(hash[CSP::HEADER_NAME]).to eq("default-src 'self'; script-src mycdn.com 'unsafe-inline' anothercdn.com")
110112
end
111113

@@ -116,12 +118,12 @@ module SecureHeaders
116118
}
117119
end
118120

119-
SecureHeaders.append_content_security_policy_directives(@request, script_src: %w(anothercdn.com))
120-
new_config = SecureHeaders.config_for(@request)
121+
SecureHeaders.append_content_security_policy_directives(request, script_src: %w(anothercdn.com))
122+
new_config = SecureHeaders.config_for(request)
121123
expect(new_config).to_not be(SecureHeaders::Configuration.get)
122124

123-
SecureHeaders.override_content_security_policy_directives(@request, script_src: %w(yet.anothercdn.com))
124-
current_config = SecureHeaders.config_for(@request)
125+
SecureHeaders.override_content_security_policy_directives(request, script_src: %w(yet.anothercdn.com))
126+
current_config = SecureHeaders.config_for(request)
125127
expect(current_config).to be(new_config)
126128
end
127129

@@ -131,15 +133,15 @@ module SecureHeaders
131133
default_src: %w('self')
132134
}
133135
end
134-
SecureHeaders.override_content_security_policy_directives(@request, default_src: %w('none'))
135-
hash = SecureHeaders.header_hash_for(@request)
136+
SecureHeaders.override_content_security_policy_directives(request, default_src: %w('none'))
137+
hash = SecureHeaders.header_hash_for(request)
136138
expect(hash[CSP::HEADER_NAME]).to eq("default-src 'none'")
137139
end
138140

139141
it "overrides non-existant directives" do
140142
Configuration.default
141-
SecureHeaders.override_content_security_policy_directives(@request, img_src: [ContentSecurityPolicy::DATA_PROTOCOL])
142-
hash = SecureHeaders.header_hash_for(@request)
143+
SecureHeaders.override_content_security_policy_directives(request, img_src: [ContentSecurityPolicy::DATA_PROTOCOL])
144+
hash = SecureHeaders.header_hash_for(request)
143145
expect(hash[CSP::HEADER_NAME]).to eq("default-src https:; img-src data:")
144146
end
145147

@@ -152,9 +154,9 @@ module SecureHeaders
152154
}
153155
end
154156

155-
request = Rack::Request.new(@request.env.merge("HTTP_USER_AGENT" => USER_AGENTS[:safari5]))
156-
nonce = SecureHeaders.content_security_policy_script_nonce(request)
157-
hash = SecureHeaders.header_hash_for(request)
157+
safari_request = Rack::Request.new(request.env.merge("HTTP_USER_AGENT" => USER_AGENTS[:safari5]))
158+
nonce = SecureHeaders.content_security_policy_script_nonce(safari_request)
159+
hash = SecureHeaders.header_hash_for(safari_request)
158160
expect(hash[CSP::HEADER_NAME]).to eq("default-src 'self'; script-src mycdn.com 'unsafe-inline'; style-src 'self'")
159161
end
160162

@@ -167,15 +169,15 @@ module SecureHeaders
167169
}
168170
end
169171

170-
request = Rack::Request.new(@request.env.merge("HTTP_USER_AGENT" => USER_AGENTS[:chrome]))
171-
nonce = SecureHeaders.content_security_policy_script_nonce(request)
172+
chrome_request = Rack::Request.new(request.env.merge("HTTP_USER_AGENT" => USER_AGENTS[:chrome]))
173+
nonce = SecureHeaders.content_security_policy_script_nonce(chrome_request)
172174

173175
# simulate the nonce being used multiple times in a request:
174-
SecureHeaders.content_security_policy_script_nonce(request)
175-
SecureHeaders.content_security_policy_script_nonce(request)
176-
SecureHeaders.content_security_policy_script_nonce(request)
176+
SecureHeaders.content_security_policy_script_nonce(chrome_request)
177+
SecureHeaders.content_security_policy_script_nonce(chrome_request)
178+
SecureHeaders.content_security_policy_script_nonce(chrome_request)
177179

178-
hash = SecureHeaders.header_hash_for(request)
180+
hash = SecureHeaders.header_hash_for(chrome_request)
179181
expect(hash['Content-Security-Policy']).to eq("default-src 'self'; script-src mycdn.com 'nonce-#{nonce}'; style-src 'self'")
180182
end
181183
end

0 commit comments

Comments
 (0)