annotate Database/Lua/HttpClient.lua @ 608:ba06331ac8dd

new test for issue #219
author Alain Mazy <am@osimis.io>
date Wed, 24 Jan 2024 13:23:29 +0100
parents f78f7ee6b660
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
104
7530eb50c3c4 renamed HttpPost.lua into HttpClient.lua + fix tests
amazy
parents: 102
diff changeset
1 -- for these tests, we issue HTTP requests to httpbin.org that performs smart echo (it returns all data/headers it has received + some extra data)
494
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
2 -- since these tests to httpbin.org fails a lot, we have added 10 retries
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
3 testSucceeded = true
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
4
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
5 local payload = {}
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
6 payload['stringMember'] = 'toto'
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
7 payload['intMember'] = 2
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
8
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
9 local httpHeaders = {}
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
10 httpHeaders['Content-Type'] = 'application/json'
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
11 httpHeaders['Toto'] = 'Tutu'
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
12
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
13 -- Issue HttpPost with body
494
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
14 retry = 10
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
15 response = nil
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
16 while retry > 0 and response == nil do
495
75d8a5261f82 more verbose httpclient test
Alain Mazy <am@osimis.io>
parents: 494
diff changeset
17 print("HttpClient test: POST with body to httpbin.org")
494
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
18 response = ParseJson(HttpPost('http://httpbin.org/post', DumpJson(payload), httpHeaders))
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
19 end
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
20 testSucceeded = testSucceeded and (response['headers']['Content-Type'] == 'application/json' and response['headers']['Toto'] == 'Tutu')
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
21 testSucceeded = testSucceeded and (response['json']['intMember'] == 2 and response['json']['stringMember'] == 'toto')
104
7530eb50c3c4 renamed HttpPost.lua into HttpClient.lua + fix tests
amazy
parents: 102
diff changeset
22 if not testSucceeded then print('Failed in HttpPost with body') PrintRecursive(response) end
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
23
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
24 -- Issue HttpPost without body
494
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
25 retry = 10
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
26 response = nil
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
27 while retry > 0 and response == nil do
495
75d8a5261f82 more verbose httpclient test
Alain Mazy <am@osimis.io>
parents: 494
diff changeset
28 print("HttpClient test: POST without body to httpbin.org")
494
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
29 response = ParseJson(HttpPost('http://httpbin.org/post', nil, httpHeaders))
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
30 end
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
31 testSucceeded = testSucceeded and (response['headers']['Content-Type'] == 'application/json' and response['headers']['Toto'] == 'Tutu')
104
7530eb50c3c4 renamed HttpPost.lua into HttpClient.lua + fix tests
amazy
parents: 102
diff changeset
32 testSucceeded = testSucceeded and (response['data'] == '')
7530eb50c3c4 renamed HttpPost.lua into HttpClient.lua + fix tests
amazy
parents: 102
diff changeset
33 if not testSucceeded then print('Failed in HttpPost without body') PrintRecursive(response) end
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
34
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
35 -- Issue HttpPut with body
494
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
36 retry = 10
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
37 response = nil
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
38 while retry > 0 and response == nil do
495
75d8a5261f82 more verbose httpclient test
Alain Mazy <am@osimis.io>
parents: 494
diff changeset
39 print("HttpClient test: PUT with body to httpbin.org")
494
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
40 response = ParseJson(HttpPut('http://httpbin.org/put', DumpJson(payload), httpHeaders))
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
41 end
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
42 testSucceeded = testSucceeded and (response['headers']['Content-Type'] == 'application/json' and response['headers']['Toto'] == 'Tutu')
104
7530eb50c3c4 renamed HttpPost.lua into HttpClient.lua + fix tests
amazy
parents: 102
diff changeset
43 testSucceeded = testSucceeded and (response['json']['intMember'] == 2 and response['json']['stringMember'] == 'toto')
7530eb50c3c4 renamed HttpPost.lua into HttpClient.lua + fix tests
amazy
parents: 102
diff changeset
44 if not testSucceeded then print('Failed in HttpPut with body') PrintRecursive(response) end
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
45
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
46 -- Issue HttpPut without body
494
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
47 retry = 10
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
48 response = nil
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
49 while retry > 0 and response == nil do
495
75d8a5261f82 more verbose httpclient test
Alain Mazy <am@osimis.io>
parents: 494
diff changeset
50 print("HttpClient test: PUT without body to httpbin.org")
494
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
51 response = ParseJson(HttpPut('http://httpbin.org/put', nil, httpHeaders))
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
52 end
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
53 testSucceeded = testSucceeded and (response['headers']['Content-Type'] == 'application/json' and response['headers']['Toto'] == 'Tutu')
104
7530eb50c3c4 renamed HttpPost.lua into HttpClient.lua + fix tests
amazy
parents: 102
diff changeset
54 testSucceeded = testSucceeded and (response['data'] == '')
7530eb50c3c4 renamed HttpPost.lua into HttpClient.lua + fix tests
amazy
parents: 102
diff changeset
55 if not testSucceeded then print('Failed in HttpPut without body') PrintRecursive(response) end
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
56
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
57 -- Issue HttpDelete (juste make sure it is issued, we can't check the response)
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
58 HttpDelete('http://httpbin.org/delete', httpHeaders)
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
59
263
a2719263fd04 test_study_series_find_inconsistency
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 104
diff changeset
60 -- TODO Very strange: Since Orthanc 1.6.0, a timeout frequently occurs
a2719263fd04 test_study_series_find_inconsistency
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 104
diff changeset
61 -- in curl at this point
a2719263fd04 test_study_series_find_inconsistency
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 104
diff changeset
62
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
63 -- Issue HttpGet
494
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
64 retry = 10
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
65 response = nil
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
66 while retry > 0 and response == nil do
495
75d8a5261f82 more verbose httpclient test
Alain Mazy <am@osimis.io>
parents: 494
diff changeset
67 print("HttpClient test: GET to httpbin.org")
494
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
68 response = ParseJson(HttpGet('http://httpbin.org/get', httpHeaders))
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
69 end
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
70 testSucceeded = testSucceeded and (response['headers']['Content-Type'] == 'application/json' and response['headers']['Toto'] == 'Tutu')
104
7530eb50c3c4 renamed HttpPost.lua into HttpClient.lua + fix tests
amazy
parents: 102
diff changeset
71 if not testSucceeded then print('Failed in HttpGet') PrintRecursive(response) end
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
72
480
5ac463ebf463 added a test for lua SetHttpTimeout
Alain Mazy <am@osimis.io>
parents: 263
diff changeset
73
481
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
74 system = ParseJson(RestApiGet('/system'))
480
5ac463ebf463 added a test for lua SetHttpTimeout
Alain Mazy <am@osimis.io>
parents: 263
diff changeset
75
481
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
76 if system['Version'] == 'mainline' or system['Version'] == '1.11.1' or system['ApiVersion'] >= 18 then -- introduced in 1.11.1 which is ApiVersion 17 (too lazy to reimplement IsAboveOrthancVersion in lua :-) )
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
77 -- Test SetHttpTimeout
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
78 SetHttpTimeout(10)
495
75d8a5261f82 more verbose httpclient test
Alain Mazy <am@osimis.io>
parents: 494
diff changeset
79 print("HttpClient test: GET with timeout (10) to httpstat.us")
481
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
80 response = HttpGet('https://httpstat.us/200?sleep=1000')
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
81 testSucceeded = testSucceeded and (response == '200 OK')
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
82 if not testSucceeded then print('Failed in SetHttpTimeout1') PrintRecursive(response) end
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
83
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
84 SetHttpTimeout(1)
495
75d8a5261f82 more verbose httpclient test
Alain Mazy <am@osimis.io>
parents: 494
diff changeset
85 print("HttpClient test: GET with timeout (1) to httpstat.us")
481
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
86 response = HttpGet('https://httpstat.us/200?sleep=2000')
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
87 testSucceeded = testSucceeded and (response == nil)
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
88 if not testSucceeded then print('Failed in SetHttpTimeout2') PrintRecursive(response) end
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
89 end
480
5ac463ebf463 added a test for lua SetHttpTimeout
Alain Mazy <am@osimis.io>
parents: 263
diff changeset
90
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
91 if testSucceeded then
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
92 print('OK')
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
93 else
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
94 print('FAILED')
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
95 end