view 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
line wrap: on
line source

-- 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)
-- since these tests to httpbin.org fails a lot, we have added 10 retries
testSucceeded = true

local payload = {}
payload['stringMember'] = 'toto'
payload['intMember'] = 2

local httpHeaders = {}
httpHeaders['Content-Type'] = 'application/json'
httpHeaders['Toto'] = 'Tutu'

-- Issue HttpPost with body
retry = 10
response = nil
while retry > 0 and response == nil do
	response = ParseJson(HttpPost('http://httpbin.org/post', DumpJson(payload), httpHeaders))
end
testSucceeded = testSucceeded and (response['headers']['Content-Type'] == 'application/json' and response['headers']['Toto'] == 'Tutu')
testSucceeded = testSucceeded and (response['json']['intMember'] == 2 and response['json']['stringMember'] == 'toto')
if not testSucceeded then print('Failed in HttpPost with body') PrintRecursive(response) end

-- Issue HttpPost without body
retry = 10
response = nil
while retry > 0 and response == nil do
	response = ParseJson(HttpPost('http://httpbin.org/post', nil, httpHeaders))
end
testSucceeded = testSucceeded and (response['headers']['Content-Type'] == 'application/json' and response['headers']['Toto'] == 'Tutu')
testSucceeded = testSucceeded and (response['data'] == '')
if not testSucceeded then print('Failed in HttpPost without body') PrintRecursive(response) end

-- Issue HttpPut with body
retry = 10
response = nil
while retry > 0 and response == nil do
	response = ParseJson(HttpPut('http://httpbin.org/put', DumpJson(payload), httpHeaders))
end
testSucceeded = testSucceeded and (response['headers']['Content-Type'] == 'application/json' and response['headers']['Toto'] == 'Tutu')
testSucceeded = testSucceeded and (response['json']['intMember'] == 2 and response['json']['stringMember'] == 'toto')
if not testSucceeded then print('Failed in HttpPut with body') PrintRecursive(response) end

-- Issue HttpPut without body
retry = 10
response = nil
while retry > 0 and response == nil do
	response = ParseJson(HttpPut('http://httpbin.org/put', nil, httpHeaders))
end
testSucceeded = testSucceeded and (response['headers']['Content-Type'] == 'application/json' and response['headers']['Toto'] == 'Tutu')
testSucceeded = testSucceeded and (response['data'] == '')
if not testSucceeded then print('Failed in HttpPut without body') PrintRecursive(response) end

-- Issue HttpDelete (juste make sure it is issued, we can't check the response)
HttpDelete('http://httpbin.org/delete', httpHeaders)

-- TODO Very strange: Since Orthanc 1.6.0, a timeout frequently occurs
-- in curl at this point

-- Issue HttpGet
retry = 10
response = nil
while retry > 0 and response == nil do
	response = ParseJson(HttpGet('http://httpbin.org/get', httpHeaders))
end
testSucceeded = testSucceeded and (response['headers']['Content-Type'] == 'application/json' and response['headers']['Toto'] == 'Tutu')
if not testSucceeded then print('Failed in HttpGet') PrintRecursive(response) end


system = ParseJson(RestApiGet('/system'))

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 :-) )
	-- Test SetHttpTimeout
	SetHttpTimeout(10)
	response = HttpGet('https://httpstat.us/200?sleep=1000')
	testSucceeded = testSucceeded and (response == '200 OK')
	if not testSucceeded then print('Failed in SetHttpTimeout1') PrintRecursive(response) end

	SetHttpTimeout(1)
	response = HttpGet('https://httpstat.us/200?sleep=2000')
	testSucceeded = testSucceeded and (response == nil)
	if not testSucceeded then print('Failed in SetHttpTimeout2') PrintRecursive(response) end
end

if testSucceeded then
	print('OK')
else
	print('FAILED')
end