Python requests.models() Examples

The following are 3 code examples for showing how to use requests.models(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module requests , or try the search function .

Example 1
Project: ClusterRunner   Author: box   File: test_cluster_slave.py    License: Apache License 2.0 5 votes vote down vote up
def _mock_network_post_and_put(self, expected_results_api_url, expected_idle_api_url):
        # Since subjob execution and teardown is async, we use Events to tell our test when each thread has completed.
        subjob_done_event = Event()
        setup_done_event = Event()
        teardown_done_event = Event()

        def _get_success_mock_response():
            mock_response = MagicMock(spec=requests.models.Response, create=True)
            mock_response.status_code = http.client.OK
            mock_response.ok = True
            return mock_response

        def fake_network_post(url, *args, **kwargs):
            if url == expected_results_api_url:
                subjob_done_event.set()  # Consider subjob finished once code posts to results url.
            return _get_success_mock_response()

        def fake_network_put(url, request_params, **kwargs):
            if url == expected_idle_api_url:
                if request_params['slave']['state'] == SlaveState.SETUP_COMPLETED:
                    setup_done_event.set()
                elif request_params['slave']['state'] == SlaveState.IDLE:
                    teardown_done_event.set()
            return _get_success_mock_response()

        self.mock_network.post = fake_network_post
        self.mock_network.post_with_digest = fake_network_post
        self.mock_network.put = fake_network_put
        self.mock_network.put_with_digest = fake_network_put
        return subjob_done_event, teardown_done_event, setup_done_event 
Example 2
Project: ClusterRunner   Author: box   File: test_cluster_slave.py    License: Apache License 2.0 5 votes vote down vote up
def test_slave_dies_on_http_404_from_master(self):
        slave = self._create_cluster_slave()
        slave.connect_to_master(self._FAKE_MASTER_URL)

        mock_response = MagicMock(spec=requests.models.Response, create=True)
        mock_response.ok = False
        mock_response.status_code = http.client.NOT_FOUND
        self.mock_network.post_with_digest.return_value = mock_response

        slave._run_heartbeat()

        self.mock_network.post_with_digest.assert_called_once_with(
                        ANY,request_params={'slave': {'heartbeat': True}}, secret=ANY)
        self.assertEqual(self._mock_sys.exit.call_count, 1,
                         'slave dies when it receives HTTP 404 from master') 
Example 3
Project: ClusterRunner   Author: box   File: test_cluster_slave.py    License: Apache License 2.0 5 votes vote down vote up
def test_slave_dies_if_master_reponds_with_is_alive_false(self):
            slave = self._create_cluster_slave()
            slave.connect_to_master(self._FAKE_MASTER_URL)

            mock_response = MagicMock(spec=requests.models.Response, create=True)
            mock_response.json.return_value = {'is_alive': False}
            self.mock_network.post_with_digest.return_value = mock_response

            slave._run_heartbeat()

            self.mock_network.post_with_digest.assert_called_once_with(
                            ANY,request_params={'slave': {'heartbeat': True}}, secret=ANY)
            self.assertEqual(self._mock_sys.exit.call_count, 1,
                             'slave dies when master responds with is_alive = False')