test_http.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. --
  2. -- tests/base/test_http.lua
  3. -- Tests the http API
  4. -- Copyright (c) 2016, 2020 Jason Perkins and the Premake project
  5. --
  6. if http.get ~= nil and _OPTIONS["test-all"] then
  7. local p = premake
  8. local suite = test.declare("premake_http")
  9. function suite.http_get()
  10. local result, err = http.get("http://httpbin.org/user-agent")
  11. if result then
  12. p.out(result)
  13. test.capture(
  14. '{"user-agent": "Premake/' .. _PREMAKE_VERSION .. '"}'
  15. )
  16. else
  17. test.fail(err);
  18. end
  19. end
  20. function suite.https_get()
  21. -- sslverifypeer = 0, so we can test from within companies like here at Blizzard where all HTTPS traffic goes through
  22. -- some strange black box that re-signs all traffic with a custom ssl certificate.
  23. local result, err = http.get("https://httpbin.org/user-agent", { sslverifypeer = 0 })
  24. if result then
  25. p.out(result)
  26. test.capture(
  27. '{"user-agent": "Premake/' .. _PREMAKE_VERSION .. '"}'
  28. )
  29. else
  30. test.fail(err);
  31. end
  32. end
  33. function suite.https_get_verify_peer()
  34. local result, err = http.get("https://httpbin.org/user-agent")
  35. if result then
  36. p.out(result)
  37. test.capture(
  38. '{"user-agent": "Premake/' .. _PREMAKE_VERSION .. '"}'
  39. )
  40. else
  41. test.fail(err);
  42. end
  43. end
  44. function suite.http_responsecode()
  45. local result, err, responseCode = http.get("http://httpbin.org/status/418")
  46. test.isequal(responseCode, 418)
  47. end
  48. -- Disable as httpbin.org returns 404 on this endpoint
  49. -- See: https://github.com/postmanlabs/httpbin/issues/617
  50. --[[
  51. function suite.http_redirect()
  52. local result, err, responseCode = http.get("http://httpbin.org/redirect/3")
  53. if result then
  54. test.isequal(responseCode, 200)
  55. else
  56. test.fail(err);
  57. end
  58. end
  59. ]]
  60. function suite.http_headers()
  61. local result, err, responseCode = http.get("http://httpbin.org/headers", {
  62. headers = { 'X-Premake: premake' }
  63. })
  64. if result then
  65. if (not result:find('X-Premake')) then
  66. test.fail("response doens't contain header")
  67. test.print(result)
  68. end
  69. else
  70. test.fail(err);
  71. end
  72. end
  73. end