跳转到主要内容

一个用于记录和回放HTTP交互的httplib注入库。

项目描述

“我希望你们都很好,直到不再好的时候。”

– Dalton, Road-House

一个用于记录和回放HTTP交互的httplib注入库。

Dalton通过monkey-patch httplib的HTTPConnection类中的两个方法来拦截请求/响应交互,并可以根据Dalton的记录回放它们。为了简化测试,记录生成了Python代码,以方便自定义响应并允许在回放路径中添加分支。

Monkey-patch的HTTPConnection方法
  • request

  • getresponse

目前不支持使用更详尽的方法通过HTTPConnection发送/接收请求。

注意: 这是一个第一个和早期的版本,主要是为了让我能用它与mechanize记录/回放交互。由于mechanize只使用HTTPConnection上的request/getresponse API,我没有兴趣添加对其他API的拦截。请随意分叉此项目以添加额外的功能,因为我没有计划自己添加它们(尽管我很乐意接受带有单元测试的错误修复和功能添加)。

警告: Dalton使用inspect.currentframe魔术方法来推导调用者,这可能在CPython上才能正常工作(PyPy和Jython未测试)。

示例

由于dalton monkey-patch了httplib,因此无需修改使用支持方法的库。

import dalton
dalton.inject() # monkey-patch httplib

from httplib import HTTPConnection
h = HTTPConnection('www.google.com')

# when recording, httplib capture is restricted by caller
recorder = dalton.Recorder(caller=h)

# record httplib calls in this block
with recorder.recording():
    h.request('GET', '/')
    resp = h.getresponse()
    body = resp.read()

# save the interaction
recorder.save('google')

将在当前目录下创建一个名为 google 的文件夹,用于与 dalton 的播放功能一起使用。

播放它

import dalton
dalton.inject() # monkey-patch httplib

from httplib import HTTPConnection
h = HTTPConnection('www.google.com')

# load the player
player = dalton.Player(caller=h, playback_dir='google')

# run httplib calls against the player
with player.playing():
    h.request('GET', '/')
    resp = h.getresponse()
    body = resp.read()

# body is now the same as it was recorded, no calls to www.google.com
# were made
这将生成一个名为 google 的目录,布局如下
  • __init__.py

  • step_0_response.txt

__init__.py 的内容包含以下生成的播放信息

import os
import dalton
from dalton import FileWrapper

here = os.path.abspath(os.path.dirname(__file__))

class StepNumber0(object):
    recorded_request = {
        'headers':  {},
        'url': '/',
        'method': 'GET',
        'body': None,
    }
    recorded_response = {
        'headers':  [('x-xss-protection', '1; mode=block'),
                     ('transfer-encoding', 'chunked'),
                     (                    'set-cookie',
                                          'PREF=ID=ff; expires=Thu, 11-Apr-2013 20:19:35 GMT; path=/; domain=.google.com, NID=45=fU; expires=Wed, 12-Oct-2011 20:19:35 GMT; path=/; domain=.google.com; HttpOnly'),
                     ('expires', '-1'),
                     ('server', 'gws'),
                     ('cache-control', 'private, max-age=0'),
                     ('date', 'Tue, 12 Apr 2011 20:19:35 GMT'),
                     ('content-type', 'text/html; charset=ISO-8859-1')],
        'body': FileWrapper('step_0_response.txt', here),
        'status': 200,
        'reason': 'OK',
        'version': 11,
    }
    next_step = 'None'

    def handle_request(self, request):
        assert dalton.request_match(request, self.recorded_request)
        return (self.next_step, dalton.create_response(self.recorded_response))

该文件可以在录制后进行修改,以自定义播放、添加额外的分支等。

支持

Dalton 已被认为是功能完整的,因为项目所有者 (Ben Bangert) 没有计划进行任何额外的功能或开发,除了错误修复。错误可以在 github 上提交,应该附带测试用例以保持当前的代码覆盖率,并且应该在准备好接受到 Dalton 代码库时以拉取请求的形式提交。

项目详情


下载文件

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

源分布

dalton-1.0.tar.gz (19.3 kB 查看散列)

上传时间

支持