Class: Pact::ProviderVerifier::AddHeaderMiddlware

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/provider_verifier/add_header_middlware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, headers) ⇒ AddHeaderMiddlware

Returns a new instance of AddHeaderMiddlware.



5
6
7
8
# File 'lib/pact/provider_verifier/add_header_middlware.rb', line 5

def initialize app, headers
  @app = app
  @headers = headers
end

Instance Method Details

#add_headers(env) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/pact/provider_verifier/add_header_middlware.rb', line 14

def add_headers env
  new_env = env.dup
  @headers.each_pair do | key, value |
    header_name = "HTTP_#{key.upcase.gsub("-", "_")}"
    warn_about_header_replacement key, new_env[header_name], value
    new_env[header_name] = value
  end
  new_env
end

#call(env) ⇒ Object



10
11
12
# File 'lib/pact/provider_verifier/add_header_middlware.rb', line 10

def call env
  @app.call(add_headers(env))
end

#warn_about_header_replacement(header_name, existing_value, new_value) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/pact/provider_verifier/add_header_middlware.rb', line 24

def warn_about_header_replacement header_name, existing_value, new_value
  if existing_value.nil?
    $stderr.puts "WARN: Adding header '#{header_name}: #{new_value}' to replayed request. This header did not exist in the pact, and hence this test cannot confirm that the request will work in real life."
  else
    # Don't mess up the json formatter by using stdout here
    $stderr.puts "INFO: Replacing header '#{header_name}: #{existing_value}' with '#{header_name}: #{new_value}'"
  end
end