From fd19b18a73d5d905aa0137727ccd89991b118912 Mon Sep 17 00:00:00 2001 From: nic-chen Date: Sun, 10 May 2020 16:54:35 +0800 Subject: [PATCH 1/6] fix bug: https://github.com/apache/incubator-apisix/issues/1553 --- apisix/plugins/grpc-transcode/util.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/plugins/grpc-transcode/util.lua b/apisix/plugins/grpc-transcode/util.lua index 83d89abaf2a0..8d9c9001430a 100644 --- a/apisix/plugins/grpc-transcode/util.lua +++ b/apisix/plugins/grpc-transcode/util.lua @@ -51,7 +51,7 @@ local function get_from_request(name, kind) local request_table if ngx.req.get_method() == "POST" then if string.find(ngx.req.get_headers()["Content-Type"] or "", - "application/json", true) then + "application/json") then request_table = json.decode(ngx.req.get_body_data()) else request_table = ngx.req.get_post_args() From b2bd4f9708923eb9f6463a3d36e2e2b5c334d78d Mon Sep 17 00:00:00 2001 From: nic-chen Date: Sun, 10 May 2020 17:16:50 +0800 Subject: [PATCH 2/6] test case --- t/plugin/grpc-transcode.t | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/t/plugin/grpc-transcode.t b/t/plugin/grpc-transcode.t index 92a760cf8529..aeb06213f4b5 100644 --- a/t/plugin/grpc-transcode.t +++ b/t/plugin/grpc-transcode.t @@ -125,7 +125,18 @@ qr/\{"message":"Hello world"\}/ -=== TEST 4: wrong service protocol +=== TEST 4: hit route by post +--- request +POST /grpctest?name=world +--- response_body eval +qr/\{"message":"Hello world"\}/ +--- no_error_log +[error] +--- ONLY + + + +=== TEST 5: wrong service protocol --- config location /t { content_by_lua_block { @@ -166,7 +177,7 @@ GET /t -=== TEST 5: wrong upstream address +=== TEST 6: wrong upstream address --- config location /t { content_by_lua_block { @@ -208,7 +219,7 @@ passed -=== TEST 6: hit route (Connection refused) +=== TEST 7: hit route (Connection refused) --- request GET /grpctest --- response_body eval @@ -219,7 +230,7 @@ Connection refused) while connecting to upstream -=== TEST 7: update proto(id: 1) +=== TEST 8: update proto(id: 1) --- config location /t { content_by_lua_block { @@ -266,7 +277,7 @@ passed -=== TEST 8: set routes(id: 2) +=== TEST 9: set routes(id: 2) --- config location /t { content_by_lua_block { @@ -309,7 +320,7 @@ passed -=== TEST 9: hit route +=== TEST 10: hit route --- request GET /grpc_plus?a=1&b=2 --- response_body eval @@ -319,7 +330,7 @@ qr/\{"result":3\}/ -=== TEST 10: hit route +=== TEST 11: hit route --- request GET /grpc_plus?a=1&b=2251799813685260 --- response_body eval @@ -329,7 +340,7 @@ qr/\{"result":"#2251799813685261"\}/ -=== TEST 11: set route3 deadline nodelay +=== TEST 12: set route3 deadline nodelay --- config location /t { content_by_lua_block { @@ -371,7 +382,7 @@ passed -=== TEST 12: hit route +=== TEST 13: hit route --- request GET /grpc_deadline?name=apisix --- response_body eval @@ -381,7 +392,7 @@ qr/\{"message":"Hello apisix"\}/ -=== TEST 13: set route4 deadline delay +=== TEST 14: set route4 deadline delay --- config location /t { content_by_lua_block { @@ -423,14 +434,14 @@ passed -=== TEST 14: hit route +=== TEST 15: hit route --- request GET /grpc_delay?name=apisix --- error_code: 504 -=== TEST 15: set routes: missing method +=== TEST 16: set routes: missing method --- config location /t { content_by_lua_block { From 35cdfa4875448d217496b705c22216d031de1c9c Mon Sep 17 00:00:00 2001 From: nic-chen Date: Sun, 10 May 2020 17:21:13 +0800 Subject: [PATCH 3/6] test case --- t/plugin/grpc-transcode.t | 1 - 1 file changed, 1 deletion(-) diff --git a/t/plugin/grpc-transcode.t b/t/plugin/grpc-transcode.t index aeb06213f4b5..2cd0e75b8743 100644 --- a/t/plugin/grpc-transcode.t +++ b/t/plugin/grpc-transcode.t @@ -132,7 +132,6 @@ POST /grpctest?name=world qr/\{"message":"Hello world"\}/ --- no_error_log [error] ---- ONLY From a6e8f3456bf139a579da923f14cb8489d1ac2590 Mon Sep 17 00:00:00 2001 From: nic-chen Date: Sun, 10 May 2020 19:18:39 +0800 Subject: [PATCH 4/6] find plain text && fix test case --- apisix/plugins/grpc-transcode/util.lua | 2 +- t/plugin/grpc-transcode.t | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apisix/plugins/grpc-transcode/util.lua b/apisix/plugins/grpc-transcode/util.lua index 8d9c9001430a..d705a1ed7126 100644 --- a/apisix/plugins/grpc-transcode/util.lua +++ b/apisix/plugins/grpc-transcode/util.lua @@ -51,7 +51,7 @@ local function get_from_request(name, kind) local request_table if ngx.req.get_method() == "POST" then if string.find(ngx.req.get_headers()["Content-Type"] or "", - "application/json") then + "application/json", 1, true) then request_table = json.decode(ngx.req.get_body_data()) else request_table = ngx.req.get_post_args() diff --git a/t/plugin/grpc-transcode.t b/t/plugin/grpc-transcode.t index 2cd0e75b8743..cb1fe71c473c 100644 --- a/t/plugin/grpc-transcode.t +++ b/t/plugin/grpc-transcode.t @@ -81,7 +81,7 @@ passed local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, [[{ - "methods": ["GET"], + "methods": ["GET", "POST"], "uri": "/grpctest", "service_protocol": "grpc", "plugins": { From 4043bedb9e4b8fbe6999fdd981928798eb685831 Mon Sep 17 00:00:00 2001 From: nic-chen Date: Sun, 10 May 2020 20:00:55 +0800 Subject: [PATCH 5/6] test case for json post --- t/plugin/grpc-transcode.t | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/t/plugin/grpc-transcode.t b/t/plugin/grpc-transcode.t index cb1fe71c473c..9318a9919c13 100644 --- a/t/plugin/grpc-transcode.t +++ b/t/plugin/grpc-transcode.t @@ -127,7 +127,8 @@ qr/\{"message":"Hello world"\}/ === TEST 4: hit route by post --- request -POST /grpctest?name=world +POST /grpctest +name=world --- response_body eval qr/\{"message":"Hello world"\}/ --- no_error_log @@ -135,7 +136,18 @@ qr/\{"message":"Hello world"\}/ -=== TEST 5: wrong service protocol +=== TEST 5: hit route by post json +--- request +POST /grpctest +{"name": "world"} +--- response_body eval +qr/\{"message":"Hello world"\}/ +--- no_error_log +[error] + + + +=== TEST 6: wrong service protocol --- config location /t { content_by_lua_block { @@ -176,7 +188,7 @@ GET /t -=== TEST 6: wrong upstream address +=== TEST 7: wrong upstream address --- config location /t { content_by_lua_block { @@ -218,7 +230,7 @@ passed -=== TEST 7: hit route (Connection refused) +=== TEST 8: hit route (Connection refused) --- request GET /grpctest --- response_body eval @@ -229,7 +241,7 @@ Connection refused) while connecting to upstream -=== TEST 8: update proto(id: 1) +=== TEST 9: update proto(id: 1) --- config location /t { content_by_lua_block { @@ -276,7 +288,7 @@ passed -=== TEST 9: set routes(id: 2) +=== TEST 10: set routes(id: 2) --- config location /t { content_by_lua_block { @@ -319,7 +331,7 @@ passed -=== TEST 10: hit route +=== TEST 11: hit route --- request GET /grpc_plus?a=1&b=2 --- response_body eval @@ -329,7 +341,7 @@ qr/\{"result":3\}/ -=== TEST 11: hit route +=== TEST 12: hit route --- request GET /grpc_plus?a=1&b=2251799813685260 --- response_body eval @@ -339,7 +351,7 @@ qr/\{"result":"#2251799813685261"\}/ -=== TEST 12: set route3 deadline nodelay +=== TEST 13: set route3 deadline nodelay --- config location /t { content_by_lua_block { @@ -381,7 +393,7 @@ passed -=== TEST 13: hit route +=== TEST 14: hit route --- request GET /grpc_deadline?name=apisix --- response_body eval @@ -391,7 +403,7 @@ qr/\{"message":"Hello apisix"\}/ -=== TEST 14: set route4 deadline delay +=== TEST 15: set route4 deadline delay --- config location /t { content_by_lua_block { @@ -433,14 +445,14 @@ passed -=== TEST 15: hit route +=== TEST 16: hit route --- request GET /grpc_delay?name=apisix --- error_code: 504 -=== TEST 16: set routes: missing method +=== TEST 17: set routes: missing method --- config location /t { content_by_lua_block { From f40ea98d114812a74c0b7690bc7ae0159aacc61a Mon Sep 17 00:00:00 2001 From: nic-chen Date: Sun, 10 May 2020 20:06:34 +0800 Subject: [PATCH 6/6] json header --- t/plugin/grpc-transcode.t | 2 ++ 1 file changed, 2 insertions(+) diff --git a/t/plugin/grpc-transcode.t b/t/plugin/grpc-transcode.t index 9318a9919c13..9baca8207595 100644 --- a/t/plugin/grpc-transcode.t +++ b/t/plugin/grpc-transcode.t @@ -140,6 +140,8 @@ qr/\{"message":"Hello world"\}/ --- request POST /grpctest {"name": "world"} +--- more_headers +Content-Type: application/json --- response_body eval qr/\{"message":"Hello world"\}/ --- no_error_log