当前位置: 首页>>代码示例>>Python>>正文


Python MagicMock.content方法代码示例

本文整理汇总了Python中mock.MagicMock.content方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.content方法的具体用法?Python MagicMock.content怎么用?Python MagicMock.content使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mock.MagicMock的用法示例。


在下文中一共展示了MagicMock.content方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_handle_response

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import content [as 别名]
    def test_handle_response(self):
        connection = Connection('store.mybigcommerce.com', ('user', 'abcdef'))
        # A normal, 200-ok response
        data = {
            'name': 'Shirt'
        }
        res = MagicMock()
        res.headers = {'Content-Type': 'application/json'}
        res.status_code = 200
        res.content = json.dumps(data)
        res.json.return_value = data
        self.assertEqual(connection._handle_response('products/1', res), data)

        res.status_code = 500
        self.assertRaisesHttpException(ServerException,
                                       lambda: connection._handle_response('products/1', res),
                                       # Test all of the properties of a HttpException
                                       500,
                                       {'Content-Type': 'application/json'},
                                       json.dumps(data))

        res.status_code = 404
        self.assertRaisesHttpException(ClientRequestException,
                                       lambda: connection._handle_response('products/1', res), 404)

        res.status_code = 301
        self.assertRaisesHttpException(RedirectionException,
                                       lambda: connection._handle_response('products/1', res), 301)
开发者ID:MUDASSARHASHMI,项目名称:bigcommerce-api-python,代码行数:30,代码来源:test_connection.py

示例2: test_handle_response

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import content [as 别名]
    def test_handle_response(self):
        connection = Connection("store.mybigcommerce.com", ("user", "abcdef"))
        # A normal, 200-ok response
        data = {"name": "Shirt"}
        res = MagicMock()
        res.headers = {"Content-Type": "application/json"}
        res.status_code = 200
        res.content = json.dumps(data)
        res.json.return_value = data
        self.assertEqual(connection._handle_response("products/1", res), data)

        res.status_code = 500
        self.assertRaisesHttpException(
            ServerException,
            lambda: connection._handle_response("products/1", res),
            # Test all of the properties of a HttpException
            500,
            {"Content-Type": "application/json"},
            json.dumps(data),
        )

        res.status_code = 404
        self.assertRaisesHttpException(
            ClientRequestException, lambda: connection._handle_response("products/1", res), 404
        )

        res.status_code = 301
        self.assertRaisesHttpException(
            RedirectionException, lambda: connection._handle_response("products/1", res), 301
        )
开发者ID:bigcommerce,项目名称:bigcommerce-api-python,代码行数:32,代码来源:test_connection.py

示例3: test__request_retries_login

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import content [as 别名]
    def test__request_retries_login(self, mock_login):
        initial_xreply = {"return_code": 1, "content": "login_required"}
        pre_login_response = MagicMock(spec=requests.Response())
        pre_login_response.status_code = 200
        pre_login_response.content = json.dumps(initial_xreply)

        final_xreply = {"return_code": 0, "content": "success"}
        post_login_response = MagicMock(spec=requests.Response())
        post_login_response.status_code = 200
        post_login_response.content = json.dumps(final_xreply)

        side_effect = [pre_login_response, post_login_response]
        self.client.session.request = MagicMock(side_effect=side_effect)

        self.assertEquals((True, "success"),
                          self.client._request("http://example.com"))
开发者ID:abduld,项目名称:bux-grader-framework,代码行数:18,代码来源:test_xqueue.py

示例4: test_missing_keys

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import content [as 别名]
    def test_missing_keys(self):

        http_session = MagicMock()

        response = MagicMock()

        http_session.get = MagicMock()
        http_session.get.return_value = response

        response.status_code = 200
        response.content = "{}"
        response.url = "http://example.com/?camli.mode=config"

        conn = _connect(
            'http://example.com/',
            http_session=http_session,
        )

        self.assertEqual(
            conn.blob_root,
            None,
        )
        self.assertEqual(
            conn.search_root,
            None,
        )
        self.assertEqual(
            conn.sign_root,
            None,
        )
开发者ID:apparentlymart,项目名称:python-camlistore,代码行数:32,代码来源:test_connect.py

