forked from getsentry/responses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_responses.py
70 lines (50 loc) · 1.57 KB
/
test_responses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from __future__ import (
absolute_import, print_function, division, unicode_literals
)
import requests
import responses
import pytest
from requests.exceptions import ConnectionError
def assert_response(resp, body=None):
assert resp.status_code == 200
assert resp.headers['Content-Type'] == 'text/plain'
assert resp.text == body
def test_response():
@responses.activate
def run():
responses.add(responses.GET, 'http://example.com', body=b'test')
resp = requests.get('http://example.com')
assert_response(resp, 'test')
run()
def test_connection_error():
@responses.activate
def run():
responses.add(responses.GET, 'http://example.com')
with pytest.raises(ConnectionError):
requests.get('http://example.com/foo')
run()
def test_reset():
@responses.activate
def run():
responses.add(responses.GET, 'http://example.com')
run()
assert len(responses._default_mock._urls) == 0
def test_match_querystring():
@responses.activate
def run():
url = 'http://example.com?test=1'
responses.add(
responses.GET, url,
match_querystring=True, body=b'test')
resp = requests.get(url)
assert_response(resp, 'test')
run()
def test_match_querystring_error():
@responses.activate
def run():
responses.add(
responses.GET, 'http://example.com/?test=1',
match_querystring=True)
with pytest.raises(ConnectionError):
requests.get('http://example.com/foo/?test=2')
run()