• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python tests.mocked_response函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中tests.mocked_response函数的典型用法代码示例。如果您正苦于以下问题:Python mocked_response函数的具体用法?Python mocked_response怎么用?Python mocked_response使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: test_long_poll_for_events_and_errors

    def test_long_poll_for_events_and_errors(self):
        client = BoxClient("my_token")

        longpoll_response = {
            "type": "realtime_server",
            "url": "http://2.realtime.services.box.net/subscribe?channel=12345678&stream_type=all",
            "ttl": "10",
            "max_retries": "10",
            "retry_timeout": 610,
        }

        (flexmock(client).should_receive("_get_long_poll_data").and_return(longpoll_response).times(2))

        expected_get_params = {
            "channel": ["12345678"],
            "stream_type": "changes",
            "stream_position": "some_stream_position",
        }

        (
            flexmock(requests)
            .should_receive("get")
            .with_args("http://2.realtime.services.box.net/subscribe", params=expected_get_params)
            .and_return(mocked_response({"message": "foo"}))
            .and_return(mocked_response("some error", status_code=400))
            .times(2)
        )

        with self.assertRaises(BoxClientException) as expect_exception:
            client.long_poll_for_events("some_stream_position", stream_type=EventFilter.CHANGES)

        self.assertEqual(400, expect_exception.exception.status_code)
        self.assertEqual("some error", expect_exception.exception.message)
开发者ID:NOVO-Construction,项目名称:box.py,代码行数:33,代码来源:test_client.py


示例2: test_handle_auth_response

    def test_handle_auth_response(self):
        _handle_auth_response(mocked_response({'code': 'bla'}))
        with self.assertRaises(BoxAuthenticationException) as expected_exception:
            _handle_auth_response(mocked_response({'error': 'some_error', 'error_description': 'foobar'}))

        self.assertEqual('foobar', expected_exception.exception.message)
        self.assertEqual('some_error', expected_exception.exception.error)
开发者ID:drucko,项目名称:box.py,代码行数:7,代码来源:test_authentication.py


示例3: test_get_client_with_retry

    def test_get_client_with_retry(self):
        client = BoxClient("my_token")

        (
            flexmock(requests)
            .should_receive("request")
            .with_args(
                "get",
                "https://api.box.com/2.0/files/123/thumbnail.png",
                params={},
                data=None,
                headers=client.default_headers,
                stream=True,
            )
            .and_return(
                mocked_response(
                    status_code=202, headers={"Location": "http://box.com/url_to_thumbnail", "Retry-After": "1"}
                ),
                mocked_response(StringIO("Thumbnail contents")),
            )
            .one_by_one()
        )

        thumbnail = client.get_thumbnail(123, max_wait=1)
        self.assertEqual("Thumbnail contents", thumbnail.read())
开发者ID:NOVO-Construction,项目名称:box.py,代码行数:25,代码来源:test_client.py


示例4: test_long_poll_for_events_multiple_tries

    def test_long_poll_for_events_multiple_tries(self):
        client = BoxClient("my_token")

        longpoll_response = {
            "type": "realtime_server",
            "url": "http://2.realtime.services.box.net/subscribe?channel=12345678&stream_type=all",
            "ttl": "10",
            "max_retries": "10",
            "retry_timeout": 610,
        }

        (flexmock(client).should_receive("_get_long_poll_data").and_return(longpoll_response).times(5))

        (flexmock(client).should_receive("get_events").times(0))

        expected_get_params = {
            "channel": ["12345678"],
            "stream_type": "changes",
            "stream_position": "some_stream_position",
        }

        (
            flexmock(requests)
            .should_receive("get")
            .with_args("http://2.realtime.services.box.net/subscribe", params=expected_get_params)
            .and_return(mocked_response({"message": "foo"}))
            .and_return(mocked_response({"message": "foo"}))
            .and_return(mocked_response({"message": "foo"}))
            .and_return(mocked_response({"message": "foo"}))
            .and_return(mocked_response({"message": "new_message"}))
            .times(5)
        )

        position = client.long_poll_for_events("some_stream_position", stream_type=EventFilter.CHANGES)
        self.assertEqual("some_stream_position", position)
开发者ID:NOVO-Construction,项目名称:box.py,代码行数:35,代码来源:test_client.py