示例5: test_json_parsing_error

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import content [as 别名]
    def test_json_parsing_error(self):
        # Make sure if the json parsing fails, we get a useful exception

        # Create a mock response that raises an exception when we try to get the json from it
        mock_response = MagicMock()
        mock_response.text = "This was the content"
        mock_response.content = mock_response.text.encode("utf-8")
        mock_response.json.side_effect = Exception("This is an exception")

        # Mock session that returns our mock response on a 'get' call.
        mock_session = MagicMock()
        mock_session.get.return_value = mock_response

        client = OnaApiClient("example.com", "2384729347234")
        with patch.object(client, "session") as mock_session_method:
            # 'session' method on our client returns our mock session
            mock_session_method.return_value = mock_session

            try:
                client.get("foo")
            except OnaApiClientException as e:
                # Make sure the exception has lots of useful info
                s = str(e)
                self.assertIn("could not be parsed", s)
                self.assertIn("This is an exception", s)
                self.assertIn("This was the content", s)
            else:
                self.fail("Expected OnaApiClientException")
开发者ID:aqueiroz,项目名称:CTS,代码行数:30,代码来源:test_api.py

示例6: test_request_user_info

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import content [as 别名]
    def test_request_user_info(self):

        with patch("wirecloud.fiware.social_auth_backend.requests") as requests_mock:
            response = MagicMock()
            response.content = '{"test": true}'
            requests_mock.get.return_value = response
            self.assertEqual(self.instance._request_user_info("token"), {"test": True})
开发者ID:perezdf,项目名称:wirecloud,代码行数:9,代码来源:social_backend.py

示例7: _mock_response

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import content [as 别名]
 def _mock_response(self, status_code=404, headers={}, content='PAGE_NOT_FOUND'):
     r = MagicMock(Response())
     r.status_code = status_code
     r.headers = CaseInsensitiveDict(headers if headers is not None else {})
     r.content = content
     r.ok = status_code < 400
     return r
开发者ID:appliedcode,项目名称:vcloudtools,代码行数:9,代码来源:requestmocker.py

示例8: test_delete_all_mails

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import content [as 别名]
    def test_delete_all_mails(self):
        doc = MagicMock(spec=SoledadDocument)
        doc.content = {'type': 'head'}
        self.soledad.get_all_docs.return_value = (1, [doc])

        delete_all_mails(self.args)

        self.soledad.delete_doc.assert_called_once_with(doc)
开发者ID:kalfonso,项目名称:pixelated-user-agent,代码行数:10,代码来源:test_commands.py

示例9: request

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import content [as 别名]
    def request(*args, **kwargs):
        mock_response = MagicMock()
        mock_response.content = "this shouldn't be raised"

        def raise_for_status():
            raise HTTPError(response=mock_response)
        mock_response.raise_for_status = raise_for_status
        return mock_response
开发者ID:konopski,项目名称:crate-python,代码行数:10,代码来源:test_http.py

示例10: test_describe_blob

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import content [as 别名]
    def test_describe_blob(self):
        http_session = MagicMock()
        http_session.get = MagicMock()

        response = MagicMock()
        http_session.get.return_value = response

        response.status_code = 200
        response.content = """
        {
            "meta": {
                "dummy1": {
                    "blobRef": "dummy1"
                },
                "dummy2": {
                    "blobRef": "dummy2"
                }
            }
        }
        """

        searcher = SearchClient(
            http_session=http_session,
            base_url="http://example.com/s/",
        )

        result = searcher.describe_blob("dummy1")

        http_session.get.assert_called_with(
            'http://example.com/s/camli/search/describe',
            params={
                'blobref': 'dummy1',
            }
        )

        self.assertEqual(
            type(result),
            BlobDescription,
        )

        self.assertEqual(
            result.raw_dict,
            {
                "blobRef": "dummy1",
            }
        )
        self.assertEqual(
            result.other_raw_dicts,
            {
                "dummy1": {
                    "blobRef": "dummy1",
                },
                "dummy2": {
                    "blobRef": "dummy2",
                },
            }
        )
开发者ID:apparentlymart,项目名称:python-camlistore,代码行数:59,代码来源:test_search_client.py

