blob: 335148680f25887b109e636ba2d290d37bb1eadb (
plain)
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
|
---
# get with mismatch https
# get with mismatch https and ignore validation
- name: get request without return_content
win_uri:
url: https://{{httpbin_host}}/get
return_content: no
register: get_request_without_content
- name: assert get request without return_content
assert:
that:
- not get_request_without_content.changed
- get_request_without_content.content is not defined
- get_request_without_content.json is not defined
- get_request_without_content.status_code == 200
- name: get request with xml content
win_uri:
url: https://{{httpbin_host}}/xml
return_content: yes
register: get_request_with_xml_content
- name: assert get request with xml content
assert:
that:
- not get_request_with_xml_content.changed
- get_request_with_xml_content.content is defined
- get_request_with_xml_content.json is not defined
- get_request_with_xml_content.status_code == 200
- name: get request with binary content
win_uri:
url: https://{{httpbin_host}}/image/png
return_content: yes
register: get_request_with_binary_content
- name: assert get request with binary content
assert:
that:
- not get_request_with_binary_content.changed
- get_request_with_binary_content.content is defined
- get_request_with_binary_content.json is not defined
- get_request_with_xml_content.status_code == 200
- name: get request with return_content and dest (check mode)
win_uri:
url: https://{{httpbin_host}}/get
return_content: yes
dest: '{{ remote_tmp_dir }}\get.json'
register: get_request_with_dest_check
check_mode: yes
- name: get stat of downloaded file (check mode)
win_stat:
path: '{{ remote_tmp_dir }}\get.json'
register: get_request_with_dest_actual_check
- name: assert get request with return_content and dest (check mode)
assert:
that:
- get_request_with_dest_check.changed
- get_request_with_dest_check.content is defined
- get_request_with_dest_check.json is defined
- get_request_with_dest_actual_check.stat.exists == False
- name: get request with return_content and dest
win_uri:
url: https://{{httpbin_host}}/get
return_content: yes
dest: '{{ remote_tmp_dir }}\get.json'
register: get_request_with_dest
- name: get stat of downloaded file
win_stat:
path: '{{ remote_tmp_dir }}\get.json'
checksum_algorithm: sha1
get_checksum: yes
register: get_request_with_dest_actual
- name: assert get request with return_content and dest
assert:
that:
- get_request_with_dest.changed
- get_request_with_dest.content is defined
- get_request_with_dest.json is defined
- get_request_with_dest_actual.stat.exists == True
- get_request_with_dest_actual.stat.checksum == get_request_with_dest.content|hash('sha1')
- name: get request with return_content and dest (idempotent)
win_uri:
url: https://{{httpbin_host}}/get
return_content: yes
dest: '{{ remote_tmp_dir }}\get.json'
register: get_request_with_dest_again
- name: assert get request with return_content and dest (idempotent)
assert:
that:
- not get_request_with_dest_again.changed
- name: test request with creates option should skip
win_uri:
url: https://{{httpbin_host}}/get
creates: '{{ remote_tmp_dir }}\get.json'
register: request_with_creates_skipped
- name: assert test request with creates option should skip
assert:
that:
- not request_with_creates_skipped.changed
- request_with_creates_skipped.skipped
- name: test request with creates option should not skip
win_uri:
url: https://{{httpbin_host}}/get
creates: '{{ remote_tmp_dir }}\fake.json'
register: request_with_creates_not_skipped
- name: assert test request with creates option should not skip
assert:
that:
- not request_with_creates_not_skipped.changed
- request_with_creates_not_skipped.skipped is not defined
- name: post request with return_content, dest and different content
win_uri:
url: https://{{httpbin_host}}/post
method: POST
content_type: application/json
body: '{"foo": "bar"}'
return_content: yes
dest: '{{ remote_tmp_dir }}\get.json'
register: post_request_with_different_content
- name: get stat of downloaded file
win_stat:
path: '{{ remote_tmp_dir }}\get.json'
checksum_algorithm: sha1
get_checksum: yes
register: post_request_with_different_content_actual
- name: assert post request with return_content, dest and different content
assert:
that:
- post_request_with_different_content.changed
- post_request_with_different_content_actual.stat.exists == True
- post_request_with_different_content_actual.stat.checksum == post_request_with_different_content.content|hash('sha1')
- name: test safe redirection of get
win_uri:
url: https://{{httpbin_host}}/redirect/2
register: redirect_safe
- name: assert safe redirection of get
assert:
that:
- redirect_safe.status_code == 200
- redirect_safe.response_uri == 'https://' + httpbin_host + '/get'
- name: test safe redirection of head
win_uri:
url: https://{{httpbin_host}}/redirect/2
method: HEAD
register: redirect_safe_head
- name: assert safe redirection of head
assert:
that:
- redirect_safe_head.status_code == 200
- redirect_safe_head.response_uri == 'https://' + httpbin_host + '/get'
- name: test safe redirection of put
win_uri:
url: https://{{httpbin_host}}/redirect-to?url=https://{{httpbin_host}}/put
body: data
status_code: 302
method: PUT
register: redirect_safe_put
- name: assert safe redirection of put
assert:
that:
- redirect_safe_put.status_code == 302
- redirect_safe_put.response_uri == 'https://' + httpbin_host + '/redirect-to?url=https://' + httpbin_host + '/put'
- name: test none redirection of get
win_uri:
url: https://{{httpbin_host}}/redirect/2
status_code: 302
follow_redirects: none
register: redirect_none
- name: assert none redirection of get
assert:
that:
- redirect_none.status_code == 302
- redirect_none.response_uri == 'https://' + httpbin_host + '/redirect/2'
- name: test none redirection of put
win_uri:
url: https://{{httpbin_host}}/redirect-to?url=https://{{httpbin_host}}/put
body: data
status_code: 302
method: PUT
follow_redirects: none
register: redirect_none_put
- name: assert none redirection of put
assert:
that:
- redirect_none_put.status_code == 302
- redirect_none_put.response_uri == 'https://' + httpbin_host + '/redirect-to?url=https://' + httpbin_host + '/put'
- name: test all redirection of get
win_uri:
url: https://{{httpbin_host}}/redirect/2
follow_redirects: all
register: redirect_all
- name: assert all redirection of get
assert:
that:
- redirect_all.status_code == 200
- redirect_all.response_uri == 'https://' + httpbin_host + '/get'
- name: test all redirection of put
win_uri:
url: https://{{httpbin_host}}/redirect-to?url=https://{{httpbin_host}}/put
body: data
method: PUT
follow_redirects: all
register: redirect_all_put
- name: assert all redirection of put
assert:
that:
- redirect_all_put.status_code == 200
- redirect_all_put.response_uri == 'https://' + httpbin_host + '/put'
- name: test exceeded maximum redirection
win_uri:
url: https://{{httpbin_host}}/redirect/5
maximum_redirection: 4
status_code: 302
register: maximum_redirection
- name: assert exceeded maximum redirection
assert:
that:
- maximum_redirection.status_code == 302
- maximum_redirection.response_uri == 'https://' + httpbin_host + '/relative-redirect/1'
- name: test basic auth
win_uri:
url: https://{{httpbin_host}}/basic-auth/user/passwd
user: user
password: passwd
register: basic_auth
- name: assert test basic auth
assert:
that:
- not basic_auth.changed
- basic_auth.status_code == 200
- name: test basic auth with force auth
win_uri:
url: https://{{httpbin_host}}/hidden-basic-auth/user/passwd
user: user
password: passwd
force_basic_auth: yes
register: basic_auth_forced
- name: assert test basic auth with forced auth
assert:
that:
- not basic_auth_forced.changed
- basic_auth_forced.status_code == 200
- name: test PUT
win_uri:
url: https://{{httpbin_host}}/put
method: PUT
body: foo=bar
return_content: yes
register: put_request
- name: assert test PUT
assert:
that:
- not put_request.changed
- put_request.status_code == 200
- put_request.json.data == 'foo=bar'
- name: test OPTIONS
win_uri:
url: https://{{httpbin_host}}/
method: OPTIONS
register: option_request
- name: assert test OPTIONS
assert:
that:
- not option_request.changed
- option_request.status_code == 200
- 'option_request.allow.split(", ")|sort == ["GET", "HEAD", "OPTIONS"]'
# SNI Tests
- name: validate status_codes are correct
win_uri:
url: https://{{httpbin_host}}/status/202
status_code:
- 202
- 418
method: POST
body: foo
register: status_code_check
- name: assert validate status_codes are correct
assert:
that:
- not status_code_check.changed
- status_code_check.status_code == 202
- name: send JSON body with dict type
win_uri:
url: https://{{httpbin_host}}/post
method: POST
body:
foo: bar
list:
- 1
- 2
dict:
foo: bar
headers:
'Content-Type': 'text/json'
return_content: yes
register: json_as_dict
- name: set fact of expected json dict
set_fact:
json_as_dict_value:
foo: bar
list:
- 1
- 2
dict:
foo: bar
- name: assert send JSON body with dict type
assert:
that:
- not json_as_dict.changed
- json_as_dict.json.json == json_as_dict_value
- json_as_dict.status_code == 200
- name: send JSON body with 1 item in list
win_uri:
url: https://{{httpbin_host}}/post
method: POST
body:
- foo: bar
headers:
'Content-Type': 'text/json'
return_content: yes
register: json_as_oneitemlist
- name: set fact of expected json 1 item list
set_fact:
json_as_oneitemlist_value:
- foo: bar
- name: assert send JSON body with 1 item in list
assert:
that:
- not json_as_oneitemlist.changed
- json_as_oneitemlist.json.json == json_as_oneitemlist_value
- json_as_oneitemlist.status_code == 200
- name: get request with custom headers
win_uri:
url: https://{{httpbin_host}}/get
headers:
Test-Header: hello
Another-Header: world
return_content: yes
register: get_custom_header
- name: assert request with custom headers
assert:
that:
- not get_custom_header.changed
- get_custom_header.status_code == 200
- get_custom_header.json.headers['Test-Header'] == 'hello'
- get_custom_header.json.headers['Another-Header'] == 'world'
- name: Validate invalid method
win_uri:
url: https://{{ httpbin_host }}/anything
method: UNKNOWN
register: invalid_method
ignore_errors: yes
- name: Assert invalid method fails
assert:
that:
- invalid_method is failure
- invalid_method.status_code == 405
- invalid_method.status_description == 'METHOD NOT ALLOWED'
# client cert auth tests
- name: get request with timeout
win_uri:
url: https://{{httpbin_host}}/delay/10
timeout: 5
register: get_with_timeout_fail
failed_when: '"The operation has timed out" not in get_with_timeout_fail.msg'
- name: connect to fakepath that does not exist
win_uri:
url: https://{{httpbin_host}}/fakepath
status_code: 404
return_content: yes
register: invalid_path
# verifies the return values are still set on a non 200 response
- name: assert connect to fakepath that does not exist
assert:
that:
- not invalid_path.changed
- invalid_path.status_code == 404
- invalid_path.status_description == 'NOT FOUND'
- invalid_path.content is defined
- invalid_path.method == 'GET'
- invalid_path.connection is defined
- name: post request with custom headers
win_uri:
url: https://{{httpbin_host}}/post
method: POST
headers:
Test-Header: hello
Another-Header: world
content_type: application/json
body: '{"foo": "bar"}'
return_content: yes
register: post_request_with_custom_headers
- name: assert post with custom headers
assert:
that:
- not post_request_with_custom_headers.changed
- post_request_with_custom_headers.status_code == 200
- post_request_with_custom_headers.json.headers['Content-Type'] == "application/json"
- post_request_with_custom_headers.json.headers['Test-Header'] == 'hello'
- post_request_with_custom_headers.json.headers['Another-Header'] == 'world'
- name: validate status codes as list of strings
win_uri:
url: https://{{httpbin_host}}/status/202
status_code:
- '202'
- '418'
method: POST
body: foo
return_content: yes
register: request_status_code_string
- name: assert status codes as list of strings
assert:
that:
- not request_status_code_string.changed
- request_status_code_string.status_code == 202
- name: validate status codes as comma separated list
win_uri:
url: https://{{httpbin_host}}/status/202
status_code: 202, 418
method: POST
body: foo
return_content: yes
register: request_status_code_comma
- name: assert status codes as comma separated list
assert:
that:
- not request_status_code_comma.changed
- request_status_code_comma.status_code == 202
# https://github.com/ansible/ansible/issues/55294
- name: get json content that is an array
win_uri:
url: https://{{httpbin_host}}/base64/{{ '[{"abc":"def"}]' | b64encode }}
return_content: yes
register: content_array
- name: assert content of json array
assert:
that:
- not content_array is changed
- content_array.content == '[{"abc":"def"}]'
- content_array.json == [{"abc":"def"}]
- name: send request with explicit http_agent
win_uri:
url: https://{{httpbin_host}}/get
http_agent: test-agent
return_content: yes
register: http_agent_option
- name: assert send request with explicit http_agent
assert:
that:
- http_agent_option.json.headers['User-Agent'] == 'test-agent'
- name: send request with explicit User-Agent header
win_uri:
url: https://{{httpbin_host}}/get
headers:
User-Agent: test-agent
return_content: yes
register: http_agent_header
- name: assert send request with explicit User-Agent header
assert:
that:
- http_agent_header.json.headers['User-Agent'] == 'test-agent'
- name: send request with explicit http_agent and header (http_agent wins)
win_uri:
url: https://{{httpbin_host}}/get
http_agent: test-agent-option
headers:
User-Agent: test-agent-header
return_content: yes
register: http_agent_combo
- name: assert send request with explicit http_agent and header (http_agent wins)
assert:
that:
- http_agent_combo.json.headers['User-Agent'] == 'test-agent-option'
- http_agent_combo.warnings[0] == "The 'User-Agent' header and the 'http_agent' was set, using the 'http_agent' for web request"
|