FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-scrapyd-api/tests/test_client.py at master · githubleaflife/python-scrapyd-api · GitHub
githubleaflife
/
python-scrapyd-api
Public
forked from
djm/python-scrapyd-api
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-scrapyd-api
/
tests
/
test_client.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
116 lines (97 loc) · 3.39 KB
Breadcrumbs
python-scrapyd-api
/
tests
/
test_client.py
Copy path
File metadata and controls
116 lines (97 loc) · 3.39 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import
json
import
responses
import
pytest
from
scrapyd_api
.
client
import
Client
from
scrapyd_api
.
exceptions
import
ScrapydResponseError
SCRAPYD_RESPONSE_OK
=
{
'status'
:
'ok'
,
'example'
:
'Test'
,
'another-example'
:
'Another Test'
,
}
SCRAPYD_RESPONSE_ERROR
=
{
'status'
:
'error'
,
'message'
:
'Test Error'
}
URL
=
'http://localhost/'
AUTH
=
(
'username'
,
'password'
)
OK_JSON
=
'{"status": "ok", "key": "value"}'
ERROR_JSON
=
'{"status": "error", "message": "some-error"}'
MALFORMED_JSON
=
'this-aint-json'
@
responses
.
activate
def
test_get_handle_ok_response
():
"""
Test that a GET request uses the requests lib properly.
"""
non_authed_client
=
Client
()
responses
.
add
(
responses
.
GET
,
URL
,
body
=
OK_JSON
,
status
=
200
)
non_authed_client
.
get
(
URL
)
assert
len
(
responses
.
calls
)
==
1
call
=
responses
.
calls
[
0
]
assert
call
.
request
.
url
==
URL
assert
call
.
response
.
json
()
==
json
.
loads
(
OK_JSON
)
# Test with some query string params.
test_params
=
{
'test'
:
'params'
}
url_with_query
=
URL
+
'?test=params'
responses
.
add
(
responses
.
GET
,
url_with_query
,
body
=
OK_JSON
,
status
=
200
)
non_authed_client
.
get
(
URL
,
params
=
test_params
)
assert
len
(
responses
.
calls
)
==
2
call
=
responses
.
calls
[
1
]
assert
call
.
response
.
json
()
==
json
.
loads
(
OK_JSON
)
@
responses
.
activate
def
test_post_handle_ok_response
():
"""
Test that a POST request uses the requests lib properly.
"""
non_authed_client
=
Client
()
responses
.
add
(
responses
.
POST
,
URL
,
body
=
OK_JSON
,
status
=
200
)
test_data
=
{
'test'
:
'json'
}
non_authed_client
.
post
(
URL
,
data
=
test_data
)
assert
len
(
responses
.
calls
)
==
1
call
=
responses
.
calls
[
0
]
assert
call
.
response
.
json
()
==
json
.
loads
(
OK_JSON
)
@
responses
.
activate
def
test_handle_http_error_response
():
"""
Test that an 'Error' response from Scrapyd handles as desired.
"""
non_authed_client
=
Client
()
responses
.
add
(
responses
.
GET
,
URL
,
body
=
MALFORMED_JSON
,
status
=
500
)
with
pytest
.
raises
(
ScrapydResponseError
)
as
excinfo
:
non_authed_client
.
get
(
URL
)
assert
'500 error'
in
str
(
excinfo
.
value
)
@
responses
.
activate
def
test_non_or_invalid_json_response_errors
():
"""
Test that a response from Scrapyd that does not parse as
valid JSON raises the correct exception.
"""
non_authed_client
=
Client
()
responses
.
add
(
responses
.
GET
,
URL
,
body
=
MALFORMED_JSON
,
status
=
200
)
with
pytest
.
raises
(
ScrapydResponseError
)
as
excinfo
:
non_authed_client
.
get
(
URL
)
assert
'invalid JSON'
in
str
(
excinfo
.
value
)
@
responses
.
activate
def
test_scrapyd_error_response
():
"""
Test that a response from Scrapyd that does not parse as
valid JSON raises the correct exception.
"""
non_authed_client
=
Client
()
responses
.
add
(
responses
.
GET
,
URL
,
body
=
ERROR_JSON
,
status
=
200
)
with
pytest
.
raises
(
ScrapydResponseError
)
as
excinfo
:
non_authed_client
.
get
(
URL
)
assert
'some-error'
in
str
(
excinfo
.
value
)
@
responses
.
activate
def
test_with_auth
():
"""
Test attaching basic auth creds results in correct headers.
"""
authed_client
=
Client
()
authed_client
.
auth
=
AUTH
# Test with just a URL call.
responses
.
add
(
responses
.
GET
,
URL
,
body
=
OK_JSON
,
status
=
200
)
authed_client
.
get
(
URL
)
assert
len
(
responses
.
calls
)
==
1
call
=
responses
.
calls
[
0
]
assert
'Authorization'
in
call
.
request
.
headers
assert
'Basic'
in
call
.
request
.
headers
[
'Authorization'
]
Back
|
FazBrowse Home
|
New Git URL