def test_request_raises(self):
        failed_resp = Mock(ok=False, status_code=500)
        failed_resp.raise_for_status = Mock(side_effect=requests.exceptions.RequestException)
        self.session.request = Mock(return_value=failed_resp)

        wc = BaseWebClient(self.session)
        with self.assertRaises(Error):
            wc.request('http://example.com/')
    def test_get_text_returns_none_for_binary(self):
        mock_response = MagicMock(headers={'content-type': 'application/blah'})
        wc = BaseWebClient(None)

        rv = wc.get_text(mock_response)

        self.assertIs(rv, None)
    def test_get_text_returns_text(self):
        mock_response = MagicMock(headers={'content-type': 'text/html'}, text='some text')
        wc = BaseWebClient(None)

        rv = wc.get_text(mock_response)

        self.assertEqual(rv, 'some text')
    def test_user_request_updates_session_cookies(self):
        cookies = {'test name': 'test value'}
        mock_acc = Mock(cookies=cookies)
        wc = BaseWebClient(self.session)

        wc.user_request(mock_acc, 'http://example.com')

        self.session.cookies.update.assert_called_once_with(cookies)
    def test_authorized_request_raises(self, is_logged_in):
        is_logged_in.return_value = False
        mock_response = MagicMock(headers={'content-type': 'text/html'})
        self.session.request = Mock(return_value=mock_response)
        wc = BaseWebClient(self.session)

        with self.assertRaises(NotLoggedIn):
            wc.authorized_request(Mock(), 'http://example.com/', 'GET')
    def test_authorized_request_returns_response(self):
        mock_response = MagicMock()
        self.session.request = Mock(return_value=mock_response)
        wc = BaseWebClient(self.session)

        resp = wc.authorized_request(Mock(), 'http://example.com/', 'GET')

        self.assertIs(mock_response, resp)
    def test_user_request_updates_user_cookies(self):
        cookies = {'name': 'value'}
        mock_acc = Mock(cookies=None)
        self.session.cookies.get_dict = Mock(return_value=cookies)
        wc = BaseWebClient(self.session)
        wc.tracker_log_in = Mock(return_value=cookies)

        wc.user_request(mock_acc, 'http://example.com')

        self.assertIs(mock_acc.cookies, cookies)
 def test_request_calls_request(self):
     url = 'http://example.com/'
     webclient = BaseWebClient(self.session)
     resp = webclient.request(url)
     self.assertTrue(self.session.request.called)