Instantly share code, notes, and snippets.

Embed
What would you like to do?
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