示例#1
0
def test_response():
    request = Request(
        'POST',
        'https://example.com/pages/1',
        json={'a': ['b', 'c', 'd']},
        headers={
            'Content-Type': 'application/json',
        }
    )
    prepped = request.prepare()

    payload = 'This is the response body.'.encode('utf-8')
    resp = Response()
    resp.raw = BytesIO(payload)
    resp.request = prepped
    resp.status_code = 200
    resp.url = 'https://example.com/pages/1'
    resp.encoding = 'utf-8'
    resp.reason = 'OK'
    resp.elapsed = timedelta(seconds=2)
    resp.headers = CaseInsensitiveDict({
        'Content-Type': 'text/plain',
        'Content-Length': len(payload)
    })
    assert pformat(resp, width=999) == """\
# Response content not loaded yet
requests.Response(
    status_code=200,  # OK
    url='https://example.com/pages/1',
    elapsed=datetime.timedelta(seconds=2),
    headers=requests.structures.CaseInsensitiveDict({
        'Content-Length': 26,
        'Content-Type': 'text/plain'
    })
)"""

    # Loads content
    resp.text

    assert pformat(resp, width=999) == """\