-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow custom rack middleware to be specified
- Loading branch information
Showing
9 changed files
with
124 additions
and
15 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
36 changes: 36 additions & 0 deletions
36
lib/pact/provider_verifier/provider_states/add_provider_states_header.rb
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,36 @@ | ||
require 'delegate' | ||
|
||
module Pact | ||
module ProviderVerifier | ||
module ProviderStates | ||
class RequestDelegate < SimpleDelegator | ||
def initialize request, extra_rack_headers | ||
super(request) | ||
@extra_rack_headers = extra_rack_headers | ||
end | ||
|
||
def headers | ||
__getobj__().headers.merge(@extra_rack_headers) | ||
end | ||
|
||
def method | ||
__getobj__().method | ||
end | ||
end | ||
|
||
class AddProviderStatesHeader | ||
|
||
def self.call(request, interaction) | ||
if interaction.provider_state | ||
extra_rack_headers = { | ||
"X_PACT_PROVIDER_STATES" => [{ "name" => interaction.provider_state }] | ||
} | ||
RequestDelegate.new(request, extra_rack_headers) | ||
else | ||
request | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
lib/pact/provider_verifier/provider_states/remove_provider_states_header_middleware.rb
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,19 @@ | ||
module Pact | ||
module ProviderVerifier | ||
module ProviderStates | ||
class RemoveProviderStatesHeaderMiddleware | ||
def initialize app | ||
@app = app | ||
end | ||
|
||
def call env | ||
@app.call(remove_header(env)) | ||
end | ||
|
||
def remove_header env | ||
env.reject { | key, value | key == "X_PACT_PROVIDER_STATES" } | ||
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
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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
class AddCustomAuth < Pact::ProviderVerifier::CustomMiddleware | ||
|
||
def initialize app | ||
@app = app | ||
end | ||
|
||
def call env | ||
env['HTTP_AUTHORIZATION'] = 'Basic cGFjdDpwYWN0' | ||
provider_states = provider_states_from(env) | ||
provider_state_name = provider_states.any? && provider_states.first.name | ||
puts "The provider state name is '#{provider_state_name}'" | ||
@app.call(env) | ||
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