Exemple #1
0
def provider(statedir, app, contract):
    if os.path.exists(PROVIDER_TEST_RESULT):
        os.remove(PROVIDER_TEST_RESULT)
    if not os.path.exists(contract):
        logger.error('Contract file %s does not exist', contract)
        sys.exit(1)
    contracts = []
    if os.path.isdir(contract):
        for dirpath, dirnames, filenames in os.walk(contract):
            for filename in filenames:
                with open(os.path.join(dirpath, filename)) as f:
                    contract = Contract.from_dict(json.loads(f.read(), object_pairs_hook=datetime_decoder))
                    contracts.append(contract)
    else:
        with open(contract) as f:
            contract = Contract.from_dict(json.loads(f.read(), object_pairs_hook=datetime_decoder))
            contracts.append(contract)
    if not contracts:
        logger.info('No contract found')
        sys.exit()

    states = load_states(statedir)
    # It's important to provide all test cases in a list so that nosetests can
    # generate a proper xunit result summary file.
    test_cases = _render_http_testsuite(app, contracts, states)
    successful = nose.run(
        argv=[__file__, '-sv', '--logging-level=INFO', '--with-xunit', '--xunit-file=' + PROVIDER_TEST_RESULT],
        suite=test_cases,
    )
    _exit(successful)
Exemple #2
0
    def test_mock_service_get_querystr(self):
        contract = Contract('provider', 'consumer')
        contract.given("Test").upon_receiving("a request").with_request(
            method="get",
            path="/path",
            query="date=2017-11-21",
        ).will_respond_with(status=200,
                            headers={
                                "content-type": "application/json",
                                'X-Request-ID': '9v7uygi2hop'
                            },
                            body={"key": "value"})

        with MockServices(MockAPI(contract, port=1234)):
            with self.assertRaises(NoMockAddress):
                requests.get('http://localhost:1234/path')
            with self.assertRaises(NoMockAddress):
                requests.get('http://localhost:1234/path?date=2017-11-22')
            response = requests.get(
                'http://localhost:1234/path?date=2017-11-21')
            self.assertEqual(response.status_code, 200)
            self.assertDictEqual({"key": "value"}, response.json())
            self.assertEqual('application/json',
                             response.headers['Content-Type'])
            self.assertEqual('9v7uygi2hop', response.headers['X-Request-ID'])
Exemple #3
0
 def test_add_interaction(self):
     contract = Contract('provider', 'consumer')
     interaction = Interaction()
     interaction.given("Test").upon_receiving("a request").with_request(
         method="get",
         path="/path",
         headers={
             "Custom-Header": "value"
         },
     ).will_respond_with(status=200,
                         headers={"Content-Type": "text/html"},
                         body={"key": "value"})
     contract.add_interaction(interaction)
     contract.add_interaction(interaction)
     self.assertEqual(1, len(contract.interactions))
Exemple #4
0
    def test_mock_service_functional_json(self):
        contract = Contract('provider', 'consumer')
        contract.given("Test").upon_receiving("a request").with_request(
            method="get",
            path="/path",
            headers={
                "Custom-Header": "value"
            },
        ).will_respond_with(status=200,
                            headers={"Custom-Header": "value"},
                            body={"key": "value"})

        with MockServices(MockAPI(contract, port=1234)):
            response = requests.get('http://localhost:1234/path')
            self.assertEqual(response.status_code, 200)
            self.assertDictEqual(response.json(), {"key": "value"})
Exemple #5
0
 def test_base(self):
     contract = Contract("test_provider", "test_consumer")
     contract.given("Test").upon_receiving("a request").with_request(
         method="get",
         path="/path",
         headers={
             "Custom-Header": "value"
         },
     ).will_respond_with(status=200,
                         headers={"Custom-Header": "value"},
                         body={"key": "value"})
     with MockServices(MockAPI(contract)) as service:
         response = requests.get('{}/path'.format(
             service.mock_apis[0].get_service_host()))
         self.assertEqual(response.status_code, 200)
         self.assertDictEqual(response.json(), {"key": "value"})
Exemple #6
0
    def test_mock_service_serialize_json(self):
        contract = Contract('provider', 'consumer')
        contract.given("Test").upon_receiving("a request").with_request(
            method="get",
            path="/path",
            headers={
                "Custom-Header": "value"
            },
        ).will_respond_with(status=200,
                            headers={"Content-Type": "text/html"},
                            body={"key": "value"})

        expected_contract = {
            'provider': {
                'name': 'provider'
            },
            'consumer': {
                'name': 'consumer'
            },
            "metadata": {
                "pacte": {
                    "version": VERSION
                }
            },
            "interactions": [{
                "providerState": "Test",
                "description": "a request",
                "request": {
                    "method": "GET",
                    "path": "/path",
                    "headers": {
                        "Custom-Header": "value"
                    },
                },
                "response": {
                    "status": 200,
                    "headers": {
                        "Content-Type": "text/html"
                    },
                    "body": {
                        "key": "value"
                    }
                }
            }],
        }
        actual_contract = contract.to_dict()
        self.assertDictEqual(actual_contract, expected_contract)
