-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Content Security Policy header
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
=begin | ||
Conditionally use Rack Middleware. | ||
Usage: | ||
condition_proc = ->(env) { env['PATH_INFO'] == '/match' } | ||
use_when condition_proc, SomeMiddleware, options | ||
I feel sure there must be something like this officially supported somewhere, but I can't find it. | ||
=end | ||
|
||
module Rack | ||
module PactBroker | ||
module UseWhen | ||
class ConditionallyUseMiddleware | ||
def initialize(app, condition_proc, middleware, *args, &block) | ||
@app_without_middleware = app | ||
@condition_proc = condition_proc | ||
@middleware = middleware | ||
@args = args | ||
@block = block | ||
end | ||
|
||
def call(env) | ||
if condition_proc.call(env) | ||
app_with_middleware.call(env) | ||
else | ||
app_without_middleware.call(env) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :app_without_middleware, :condition_proc, :middleware, :args, :block | ||
|
||
def app_with_middleware | ||
@app_with_middleware ||= begin | ||
rack_builder = ::Rack::Builder.new | ||
rack_builder.use middleware, *args, &block | ||
rack_builder.run app_without_middleware | ||
rack_builder.to_app | ||
end | ||
end | ||
end | ||
|
||
refine Rack::Builder do | ||
def use_when(condition_proc, middleware, *args, &block) | ||
use(ConditionallyUseMiddleware, condition_proc, middleware, *args, &block) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'rack/pact_broker/use_when' | ||
require 'rack/test' | ||
|
||
module Rack | ||
module PactBroker | ||
describe UseWhen do | ||
|
||
using Rack::PactBroker::UseWhen | ||
include Rack::Test::Methods | ||
|
||
class TestMiddleware | ||
def initialize(app, additional_headers) | ||
@app = app | ||
@additional_headers = additional_headers | ||
end | ||
|
||
def call(env) | ||
status, headers, body = @app.call(env) | ||
[status, headers.merge(@additional_headers), body] | ||
end | ||
end | ||
|
||
let(:app) do | ||
target_app = -> (env) { [200, {}, []] } | ||
builder = Rack::Builder.new | ||
condition = ->(env) { env['PATH_INFO'] == '/match' } | ||
builder.use_when condition, TestMiddleware, { "Foo" => "Bar" } | ||
builder.run target_app | ||
builder.to_app | ||
end | ||
|
||
context "when the condition matches" do | ||
subject { get '/match' } | ||
|
||
it "uses the middleware" do | ||
expect(subject.headers).to include "Foo" => "Bar" | ||
end | ||
end | ||
|
||
context "when the condition does not match" do | ||
subject { get '/no-match' } | ||
|
||
it "does not use the middleware" do | ||
expect(subject.headers.keys).to_not include "Foo" | ||
end | ||
end | ||
end | ||
end | ||
end |