示例5: test_finish_authenticate_error

    def test_finish_authenticate_error(self):
        response = mocked_response('something_terrible', status_code=400)
        (flexmock(requests)
            .should_receive('get')
            .with_args('https://www.box.com/api/1.0/rest', params={
                'action': 'get_auth_token',
                'api_key': 'my_api_key',
                'ticket': 'golden_ticket'
            })
            .and_return(response))

        with self.assertRaises(BoxAuthenticationException) as expected_exception:
            finish_authenticate_v1('my_api_key', 'golden_ticket')

        self.assertEqual('something_terrible', expected_exception.exception.message)

        response = mocked_response('<response><status>something_terrible</status></response>')
        (flexmock(requests)
            .should_receive('get')
            .with_args('https://www.box.com/api/1.0/rest', params={
                'action': 'get_auth_token',
                'api_key': 'my_api_key',
                'ticket': 'golden_ticket'
            })
            .and_return(response))

        with self.assertRaises(BoxAuthenticationException) as expected_exception:
            finish_authenticate_v1('my_api_key', 'golden_ticket')

        self.assertEqual('something_terrible', expected_exception.exception.message)
开发者ID:drucko,项目名称:box.py,代码行数:30,代码来源:test_authentication.py


示例6: test_error

    def test_error(self):
        expected_args = {
            'sensor': 'false',
            'address': 'my magic address'
        }
        (flexmock(requests)
            .should_receive('get')
            .with_args('http://maps.googleapis.com/maps/api/geocode/json', params=expected_args)
            .and_return(mocked_response({'status': 'ERROR'}))
        )

        with self.assertRaises(GeolocationExcetion) as expected_exception:
            geolocate('my magic address')

        self.assertEqual(expected_exception.exception.message, {'status': 'ERROR'})

        expected_args = {
            'sensor': 'false',
            'address': 'my magic address'
        }
        (flexmock(requests)
            .should_receive('get')
            .with_args('http://maps.googleapis.com/maps/api/geocode/json', params=expected_args)
            .and_return(mocked_response({'say': 'what'}, status_code=401))
        )

        with self.assertRaises(GeolocationExcetion) as expected_exception:
            geolocate('my magic address')

        self.assertEqual(expected_exception.exception.message, {'say': 'what'})
开发者ID:JustinLimT,项目名称:uber.py,代码行数:30,代码来源:test_geolocation.py


示例7: test_long_poll_for_events_and_errors

    def test_long_poll_for_events_and_errors(self):
        client = BoxClient('my_token')

        longpoll_response = {
            'type': 'realtime_server',
            'url': 'http://2.realtime.services.box.net/subscribe?channel=12345678&stream_type=all',
            'ttl': '10',
            'max_retries': '10',
            'retry_timeout': 610
        }

        (flexmock(client)
            .should_receive('_get_long_poll_data')
            .and_return(longpoll_response)
            .times(2))

        expected_get_params = {
            'channel': ['12345678'],
            'stream_type': 'changes',
            'stream_position': 'some_stream_position',
        }

        (flexmock(requests)
            .should_receive('get')
            .with_args('http://2.realtime.services.box.net/subscribe', params=expected_get_params)
            .and_return(mocked_response({'message': 'foo'}))
            .and_return(mocked_response('some error', status_code=400))
            .times(2))

        with self.assertRaises(BoxClientException) as expect_exception:
            client.long_poll_for_events('some_stream_position', stream_type=EventFilter.CHANGES)

        self.assertEqual(400, expect_exception.exception.status_code)
        self.assertEqual('some error', expect_exception.exception.message)
开发者ID:drucko,项目名称:box.py,代码行数:34,代码来源:test_client.py


示例8: test_get_thumbnail_with_params

    def test_get_thumbnail_with_params(self):
        client = BoxClient("my_token")

        # Not available
        (
            flexmock(requests)
            .should_receive("request")
            .with_args(
                "get",
                "https://api.box.com/2.0/files/123/thumbnail.png",
                params={},
                data=None,
                headers=client.default_headers,
                stream=True,
            )
            .and_return(mocked_response(status_code=302))
            .once()
        )

        thumbnail = client.get_thumbnail(123)
        self.assertIsNone(thumbnail)

        # Already available
        (
            flexmock(requests)
            .should_receive("request")
            .with_args(
                "get",
                "https://api.box.com/2.0/files/123/thumbnail.png",
                params={},
                data=None,
                headers=client.default_headers,
                stream=True,
            )
            .and_return(mocked_response(StringIO("Thumbnail contents")))
            .once()
        )

        thumbnail = client.get_thumbnail(123)
        self.assertEqual("Thumbnail contents", thumbnail.read())

        # With size requirements
        (
            flexmock(requests)
            .should_receive("request")
            .with_args(
                "get",
                "https://api.box.com/2.0/files/123/thumbnail.png",
                params={"min_height": 1, "max_height": 2, "min_width": 3, "max_width": 4},
                data=None,
                headers=client.default_headers,
                stream=True,
            )
            .and_return(mocked_response(StringIO("Thumbnail contents")))
            .once()
        )

        thumbnail = client.get_thumbnail(123, min_height=1, max_height=2, min_width=3, max_width=4)
        self.assertEqual("Thumbnail contents", thumbnail.read())
开发者ID:NOVO-Construction,项目名称:box.py,代码行数:59,代码来源:test_client.py


示例9: test_automatic_refresh

    def test_automatic_refresh(self):
        credentials = CredentialsV2("access_token", "refresh_token", "client_id", "client_secret")
        client = BoxClient(credentials)

        requests_mock = flexmock(requests)

        # The first attempt, which is denied
        (
            requests_mock.should_receive("request")
            .with_args(
                "get", "https://api.box.com/2.0/users/me", params=None, data=None, headers=client.default_headers
            )
            .and_return(mocked_response(status_code=401))
            .once()
        )

        # The call to refresh the token
        (
            requests_mock.should_receive("post")
            .with_args(
                "https://www.box.com/api/oauth2/token",
                {
                    "client_id": "client_id",
                    "client_secret": "client_secret",
                    "refresh_token": "refresh_token",
                    "grant_type": "refresh_token",
                },
            )
            .and_return(mocked_response({"access_token": "new_access_token", "refresh_token": "new_refresh_token"}))
            .once()
        )

        # The second attempt with the new access token
        (
            requests_mock.should_receive("request")
            .with_args(
                "get",
                "https://api.box.com/2.0/users/me",
                params=None,
                data=None,
                headers={"Authorization": "Bearer new_access_token"},
            )
            .and_return(mocked_response({"name": "bla"}))
            .once()
        )

        result = client.get_user_info()
        self.assertDictEqual(result, {"name": "bla"})

        self.assertEqual(credentials._access_token, "new_access_token")
        self.assertEqual(credentials._refresh_token, "new_refresh_token")
开发者ID:NOVO-Construction,项目名称:box.py,代码行数:51,代码来源:test_client.py


示例10: test_overwrite_file

    def test_overwrite_file(self):
        client = BoxClient("my_token")

        (flexmock(client).should_receive("_check_for_errors").once())

        expected_headers = {"If-Match": "some_tag"}
        expected_headers.update(client.default_headers)

        expected_response = mocked_response({"entries": [{"id": "1"}]})
        (
            flexmock(requests)
            .should_receive("post")
            .with_args(
                "https://upload.box.com/api/2.0/files/666/content",
                {"content_modified_at": "2006-05-04T03:02:01+00:00"},
                headers=expected_headers,
                files={"file": FileObjMatcher("hello world")},
            )
            .and_return(expected_response)
            .once()
        )

        result = client.overwrite_file(
            666,
            StringIO("hello world"),
            etag="some_tag",
            content_modified_at=datetime(2006, 5, 4, 3, 2, 1, 0, tzinfo=UTC()),
        )
        self.assertEqual({"id": "1"}, result)
开发者ID:NOVO-Construction,项目名称:box.py,代码行数:29,代码来源:test_client.py


示例11: test_upload_file_with_timestamps

    def test_upload_file_with_timestamps(self):
        client = BoxClient("my_token")
        response = mocked_response({"entries": [{"id": "1"}]})

        (flexmock(client).should_receive("_check_for_errors").once())
        (
            flexmock(requests)
            .should_receive("post")
            .with_args(
                "https://upload.box.com/api/2.0/files/content",
                {
                    "parent_id": "666",
                    "content_modified_at": "2007-05-04T03:02:01+00:00",
                    "content_created_at": "2006-05-04T03:02:01+00:00",
                },
                headers=client.default_headers,
                files={"hello.jpg": ("hello.jpg", FileObjMatcher("hello world"))},
            )
            .and_return(response)
            .once()
        )

        result = client.upload_file(
            "hello.jpg",
            StringIO("hello world"),
            parent=666,
            content_created_at=datetime(2006, 5, 4, 3, 2, 1, 0, tzinfo=UTC()),
            content_modified_at=datetime(2007, 5, 4, 3, 2, 1, 0, tzinfo=UTC()),
        )
        self.assertEqual({"id": "1"}, result)
开发者ID:NOVO-Construction,项目名称:box.py,代码行数:30,代码来源:test_client.py


示例12: test_add_task

    def test_add_task(self):
        client = BoxClient("my_token")
        due_at = datetime.now()

        expected_data = {
            "item": {"type": "file", "id": 123},
            "action": "review",
            "due_at": str(due_at),
            "message": "test",
        }

        response = {"type": "task", "id": 123, "action": "review", "message": "test", "due_at": str(due_at)}

        (
            flexmock(requests)
            .should_receive("request")
            .with_args(
                "post",
                "https://api.box.com/2.0/tasks",
                params=None,
                data=json.dumps(expected_data),
                headers=client.default_headers,
            )
            .and_return(mocked_response(response))
        )

        task = client.add_task(123, due_at, message="test")
        self.assertEquals(task, response)
开发者ID:NOVO-Construction,项目名称:box.py,代码行数:28,代码来源:test_client.py


示例13: test_get_long_poll_data

    def test_get_long_poll_data(self):
        client = BoxClient("my_token")

        expected_response = {
            "type": "realtime_server",
            "url": "http://2.realtime.services.box.net/subscribe?channel=e9de49a73f0c93a872d7&stream_type=all",
            "ttl": "10",
            "max_retries": "10",
            "retry_timeout": 610,
        }

        response = mocked_response({"chunk_size": 1, "entries": [expected_response]})

        (
            flexmock(requests)
            .should_receive("request")
            .with_args(
                "options", "https://api.box.com/2.0/events", headers=client.default_headers, data=None, params=None
            )
            .and_return(response)
            .once()
        )

        actual_response = client._get_long_poll_data()
        self.assertDictEqual(expected_response, actual_response)
开发者ID:NOVO-Construction,项目名称:box.py,代码行数:25,代码来源:test_client.py


示例14: test_send_message

    def test_send_message(self):
        client = UberClient("[email protected]", "12345")
        response = mocked_response("omg")
        expected_data = {
            "email": "[email protected]",
            "deviceOS": settings.DEVICE_OS,
            "language": "en",
            "deviceModel": settings.DEVICE_MODEL,
            "app": "client",
            "messageType": "111",
            "token": "12345",
            "version": settings.UBER_VERSION,
            "device": settings.DEVICE_NAME,
            "aaa": "bbb",
        }

        (flexmock(UberClient).should_receive("_copy_location_for_message").with_args(self.mock_location, dict).times(1))

        (
            flexmock(UberClient)
            .should_receive("_post")
            .with_args(UberClient.ENDPOINT, data=DictPartialMatcher(expected_data))
            .times(1)
            .and_return(response)
        )

        (flexmock(UberClient).should_receive("_validate_message_response").with_args(response.json()).times(1))

        params = {"aaa": "bbb"}

        self.assertEqual("omg", client._send_message("111", params, self.mock_location))
开发者ID:Lee-Kevin,项目名称:uber.py,代码行数:31,代码来源:test_client.py


示例15: test_long_poll_for_events_ok

    def test_long_poll_for_events_ok(self):
        client = BoxClient('my_token')

        longpoll_response = {
            'type': 'realtime_server',
            'url': 'http://2.realtime.services.box.net/subscribe?channel=12345678&stream_type=all',
            'ttl': '10',
            'max_retries': '10',
            'retry_timeout': 610
        }

        (flexmock(client)
            .should_receive('_get_long_poll_data')
            .and_return(longpoll_response)
            .once())

        expected_get_params = {
            'channel': ['12345678'],
            'stream_type': 'changes',
            'stream_position': 'some_stream_position',
        }

        (flexmock(requests)
            .should_receive('get')
            .with_args('http://2.realtime.services.box.net/subscribe', params=expected_get_params)
            .and_return(mocked_response({'message': 'new_message'}))
            .once())

        position = client.long_poll_for_events('some_stream_position', stream_type=EventFilter.CHANGES)
        self.assertEqual('some_stream_position', position)
开发者ID:drucko,项目名称:box.py,代码行数:30,代码来源:test_client.py


