Skip to content

Instantly share code, notes, and snippets.

@bethesque
Last active July 10, 2018 06:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bethesque/9d81f21d6f77650811f4 to your computer and use it in GitHub Desktop.
Save bethesque/9d81f21d6f77650811f4 to your computer and use it in GitHub Desktop.
HTTP requests to ruby mock server
You will need ruby or the standalone mock service to run these examples.
$ gem install pact-mock_service
# called before suite (preferably by the framework)
mkdir -p /tmp/pacts
pact-mock-service start -p 1234
# called before each test (preferably by the framework)
curl -XDELETE -H "X-Pact-Mock-Service: true" localhost:1234/interactions
echo ''
echo ''
# during test setup, in user code
curl -XPOST -H "X-Pact-Mock-Service: true" -H "Content-Type: application/json" localhost:1234/interactions -d@interaction.json
echo ''
echo ''
# test, in user code
curl -XGET localhost:1234/foo
echo ''
echo ''
# called after each test (preferrably by the framework)
curl -XGET -H "X-Pact-Mock-Service: true" localhost:1234/interactions/verification
echo ''
echo ''
# called after suite (preferrably by the framework)
curl -XPOST -H "X-Pact-Mock-Service: true" localhost:1234/pact -d@pact-details.json
echo ''
pact-mock-service stop -p 1234
# called before suite (preferably by the framework)
pact-mock-service start -p 1234
# called before each test (preferably by the framework)
curl -XDELETE -H "X-Pact-Mock-Service: true" localhost:1234/interactions
echo ''
echo ''
# during test setup, in user code
curl -XPOST -H "X-Pact-Mock-Service: true" -H "Content-Type: application/json" localhost:1234/interactions -d@interaction.json
curl -XPOST -H "X-Pact-Mock-Service: true" -H "Content-Type: application/json" localhost:1234/interactions -d@interaction3.json
echo ''
echo ''
# test, in user code
curl -XGET localhost:1234/foo
curl -XGET localhost:1234/bar
echo ''
echo ''
# called after each test (preferrably by the framework)
curl -XGET -H "X-Pact-Mock-Service: true" localhost:1234/interactions/verification
echo ''
echo ''
# called after suite (preferrably by the framework)
curl -XPOST -H "X-Pact-Mock-Service: true" localhost:1234/pact -d@pact-details.json
echo ''
# called after suite (preferrably by the framework)
pact-mock-service stop -p 1234
# called before suite (preferably by the framework)
pact-mock-service start -p 1234
# INTERACTION 1
# called before each test (preferably by the framework)
curl -XDELETE -H "X-Pact-Mock-Service: true" localhost:1234/interactions
echo ''
echo ''
# during test setup, in user code
curl -XPOST -H "X-Pact-Mock-Service: true" -H "Content-Type: application/json" localhost:1234/interactions -d@interaction.json
echo ''
echo ''
# test, in user code
curl -XGET localhost:1234/foo
echo ''
echo ''
# called after each test (preferrably by the framework)
curl -XGET -H "X-Pact-Mock-Service: true" localhost:1234/interactions/verification
echo ''
echo ''
# INTERACTION 2
# called before each test (preferably by the framework)
curl -XDELETE -H "X-Pact-Mock-Service: true" localhost:1234/interactions
echo ''
echo ''
# during test setup, in user code
curl -XPOST -H "X-Pact-Mock-Service: true" -H "Content-Type: application/json" localhost:1234/interactions -d@interaction2.json
echo ''
echo ''
# test, in user code
curl -XGET localhost:1234/foo
echo ''
echo ''
# called after each test (preferrably by the framework)
curl -XGET -H "X-Pact-Mock-Service: true" localhost:1234/interactions/verification
echo ''
echo ''
# called after suite (preferrably by the framework)
curl -XPOST -H "X-Pact-Mock-Service: true" localhost:1234/pact -d@pact-details.json
echo ''
# called after suite (preferrably by the framework)
pact-mock-service stop -p 1234
{
"description": "a request for foo",
"provider_state": "foo exists",
"request": {
"method": "get",
"path": "/foo"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {
"foo": "bar"
}
}
}
{
"description": "another request for foo",
"provider_state": "foo exists",
"request": {
"method": "get",
"path": "/foo"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {
"foo": "barbarbarbar"
}
}
}
{
"description": "a request for bar",
"provider_state": "bar exists",
"request": {
"method": "get",
"path": "/bar"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {
"foo": "bar"
}
}
}
{
"consumer" : {
"name": "Consumer Name"
},
"provider": {
"name": "Provider Name"
},
"pact_dir": "/tmp/pacts"
}
@mefellows
Copy link

@bethesque is the field pact_dir described somewhere in the specification? I can't seem to find it.

Asking in relation to pact-foundation/pact-js#79

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment