annotate Database/Lua/HttpClient.lua @ 481:e0b502b31a8a

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