Skip to content

Instantly share code, notes, and snippets.

@bethesque
Last active August 29, 2015 14:02
Show Gist options
  • Save bethesque/7fa8947c107f92ace9a4 to your computer and use it in GitHub Desktop.
Save bethesque/7fa8947c107f92ace9a4 to your computer and use it in GitHub Desktop.
Server with preloaded interactions
# $ pact service -p 8080
# (new window)
# $ ruby preload-interactions.rb
# $ curl localhost:8080/path_one #Yay all good!
# $ curl localhost:8080/path_two #Boo... which response do we want???
require 'json'
require 'net/http'
require 'pact/consumer/consumer_contract_builder'
require 'pact/consumer_contract'
require 'pact/consumer/interaction_builder'
pact = JSON.parse(DATA.read, symbolize_names: true)
consumer_contract_builder = Pact::Consumer::ConsumerContractBuilder.new(provider_name: 'a provider', consumer_name: 'a consumer', pactfile_write_mode: :overwrite, port: 8080)
pact[:interactions].each do | interaction |
consumer_contract_builder.given(interaction[:provider_state]).
upon_receiving(interaction[:description]).
with(interaction[:request]).
will_respond_with(interaction[:response])
end
__END__
{
"provider": {
"name": "a provider"
},
"consumer": {
"name": "a consumer"
},
"interactions": [
{
"description": "request one",
"provider_state": "state one",
"request": {
"method": "get",
"path": "/path_one"
},
"response": {
"status": 200,
"headers" : {"Content-Type": "application/json"},
"body": {"message": "Yay! A matching response!!!"}
}
},
{
"description": "request two",
"provider_state": "when a thing exists",
"request": {
"method": "get",
"path": "/path_two"
},
"response": {
"status": 200,
"headers" : {"Content-Type": "application/json"},
"body": {"message": "Will you get this???"}
}
},
{
"description": "request two",
"provider_state": "when a thing does not exist",
"request": {
"method": "get",
"path": "/path_two"
},
"response": {
"status": 404,
"body": "Or this?"
}
}
],
"metadata": {
"pactSpecificationVersion": "1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment