跳转到主要内容

requests的静态HTTP模拟接口

项目描述

requests-staticmock

https://img.shields.io/pypi/v/requests-staticmock.svg https://img.shields.io/travis/tonybaloney/requests-staticmock.svg Documentation Status https://coveralls.io/repos/github/tonybaloney/requests-staticmock/badge.svg?branch=master

为测试利用Python requests的类提供静态HTTP模拟接口,无需monkey patching!

用法

作为requests Session实例的上下文管理器

requests_staticmock

import requests
import requests_staticmock

session = requests.Session()
with requests_staticmock.mock_session_with_fixtures(session, 'tests/fixtures', 'http://test_context.com'):
    # will return a response object with the contents of tests/fixtures/test.json
    response = new_session.request('get', 'http://test_context.com/test.json')

作为适配器

您可以将requests_staticmock适配器注入现有的(或新的)requests会话以模拟特定的URL或域名,例如

import requests
from requests_staticmock import Adapter

session = requests.Session()
special_adapter = Adapter('fixtures')
session.mount('http://specialwebsite.com', special_adapter)
session.request('http://normal.com/api/example') # works as normal
session.request('http://specialwebsite.com') # returns static mocks

类适配器

除了使用静态资源适配器,您还可以使用一个期望以字符串响应内部方法的适配器,例如

GET /test/example.xml 将调用方法 _test_example_xml(self, request)

GET /test/example.xml?query=param 将调用方法 _test_example_xml(self, request)

这可以通过 requests_staticmock.ClassAdapter 或上下文管理器来实现

import requests
import requests_staticmock


class MyTestClass(requests_staticmock.BaseMockClass):
    def _api_v1_idea(self, request):
        return "woop woop"

session = requests.Session()
with requests_staticmock.mock_session_with_class(session, MyTestClass, 'http://test_context.com'):
    # will return a response object with the contents 'woop woop'
    response = new_session.request('get', 'http://test_context.com/api/v1/idea')

类适配器与解包requests

类适配器支持解包以下组件,只需将这些关键字参数添加到您的回调方法中,类适配器就会将它们与参数匹配。

  • method - HTTP动词,例如GET

  • url - 完整的URL

  • params - 请求参数的字典

  • headers - 请求头

  • body - 请求体文本

import requests
import requests_staticmock

class_session = Session()
class TestMockClass(BaseMockClass):
    def _api_v1_idea(self, method, params, headers):
        if params['special'] == 'value':
            return 'yes'
    def _api_v1_brillo(self, url, body):
        if json.loads(body)['special'] == 'value':
            return 'yes'

a = ClassAdapter(TestMockClass)

session = requests.Session()
with requests_staticmock.mock_session_with_class(session, MyTestClass, 'http://test_context.com'):
    response = new_session.request('get', 'http://test_context.com/api/v1/idea')

功能

  • 允许通过静态固定文件目录模拟HTTP响应

  • 支持匹配URL路径的子目录

致谢

本项目受到由OpenStack基金会维护的requests_mock包的灵感和想法的启发。

此包是用Cookiecutteraudreyr/cookiecutter-pypackage项目模板创建的。

历史

1.4.0 (2017-09-01)

  • 类适配器正确地将 - 字符映射为 _ ,因为 - 在Python中是无效的方法名称

1.3.0 (2017-09-01)

  • 在MockClass中添加一个属性用于适配器实例,有助于你想使用静态固定数据响应时

1.2.0 (2017-05-10)

  • 添加对不区分大小写的文件匹配的支持

1.1.0 (2017-05-10)

  • 添加对查询参数作为文件路径一部分的支持

0.8.0 (2017-02-02)

  • 添加对流式请求和iter_content/iter_lines的支持

0.7.0 (2017-01-29)

  • 添加支持版本解包,类适配器现在支持一系列关键字参数,提供无特定顺序。

0.6.0 (2017-01-29)

  • 添加对类适配器方法返回字符串或响应对象的支持

  • 迁移到Py.Test

0.3.0 (2017-01-29)

  • 添加了一个类适配器

0.2.0 (2017-01-28)

  • 为静态模拟添加了一个上下文管理器

0.1.0 (2017-01-01)

  • 在PyPI上的第一次发布。

项目详情


下载文件

下载适合您平台文件的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

requests-staticmock-1.4.0.tar.gz (20.9 kB 查看散列)

上传时间

构建分布

requests_staticmock-1.4.0-py2.py3-none-any.whl (12.0 kB 查看散列)

上传时间 Python 2 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面