grpc-proxy.t 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. # As the test framework doesn't support sending grpc request, this
  19. # test file is only for grpc irrelative configuration check.
  20. # To avoid confusion, we configure a closed port so if th configuration works,
  21. # the result will be `connect refused`.
  22. repeat_each(1);
  23. log_level('info');
  24. no_root_location();
  25. no_shuffle();
  26. add_block_preprocessor(sub {
  27. my ($block) = @_;
  28. my $yaml_config = $block->yaml_config // <<_EOC_;
  29. apisix:
  30. node_listen: 1984
  31. deployment:
  32. role: data_plane
  33. role_data_plane:
  34. config_provider: yaml
  35. _EOC_
  36. $block->set_value("yaml_config", $yaml_config);
  37. if (!$block->request) {
  38. $block->set_value("request", "POST /hello");
  39. }
  40. });
  41. run_tests();
  42. __DATA__
  43. === TEST 1: with upstream_id
  44. --- apisix_yaml
  45. upstreams:
  46. - id: 1
  47. type: roundrobin
  48. scheme: grpc
  49. nodes:
  50. "127.0.0.1:9088": 1
  51. routes:
  52. - id: 1
  53. methods:
  54. - POST
  55. uri: "/hello"
  56. upstream_id: 1
  57. #END
  58. --- error_code: 502
  59. --- error_log
  60. proxy request to 127.0.0.1:9088
  61. === TEST 2: with consumer
  62. --- apisix_yaml
  63. consumers:
  64. - username: jack
  65. plugins:
  66. key-auth:
  67. key: user-key
  68. #END
  69. routes:
  70. - id: 1
  71. methods:
  72. - POST
  73. uri: "/hello"
  74. plugins:
  75. key-auth:
  76. consumer-restriction:
  77. whitelist:
  78. - jack
  79. upstream:
  80. scheme: grpc
  81. type: roundrobin
  82. nodes:
  83. "127.0.0.1:9088": 1
  84. #END
  85. --- more_headers
  86. apikey: user-key
  87. --- error_code: 502
  88. --- error_log
  89. Connection refused
  90. === TEST 3: with upstream_id (old way)
  91. --- apisix_yaml
  92. upstreams:
  93. - id: 1
  94. type: roundrobin
  95. scheme: grpc
  96. nodes:
  97. "127.0.0.1:9088": 1
  98. routes:
  99. - id: 1
  100. methods:
  101. - POST
  102. uri: "/hello"
  103. upstream_id: 1
  104. #END
  105. --- error_code: 502
  106. --- error_log
  107. proxy request to 127.0.0.1:9088
  108. === TEST 4: with consumer (old way)
  109. --- apisix_yaml
  110. consumers:
  111. - username: jack
  112. plugins:
  113. key-auth:
  114. key: user-key
  115. #END
  116. routes:
  117. - id: 1
  118. methods:
  119. - POST
  120. uri: "/hello"
  121. plugins:
  122. key-auth:
  123. consumer-restriction:
  124. whitelist:
  125. - jack
  126. upstream:
  127. type: roundrobin
  128. scheme: grpc
  129. nodes:
  130. "127.0.0.1:9088": 1
  131. #END
  132. --- more_headers
  133. apikey: user-key
  134. --- error_code: 502
  135. --- error_log
  136. Connection refused
  137. === TEST 5: use 443 as the grpcs' default port
  138. --- apisix_yaml
  139. routes:
  140. -
  141. uri: /hello
  142. upstream:
  143. scheme: grpcs
  144. nodes:
  145. "127.0.0.1": 1
  146. type: roundrobin
  147. #END
  148. --- request
  149. GET /hello
  150. --- error_code: 502
  151. --- error_log
  152. upstream: "grpcs://127.0.0.1:443"
  153. === TEST 6: use 80 as the grpc's default port
  154. --- apisix_yaml
  155. routes:
  156. -
  157. uri: /hello
  158. upstream:
  159. scheme: grpc
  160. nodes:
  161. "127.0.0.1": 1
  162. type: roundrobin
  163. #END
  164. --- request
  165. GET /hello
  166. --- error_code: 502
  167. --- error_log
  168. upstream: "grpc://127.0.0.1:80"
  169. === TEST 7: set authority header
  170. --- log_level: debug
  171. --- http2
  172. --- apisix_yaml
  173. routes:
  174. -
  175. id: 1
  176. uris:
  177. - /helloworld.Greeter/SayHello
  178. methods: [
  179. POST
  180. ]
  181. upstream:
  182. scheme: grpc
  183. nodes:
  184. "127.0.0.1:10051": 1
  185. type: roundrobin
  186. #END
  187. --- exec
  188. grpcurl -import-path ./t/grpc_server_example/proto -proto helloworld.proto -plaintext -d '{"name":"apisix"}' 127.0.0.1:1984 helloworld.Greeter.SayHello
  189. --- response_body
  190. {
  191. "message": "Hello apisix"
  192. }
  193. --- grep_error_log eval
  194. qr/grpc header: "(:authority|host): [^"]+"/
  195. --- grep_error_log_out eval
  196. qr/grpc header: "(:authority|host): 127.0.0.1:1984"/
  197. === TEST 8: set authority header to node header
  198. --- log_level: debug
  199. --- http2
  200. --- apisix_yaml
  201. routes:
  202. -
  203. id: 1
  204. uris:
  205. - /helloworld.Greeter/SayHello
  206. methods: [
  207. POST
  208. ]
  209. upstream:
  210. scheme: grpc
  211. pass_host: node
  212. nodes:
  213. "127.0.0.1:10051": 1
  214. type: roundrobin
  215. #END
  216. --- exec
  217. grpcurl -import-path ./t/grpc_server_example/proto -proto helloworld.proto -plaintext -d '{"name":"apisix"}' 127.0.0.1:1984 helloworld.Greeter.SayHello
  218. --- response_body
  219. {
  220. "message": "Hello apisix"
  221. }
  222. --- grep_error_log eval
  223. qr/grpc header: "(:authority|host): [^"]+"/
  224. --- grep_error_log_out eval
  225. qr/grpc header: "(:authority|host): 127.0.0.1:10051"/
  226. === TEST 9: set authority header to specific value
  227. --- log_level: debug
  228. --- http2
  229. --- apisix_yaml
  230. routes:
  231. -
  232. id: 1
  233. uris:
  234. - /helloworld.Greeter/SayHello
  235. methods: [
  236. POST
  237. ]
  238. upstream:
  239. scheme: grpc
  240. pass_host: rewrite
  241. upstream_host: hello.world
  242. nodes:
  243. "127.0.0.1:10051": 1
  244. type: roundrobin
  245. #END
  246. --- exec
  247. grpcurl -import-path ./t/grpc_server_example/proto -proto helloworld.proto -plaintext -d '{"name":"apisix"}' 127.0.0.1:1984 helloworld.Greeter.SayHello
  248. --- response_body
  249. {
  250. "message": "Hello apisix"
  251. }
  252. --- grep_error_log eval
  253. qr/grpc header: "(:authority|host): [^"]+"/
  254. --- grep_error_log_out eval
  255. qr/grpc header: "(:authority|host): hello.world"/