def test_send_with_appropriate_method(mocker, req_params): req = Request(**req_params) methods = Request._allowed_methods for method in methods: req.method = method mock_reqfunc = mocker.patch.object(requests.Session, method.lower(), autospec=True) assert mock_reqfunc.call_count == 0 req.send() mock_reqfunc.assert_called_once_with(mocker.ANY, req.build_url(), data=req._content, headers=req.headers)
def test_send_returns_appropriate_sorna_response(mocker, req_params, mock_sorna_resp): req = Request(**req_params) methods = Request._allowed_methods for method in methods: req.method = method mock_reqfunc = mocker.patch.object(requests.Session, method.lower(), autospec=True) mock_reqfunc.return_value, conf = mock_sorna_resp resp = req.send() assert resp.status == conf['status_code'] assert resp.reason == conf['reason'] assert resp.content_type == conf['headers']['content-type'] assert resp.content_length == conf['headers']['content-length'] assert resp.text() == conf['text'] assert resp.json() == json.loads(conf['text'])
async def test_asend_with_appropriate_method(mocker, req_params): req = Request(**req_params) methods = Request._allowed_methods for method in methods: req.method = method mock_reqfunc = mocker.patch.object(aiohttp.ClientSession, method.lower(), autospec=True) assert mock_reqfunc.call_count == 0 try: # Ignore exceptions in `async with` statement. We're only # interested in request call here. await req.asend() except BackendAPIError: pass mock_reqfunc.assert_called_once_with(mocker.ANY, req.build_url(), data=req._content, headers=req.headers)
async def test_asend_returns_appropriate_sorna_response( mocker, req_params, mock_sorna_aresp): req = Request(**req_params) methods = Request._allowed_methods for method in methods: req.method = method mock_reqfunc = mocker.patch.object( aiohttp.ClientSession, method.lower(), new_callable=asynctest.CoroutineMock) mock_reqfunc.return_value, conf = mock_sorna_aresp resp = await req.asend() assert isinstance(resp, Response) assert resp.status == conf['status'] assert resp.reason == conf['reason'] assert resp.content_type == conf['content_type'] body = await conf['text']() assert resp.content_length == len(body) assert resp.text() == body assert resp.json() == json.loads(body)