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())
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())
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', })
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())
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!')
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!')
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())
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())
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>')
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>')
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)
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)
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)
def setUp(self): mock_services.update_http_rules(mock_lxd.RULES) mock_services.start_http_mock() self.client = Client(endpoint='http://pylxd.test')
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' })
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' })