Exemple #7
0
 def test_from_dict(self):
     contract = Contract.from_dict({
         'provider': {
             'name': 'provider'
         },
         'consumer': {
             'name': 'consumer'
         },
         "metadata": {
             "pacte": {
                 "version": VERSION
             }
         },
         "interactions": [{
             "providerState": "Test",
             "description": "a request",
             "request": {
                 "method": "GET",
                 "path": "/path",
                 "headers": {
                     "Custom-Header": "value"
                 },
             },
             "response": {
                 "status": 200,
                 "headers": {
                     "Content-Type": "text/html"
                 },
                 "body": "Test String Response"
             }
         }, {
             "providerState": "Test2",
             "description": "a request2",
             "request": {
                 "method": "POST",
                 "path": "/path",
                 "query": "name=ron&status=good",
                 "headers": {
                     "Custom-Header": "value"
                 },
             },
             "response": {
                 "status": 200,
                 "headers": {
                     "Content-Type": "text/html"
                 },
                 "body": {
                     "key": "value"
                 }
             }
         }],
     })
     self.assertEqual('consumer', contract.consumer)
     self.assertEqual('provider', contract.provider)
     self.assertEqual(2, len(contract.interactions))
Exemple #8
0
def register(provider, consumer):
    global _contracts, _consumer
    if not _consumer:
        _consumer = consumer
    else:
        if _consumer != consumer:
            raise PacteServiceException('More than one consumers are registered: %s, %s', _consumer, consumer)
    contract = Contract(provider, consumer)
    contracts = _contracts.setdefault(provider, [])
    contracts.append(contract)
    return contract
Exemple #9
0
    def test_mock_service_multi_functional(self):
        contract = Contract('provider', 'consumer')
        contract.given("Test").upon_receiving("a request").with_request(
            method="get",
            path="/get_json",
            headers={
                "Custom-Header": "value"
            },
        ).will_respond_with(status=200,
                            headers={"Custom-Header": "value"},
                            body={"key": "value"})

        contract.given("Test2").upon_receiving("second request").with_request(
            method="get",
            path="/get_str",
            headers={
                "Custom-Header": "value"
            },
        ).will_respond_with(status=200,
                            headers={"Content-Type": "text/html"},
                            body="Test String Response")

        with MockServices(MockAPI(contract, port=1234)):
            response = requests.get('http://localhost:1234/get_json')
            self.assertEqual(response.status_code, 200)
            self.assertDictEqual(response.json(), {"key": "value"})

            response = requests.get('http://localhost:1234/get_str')
            self.assertEqual(response.status_code, 200)
            self.assertEqual(response.text, "Test String Response")
Exemple #10
0
    def test_mock_service_post_querystr(self):
        contract = Contract('provider', 'consumer')
        contract.given("Test2").upon_receiving("a request2").with_request(
            method="post",
            path="/path",
            headers={
                "Custom-Header": "value"
            },
        ).will_respond_with(status=200,
                            headers={"Content-Type": "application/json"},
                            body={"key": "value"})

        with MockServices(MockAPI(contract, port=1234)):
            with self.assertRaises(NoMockAddress):
                requests.get('http://localhost:1234/path')
            with self.assertRaises(NoMockAddress):
                requests.get('http://localhost:1234/path?date=2017-11-22')
            response = requests.post('http://localhost:1234/path',
                                     data={"test": "data"})
            self.assertEqual(response.status_code, 200)
            self.assertDictEqual({"key": "value"}, response.json())
            self.assertEqual('application/json',
                             response.headers['Content-Type'])
Exemple #11
0
    def test_mock_service_multi_interactions_requests(self):
        contract_get = Contract('provider_get', 'consumer')
        contract_get.given("Test").upon_receiving("a request").with_request(
            method="get",
            path="/path",
            query="date=2017-11-21",
        ).will_respond_with(status=200,
                            headers={
                                "content-type": "application/json",
                                'X-Request-ID': '9v7uygi2hop'
                            },
                            body={"key": "value"})
        contract_post = Contract('provider_post', 'consumer')
        contract_post.given("Test2").upon_receiving("a request2").with_request(
            method="post",
            path="/path",
            headers={
                "Custom-Header": "value"
            },
        ).will_respond_with(status=200,
                            headers={"Content-Type": "application/json"},
                            body={"key": "value"})

        with MockServices(MockAPI(contract_get, domain='domain_get',
                                  port=1234)):
            response_get = requests.get(
                'http://domain_get:1234/path?date=2017-11-21')
            self.assertEqual(response_get.status_code, 200)
            self.assertDictEqual({"key": "value"}, response_get.json())
            self.assertEqual('application/json',
                             response_get.headers['Content-Type'])
            self.assertEqual('9v7uygi2hop',
                             response_get.headers['X-Request-ID'])

        with MockServices(
                MockAPI(contract_post, domain='domain_post', port=1234)):
            response_post = requests.post('http://domain_post:1234/path',
                                          data={"test": "data"})
            self.assertEqual(response_post.status_code, 200)
            self.assertDictEqual({"key": "value"}, response_post.json())
            self.assertEqual('application/json',
                             response_post.headers['Content-Type'])