Skip to content

Instantly share code, notes, and snippets.

@nickshoust-wf
Created April 11, 2017 20:11
Show Gist options
  • Save nickshoust-wf/5850df10ec556632e8eee467f6819199 to your computer and use it in GitHub Desktop.
Save nickshoust-wf/5850df10ec556632e8eee467f6819199 to your computer and use it in GitHub Desktop.
mock-pact-service issue
# Given that you have pactpy installed in your environment
# And that madkom/pact-mock-service is stood up at localhost:1234
# And that a request with a trailing slash is requested
# The contract that is written does not contain the slash in the path
# This results in the wrong URL being requested with the generated
# contract is verified against the server
import requests
from unittest import TestCase
from pact import Consumer, Provider
SERVER_HOSTNAME = "localhost"
class BaseContract(TestCase):
def setUp(self):
pass
class SomeContract(BaseContract):
def setUp(self):
super(SomeContract, self).setUp()
self.pact = Consumer('Consumer').has_pact_with(
Provider('Provider'), host_name=SERVER_HOSTNAME, port=1234)
def test_case(self):
expected = "stuff"
(self.pact
.given('a request for stuff')
.upon_receiving('a request for stuff with a trailing slash')
.with_request('get', '/stuff/', query='sorted')
.will_respond_with(200, body=expected))
with self.pact:
result = requests.get('http://' + SERVER_HOSTNAME + ':1234' + '/stuff/?sorted')
print result
self.assertEqual(result.content, expected)
@nickshoust-wf
Copy link
Author

The contract looks like

{
  "consumer": {
    "name": "Consumer"
  },
  "provider": {
    "name": "Provider"
  },
  "interactions": [
    {
      "description": "a request for stuff with a trailing slash",
      "provider_state": "a request for stuff",
      "request": {
        "method": "get",
        "path": "/stuff",
        "query": "sorted"
      },
      "response": {
        "status": 200,
        "headers": {
        },
        "body": "stuff"
      }
    }
  ],
  "metadata": {
    "pactSpecificationVersion": "2.0.0"
  }
}

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