コード例 #1
0
    def test_is_http_mock_started(self):

        update_http_rules(rules)

        self.assertFalse(is_http_mock_started())
        self.assertTrue(start_http_mock())
        self.assertTrue(is_http_mock_started())
コード例 #2
0
    def test_restart_http_mock(self):

        update_http_rules(rules)

        start_http_mock()

        response = requests.get('https://duckduckgo.com/?q=mock-services')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, b'Coincoin!')

        self.assertTrue(stop_http_mock())

        # already stopped
        self.assertFalse(stop_http_mock())

        response = requests.get('https://duckduckgo.com/?q=mock-services')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content[:15], b'<!DOCTYPE html>')

        self.assertTrue(start_http_mock())

        response = requests.get('https://duckduckgo.com/?q=mock-services')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, b'Coincoin!')

        # already started
        self.assertFalse(start_http_mock())
コード例 #3
0
ファイル: test_http_mock.py プロジェクト: jpic/mock-services
    def test_is_http_mock_started(self):

        update_http_rules(rules)

        self.assertFalse(is_http_mock_started())
        self.assertTrue(start_http_mock())
        self.assertTrue(is_http_mock_started())
コード例 #4
0
ファイル: test_http_mock.py プロジェクト: jpic/mock-services
    def test_restart_http_mock(self):

        update_http_rules(rules)

        start_http_mock()

        response = requests.get('https://duckduckgo.com/?q=mock-services')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, 'Coincoin!')

        self.assertTrue(stop_http_mock())

        # already stopped
        self.assertFalse(stop_http_mock())

        response = requests.get('https://duckduckgo.com/?q=mock-services')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content[:15], '<!DOCTYPE html>')

        self.assertTrue(start_http_mock())

        response = requests.get('https://duckduckgo.com/?q=mock-services')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, 'Coincoin!')

        # already started
        self.assertFalse(start_http_mock())
コード例 #5
0
ファイル: test_http_mock.py プロジェクト: jpic/mock-services
    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())
コード例 #6
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())
コード例 #7
0
    def test_no_http_mock(self):

        update_http_rules(rules)

        self.assertTrue(start_http_mock())

        @no_http_mock
        def please_do_not_mock_me():

            self.assertFalse(is_http_mock_started())

            response = requests.get('https://duckduckgo.com/?q=mock-services')
            self.assertEqual(response.status_code, 200)
            self.assertEqual(response.content[:15], b'<!DOCTYPE html>')

        self.assertTrue(is_http_mock_started())
コード例 #8
0
    def test_with_http_mock(self):

        update_http_rules(rules)

        self.assertFalse(is_http_mock_started())

        @with_http_mock
        def please_do_not_mock_me():

            self.assertTrue(is_http_mock_started())

            response = requests.get('https://duckduckgo.com/?q=mock-services')
            self.assertEqual(response.status_code, 200)
            self.assertEqual(response.content, b'Coincoin!')

        self.assertFalse(is_http_mock_started())
コード例 #9
0
ファイル: test_http_mock.py プロジェクト: jpic/mock-services
    def test_with_http_mock(self):

        update_http_rules(rules)

        self.assertFalse(is_http_mock_started())

        @with_http_mock
        def please_do_not_mock_me():

            self.assertTrue(is_http_mock_started())

            response = requests.get('https://duckduckgo.com/?q=mock-services')
            self.assertEqual(response.status_code, 200)
            self.assertEqual(response.content, 'Coincoin!')

        self.assertFalse(is_http_mock_started())
コード例 #10
0
ファイル: test_http_mock.py プロジェクト: jpic/mock-services
    def test_no_http_mock(self):

        update_http_rules(rules)

        self.assertTrue(start_http_mock())

        @no_http_mock
        def please_do_not_mock_me():

            self.assertFalse(is_http_mock_started())

            response = requests.get('https://duckduckgo.com/?q=mock-services')
            self.assertEqual(response.status_code, 200)
            self.assertEqual(response.content[:15], '<!DOCTYPE html>')

        self.assertTrue(is_http_mock_started())
コード例 #11
0
    def test_real_http_1(self):

        update_http_rules(rules)
        self.assertTrue(start_http_mock())

        # allow external call
        http_mock.set_allow_external(True)

        # mocked
        response = requests.get('https://duckduckgo.com/?q=mock-services')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, b'Coincoin!')

        # not mocked but do an external call
        response = requests.get('https://www.google.com/#q=mock-services')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content[:15], b'<!doctype html>')
コード例 #12
0
ファイル: test_http_mock.py プロジェクト: jpic/mock-services
    def test_real_http_1(self):

        update_http_rules(rules)
        self.assertTrue(start_http_mock())

        # allow external call
        http_mock.set_allow_external(True)

        # mocked
        response = requests.get('https://duckduckgo.com/?q=mock-services')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, 'Coincoin!')

        # not mocked but do an external call
        response = requests.get('https://www.google.com/#q=mock-services')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content[:15], '<!doctype html>')
コード例 #13
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
コード例 #14
0
    def test_real_http_0(self):

        update_http_rules(rules)

        self.assertTrue(start_http_mock())

        # mocked
        response = requests.get('https://duckduckgo.com/?q=mock-services')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, b'Coincoin!')

        # not mocked but fail
        self.assertRaises(ConnectionError, requests.get,
                          'https://www.google.com/#q=mock-services')
        # test we keep the request
        try:
            url = 'https://www.google.com/#q=mock-services'
            requests.get(url)
        except ConnectionError as e:
            self.assertEqual(e.request.url, url)
コード例 #15
0
ファイル: test_http_mock.py プロジェクト: jpic/mock-services
    def test_real_http_0(self):

        update_http_rules(rules)

        self.assertTrue(start_http_mock())

        # mocked
        response = requests.get('https://duckduckgo.com/?q=mock-services')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, 'Coincoin!')

        # not mocked but fail
        self.assertRaises(ConnectionError, requests.get,
                          'https://www.google.com/#q=mock-services')
        # test we keep the request
        try:
            url = 'https://www.google.com/#q=mock-services'
            requests.get(url)
        except ConnectionError as e:
            self.assertEqual(e.request.url, url)
コード例 #16
0
ファイル: test_http_mock.py プロジェクト: jpic/mock-services
    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
コード例 #17
0
ファイル: testing.py プロジェクト: tanacca/pylxd
 def add_rules(self, rules):
     mock_services.update_http_rules(rules)
コード例 #18
0
ファイル: testing.py プロジェクト: mrt-k/pylxd
    def setUp(self):
        mock_services.update_http_rules(mock_lxd.RULES)
        mock_services.start_http_mock()

        self.client = Client(endpoint='http://pylxd.test')
コード例 #19
0
ファイル: testing.py プロジェクト: mrt-k/pylxd
 def add_rule(self, rule):
     """Add a rule to the mock LXD service."""
     mock_services.update_http_rules([rule])
コード例 #20
0
ファイル: testing.py プロジェクト: moreati/pylxd
    def setUp(self):
        mock_services.update_http_rules(mock_lxd.RULES)
        mock_services.start_http_mock()

        self.client = Client(endpoint='http://pylxd.test')
コード例 #21
0
ファイル: testing.py プロジェクト: moreati/pylxd
 def add_rule(self, rule):
     """Add a rule to the mock LXD service."""
     mock_services.update_http_rules([rule])