PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/bundle/ngx_lua-0.9.2/t/040-gsub-o.t

https://github.com/dome/lixen_app
Unknown | 202 lines | 164 code | 38 blank | 0 comment | 0 complexity | 245596860f132c7a8a4172dba6b401fb MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause
  1. # vim:set ft= ts=4 sw=4 et fdm=marker:
  2. use lib 'lib';
  3. use t::TestNginxLua;
  4. #worker_connections(1014);
  5. #master_on();
  6. #workers(2);
  7. #log_level('warn');
  8. repeat_each(2);
  9. plan tests => repeat_each() * (blocks() * 2);
  10. #no_diff();
  11. no_long_string();
  12. run_tests();
  13. __DATA__
  14. === TEST 1: sanity
  15. --- config
  16. location /re {
  17. content_by_lua '
  18. local s, n = ngx.re.gsub("[hello, world]", "[a-z]+", "howdy", "o")
  19. ngx.say(s)
  20. ngx.say(n)
  21. ';
  22. }
  23. --- request
  24. GET /re
  25. --- response_body
  26. [howdy, howdy]
  27. 2
  28. === TEST 2: trimmed
  29. --- config
  30. location /re {
  31. content_by_lua '
  32. local s, n = ngx.re.gsub("hello, world", "[a-z]+", "howdy", "o")
  33. ngx.say(s)
  34. ngx.say(n)
  35. ';
  36. }
  37. --- request
  38. GET /re
  39. --- response_body
  40. howdy, howdy
  41. 2
  42. === TEST 3: not matched
  43. --- config
  44. location /re {
  45. content_by_lua '
  46. local s, n = ngx.re.gsub("hello, world", "[A-Z]+", "howdy", "o")
  47. ngx.say(s)
  48. ngx.say(n)
  49. ';
  50. }
  51. --- request
  52. GET /re
  53. --- response_body
  54. hello, world
  55. 0
  56. === TEST 4: replace by function (trimmed)
  57. --- config
  58. location /re {
  59. content_by_lua '
  60. local f = function (m)
  61. return "[" .. m[0] .. "," .. m[1] .. "]"
  62. end
  63. local s, n = ngx.re.gsub("hello, world", "([a-z])[a-z]+", f, "o")
  64. ngx.say(s)
  65. ngx.say(n)
  66. ';
  67. }
  68. --- request
  69. GET /re
  70. --- response_body
  71. [hello,h], [world,w]
  72. 2
  73. === TEST 5: replace by function (not trimmed)
  74. --- config
  75. location /re {
  76. content_by_lua '
  77. local f = function (m)
  78. return "[" .. m[0] .. "," .. m[1] .. "]"
  79. end
  80. local s, n = ngx.re.gsub("{hello, world}", "([a-z])[a-z]+", f, "o")
  81. ngx.say(s)
  82. ngx.say(n)
  83. ';
  84. }
  85. --- request
  86. GET /re
  87. --- response_body
  88. {[hello,h], [world,w]}
  89. 2
  90. === TEST 6: replace by script (trimmed)
  91. --- config
  92. location /re {
  93. content_by_lua '
  94. local s, n = ngx.re.gsub("hello, world", "([a-z])[a-z]+", "[$0,$1]", "o")
  95. ngx.say(s)
  96. ngx.say(n)
  97. ';
  98. }
  99. --- request
  100. GET /re
  101. --- response_body
  102. [hello,h], [world,w]
  103. 2
  104. === TEST 7: replace by script (not trimmed)
  105. --- config
  106. location /re {
  107. content_by_lua '
  108. local s, n = ngx.re.gsub("{hello, world}", "([a-z])[a-z]+", "[$0,$1]", "o")
  109. ngx.say(s)
  110. ngx.say(n)
  111. ';
  112. }
  113. --- request
  114. GET /re
  115. --- response_body
  116. {[hello,h], [world,w]}
  117. 2
  118. === TEST 8: set_by_lua
  119. --- config
  120. location /re {
  121. set_by_lua $res '
  122. local f = function (m)
  123. return "[" .. m[0] .. "," .. m[1] .. "]"
  124. end
  125. local s, n = ngx.re.gsub("{hello, world}", "([a-z])[a-z]+", f, "o")
  126. return s
  127. ';
  128. echo $res;
  129. }
  130. --- request
  131. GET /re
  132. --- response_body
  133. {[hello,h], [world,w]}
  134. === TEST 9: look-behind assertion
  135. --- config
  136. location /re {
  137. content_by_lua '
  138. local s, n = ngx.re.gsub("{foobarbaz}", "(?<=foo)bar|(?<=bar)baz", "h$0", "o")
  139. ngx.say(s)
  140. ngx.say(n)
  141. ';
  142. }
  143. --- request
  144. GET /re
  145. --- response_body
  146. {foohbarhbaz}
  147. 2
  148. === TEST 10: named pattern repl w/ callback
  149. --- config
  150. location /re {
  151. content_by_lua '
  152. local repl = function (m)
  153. return "[" .. m[0] .. "," .. m["first"] .. "]"
  154. end
  155. local s, n = ngx.re.gsub("hello, world", "(?<first>[a-z])[a-z]+", repl, "o")
  156. ngx.say(s)
  157. ngx.say(n)
  158. ';
  159. }
  160. --- request
  161. GET /re
  162. --- response_body
  163. [hello,h], [world,w]
  164. 2