test_module_clone.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:foocenter 作者: jctanner 项目源码 文件源码
def record_requests(request):
    # vcr interceptor

    global REQUESTS
    global REQUESTID

    if type(request) == dict:
        thistype = 'response'
    else:
        REQUESTID += 1
        thistype = 'request'

    if thistype == 'request':
        ydata = {}
        ydata['request'] = {}
        ydata['request']['method'] = request.method
        ydata['request']['uri'] = request.uri
        if request.headers:
            ydata['request']['headers'] = {}
            for x in request.headers.items():
                ydata['request']['headers'][x[0]] = x[1]
        else:
            ydata['request']['headers'] = request.headers
        ydata['request']['body'] = literal(pretty_xml(request.body))
        REQUESTS.append(ydata)
    else:
        ydata = REQUESTS[-1]
        ydata['response'] = request.copy()
        REQUESTS[-1]['response'] = request.copy()
        REQUESTS[-1]['response']['body'] = literal(pretty_xml(request['body']['string']))
        fid = str(REQUESTID).rjust(4, '0')
        fname = os.path.join('/tmp', 'fixtures', '%s.yml' % (fid))
        with open(fname, 'wb') as f:
            yaml.dump(REQUESTS[-1], f, width=1000, indent=2)

    return request

# Make a custom VCR instance so we can inject our spyware into it
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号