コード例 #1
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())
コード例 #2
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())
コード例 #3
0
ファイル: test_rest_mock.py プロジェクト: jpic/mock-services
    def test_rest_mock_with_uuid(self):

        url = 'http://my_fake_service/api/v2'

        update_rest_rules(rest_rules)
        self.assertTrue(start_http_mock())

        r = requests.get(url + '/{0}'.format(uuid.uuid4()))
        self.assertEqual(r.status_code, 404)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {'error': 'Not Found'})

        r = requests.post(url, data=json.dumps({'foo': 'bar'}),
                          headers=CONTENTTYPE_JSON)
        self.assertEqual(r.status_code, 201)
        self.assertEqual(r.headers, {'content-type': 'application/json'})

        data = r.json()
        _uuid = data.get('uuid')
        self.assertTrue(uuid.UUID(_uuid))
        self.assertEqual(data, {
            'uuid': _uuid,
            'foo': 'bar',
        })

        r = requests.get(url + '/' + _uuid)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {
            'uuid': _uuid,
            'foo': 'bar',
        })
コード例 #4
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())
コード例 #5
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())
コード例 #6
0
    def test_rest_mock_with_uuid(self):

        url = 'http://my_fake_service/api/v2'

        update_rest_rules(rest_rules)
        self.assertTrue(start_http_mock())

        r = requests.get(url + '/{0}'.format(uuid.uuid4()))
        self.assertEqual(r.status_code, 404)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {'error': 'Not Found'})

        r = requests.post(url, data=json.dumps({'foo': 'bar'}),
                          headers=CONTENTTYPE_JSON)
        self.assertEqual(r.status_code, 201)
        self.assertEqual(r.headers, {'content-type': 'application/json'})

        data = r.json()
        _uuid = data.get('uuid')
        self.assertTrue(uuid.UUID(_uuid))
        self.assertEqual(data, {
            'uuid': _uuid,
            'foo': 'bar',
        })

        r = requests.get(url + '/' + _uuid)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {
            'uuid': _uuid,
            'foo': 'bar',
        })
コード例 #7
0
    def test_update_rules_with_another_body_arg(self):

        update_rest_rules([
            {
                'content': b'Coincoin Content!',
                'method': 'GET',
                'url': r'^http://my_fake_service',
            }
        ])
        self.assertTrue(start_http_mock())

        r = requests.get('http://my_fake_service')
        self.assertEqual(r.content, b'Coincoin Content!')
コード例 #8
0
ファイル: test_rest_mock.py プロジェクト: jpic/mock-services
    def test_update_rules_with_another_body_arg(self):

        update_rest_rules([
            {
                'content': 'Coincoin Content!',
                'method': 'GET',
                'url': r'^http://my_fake_service',
            }
        ])
        self.assertTrue(start_http_mock())

        r = requests.get('http://my_fake_service')
        self.assertEqual(r.content, 'Coincoin Content!')
コード例 #9
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())
コード例 #10
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())
コード例 #11
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>')
コード例 #12
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>')
コード例 #13
0
ファイル: test_rest_mock.py プロジェクト: jpic/mock-services
    def test_validators(self):

        url = 'http://my_fake_service/api'

        update_rest_rules(rest_rules_with_validators)
        self.assertTrue(start_http_mock())

        r = requests.post(url, data=json.dumps({}), headers=CONTENTTYPE_JSON)
        self.assertEqual(r.status_code, 400)

        r = requests.post(url, data=json.dumps({
            'foo': 'bar',
        }), headers=CONTENTTYPE_JSON)
        self.assertEqual(r.status_code, 201)

        r = requests.post(url, data=json.dumps({
            'foo': 'bar',
        }), headers=CONTENTTYPE_JSON)
        self.assertEqual(r.status_code, 409)
コード例 #14
0
    def test_validators(self):

        url = 'http://my_fake_service/api'

        update_rest_rules(rest_rules_with_validators)
        self.assertTrue(start_http_mock())

        r = requests.post(url, data=json.dumps({}), headers=CONTENTTYPE_JSON)
        self.assertEqual(r.status_code, 400)

        r = requests.post(url, data=json.dumps({
            'foo': 'bar',
        }), headers=CONTENTTYPE_JSON)
        self.assertEqual(r.status_code, 201)

        r = requests.post(url, data=json.dumps({
            'foo': 'bar',
        }), headers=CONTENTTYPE_JSON)
        self.assertEqual(r.status_code, 409)
コード例 #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
    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)
