cgi.es.tst 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. cgi.tst - CGI tests
  3. */
  4. const HTTP = tget('TM_HTTP') || "127.0.0.1:8080"
  5. let http: Http = new Http
  6. if (thas('ME_GOAHEAD_CGI')) {
  7. /* Suport routines */
  8. function contains(pat): Void {
  9. ttrue(http.response.contains(pat))
  10. }
  11. function keyword(pat: String): String? {
  12. pat.replace(/\//, "\\/").replace(/\[/, "\\[")
  13. let reg = RegExp(".*" + pat + "=([^<]*).*", "s")
  14. return http.response.replace(reg, "$1")
  15. }
  16. function match(key: String, value: String): Void {
  17. if (keyword(key) != value) {
  18. print(http.response)
  19. print("\nKey \"" + key + "\"")
  20. print("Expected: " + value)
  21. // print("Actual : " + keyword(value))
  22. }
  23. ttrue(keyword(key) == value)
  24. }
  25. /* Tests */
  26. function forms() {
  27. // Test various forms to invoke cgi programs
  28. http.get(HTTP + "/cgi-bin/cgitest")
  29. ttrue(http.status == 200)
  30. contains("cgitest: Output")
  31. http.close()
  32. if (Config.OS == "windows") {
  33. http.get(HTTP + "/cgi-bin/cgitest.exe")
  34. ttrue(http.status == 200)
  35. contains("cgitest: Output")
  36. http.close()
  37. }
  38. }
  39. function extraPath() {
  40. http.get(HTTP + "/cgi-bin/cgitest")
  41. ttrue(http.status == 200)
  42. match("PATH_INFO", "")
  43. match("PATH_TRANSLATED", "")
  44. http.close()
  45. http.get(HTTP + "/cgi-bin/cgitest/extra/path")
  46. match("PATH_INFO", "/extra/path")
  47. let scriptFilename = keyword("SCRIPT_FILENAME")
  48. let path = Path(scriptFilename).dirname.join("extra/path")
  49. let translated = Path(keyword("PATH_TRANSLATED"))
  50. ttrue(path == translated)
  51. http.close()
  52. }
  53. function query() {
  54. http.get(HTTP + "/cgi-bin/cgitest/extra/path?a=b&c=d&e=f")
  55. match("SCRIPT_NAME", "/cgi-bin/cgitest")
  56. match("PATH_INFO", "/extra/path")
  57. contains("QVAR a=b")
  58. contains("QVAR c=d")
  59. http.close()
  60. http.get(HTTP + "/cgi-bin/cgitest?a+b+c")
  61. match("QUERY_STRING", "a+b+c")
  62. contains("QVAR a b c")
  63. http.close()
  64. //
  65. // Query string vars should not be turned into variables for GETs
  66. // Extra path only supported for cgi programs with extensions.
  67. //
  68. http.get(HTTP + "/cgi-bin/cgitest/extra/path?var1=a+a&var2=b%20b&var3=c")
  69. match("SCRIPT_NAME", "/cgi-bin/cgitest")
  70. match("QUERY_STRING", "var1=a+a&var2=b%20b&var3=c")
  71. match("QVAR var1", "a a")
  72. match("QVAR var2", "b b")
  73. match("QVAR var3", "c")
  74. http.close()
  75. //
  76. // Post data should be turned into variables
  77. //
  78. http.form(HTTP + "/cgi-bin/cgitest/extra/path?var1=a+a&var2=b%20b&var3=c",
  79. { name: "Peter", address: "777 Mulberry Lane" })
  80. match("QUERY_STRING", "var1=a+a&var2=b%20b&var3=c")
  81. match("QVAR var1", "a a")
  82. match("QVAR var2", "b b")
  83. match("QVAR var3", "c")
  84. match("PVAR name", "Peter")
  85. match("PVAR address", "777 Mulberry Lane")
  86. http.close()
  87. }
  88. function encoding() {
  89. http.get(HTTP + "/cgi-bin/cgitest/extra%20long/a/../path/a/..?var%201=value%201")
  90. match("QUERY_STRING", "var%201=value%201")
  91. match("SCRIPT_NAME", "/cgi-bin/cgitest")
  92. match("QVAR var 1", "value 1")
  93. match("PATH_INFO", "/extra long/path")
  94. let scriptFilename = keyword("SCRIPT_FILENAME")
  95. let path = Path(scriptFilename).dirname.join("extra long/path")
  96. let translated = Path(keyword("PATH_TRANSLATED"))
  97. ttrue(path == translated)
  98. http.close()
  99. }
  100. function status() {
  101. let http = new Http
  102. http.setHeader("SWITCHES", "-s%20711")
  103. http.get(HTTP + "/cgi-bin/cgitest?mob=111")
  104. ttrue(http.status == 711)
  105. http.close()
  106. }
  107. function location() {
  108. let http = new Http
  109. http.setHeader("SWITCHES", "-l%20/index.html")
  110. http.followRedirects = false
  111. http.get(HTTP + "/cgi-bin/cgitest")
  112. ttrue(http.status == 302)
  113. http.close()
  114. }
  115. function quoting() {
  116. http.get(HTTP + "/cgi-bin/cgitest?a+b+c")
  117. match("QUERY_STRING", "a+b+c")
  118. match("QVAR a b c", "")
  119. http.close()
  120. http.get(HTTP + "/cgi-bin/cgitest?a=1&b=2&c=3")
  121. match("QUERY_STRING", "a=1&b=2&c=3")
  122. match("QVAR a", "1")
  123. match("QVAR b", "2")
  124. match("QVAR c", "3")
  125. http.close()
  126. http.get(HTTP + "/cgi-bin/cgitest?a%20a=1%201+b%20b=2%202")
  127. match("QUERY_STRING", "a%20a=1%201+b%20b=2%202")
  128. match("QVAR a a", "1 1 b b=2 2")
  129. http.close()
  130. http.get(HTTP + "/cgi-bin/cgitest?a%20a=1%201&b%20b=2%202")
  131. match("QUERY_STRING", "a%20a=1%201&b%20b=2%202")
  132. match("QVAR a a", "1 1")
  133. match("QVAR b b", "2 2")
  134. http.close()
  135. }
  136. function post() {
  137. // Simple post
  138. http.post(HTTP + '/cgi-bin/cgitest', 'Some data')
  139. ttrue(http.status == 200)
  140. match('CONTENT_LENGTH', '9')
  141. http.close()
  142. // Simple form
  143. http.form(HTTP + '/cgi-bin/cgitest', {name: 'John', address: '700 Park Ave'})
  144. ttrue(http.status == 200)
  145. match('PVAR name', 'John')
  146. match('PVAR address', '700 Park Ave')
  147. http.close()
  148. }
  149. forms()
  150. extraPath()
  151. query()
  152. encoding()
  153. status()
  154. location()
  155. quoting()
  156. post()
  157. } else {
  158. tskip("CGI not enabled")
  159. }