lib/pact/consumer/mock_service/app.rb in pact-mock_service-0.2.3.pre.rc1 vs lib/pact/consumer/mock_service/app.rb in pact-mock_service-0.2.3.pre.rc2
- old
+ new
@@ -1,10 +1,9 @@
require 'rack'
require 'uri'
require 'json'
require 'logger'
-require 'awesome_print'
require 'pact/consumer/request'
require 'pact/consumer/mock_service/expected_interactions'
require 'pact/consumer/mock_service/actual_interactions'
require 'pact/consumer/mock_service/verified_interactions'
require 'pact/consumer/mock_service/interaction_delete'
@@ -12,18 +11,13 @@
require 'pact/consumer/mock_service/interaction_replay'
require 'pact/consumer/mock_service/missing_interactions_get'
require 'pact/consumer/mock_service/verification_get'
require 'pact/consumer/mock_service/log_get'
require 'pact/consumer/mock_service/pact_post'
+require 'pact/consumer/mock_service/options'
require 'pact/support'
-AwesomePrint.defaults = {
- indent: -2,
- plain: true,
- index: false
-}
-
module Pact
module Consumer
class MockService
@@ -33,22 +27,54 @@
@name = options.fetch(:name, "MockService")
pact_dir = options[:pact_dir]
expected_interactions = ExpectedInteractions.new
actual_interactions = ActualInteractions.new
verified_interactions = VerifiedInteractions.new
+ @consumer_contact_details = {
+ pact_dir: options[:pact_dir],
+ consumer: {name: options[:consumer]},
+ provider: {name: options[:provider]},
+ interactions: verified_interactions
+ }
@handlers = [
+ Options.new(@name, @logger, options[:cors_enabled]),
MissingInteractionsGet.new(@name, @logger, expected_interactions, actual_interactions),
VerificationGet.new(@name, @logger, expected_interactions, actual_interactions, log_description),
InteractionPost.new(@name, @logger, expected_interactions, verified_interactions),
InteractionDelete.new(@name, @logger, expected_interactions, actual_interactions),
LogGet.new(@name, @logger),
- PactPost.new(@name, @logger, verified_interactions, pact_dir),
- InteractionReplay.new(@name, @logger, expected_interactions, actual_interactions, verified_interactions)
+ PactPost.new(@name, @logger, verified_interactions, pact_dir, options[:consumer_contract_details]),
+ InteractionReplay.new(@name, @logger, expected_interactions, actual_interactions, verified_interactions, options[:cors_enabled])
]
end
+ def call env
+ response = []
+ begin
+ relevant_handler = @handlers.detect { |handler| handler.match? env }
+ res = relevant_handler.respond(env)
+ response = relevant_handler.enable_cors? ? add_cors_header(res) : res
+ rescue StandardError => e
+ @logger.error "Error ocurred in mock service: #{e.class} - #{e.message}"
+ @logger.error e.backtrace.join("\n")
+ response = [500, {'Content-Type' => 'application/json'}, [{message: e.message, backtrace: e.backtrace}.to_json]]
+ rescue Exception => e
+ @logger.error "Exception ocurred in mock service: #{e.class} - #{e.message}"
+ @logger.error e.backtrace.join("\n")
+ raise e
+ end
+ response
+ end
+
+ def write_pact_if_configured
+ consumer_contract_writer = ConsumerContractWriter.new(@consumer_contact_details, StdoutLogger.new)
+ consumer_contract_writer.write if consumer_contract_writer.can_write?
+ end
+
+ private
+
def configure_logger options
options = {log_file: $stdout}.merge options
log_stream = options[:log_file]
@logger = Logger.new log_stream
@logger.formatter = options[:log_formatter] if options[:log_formatter]
@@ -63,24 +89,17 @@
def to_s
"#{@name} #{super.to_s}"
end
- def call env
- response = []
- begin
- relevant_handler = @handlers.detect { |handler| handler.match? env }
- response = relevant_handler.respond(env)
- rescue StandardError => e
- @logger.error "Error ocurred in mock service: #{e.class} - #{e.message}"
- @logger.error e.backtrace.join("\n")
- response = [500, {'Content-Type' => 'application/json'}, [{message: e.message, backtrace: e.backtrace}.to_json]]
- rescue Exception => e
- @logger.error "Exception ocurred in mock service: #{e.class} - #{e.message}"
- @logger.error e.backtrace.join("\n")
- raise e
+ class StdoutLogger
+ def info message
+ $stdout.puts "\n#{message}"
end
- response
+ end
+
+ def add_cors_header response
+ [response[0], response[1].merge('Access-Control-Allow-Origin' => '*'), response[2]]
end
end
end
end