2
0

upstream-websocket.t 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. use t::APISIX 'no_plan';
  18. repeat_each(1);
  19. log_level('info');
  20. worker_connections(256);
  21. no_root_location();
  22. no_shuffle();
  23. run_tests();
  24. __DATA__
  25. === TEST 1: set upstream with websocket (id: 1)
  26. --- config
  27. location /t {
  28. content_by_lua_block {
  29. local t = require("lib.test_admin").test
  30. local code, body = t('/apisix/admin/routes/1',
  31. ngx.HTTP_PUT,
  32. [[{
  33. "upstream": {
  34. "nodes": {
  35. "127.0.0.1:1980": 1
  36. },
  37. "type": "roundrobin"
  38. },
  39. "enable_websocket": true,
  40. "uri": "/websocket_handshake"
  41. }]]
  42. )
  43. if code >= 300 then
  44. ngx.status = code
  45. end
  46. ngx.say(body)
  47. }
  48. }
  49. --- request
  50. GET /t
  51. --- response_body
  52. passed
  53. === TEST 2: send websocket
  54. --- raw_request eval
  55. "GET /websocket_handshake HTTP/1.1\r
  56. Host: server.example.com\r
  57. Upgrade: websocket\r
  58. Connection: Upgrade\r
  59. Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r
  60. Sec-WebSocket-Protocol: chat\r
  61. Sec-WebSocket-Version: 13\r
  62. Origin: http://example.com\r
  63. \r
  64. "
  65. --- response_headers
  66. Upgrade: websocket
  67. Connection: upgrade
  68. Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
  69. Sec-WebSocket-Protocol: chat
  70. !Content-Type
  71. --- raw_response_headers_like: ^HTTP/1.1 101 Switching Protocols\r\n
  72. --- response_body_like eval
  73. qr/hello/
  74. --- error_code: 101
  75. === TEST 3: set upstream(id: 6)
  76. --- config
  77. location /t {
  78. content_by_lua_block {
  79. local t = require("lib.test_admin").test
  80. local code, body = t('/apisix/admin/upstreams/6',
  81. ngx.HTTP_PUT,
  82. [[{
  83. "nodes": {
  84. "127.0.0.1:1981": 1
  85. },
  86. "type": "roundrobin",
  87. "desc": "new upstream"
  88. }]]
  89. )
  90. if code >= 300 then
  91. ngx.status = code
  92. end
  93. ngx.say(body)
  94. }
  95. }
  96. --- request
  97. GET /t
  98. --- response_body
  99. passed
  100. === TEST 4: set route with upstream websocket enabled(id: 6)
  101. --- config
  102. location /t {
  103. content_by_lua_block {
  104. local t = require("lib.test_admin").test
  105. local code, body = t('/apisix/admin/routes/6',
  106. ngx.HTTP_PUT,
  107. [[{
  108. "uri": "/websocket_handshake/route",
  109. "enable_websocket": true,
  110. "upstream_id": "6"
  111. }]]
  112. )
  113. if code >= 300 then
  114. ngx.status = code
  115. end
  116. ngx.say(body)
  117. }
  118. }
  119. --- request
  120. GET /t
  121. --- response_body
  122. passed
  123. === TEST 5: send success websocket to upstream
  124. --- raw_request eval
  125. "GET /websocket_handshake/route HTTP/1.1\r
  126. Host: server.example.com\r
  127. Upgrade: websocket\r
  128. Connection: Upgrade\r
  129. Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r
  130. Sec-WebSocket-Protocol: chat\r
  131. Sec-WebSocket-Version: 13\r
  132. Origin: http://example.com\r
  133. \r
  134. "
  135. --- response_headers
  136. Upgrade: websocket
  137. Connection: upgrade
  138. Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
  139. Sec-WebSocket-Protocol: chat
  140. !Content-Type
  141. --- raw_response_headers_like: ^HTTP/1.1 101 Switching Protocols\r\n
  142. --- response_body_like eval
  143. qr/hello/
  144. --- error_code: 101
  145. === TEST 6: disable websocket(id: 6)
  146. --- config
  147. location /t {
  148. content_by_lua_block {
  149. local t = require("lib.test_admin").test
  150. local code, body = t('/apisix/admin/routes/6',
  151. ngx.HTTP_PUT,
  152. [[{
  153. "uri": "/websocket_handshake/route",
  154. "enable_websocket": false,
  155. "upstream_id": "6"
  156. }]]
  157. )
  158. if code >= 300 then
  159. ngx.status = code
  160. end
  161. ngx.say(body)
  162. }
  163. }
  164. --- request
  165. GET /t
  166. --- response_body
  167. passed
  168. === TEST 7: send websocket to upstream without header support
  169. --- raw_request eval
  170. "GET /websocket_handshake/route HTTP/1.1\r
  171. Host: server.example.com\r
  172. Upgrade: websocket\r
  173. Connection: Upgrade\r
  174. Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r
  175. Sec-WebSocket-Protocol: chat\r
  176. Sec-WebSocket-Version: 13\r
  177. Origin: http://example.com\r
  178. \r
  179. "
  180. [error]
  181. --- error_code: 400
  182. --- grep_error_log eval
  183. qr/failed to new websocket: bad "upgrade" request header: nil/
  184. --- grep_error_log_out
  185. === TEST 8: set wss
  186. --- config
  187. location /t {
  188. content_by_lua_block {
  189. local core = require("apisix.core")
  190. local t = require("lib.test_admin")
  191. local code, body = t.test('/apisix/admin/routes/1',
  192. ngx.HTTP_PUT,
  193. [[{
  194. "upstream": {
  195. "scheme": "https",
  196. "nodes": {
  197. "127.0.0.1:1983": 1
  198. },
  199. "type": "roundrobin"
  200. },
  201. "enable_websocket": true,
  202. "uri": "/websocket_handshake"
  203. }]]
  204. )
  205. if code >= 300 then
  206. ngx.status = code
  207. ngx.say(body)
  208. return
  209. end
  210. local ssl_cert = t.read_file("t/certs/apisix.crt")
  211. local ssl_key = t.read_file("t/certs/apisix.key")
  212. local data = {cert = ssl_cert, key = ssl_key, sni = "127.0.0.1"}
  213. local code, body = t.test('/apisix/admin/ssls/1',
  214. ngx.HTTP_PUT,
  215. core.json.encode(data)
  216. )
  217. if code >= 300 then
  218. ngx.status = code
  219. ngx.say(body)
  220. return
  221. end
  222. ngx.say(body)
  223. }
  224. }
  225. --- request
  226. GET /t
  227. --- response_body
  228. passed
  229. === TEST 9: send websocket
  230. --- config
  231. location /t {
  232. content_by_lua_block {
  233. local client = require "resty.websocket.client"
  234. local wb = client:new()
  235. local uri = "wss://127.0.0.1:1994/websocket_handshake"
  236. local opts = {
  237. server_name = "127.0.0.1"
  238. }
  239. local ok, err = wb:connect(uri, opts)
  240. if not ok then
  241. ngx.say("failed to connect: " .. err)
  242. return
  243. end
  244. local typ
  245. data, typ, err = wb:recv_frame()
  246. if not data then
  247. ngx.say("failed to receive 2nd frame: ", err)
  248. return
  249. end
  250. ngx.say("received: ", data, " (", typ, ")")
  251. }
  252. }
  253. --- request
  254. GET /t
  255. --- response_body
  256. received: hello (text)