Esempio n. 1
0
 def test_waits_if_retry_after_is_forced(self):
     self.requests_mock.request.side_effect = [self.response_503,
                                               self.response_200]
     with mock.patch('kinto_client.session.time.sleep') as sleep_mocked:
         session = Session('https://example.org', retry=1, retry_after=10)
         session.request('GET', '/v1/foobar')
         sleep_mocked.assert_called_with(10)
Esempio n. 2
0
 def test_body_is_none_on_304(self):
     response = mock.MagicMock()
     response.status_code = 304
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     body, headers = session.request('get', 'https://example.org/test')
     assert body is None
Esempio n. 3
0
 def test_body_is_none_on_304(self):
     response = mock.MagicMock()
     response.status_code = 304
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     body, headers = session.request('get', 'https://example.org/test')
     assert body is None
Esempio n. 4
0
 def test_forced_retry_after_overrides_value_of_header(self):
     self.response_503.headers["Retry-After"] = 27
     self.requests_mock.request.side_effect = [self.response_503,
                                               self.response_200]
     with mock.patch('kinto_client.session.time.sleep') as sleep_mocked:
         session = Session('https://example.org', retry=1, retry_after=10)
         session.request('GET', '/v1/foobar')
         sleep_mocked.assert_called_with(10)
Esempio n. 5
0
 def test_payload_is_sent_on_put_requests(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     session.request('put', 'https://example.org/anothertest')
     self.requests_mock.request.assert_called_with(
         'put', 'https://example.org/anothertest', json={'data': {}})
Esempio n. 6
0
 def test_can_retry_several_times(self):
     self.requests_mock.request.side_effect = [
         self.response_503,
         self.response_503,  # retry 1
         self.response_200
     ]  # retry 2
     session = Session('https://example.org', retry=2)
     session.request('GET', '/v1/foobar')  # Not raising.
Esempio n. 7
0
 def test_no_payload_is_sent_on_get_requests(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     session.request('get', 'https://example.org/anothertest')
     self.requests_mock.request.assert_called_with(
         'get', 'https://example.org/anothertest')
Esempio n. 8
0
 def test_requests_arguments_are_forwarded(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     session.request('get', '/test', foo=mock.sentinel.bar)
     self.requests_mock.request.assert_called_with(
         'get', 'https://example.org/test', foo=mock.sentinel.bar)
Esempio n. 9
0
 def test_fails_if_retry_exhausted(self):
     self.requests_mock.request.side_effect = [self.response_503,
                                               self.response_503,  # retry 1
                                               self.response_503,  # retry 2
                                               self.response_200]
     session = Session('https://example.org', retry=2)
     with self.assertRaises(KintoException):
         session.request('GET', '/v1/foobar')
Esempio n. 10
0
 def test_waits_if_retry_after_is_forced(self):
     self.requests_mock.request.side_effect = [
         self.response_503, self.response_200
     ]
     with mock.patch('kinto_client.session.time.sleep') as sleep_mocked:
         session = Session('https://example.org', retry=1, retry_after=10)
         session.request('GET', '/v1/foobar')
         sleep_mocked.assert_called_with(10)
Esempio n. 11
0
 def test_waits_if_retry_after_header_is_present(self):
     self.response_503.headers["Retry-After"] = 27
     self.requests_mock.request.side_effect = [self.response_503,
                                               self.response_200]
     with mock.patch('kinto_client.session.time.sleep') as sleep_mocked:
         session = Session('https://example.org', retry=1)
         session.request('GET', '/v1/foobar')
         self.assertTrue(sleep_mocked.called)
Esempio n. 12
0
 def test_waits_if_retry_after_header_is_present(self):
     self.response_503.headers["Retry-After"] = 27
     self.requests_mock.request.side_effect = [
         self.response_503, self.response_200
     ]
     with mock.patch('kinto_client.session.time.sleep') as sleep_mocked:
         session = Session('https://example.org', retry=1)
         session.request('GET', '/v1/foobar')
         self.assertTrue(sleep_mocked.called)
Esempio n. 13
0
 def test_no_auth_is_used_by_default(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     self.assertEquals(session.auth, None)
     session.request('get', '/test')
     self.requests_mock.request.assert_called_with(
         'get', 'https://example.org/test')
Esempio n. 14
0
 def test_session_injects_auth_on_requests(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session(auth=mock.sentinel.auth,
                       server_url='https://example.org')
     session.request('get', '/test')
     self.requests_mock.request.assert_called_with(
         'get', 'https://example.org/test', auth=mock.sentinel.auth)
Esempio n. 15
0
 def test_forced_retry_after_overrides_value_of_header(self):
     self.response_503.headers["Retry-After"] = 27
     self.requests_mock.request.side_effect = [
         self.response_503, self.response_200
     ]
     with mock.patch('kinto_client.session.time.sleep') as sleep_mocked:
         session = Session('https://example.org', retry=1, retry_after=10)
         session.request('GET', '/v1/foobar')
         sleep_mocked.assert_called_with(10)
Esempio n. 16
0
 def test_no_auth_is_used_by_default(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     self.assertEquals(session.auth, None)
     session.request('get', '/test')
     self.requests_mock.request.assert_called_with(
         'get', 'https://example.org/test')
Esempio n. 17
0
 def test_passed_data_is_encoded_to_json(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     session.request('post', '/test', data={'foo': 'bar'})
     self.requests_mock.request.assert_called_with(
         'post', 'https://example.org/test', json={"data": {
             'foo': 'bar'
         }})
Esempio n. 18
0
 def test_fails_if_retry_exhausted(self):
     self.requests_mock.request.side_effect = [
         self.response_503,
         self.response_503,  # retry 1
         self.response_503,  # retry 2
         self.response_200
     ]
     session = Session('https://example.org', retry=2)
     with self.assertRaises(KintoException):
         session.request('GET', '/v1/foobar')
Esempio n. 19
0
 def test_url_is_used_if_schema_is_present(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     permissions = mock.MagicMock()
     permissions.as_dict.return_value = {'foo': 'bar'}
     session.request('get', 'https://example.org/anothertest')
     self.requests_mock.request.assert_called_with(
         'get', 'https://example.org/anothertest')
Esempio n. 20
0
 def test_passed_data_is_encoded_to_json(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     session.request('post', '/test',
                     data={'foo': 'bar'})
     self.requests_mock.request.assert_called_with(
         'post', 'https://example.org/test',
         json={"data": {'foo': 'bar'}})
Esempio n. 21
0
 def test_session_injects_auth_on_requests(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session(auth=mock.sentinel.auth,
                       server_url='https://example.org')
     session.request('get', '/test')
     self.requests_mock.request.assert_called_with(
         'get', 'https://example.org/test',
         auth=mock.sentinel.auth)
Esempio n. 22
0
 def test_requests_arguments_are_forwarded(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     session.request('get', '/test',
                     foo=mock.sentinel.bar)
     self.requests_mock.request.assert_called_with(
         'get', 'https://example.org/test',
         foo=mock.sentinel.bar)
Esempio n. 23
0
 def test_url_is_used_if_schema_is_present(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     permissions = mock.MagicMock()
     permissions.as_dict.return_value = {'foo': 'bar'}
     session.request('get', 'https://example.org/anothertest')
     self.requests_mock.request.assert_called_with(
         'get', 'https://example.org/anothertest')
Esempio n. 24
0
 def test_passed_data_is_passed_as_is_when_files_are_posted(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     session.request('post', '/test',
                     data='{"foo": "bar"}',
                     files={"attachment": {"filename"}})
     self.requests_mock.request.assert_called_with(
         'post', 'https://example.org/test',
         data={"data": '{"foo": "bar"}'},
         files={"attachment": {"filename"}})
Esempio n. 25
0
 def test_passed_permissions_is_added_in_the_payload(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     permissions = mock.MagicMock()
     permissions.as_dict.return_value = {'foo': 'bar'}
     session.request('post', '/test',
                     permissions=permissions)
     self.requests_mock.request.assert_called_with(
         'post', 'https://example.org/test',
         json={'data': {}, 'permissions': {'foo': 'bar'}})
Esempio n. 26
0
 def test_passed_data_is_passed_as_is_when_files_are_posted(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     session.request('post',
                     '/test',
                     data='{"foo": "bar"}',
                     files={"attachment": {"filename"}})
     self.requests_mock.request.assert_called_with(
         'post',
         'https://example.org/test',
         data={"data": '{"foo": "bar"}'},
         files={"attachment": {"filename"}})
Esempio n. 27
0
    def test_bad_http_status_raises_exception(self):
        response = mock.MagicMock()
        response.status_code = 400
        self.requests_mock.request.return_value = response
        session = Session('https://example.org')

        self.assertRaises(KintoException, session.request, 'get', '/test')
Esempio n. 28
0
 def test_passed_permissions_is_added_in_the_payload(self):
     response = mock.MagicMock()
     response.status_code = 200
     self.requests_mock.request.return_value = response
     session = Session('https://example.org')
     permissions = mock.MagicMock()
     permissions.as_dict.return_value = {'foo': 'bar'}
     session.request('post', '/test', permissions=permissions)
     self.requests_mock.request.assert_called_with(
         'post',
         'https://example.org/test',
         json={
             'data': {},
             'permissions': {
                 'foo': 'bar'
             }
         })
Esempio n. 29
0
 def test_does_not_retry_by_default(self):
     session = Session('https://example.org')
     with self.assertRaises(KintoException):
         session.request('GET', '/v1/foobar')
Esempio n. 30
0
 def test_succeeds_on_retry(self):
     self.requests_mock.request.side_effect = [
         self.response_503, self.response_200
     ]  # retry 1
     session = Session('https://example.org', retry=1)
     session.request('GET', '/v1/foobar')  # Not raising.
Esempio n. 31
0
 def test_does_not_retry_on_400_errors(self):
     self.requests_mock.request.side_effect = [self.response_403]
     session = Session('https://example.org', retry=1)
     with self.assertRaises(KintoException):
         session.request('GET', '/v1/foobar')
Esempio n. 32
0
 def test_can_retry_several_times(self):
     self.requests_mock.request.side_effect = [self.response_503,
                                               self.response_503,  # retry 1
                                               self.response_200]  # retry 2
     session = Session('https://example.org', retry=2)
     session.request('GET', '/v1/foobar')  # Not raising.
Esempio n. 33
0
 def test_succeeds_on_retry(self):
     self.requests_mock.request.side_effect = [self.response_503,
                                               self.response_200]  # retry 1
     session = Session('https://example.org', retry=1)
     session.request('GET', '/v1/foobar')  # Not raising.
Esempio n. 34
0
 def test_does_not_retry_by_default(self):
     session = Session('https://example.org')
     with self.assertRaises(KintoException):
         session.request('GET', '/v1/foobar')
Esempio n. 35
0
 def test_does_not_retry_on_400_errors(self):
     self.requests_mock.request.side_effect = [self.response_403]
     session = Session('https://example.org', retry=1)
     with self.assertRaises(KintoException):
         session.request('GET', '/v1/foobar')
Esempio n. 36
0
 def test_uses_specified_server_url(self):
     session = Session(mock.sentinel.server_url)
     self.assertEquals(session.server_url, mock.sentinel.server_url)