-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathRequestsKeywords.py
393 lines (320 loc) · 17.4 KB
/
RequestsKeywords.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import requests
import robot
from robot.api.deco import keyword
from robot.libraries.BuiltIn import BuiltIn
from RequestsLibrary import log
from RequestsLibrary.compat import urljoin
from RequestsLibrary.utils import (
is_file_descriptor,
warn_if_equal_symbol_in_url_session_less,
)
class RequestsKeywords(object):
def __init__(self):
self._cache = robot.utils.ConnectionCache("No sessions created")
self.builtin = BuiltIn()
self.debug = 0
# The following variables are related to session but used in _common_request :(
self.timeout = None
self.cookies = None
self.last_response = None
def _common_request(self, method, session, uri, **kwargs):
if session:
request_function = getattr(session, "request")
else:
request_function = getattr(requests, "request")
self._capture_output()
resp = request_function(
method,
self._merge_url(session, uri),
timeout=self._get_timeout(kwargs.pop("timeout", None)),
cookies=kwargs.pop("cookies", self.cookies),
**kwargs
)
log.log_request(resp)
self._print_debug()
log.log_response(resp)
self.last_response = resp
files = kwargs.get("files", {}) or {}
data = kwargs.get("data", []) or []
files_descriptor_to_close = filter(
is_file_descriptor, list(files.values()) + [data]
)
for file_descriptor in files_descriptor_to_close:
file_descriptor.close()
return resp
@staticmethod
def _merge_url(session, uri):
"""
Helper method that join session url and request url.
It relies on urljoin that handles quite good join urls and multiple /
but has some counterintuitive behaviours if you join uri starting with /
It handles also override in case a full url (http://etc) is passed as uri.
"""
base = ""
if session:
base = session.url
if session and uri and not session.url.endswith("/"):
base = session.url + "/"
if session and uri and uri.startswith("/"):
uri = uri[1:]
url = urljoin(base, uri)
return url
@keyword("Status Should Be")
def status_should_be(self, expected_status, response=None, msg=None):
"""
Fails if response status code is different than the expected.
``expected_status`` could be the code number as an integer or as string.
But it could also be a named status code like 'ok', 'created', 'accepted' or
'bad request', 'not found' etc.
``response`` is the output of other requests keywords like `GET` or `GET On Session`.
If omitted the last response will be used.
In case of failure an HTTPError will be automatically raised.
A custom failure message ``msg`` can be added like in built-in keywords.
New requests keywords like `GET` or `GET On Session` (starting from 0.8 version) already have an implicit assert
mechanism that, by default, verifies the response status code.
`Status Should Be` keyword can be useful when you disable implicit assert using ``expected_status=anything``.
For example when you have a nested keyword that is used for both OK and ERROR responses:
| ***** Test Cases *****
|
| Test Get Request And Make Sure Is A 404 Response
| ${resp}= GET Custom Keyword That Returns OK or ERROR Response case=notfound
| Status Should Be 404 ${resp}
| Should Be Equal As Strings NOT FOUND ${resp.reason}
|
| Test Get Request And Make Sure Is OK
| ${resp}= GET Custom Keyword That Returns OK or ERROR Response case=pass
| Status Should Be 200 ${resp}
| Should Be Equal As Strings OK ${resp.reason}
|
| ***** Keywords *****
|
| GET Custom Keyword That Returns OK or ERROR Response
| [Arguments] $case
| [...]
| IF $case == notfound
| $resp= GET [...] expected_status=Anything
| [Return] $resp
| ELSE
| [...]
"""
if response is None:
response = self.last_response
self._check_status(expected_status, response, msg)
@keyword("Request Should Be Successful")
def request_should_be_successful(self, response=None):
"""
Fails if response status code is a client or server error (4xx, 5xx).
``response`` is the output of other requests keywords like `GET On Session`.
If omitted the last response will be used.
In case of failure an HTTPError will be automatically raised.
For a more versatile assert keyword see `Status Should Be`.
"""
if not response:
response = self.last_response
self._check_status(None, response, msg=None)
@staticmethod
@keyword("Get File For Streaming Upload")
def get_file_for_streaming_upload(path):
"""
Opens and returns a file descriptor of a specified file to be passed as ``data`` parameter
to other requests keywords.
This allows streaming upload of large files without reading them into memory.
File descriptor is binary mode and read only. Requests keywords will automatically close the file,
if used outside this library it's up to the caller to close it.
"""
return open(path, "rb")
@keyword("GET")
@warn_if_equal_symbol_in_url_session_less
def session_less_get(
self, url, params=None, expected_status=None, msg=None, **kwargs
):
"""
Sends a GET request.
The endpoint used to retrieve the resource is the ``url``, while query
string parameters can be passed as string, dictionary (or list of tuples or bytes)
through the ``params``.
By default this keyword fails if a status code with error values is returned in the response,
this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
read more about it in `Status Should Be` keyword documentation.
In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
``anything``.
Other optional requests arguments can be passed using ``**kwargs`` here is a list:
| ``data`` | Dictionary, list of tuples, bytes, or file-like object to send in the body of the request. |
| ``json`` | A JSON serializable Python object to send in the body of the request. |
| ``headers`` | Dictionary of HTTP Headers to send with the request. |
| ``cookies`` | Dict or CookieJar object to send with the request. |
| ``files`` | Dictionary of file-like-objects (or ``{'name': file-tuple}``) for multipart encoding upload. ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')`` or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers to add for the file. |
| ``auth`` | Auth tuple to enable Basic/Digest/Custom HTTP Auth. |
| ``timeout`` | How many seconds to wait for the server to send data before giving up, as a float, or a ``(connect timeout, read timeout)`` tuple. |
| ``allow_redirects`` | Boolean. Enable/disable (values ``${True}`` or ``${False}``). Only for HEAD method keywords allow_redirection defaults to ``${False}``, all others ``${True}``. |
| ``proxies`` | Dictionary mapping protocol or protocol and host to the URL of the proxy (e.g. {'http': 'foo.bar:3128', 'http://host.name': 'foo.bar:4012'}) |
| ``verify`` | Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to ``${True}``. Warning: if a session has been created with ``verify=${False}`` any other requests will not verify the SSL certificate. |
| ``stream`` | if ``${False}``, the response content will be immediately downloaded. |
| ``cert`` | if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. |
For more updated and complete information verify the official Requests api documentation:
https://requests.readthedocs.io/en/latest/api/
"""
response = self._common_request("GET", None, url, params=params, **kwargs)
self._check_status(expected_status, response, msg)
return response
@keyword("POST")
@warn_if_equal_symbol_in_url_session_less
def session_less_post(
self, url, data=None, json=None, expected_status=None, msg=None, **kwargs
):
"""
Sends a POST request.
The endpoint used to send the request is the ``url`` parameter, while its body
can be passed using ``data`` or ``json`` parameters.
``data`` can be a dictionary, list of tuples, bytes, or file-like object.
If you want to pass a json body pass a dictionary as ``json`` parameter.
By default this keyword fails if a status code with error values is returned in the response,
this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
read more about it in `Status Should Be` keyword documentation.
In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
``anything``.
Other optional requests arguments can be passed using ``**kwargs``
see the `GET` keyword for the complete list.
"""
response = self._common_request(
"POST", None, url, data=data, json=json, **kwargs
)
self._check_status(expected_status, response, msg)
return response
@keyword("PUT")
@warn_if_equal_symbol_in_url_session_less
def session_less_put(
self, url, data=None, json=None, expected_status=None, msg=None, **kwargs
):
"""
Sends a PUT request.
The endpoint used to send the request is the ``url`` parameter, while its body
can be passed using ``data`` or ``json`` parameters.
``data`` can be a dictionary, list of tuples, bytes, or file-like object.
If you want to pass a json body pass a dictionary as ``json`` parameter.
By default this keyword fails if a status code with error values is returned in the response,
this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
read more about it in `Status Should Be` keyword documentation.
In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
``anything``.
Other optional requests arguments can be passed using ``**kwargs``
see the `GET` keyword for the complete list.
"""
response = self._common_request(
"PUT", None, url, data=data, json=json, **kwargs
)
self._check_status(expected_status, response, msg)
return response
@keyword("HEAD")
@warn_if_equal_symbol_in_url_session_less
def session_less_head(self, url, expected_status=None, msg=None, **kwargs):
"""
Sends a HEAD request.
The endpoint used to retrieve the HTTP headers is the ``url``.
``allow_redirects`` parameter is not provided, it will be set to ``${False}`` (as
opposed to the default behavior ``${True}``).
By default this keyword fails if a status code with error values is returned in the response,
this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
read more about it in `Status Should Be` keyword documentation.
In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
``anything``.
Other optional requests arguments can be passed using ``**kwargs``
see the `GET` keyword for the complete list.
"""
if "allow_redirects" not in kwargs:
kwargs["allow_redirects"] = False
response = self._common_request("HEAD", None, url, **kwargs)
self._check_status(expected_status, response, msg)
return response
@keyword("PATCH")
@warn_if_equal_symbol_in_url_session_less
def session_less_patch(
self, url, data=None, json=None, expected_status=None, msg=None, **kwargs
):
"""
Sends a PATCH request.
The endpoint used to send the request is the ``url`` parameter, while its body
can be passed using ``data`` or ``json`` parameters.
``data`` can be a dictionary, list of tuples, bytes, or file-like object.
If you want to pass a json body pass a dictionary as ``json`` parameter.
By default this keyword fails if a status code with error values is returned in the response,
this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
read more about it in `Status Should Be` keyword documentation.
In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
``anything``.
Other optional requests arguments can be passed using ``**kwargs``
see the `GET` keyword for the complete list.
"""
response = self._common_request(
"PATCH", None, url, data=data, json=json, **kwargs
)
self._check_status(expected_status, response, msg)
return response
@keyword("DELETE")
@warn_if_equal_symbol_in_url_session_less
def session_less_delete(self, url, expected_status=None, msg=None, **kwargs):
"""
Sends a DELETE request.
The endpoint used to send the request is the ``url`` parameter.
By default this keyword fails if a status code with error values is returned in the response,
this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
read more about it in `Status Should Be` keyword documentation.
In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
``anything``.
Other optional requests arguments can be passed using ``**kwargs``
see the `GET` keyword for the complete list.
"""
response = self._common_request("DELETE", None, url, **kwargs)
self._check_status(expected_status, response, msg)
return response
@keyword("OPTIONS")
@warn_if_equal_symbol_in_url_session_less
def session_less_options(self, url, expected_status=None, msg=None, **kwargs):
"""
Sends an OPTIONS request.
The endpoint used to retrieve the resource is the ``url``.
By default this keyword fails if a status code with error values is returned in the response,
this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
read more about it in `Status Should Be` keyword documentation.
In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
``anything``.
Other optional requests arguments can be passed using ``**kwargs``
see the `GET` keyword for the complete list.
"""
response = self._common_request("OPTIONS", None, url, **kwargs)
self._check_status(expected_status, response, msg)
return response
@keyword("CONNECT")
@warn_if_equal_symbol_in_url_session_less
def session_less_connect(self, url, expected_status=None, msg=None, **kwargs):
"""
Sends a CONNECT request.
The endpoint used to retrieve the resource is the ``url``.
By default this keyword fails if a status code with error values is returned in the response,
this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
read more about it in `Status Should Be` keyword documentation.
In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
``anything``.
Other optional requests arguments can be passed using ``**kwargs``
see the `GET` keyword for the complete list.
"""
response = self._common_request("CONNECT", None, url, **kwargs)
self._check_status(expected_status, response, msg)
return response
@keyword("TRACE")
@warn_if_equal_symbol_in_url_session_less
def session_less_trace(self, url, expected_status=None, msg=None, **kwargs):
"""
Sends a TRACE request.
The endpoint used to retrieve the resource is the ``url``.
By default this keyword fails if a status code with error values is returned in the response,
this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
read more about it in `Status Should Be` keyword documentation.
In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
``anything``.
Other optional requests arguments can be passed using ``**kwargs``
see the `GET` keyword for the complete list.
"""
response = self._common_request("TRACE", None, url, **kwargs)
self._check_status(expected_status, response, msg)
return response