コード例 #17
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')
コード例 #18
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')
コード例 #19
0
    def test_rest_mock(self):

        url = 'http://my_fake_service/api'

        update_rest_rules(rest_rules)
        self.assertTrue(start_http_mock())

        r = requests.get(url)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), [])

        r = requests.get(url + '/1')
        self.assertEqual(r.status_code, 404)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {'error': 'Not Found'})

        r = requests.get(url + '/1/download')
        self.assertEqual(r.status_code, 404)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {'error': 'Not Found'})

        r = requests.post(url, data=json.dumps({}), headers=CONTENTTYPE_JSON)
        self.assertEqual(r.status_code, 400)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {'error': 'Bad Request'})

        r = requests.patch(url + '/1', data=json.dumps({}))
        self.assertEqual(r.status_code, 404)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {'error': 'Not Found'})

        r = requests.delete(url + '/1')
        self.assertEqual(r.status_code, 404)
        self.assertEqual(r.headers, {'content-type': 'text/plain'})
        self.assertEqual(r.content, b'Not Found')

        # add some data

        r = requests.post(url, data=json.dumps({
            'foo': True,
            'bar': 'Python will save the world.',
        }), headers=CONTENTTYPE_JSON)
        self.assertEqual(r.status_code, 201)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {
            'id': 1,
            'foo': True,
            'bar': 'Python will save the world.',
        })

        r = requests.head(url + '/1')
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers, {
            'content-type': 'text/plain',
            'id': '1',
        })
        self.assertEqual(r.content, b'')

        # recheck list get ...

        r = requests.get(url)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), [
            {
                'id': 1,
                'foo': True,
                'bar': 'Python will save the world.',
            }
        ])

        r = requests.patch(url + '/1', data=json.dumps({
            'bar': "Python will save the world. I don't know how. But it will."
        }))
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {
            'id': 1,
            'foo': True,
            'bar': "Python will save the world. I don't know how. But it will.",  # noqa
        })

        # missing foo field -> 400
        r = requests.put(url + '/1', data=json.dumps({
            'bar': "Python will save the world. I don't know how. But it will."
        }))
        self.assertEqual(r.status_code, 400)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {'error': 'Bad Request'})

        r = requests.put(url + '/1', data=json.dumps({
            'foo': False,
            'bar': "Python will save the world. I don't know how. But it will."
        }))
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {
            'id': 1,
            'foo': False,
            'bar': "Python will save the world. I don't know how. But it will.",  # noqa
        })

        r = requests.get(url + '/1')
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {
            'id': 1,
            'foo': False,
            'bar': "Python will save the world. I don't know how. But it will.",  # noqa
        })

        r = requests.delete(url + '/1')
        self.assertEqual(r.status_code, 204)
        self.assertEqual(r.headers, {'content-type': 'text/plain'})
        self.assertEqual(r.content, b'')

        r = requests.get(url + '/1')
        self.assertEqual(r.status_code, 404)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {
            'error': 'Not Found'
        })
コード例 #20
0
ファイル: test_rest_mock.py プロジェクト: jpic/mock-services
    def test_rest_mock(self):

        url = 'http://my_fake_service/api'

        update_rest_rules(rest_rules)
        self.assertTrue(start_http_mock())

        r = requests.get(url)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), [])

        r = requests.get(url + '/1')
        self.assertEqual(r.status_code, 404)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {'error': 'Not Found'})

        r = requests.get(url + '/1/download')
        self.assertEqual(r.status_code, 404)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {'error': 'Not Found'})

        r = requests.post(url, data=json.dumps({}), headers=CONTENTTYPE_JSON)
        self.assertEqual(r.status_code, 400)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {'error': 'Bad Request'})

        r = requests.patch(url + '/1', data=json.dumps({}))
        self.assertEqual(r.status_code, 404)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {'error': 'Not Found'})

        r = requests.delete(url + '/1')
        self.assertEqual(r.status_code, 404)
        self.assertEqual(r.headers, {'content-type': 'text/plain'})
        self.assertEqual(r.content, 'Not Found')

        # add some data

        r = requests.post(url, data=json.dumps({
            'foo': True,
            'bar': 'Python will save the world.',
        }), headers=CONTENTTYPE_JSON)
        self.assertEqual(r.status_code, 201)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {
            'id': 1,
            'foo': True,
            'bar': 'Python will save the world.',
        })

        r = requests.head(url + '/1')
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers, {
            'content-type': 'text/plain',
            'id': '1',
        })
        self.assertEqual(r.content, '')

        # recheck list get ...

        r = requests.get(url)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), [
            {
                'id': 1,
                'foo': True,
                'bar': 'Python will save the world.',
            }
        ])

        r = requests.patch(url + '/1', data=json.dumps({
            'bar': "Python will save the world. I don't know how. But it will."
        }))
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {
            'id': 1,
            'foo': True,
            'bar': "Python will save the world. I don't know how. But it will.",  # noqa
        })

        # missing foo field -> 400
        r = requests.put(url + '/1', data=json.dumps({
            'bar': "Python will save the world. I don't know how. But it will."
        }))
        self.assertEqual(r.status_code, 400)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {'error': 'Bad Request'})

        r = requests.put(url + '/1', data=json.dumps({
            'foo': False,
            'bar': "Python will save the world. I don't know how. But it will."
        }))
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {
            'id': 1,
            'foo': False,
            'bar': "Python will save the world. I don't know how. But it will.",  # noqa
        })

        r = requests.get(url + '/1')
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {
            'id': 1,
            'foo': False,
            'bar': "Python will save the world. I don't know how. But it will.",  # noqa
        })

        r = requests.delete(url + '/1')
        self.assertEqual(r.status_code, 204)
        self.assertEqual(r.headers, {'content-type': 'text/plain'})
        self.assertEqual(r.content, '')

        r = requests.get(url + '/1')
        self.assertEqual(r.status_code, 404)
        self.assertEqual(r.headers, {'content-type': 'application/json'})
        self.assertEqual(r.json(), {
            'error': 'Not Found'
        })