add API v2
This commit is contained in:
parent
f809c3a8ff
commit
2df0189a90
71 changed files with 2740 additions and 315 deletions
|
@ -1,26 +1,41 @@
|
|||
import unittest
|
||||
import json
|
||||
from mock import MagicMock, patch
|
||||
from api_clients.api_client import ApcUrl, ApiRequest
|
||||
from api_clients.utils import ApcUrl, ApiRequest
|
||||
|
||||
class ResponseInfo:
|
||||
def get_content_type(self):
|
||||
return 'application/json'
|
||||
@property
|
||||
def headers(self):
|
||||
return {'content-type': 'application/json'}
|
||||
|
||||
def json(self):
|
||||
return {'ok', 'ok'}
|
||||
|
||||
class TestApiRequest(unittest.TestCase):
|
||||
def test_init(self):
|
||||
u = ApiRequest('request_name', ApcUrl('/test/ing'))
|
||||
self.assertEqual(u.name, "request_name")
|
||||
|
||||
def test_call(self):
|
||||
ret = json.dumps( {'ok':'ok'} )
|
||||
def test_call_json(self):
|
||||
ret = {'ok':'ok'}
|
||||
read = MagicMock()
|
||||
read.read = MagicMock(return_value=ret)
|
||||
read.info = MagicMock(return_value=ResponseInfo())
|
||||
read.headers = {'content-type': 'application/json'}
|
||||
read.json = MagicMock(return_value=ret)
|
||||
u = 'http://localhost/testing'
|
||||
with patch('urllib.request.urlopen') as mock_method:
|
||||
with patch('requests.get') as mock_method:
|
||||
mock_method.return_value = read
|
||||
request = ApiRequest('mm', ApcUrl(u))()
|
||||
self.assertEqual(request, json.loads(ret))
|
||||
self.assertEqual(request, ret)
|
||||
|
||||
def test_call_html(self):
|
||||
ret = '<html><head></head><body></body></html>'
|
||||
read = MagicMock()
|
||||
read.headers = {'content-type': 'application/html'}
|
||||
read.text = MagicMock(return_value=ret)
|
||||
u = 'http://localhost/testing'
|
||||
with patch('requests.get') as mock_method:
|
||||
mock_method.return_value = read
|
||||
request = ApiRequest('mm', ApcUrl(u))()
|
||||
self.assertEqual(request.text(), ret)
|
||||
|
||||
if __name__ == '__main__': unittest.main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue