utils.t 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. use Test::Nginx::Socket;
  2. our $HttpConfig = <<'_EOC_';
  3. lua_package_path "lib/?.lua;/usr/local/share/lua/5.1/?.lua;;";
  4. _EOC_
  5. repeat_each(3);
  6. plan tests => repeat_each() * (blocks() * 3);
  7. run_tests();
  8. __DATA__
  9. === TEST 1: int_to_char_length
  10. --- http_config eval: $::HttpConfig
  11. --- config
  12. location /t {
  13. content_by_lua_block {
  14. local utils = require "resty.t1k.utils"
  15. ngx.say("255 to char length: ", utils.int_to_char_length(255))
  16. ngx.print("16777216 to char length: ", utils.int_to_char_length(16777216))
  17. }
  18. }
  19. --- request
  20. GET /t
  21. --- response_body eval
  22. "255 to char length: \x{ff}\x{00}\x{00}\x{00}
  23. 16777216 to char length: \x{00}\x{00}\x{00}\x{01}"
  24. --- no_error_log
  25. [error]
  26. === TEST 2: int_to_char_length
  27. --- http_config eval: $::HttpConfig
  28. --- config
  29. location /t {
  30. content_by_lua_block {
  31. local utils = require "resty.t1k.utils"
  32. ngx.say("0xff 0x00 0x00 0x00 to int length: ", utils.char_to_int_length("\xff\x00\x00\x00"))
  33. ngx.say("0x00 0x00 0x00 0x01 to int length: ", utils.char_to_int_length("\x00\x00\x00\x01"))
  34. }
  35. }
  36. --- request
  37. GET /t
  38. --- response_body
  39. 0xff 0x00 0x00 0x00 to int length: 255
  40. 0x00 0x00 0x00 0x01 to int length: 16777216
  41. --- no_error_log
  42. [error]
  43. === TEST 3: is_mask_first
  44. --- http_config eval: $::HttpConfig
  45. --- config
  46. location /t {
  47. content_by_lua_block {
  48. local utils = require "resty.t1k.utils"
  49. ngx.say("0x30 is MASK FIRST: ", utils.is_mask_first(0x30))
  50. ngx.say("0x41 is MASK FIRST: ", utils.is_mask_first(0x41))
  51. ngx.say("0xc0 is MASK FIRST: ", utils.is_mask_first(0xc0))
  52. ngx.say("0xc1 is MASK FIRST: ", utils.is_mask_first(0xc1))
  53. }
  54. }
  55. --- request
  56. GET /t
  57. --- response_body
  58. 0x30 is MASK FIRST: false
  59. 0x41 is MASK FIRST: true
  60. 0xc0 is MASK FIRST: true
  61. 0xc1 is MASK FIRST: true
  62. --- no_error_log
  63. [error]
  64. === TEST 4: is_mask_last
  65. --- http_config eval: $::HttpConfig
  66. --- config
  67. location /t {
  68. content_by_lua_block {
  69. local utils = require "resty.t1k.utils"
  70. ngx.say("0x41 is MASK LAST: ", utils.is_mask_last(65))
  71. ngx.say("0x80 is MASK LAST: ", utils.is_mask_last(128))
  72. }
  73. }
  74. --- request
  75. GET /t
  76. --- response_body
  77. 0x41 is MASK LAST: false
  78. 0x80 is MASK LAST: true
  79. --- no_error_log
  80. [error]
  81. === TEST 5: packet_parser unfinished
  82. --- http_config eval: $::HttpConfig
  83. --- config
  84. location /t {
  85. content_by_lua_block {
  86. local utils = require "resty.t1k.utils"
  87. local finished, tag, length = utils.packet_parser("\x41\x59\x00\x00\x00")
  88. ngx.say("finished: ", finished)
  89. ngx.say("tag == TAG_HEAD: ", tag == 1)
  90. ngx.say("length: ", 89)
  91. }
  92. }
  93. --- request
  94. GET /t
  95. --- response_body
  96. finished: false
  97. tag == TAG_HEAD: true
  98. length: 89
  99. --- no_error_log
  100. [error]
  101. === TEST 6: packet_parser finished
  102. --- http_config eval: $::HttpConfig
  103. --- config
  104. location /t {
  105. content_by_lua_block {
  106. local utils = require "resty.t1k.utils"
  107. local finished, tag, length = utils.packet_parser("\xa0\x08\x00\x00\x00")
  108. ngx.say("finished: ", finished)
  109. ngx.say("tag == TAG_VERSION: ", tag == 32)
  110. ngx.say("length: ", 8)
  111. }
  112. }
  113. --- request
  114. GET /t
  115. --- response_body
  116. finished: true
  117. tag == TAG_VERSION: true
  118. length: 8
  119. --- no_error_log
  120. [error]
  121. === TEST 7: starts_with
  122. --- http_config eval: $::HttpConfig
  123. --- config
  124. location /t {
  125. content_by_lua_block {
  126. local utils = require "resty.t1k.utils"
  127. ngx.say("http://www.baidu.com starts with \"http\": ", utils.starts_with("http://www.baidu.com", "http"))
  128. ngx.say("http://www.baidu.com starts with \"https\": ", utils.starts_with("http://www.baidu.com", "https"))
  129. }
  130. }
  131. --- request
  132. GET /t
  133. --- response_body
  134. http://www.baidu.com starts with "http": true
  135. http://www.baidu.com starts with "https": false
  136. --- no_error_log
  137. [error]
  138. === TEST 8: to_var_idx
  139. --- http_config eval: $::HttpConfig
  140. --- config
  141. location /t {
  142. content_by_lua_block {
  143. local utils = require "resty.t1k.utils"
  144. local fmt = string.format
  145. ngx.say(fmt("http_x_real_ip is %s and %s", utils.to_var_idx("http_x_real_ip")))
  146. ngx.say(fmt("X_REAL_IP is %s and %s", utils.to_var_idx("X_REAL_IP")))
  147. ngx.say(fmt("X-Forwarded-For: 1 is %s and %d", utils.to_var_idx("X-Forwarded-For: 1")))
  148. ngx.say(fmt("X-Forwarded-For: -1 is %s and %d", utils.to_var_idx("X-Forwarded-For: -1")))
  149. ngx.say(fmt("X-FORWARDED-FOR:100 is %s and %d", utils.to_var_idx("X-FORWARDED-FOR:-100")))
  150. }
  151. }
  152. --- request
  153. GET /t
  154. --- response_body
  155. http_x_real_ip is http_x_real_ip and nil
  156. X_REAL_IP is http_x_real_ip and nil
  157. X-Forwarded-For: 1 is http_x_forwarded_for and 1
  158. X-Forwarded-For: -1 is http_x_forwarded_for and -1
  159. X-FORWARDED-FOR:100 is http_x_forwarded_for and -100
  160. --- no_error_log
  161. [error]
  162. === TEST 9: get_indexed_element
  163. --- http_config eval: $::HttpConfig
  164. --- config
  165. location /t {
  166. content_by_lua_block {
  167. local utils = require "resty.t1k.utils"
  168. ngx.say("X-Forwarded-For: 1 is ", utils.get_indexed_element(ngx.var.http_x_forwarded_for, 1))
  169. ngx.say("X-Forwarded-For: 2 is ", utils.get_indexed_element(ngx.var.http_x_forwarded_for, 2))
  170. ngx.say("X-Forwarded-For: 3 is ", utils.get_indexed_element(ngx.var.http_x_forwarded_for, 3))
  171. ngx.say("X-Forwarded-For: 4 is ", utils.get_indexed_element(ngx.var.http_x_forwarded_for, 4))
  172. ngx.say("X-Forwarded-For: 5 is ", utils.get_indexed_element(ngx.var.http_x_forwarded_for, 5))
  173. ngx.say("X-Forwarded-For: -1 is ", utils.get_indexed_element(ngx.var.http_x_forwarded_for, -1))
  174. ngx.say("X-Forwarded-For: -2 is ", utils.get_indexed_element(ngx.var.http_x_forwarded_for, -2))
  175. ngx.say("X-Forwarded-For: -3 is ", utils.get_indexed_element(ngx.var.http_x_forwarded_for, -3))
  176. ngx.say("X-Forwarded-For: -4 is ", utils.get_indexed_element(ngx.var.http_x_forwarded_for, -4))
  177. ngx.say("X-Forwarded-For: -5 is ", utils.get_indexed_element(ngx.var.http_x_forwarded_for, -5))
  178. ngx.say("X-Forwarded-For: 0 is ", utils.get_indexed_element(ngx.var.http_x_forwarded_for, 0))
  179. ngx.say("X-Non-Existent-Header is ", utils.get_indexed_element(ngx.var.http_non_existent_header))
  180. ngx.say("X-Real-IP is ", utils.get_indexed_element(ngx.var.http_x_real_ip))
  181. ngx.say("X-Real-IP: 1 is ", utils.get_indexed_element(ngx.var.http_x_real_ip, 1))
  182. ngx.say("X-Real-IP: 2 is ", utils.get_indexed_element(ngx.var.http_x_real_ip, 2))
  183. ngx.say("X-Real-IP: -1 is ", utils.get_indexed_element(ngx.var.http_x_real_ip, -1))
  184. ngx.say("X-Real-IP: -2 is ", utils.get_indexed_element(ngx.var.http_x_real_ip, -2))
  185. }
  186. }
  187. --- request
  188. GET /t
  189. --- more_headers
  190. X-Forwarded-For: 1.1.1.1, 2.2.2.2
  191. X-Forwarded-For: 3.3.3.3, 4.4.4.4
  192. X-Real-IP: 10.10.10.10
  193. --- response_body
  194. X-Forwarded-For: 1 is 1.1.1.1
  195. X-Forwarded-For: 2 is 2.2.2.2
  196. X-Forwarded-For: 3 is 3.3.3.3
  197. X-Forwarded-For: 4 is 4.4.4.4
  198. X-Forwarded-For: 5 is nil
  199. X-Forwarded-For: -1 is 4.4.4.4
  200. X-Forwarded-For: -2 is 3.3.3.3
  201. X-Forwarded-For: -3 is 2.2.2.2
  202. X-Forwarded-For: -4 is 1.1.1.1
  203. X-Forwarded-For: -5 is nil
  204. X-Forwarded-For: 0 is 1.1.1.1, 2.2.2.2, 3.3.3.3, 4.4.4.4
  205. X-Non-Existent-Header is nil
  206. X-Real-IP is 10.10.10.10
  207. X-Real-IP: 1 is 10.10.10.10
  208. X-Real-IP: 2 is nil
  209. X-Real-IP: -1 is 10.10.10.10
  210. X-Real-IP: -2 is nil
  211. --- no_error_log
  212. [error]
  213. === TEST 10: get_event_id
  214. --- http_config eval: $::HttpConfig
  215. --- config
  216. location /t {
  217. content_by_lua_block {
  218. local utils = require "resty.t1k.utils"
  219. ngx.say(utils.get_event_id("<!-- event_id: 0988987de04844c7a3ce6d27865c9513 -->"))
  220. ngx.say(utils.get_event_id("<!-- event_id: 8bae6adf33864c7f8bf715a9b7a65b2c TYPE: A -->"))
  221. ngx.say(utils.get_event_id("<!-- TYPE B -->"))
  222. }
  223. }
  224. --- request
  225. GET /t
  226. --- response_body
  227. 0988987de04844c7a3ce6d27865c9513
  228. 8bae6adf33864c7f8bf715a9b7a65b2c
  229. nil
  230. --- no_error_log
  231. [error]