test_mock.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.asyncio
  12. def test_mock_client(api_server):
  13. expectation = {
  14. 'method': 'POST',
  15. 'request': {'body': {'email': 'john@doe.com',
  16. 'name': 'John Doe',
  17. 'password': 'secret'},
  18. 'headers': {'accept': 'application/json',
  19. 'content_type': 'application/json'}},
  20. 'response': {'body': {'email': 'john@doe.com', 'name': 'John Doe'},
  21. 'headers': {'content_type': 'application/json'},
  22. 'status_code': 201}
  23. }
  24. netloc = urlsplit(api_server).netloc
  25. host, port = splitport(netloc)
  26. with Mock(expectation) as client:
  27. health = client.health()
  28. response = client.response()
  29. assert health.status_code == 200
  30. assert response.status_code == 201
  31. data = json.dumps(expectation['request']['body'])
  32. response = yield from aiohttp.request('POST', client.mock_url,
  33. data=data,
  34. headers=request_headers)
  35. assert response.status == 201
  36. @pytest.mark.asyncio
  37. def test_mock_client_without_request(api_server):
  38. expectation = {
  39. 'method': 'POST',
  40. 'response': {'body': {'email': 'john@doe.com', 'name': 'John Doe'},
  41. 'headers': {'content_type': 'application/json'},
  42. 'status_code': 201}
  43. }
  44. netloc = urlsplit(api_server).netloc
  45. host, port = splitport(netloc)
  46. with Mock(expectation) as client:
  47. health = client.health()
  48. response = client.response()
  49. assert health.status_code == 200
  50. assert response.status_code == 201
  51. data = json.dumps('')
  52. response = yield from aiohttp.request('POST', client.mock_url,
  53. data=data,
  54. headers=request_headers)
  55. assert response.status == 201