Beispiel #1
0
    def test_reset_rules(self):

        self.assertFalse(http_mock.get_rules())

        update_http_rules(rules)
        self.assertEqual(len(http_mock.get_rules()), 1)

        # reset
        reset_rules()
        self.assertFalse(http_mock.get_rules())
Beispiel #2
0
    def test_reset_rules(self):

        self.assertFalse(http_mock.get_rules())

        update_http_rules(rules)
        self.assertEqual(len(http_mock.get_rules()), 1)

        # reset
        reset_rules()
        self.assertFalse(http_mock.get_rules())
Beispiel #3
0
    def test_update_rules(self):

        self.assertFalse(http_mock.get_rules())

        # add first rule
        update_http_rules(rules)

        self.assertEqual(len(http_mock.get_rules()), 1)

        matcher = http_mock.get_rules()[0]
        self.assertEqual(matcher._method, 'GET')
        self.assertTrue(hasattr(matcher._url, 'match'))
        self.assertTrue(
            matcher._url.match(
                'https://duckduckgo.com/?q=mock-services'))  # noqa

        response = matcher._responses[0]
        self.assertTrue(hasattr(response._params['text'], '__call__'))
        self.assertEqual(response._params['headers']['Content-Type'],
                         'text/html')  # noqa

        # add second rule
        update_http_rules([
            {
                'method': 'POST',
                'status_code': 201,
                'text': '{"coin": 1}',
                'url': r'http://dummy/',
            },
        ])

        self.assertEqual(len(http_mock.get_rules()), 2)

        matcher = http_mock.get_rules()[1]
        self.assertTrue(hasattr(matcher._url, 'match'))
        self.assertTrue(matcher._url.match('http://dummy/'))
        self.assertEqual(matcher._method, 'POST')

        response = matcher._responses[0]
        self.assertEqual(response._params['status_code'], 201)
        self.assertEqual(response._params['text'], '{"coin": 1}')
        self.assertEqual(response._params['headers']['Content-Type'],
                         'text/plain')  # noqa
Beispiel #4
0
    def test_update_rules(self):

        self.assertFalse(http_mock.get_rules())

        update_rest_rules(rest_rules)

        self.assertEqual(len(http_mock.get_rules()), 10)

        self._test_rule(0, 'GET', 'http://my_fake_service/api')
        self._test_rule(1, 'HEAD', 'http://my_fake_service/api/1',
                        content_type='text/plain')
        self._test_rule(2, 'GET', 'http://my_fake_service/api/1')
        self._test_rule(3, 'GET', 'http://my_fake_service/api/1/download')
        self._test_rule(4, 'POST', 'http://my_fake_service/api')
        self._test_rule(5, 'PATCH', 'http://my_fake_service/api/1')
        self._test_rule(6, 'PUT', 'http://my_fake_service/api/1')
        self._test_rule(7, 'DELETE', 'http://my_fake_service/api/1',
                        content_type='text/plain')
        self._test_rule(8, 'GET', 'http://my_fake_service/api/v2/{0}'.format(uuid.uuid4()))  # noqa
        self._test_rule(9, 'POST', 'http://my_fake_service/api/v2')
Beispiel #5
0
    def test_update_rules(self):

        self.assertFalse(http_mock.get_rules())

        update_rest_rules(rest_rules)

        self.assertEqual(len(http_mock.get_rules()), 10)

        self._test_rule(0, 'GET', 'http://my_fake_service/api')
        self._test_rule(1, 'HEAD', 'http://my_fake_service/api/1',
                        content_type='text/plain')
        self._test_rule(2, 'GET', 'http://my_fake_service/api/1')
        self._test_rule(3, 'GET', 'http://my_fake_service/api/1/download')
        self._test_rule(4, 'POST', 'http://my_fake_service/api')
        self._test_rule(5, 'PATCH', 'http://my_fake_service/api/1')
        self._test_rule(6, 'PUT', 'http://my_fake_service/api/1')
        self._test_rule(7, 'DELETE', 'http://my_fake_service/api/1',
                        content_type='text/plain')
        self._test_rule(8, 'GET', 'http://my_fake_service/api/v2/{0}'.format(uuid.uuid4()))  # noqa
        self._test_rule(9, 'POST', 'http://my_fake_service/api/v2')
Beispiel #6
0
    def _test_rule(self, index, method, url_to_match,
                   content_type='application/json'):

        matcher = http_mock.get_rules()[index]

        self.assertEqual(matcher._method, method)

        self.assertTrue(hasattr(matcher._url, 'match'))
        self.assertTrue(matcher._url.match(url_to_match))

        response = matcher._responses[0]
        self.assertTrue(hasattr(response._params['text'], '__call__'))
        self.assertEqual(response._params['headers']['Content-Type'], content_type)  # noqa
Beispiel #7
0
    def test_update_rules(self):

        self.assertFalse(http_mock.get_rules())

        # add first rule
        update_http_rules(rules)

        self.assertEqual(len(http_mock.get_rules()), 1)

        matcher = http_mock.get_rules()[0]
        self.assertEqual(matcher._method, 'GET')
        self.assertTrue(hasattr(matcher._url, 'match'))
        self.assertTrue(matcher._url.match('https://duckduckgo.com/?q=mock-services'))  # noqa

        response = matcher._responses[0]
        self.assertTrue(hasattr(response._params['text'], '__call__'))
        self.assertEqual(response._params['headers']['Content-Type'], 'text/html')  # noqa

        # add second rule
        update_http_rules([
            {
                'method': 'POST',
                'status_code': 201,
                'text': '{"coin": 1}',
                'url': r'http://dummy/',
            },
        ])

        self.assertEqual(len(http_mock.get_rules()), 2)

        matcher = http_mock.get_rules()[1]
        self.assertTrue(hasattr(matcher._url, 'match'))
        self.assertTrue(matcher._url.match('http://dummy/'))
        self.assertEqual(matcher._method, 'POST')

        response = matcher._responses[0]
        self.assertEqual(response._params['status_code'], 201)
        self.assertEqual(response._params['text'], '{"coin": 1}')
        self.assertEqual(response._params['headers']['Content-Type'], 'text/plain')  # noqa
Beispiel #8
0
    def _test_rule(self, index, method, url_to_match,
                   content_type='application/json'):

        matcher = http_mock.get_rules()[index]

        self.assertEqual(matcher._method, method)

        self.assertTrue(hasattr(matcher._url, 'match'))
        self.assertTrue(matcher._url.match(url_to_match))

        response = matcher._responses[0]
        self.assertTrue(hasattr(response._params['text'], '__call__'))
        self.assertEqual(response._params['headers']['Content-Type'], content_type)  # noqa