test_mock.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # coding: utf-8
  2. import json
  3. from urllib.parse import urlsplit, splitport
  4. import pytest
  5. import aiohttp
  6. from echod.mock import Mock
  7. request_headers = {
  8. 'content_type': 'application/json',
  9. 'accept': 'application/json',
  10. }
  11. @pytest.mark.skipif(True, reason='https://github.com/wiliamsouza/echod/issues/5')
  12. @pytest.mark.asyncio
  13. def test_mock_client(api_server):
  14. expectation = {
  15. 'method': 'POST',
  16. 'request': {'body': {'email': 'john@doe.com',
  17. 'name': 'John Doe',
  18. 'password': 'secret'},
  19. 'headers': {'accept': 'application/json',
  20. 'content_type': 'application/json'}},
  21. 'response': {'body': {'email': 'john@doe.com', 'name': 'John Doe'},
  22. 'headers': {'content_type': 'application/json'},
  23. 'status_code': 201}
  24. }
  25. netloc = urlsplit(api_server).netloc
  26. host, port = splitport(netloc)
  27. # TODO: Fix this api_server not work inside a context manager
  28. with Mock(expectation) as client:
  29. health = client.health()
  30. response = client.response()
  31. assert health.status_code == 200
  32. assert response.status_code == 201
  33. data = json.dumps(expectation['request']['body'])
  34. response = yield from aiohttp.request('POST', client.mock_url,
  35. data=data,
  36. headers=request_headers)
  37. assert response.status == 201
  38. @pytest.mark.skipif(True, reason='https://github.com/wiliamsouza/echod/issues/5')
  39. @pytest.mark.asyncio
  40. def test_mock_client_without_request(api_server):
  41. expectation = {
  42. 'method': 'POST',
  43. 'response': {'body': {'email': 'john@doe.com', 'name': 'John Doe'},
  44. 'headers': {'content_type': 'application/json'},
  45. 'status_code': 201}
  46. }
  47. netloc = urlsplit(api_server).netloc
  48. host, port = splitport(netloc)
  49. # TODO: Fix this api_server not work inside a context manager
  50. with Mock(expectation) as client:
  51. health = client.health()
  52. response = client.response()
  53. assert health.status_code == 200
  54. assert response.status_code == 201
  55. data = json.dumps('')
  56. response = yield from aiohttp.request('POST', client.mock_url,
  57. data=data,
  58. headers=request_headers)
  59. assert response.status == 201