示例11: test__request_non_200

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import content [as 别名]
    def test__request_non_200(self):
        response = (False, "Unexpected HTTP status code [503]")
        mock_response = MagicMock(spec=requests.Response())
        mock_response.status_code = 503
        mock_response.content = 'Xqueue 500 error'
        mock_request = MagicMock(return_value=mock_response)
        self.client.session.request = mock_request

        self.assertEquals(response,
                          self.client._request('http://example.com', 'get'))
开发者ID:abduld,项目名称:bux-grader-framework,代码行数:12,代码来源:test_xqueue.py

示例12: test__request

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import content [as 别名]
    def test__request(self):
        xreply = {"return_code": 0, "content": "success"}
        response = MagicMock(spec=requests.Response())
        response.status_code = 200
        response.content = json.dumps(xreply)

        self.client.session.request = MagicMock(return_value=response)

        self.assertEquals((True, "success"),
                          self.client._request("http://example.com"))
开发者ID:abduld,项目名称:bux-grader-framework,代码行数:12,代码来源:test_xqueue.py

示例13: test_add_image_from_url

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import content [as 别名]
    def test_add_image_from_url(self, mock_requests_get, mock_add_image_from_string):
        response_mock = MagicMock()
        response_mock.status_code = 200
        response_mock.content = 'imagedatafromhttp'

        mock_requests_get.return_value = response_mock

        self._client.add_image_from_url('validimageurl')
        mock_requests_get.assert_called_once_with('validimageurl')
        mock_add_image_from_string.assert_called_once_with('imagedatafromhttp')
开发者ID:andreasrs,项目名称:imboclient-python,代码行数:12,代码来源:test_client.py

示例14: test_b2_error

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import content [as 别名]
 def test_b2_error(self):
     response = MagicMock()
     response.status_code = 503
     response.content = six.b('{"status": 503, "code": "server_busy", "message": "busy"}')
     # no assertRaises until 2.7
     try:
         _translate_errors(lambda: response)
         self.fail('should have raised ServiceError')
     except ServiceError:
         pass
开发者ID:Backblaze,项目名称:B2_Command_Line_Tool,代码行数:12,代码来源:test_b2http.py

示例15: test_put_observation

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import content [as 别名]
    def test_put_observation(self, mock_conn, caps_mock):
        caps_mock.get_service_host.return_value = 'some.host.com'
        caps_mock.return_value.get_access_url.return_value =\
            'http://serviceurl/caom2repo/pub'
        collection = 'cfht'
        observation_id = '7000000o'
        service = 'caom2repo'
        service_url = 'www.cadc.nrc.ca'

        obs = SimpleObservation(collection, observation_id)
        subject = auth.Subject(certificate='somefile.pem')
        level = logging.DEBUG
        visitor = CAOM2RepoClient(subject, level, host=service_url)
        response = MagicMock()
        response.status = 200
        mock_conn.return_value = response
        iobuffer = BytesIO()
        ObservationWriter().write(obs, iobuffer)
        obsxml = iobuffer.getvalue()
        response.content = obsxml

        visitor.put_observation(obs)
        self.assertEqual('PUT', mock_conn.call_args[0][0].method)
        self.assertEqual(
            '/{}/pub/{}/{}'.format(service, collection, observation_id),
            mock_conn.call_args[0][0].path_url)
        self.assertEqual('application/xml',
                         mock_conn.call_args[0][0].headers['Content-Type'])
        self.assertEqual(obsxml, mock_conn.call_args[0][0].body)

        # signal problems
        http_error = requests.HTTPError()
        response.status_code = 500
        http_error.response = response
        response.raise_for_status.side_effect = [http_error]
        with self.assertRaises(exceptions.InternalServerException):
            visitor.create(obs)

        # temporary transient errors
        http_error = requests.HTTPError()
        response.status_code = 503
        http_error.response = response
        response.raise_for_status.side_effect = [http_error, None]
        visitor.put_observation(obs)

        # permanent transient errors
        http_error = requests.HTTPError()
        response.status_code = 503
        http_error.response = response

        def raise_error(): raise http_error

        response.raise_for_status.side_effect = raise_error
        with self.assertRaises(exceptions.HttpException):
            visitor.put_observation(obs)
开发者ID:opencadc,项目名称:caom2eng,代码行数:57,代码来源:test_core.py


注:本文中的mock.MagicMock.content方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。