annotate Database/Lua/HttpClient.lua @ 494:cffef67c9a94

added retries to httpbin
author Alain Mazy <am@osimis.io>
date Wed, 10 Aug 2022 15:15:20 +0200
parents e0b502b31a8a
children 75d8a5261f82
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
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
17 response = ParseJson(HttpPost('http://httpbin.org/post', DumpJson(payload), httpHeaders))
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
18 end
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
19 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
20 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
21 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
22
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
23 -- Issue HttpPost without body
494
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
24 retry = 10
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
25 response = nil
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
26 while retry > 0 and response == nil do
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
27 response = ParseJson(HttpPost('http://httpbin.org/post', nil, httpHeaders))
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
28 end
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
29 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
30 testSucceeded = testSucceeded and (response['data'] == '')
7530eb50c3c4 renamed HttpPost.lua into HttpClient.lua + fix tests
amazy
parents: 102
diff changeset
31 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
32
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
33 -- Issue HttpPut with body
494
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
34 retry = 10
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
35 response = nil
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
36 while retry > 0 and response == nil do
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
37 response = ParseJson(HttpPut('http://httpbin.org/put', DumpJson(payload), httpHeaders))
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
38 end
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
39 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
40 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
41 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
42
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
43 -- Issue HttpPut without body
494
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
44 retry = 10
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
45 response = nil
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
46 while retry > 0 and response == nil do
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
47 response = ParseJson(HttpPut('http://httpbin.org/put', nil, httpHeaders))
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
48 end
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
49 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
50 testSucceeded = testSucceeded and (response['data'] == '')
7530eb50c3c4 renamed HttpPost.lua into HttpClient.lua + fix tests
amazy
parents: 102
diff changeset
51 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
52
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
53 -- 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
54 HttpDelete('http://httpbin.org/delete', httpHeaders)
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
55
263
a2719263fd04 test_study_series_find_inconsistency
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 104
diff changeset
56 -- 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
57 -- in curl at this point
a2719263fd04 test_study_series_find_inconsistency
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 104
diff changeset
58
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
59 -- Issue HttpGet
494
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
60 retry = 10
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
61 response = nil
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
62 while retry > 0 and response == nil do
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
63 response = ParseJson(HttpGet('http://httpbin.org/get', httpHeaders))
cffef67c9a94 added retries to httpbin
Alain Mazy <am@osimis.io>
parents: 481
diff changeset
64 end
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
65 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
66 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
67
480
5ac463ebf463 added a test for lua SetHttpTimeout
Alain Mazy <am@osimis.io>
parents: 263
diff changeset
68
481
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
69 system = ParseJson(RestApiGet('/system'))
480
5ac463ebf463 added a test for lua SetHttpTimeout
Alain Mazy <am@osimis.io>
parents: 263
diff changeset
70
481
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
71 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
72 -- Test SetHttpTimeout
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
73 SetHttpTimeout(10)
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
74 response = HttpGet('https://httpstat.us/200?sleep=1000')
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
75 testSucceeded = testSucceeded and (response == '200 OK')
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
76 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
77
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
78 SetHttpTimeout(1)
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
79 response = HttpGet('https://httpstat.us/200?sleep=2000')
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
80 testSucceeded = testSucceeded and (response == nil)
e0b502b31a8a fix test for older version
Alain Mazy <am@osimis.io>
parents: 480
diff changeset
81 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
82 end
480
5ac463ebf463 added a test for lua SetHttpTimeout
Alain Mazy <am@osimis.io>
parents: 263
diff changeset
83
102
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
84 if testSucceeded then
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
85 print('OK')
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
86 else
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
87 print('FAILED')
9671578fd4d3 added tests for Lua HttpPost/Put/Get/Delete methods
amazy
parents:
diff changeset
88 end