Beispiel #1
0
def test_httprequest_create(monkeypatch):

    monkeypatch.setattr("urllib2.build_opener", lambda h: MockedOpener(h))

    req = reqres.Request('http://example.com', headers={'k1': 'v1'})
    assert isinstance(req, reqres.Request)
    assert req.headers == {'k1': 'v1'}

    req = reqres.Request(u'http://example.com', headers={'k1': 'v1'})
    assert isinstance(req, reqres.Request)
    assert req.headers == {'k1': 'v1'}
Beispiel #2
0
def test_httprequest_patch(monkeypatch):

    monkeypatch.setattr("urllib2.build_opener", lambda h: MockedOpener(h))

    req = reqres.Request('http://example.com', headers={'k1': 'v1'})
    resp = req.patch().send()
    assert isinstance(resp, reqres.Response)
    assert resp.body == 'MockedResponse.read()'
Beispiel #3
0
def test_httprequest_get_error(monkeypatch):

    monkeypatch.setattr("urllib2.build_opener", lambda h: MockedOpener(h))

    URL = 'http://non-exists.com'

    req = reqres.Request(URL)
    resp = req.get().send()
    assert isinstance(resp, reqres.Response)
    assert resp.code == 500
    assert resp.error_msg == 'The host does not exist, %s' % URL
Beispiel #4
0
def test_httprequest_get_stream(monkeypatch):

    monkeypatch.setattr("urllib2.build_opener", lambda h: MockedOpener(h))

    req = reqres.Request('http://example.com', headers={'k1': 'v1'})
    resp = req.get().send(stream=True)
    assert isinstance(resp, reqres.Response)
    body = ''
    while True:
        chunk = resp.body.read(64000)
        if not chunk:
            break
        body += chunk
    assert body == 'MockedResponse.read()'
Beispiel #5
0
def test_httprequest_failed_request(monkeypatch):

    monkeypatch.setattr("urllib2.Request", lambda url, data: None)
    req = reqres.Request('http://example.com')
    with pytest.raises(RuntimeError):
        req.get()