示例16: test_update_assignment

    def test_update_assignment(self):
        client = BoxClient("my_token")

        response = {"type": "task_assignment",
                    "id": 123,
                    "message": "All good !!!",
                    "resolution_state": "completed",
                    "assigned_to": {"type": "user",
                                    "id": 123,
                                    "login": "[email protected]"},
                    "item": {"type": "task",
                             "id": 123}
        }

        expected_data = {"resolution_state": "completed",
                         "message": "All good !!!"}

        (flexmock(requests)
            .should_receive('request')
            .with_args("put",
                 "https://api.box.com/2.0/task_assignments/123",
                 params=None,
                 data=json.dumps(expected_data),
                 headers=client.default_headers)
        .and_return(mocked_response(response)))

        changed = client.update_assignment(123, "completed", "All good !!!")
        self.assertEquals(changed, response)
开发者ID:cogniteev,项目名称:box.py,代码行数:28,代码来源:test_client.py


示例17: make_client

    def make_client(self, method, path, params=None, data=None, headers=None, endpoint="api", result=None, **kwargs):
        """
        Makes a new test client
        """
        client = BoxClient('my_token')
        (flexmock(client)
            .should_receive('_check_for_errors')
            .once())

        if headers:
            headers = dict(headers)
            headers.update(client.default_headers)
        else:
            headers = client.default_headers

        if isinstance(data, dict):
            data = json.dumps(data)

        (flexmock(requests)
            .should_receive('request')
            .with_args(method,
                       'https://%s.box.com/2.0/%s' % (endpoint, path),
                       params=params,
                       data=data,
                       headers=headers,
                       **kwargs)
            .and_return(mocked_response(result))
            .once())

        return client
开发者ID:drucko,项目名称:box.py,代码行数:30,代码来源:test_client.py


示例18: test_add_assignment

    def test_add_assignment(self):
        client = BoxClient("my_token")

        response = {"type": "task_assignment",
                    "id": 123,
                    "assigned_to": {"type": "user",
                                    "id": 123,
                                    "login": "[email protected]"},
                    "item": {"type": "task",
                             "id": 123}
        }

        expected_data = {"task": {"id": 123,
                                  "type": "task"},
                         "assign_to": {"id": 123,
                                       "login": "[email protected]"}
        }

        (flexmock(requests)
            .should_receive('request')
            .with_args("post",
                 "https://api.box.com/2.0/task_assignments",
                 params=None,
                 data=json.dumps(expected_data),
                 headers=client.default_headers)
        .and_return(mocked_response(response)))

        assignment = client.assign_task(123, user_id=123, login="[email protected]")
        self.assertEquals(assignment, response)
开发者ID:cogniteev,项目名称:box.py,代码行数:29,代码来源:test_client.py


示例19: test_change_task

    def test_change_task(self):
        client = BoxClient("my_token")
        due_at = datetime.now()

        expected_data = {"action": "review",
                         "due_at": str(due_at),
                         "message": "changed"
        }

        response = {"type": "task",
                    "id": 123,
                    "action": "review",
                    "message": "changed",
                    "due_at": str(due_at)
        }

        (flexmock(requests)
            .should_receive('request')
            .with_args("put",
                 "https://api.box.com/2.0/tasks/123",
                 params=None,
                 data=json.dumps(expected_data),
                 headers=client.default_headers)
        .and_return(mocked_response(response)))

        changed = client.change_task(123, due_at, message="changed")
        self.assertEquals(changed, response)
开发者ID:cogniteev,项目名称:box.py,代码行数:27,代码来源:test_client.py


示例20: test_add_comment_to_comment

    def test_add_comment_to_comment(self):
        client = BoxClient("my_token")

        response = {"type": "comment",
                    "id": 123,
                    "item": {"id": 123,
                             "type": "comment"},
                    "message": "test"
        }

        expected_data={"item": {"type": "comment",
                                "id": 123},
                       "message": "test"
        }

        (flexmock(requests)
            .should_receive('request')
            .with_args("post",
                 "https://api.box.com/2.0/comments",
                 params=None,
                 data=json.dumps(expected_data),
                 headers=client.default_headers)
        .and_return(mocked_response(response)))

        comment = client.add_comment(123, "comment", "test")
        self.assertEquals(comment, response)
开发者ID:cogniteev,项目名称:box.py,代码行数:26,代码来源:test_client.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python tests.new_notebook函数代码示例发布时间:2022-05-27
下一篇:
Python tests.mkstemp函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap