Class: Sbmt::Pact::Provider::PactBrokerProxy

Inherits:
Rack::Proxy
  • Object
show all
Defined in:
lib/sbmt/pact/provider/pact_broker_proxy.rb

Constant Summary collapse

PACT_FILE_REQUEST_PATH_REGEX =

e.g. /pacts/provider/paas-stand-seeker/consumer/paas-stand-placer/pact-version/2967a9343bd8fdd28a286c4b8322380020618892/metadata/c1tdW2VdPXByb2R1Y3Rpb24mc1tdW2N2XT03MzIy

%r{/pacts/provider/.+?/consumer/.+?/pact-version/.+}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, opts = {}) ⇒ PactBrokerProxy

Returns a new instance of PactBrokerProxy.



14
15
16
17
18
19
20
# File 'lib/sbmt/pact/provider/pact_broker_proxy.rb', line 14

def initialize(app = nil, opts = {})
  super
  @backend_uri = URI(opts[:backend])
  @path = nil
  @filter_type = opts[:filter_type]
  @logger = opts[:logger] || Logger.new($stdout)
end

Instance Attribute Details

#backend_uriObject (readonly)

Returns the value of attribute backend_uri.



9
10
11
# File 'lib/sbmt/pact/provider/pact_broker_proxy.rb', line 9

def backend_uri
  @backend_uri
end

#filter_typeObject (readonly)

Returns the value of attribute filter_type.



9
10
11
# File 'lib/sbmt/pact/provider/pact_broker_proxy.rb', line 9

def filter_type
  @filter_type
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/sbmt/pact/provider/pact_broker_proxy.rb', line 9

def logger
  @logger
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/sbmt/pact/provider/pact_broker_proxy.rb', line 9

def path
  @path
end

Instance Method Details

#perform_request(env) ⇒ Object



22
23
24
25
26
27
# File 'lib/sbmt/pact/provider/pact_broker_proxy.rb', line 22

def perform_request(env)
  request = Rack::Request.new(env)
  @path = request.path

  super
end

#rewrite_env(env) ⇒ Object



29
30
31
32
# File 'lib/sbmt/pact/provider/pact_broker_proxy.rb', line 29

def rewrite_env(env)
  env["HTTP_HOST"] = backend_uri.host
  env
end

#rewrite_response(triplet) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sbmt/pact/provider/pact_broker_proxy.rb', line 34

def rewrite_response(triplet)
  status, headers, body = triplet

  if status == "200" && PACT_FILE_REQUEST_PATH_REGEX.match?(path)
    patched_body = patch_response(body.first)

    # we need to recalculate content length
    headers[Rack::CONTENT_LENGTH] = patched_body.bytesize.to_s

    return [status, headers, [patched_body]]
  end

  triplet
end