-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(body-transformer): support mulipart content-type #11767
Merged
Revolyssup
merged 7 commits into
apache:master
from
Revolyssup:revolyssup/support-multipart-content
Nov 29, 2024
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bccc64b
feat(body-transformer): support mulipart content-type
Revolyssup d267c09
fix lint
Revolyssup 4ebb102
reindex
Revolyssup 30e170a
fix lint
Revolyssup e4eaef1
Merge branch 'master' of github.com:apache/apisix into revolyssup/sup…
Revolyssup 8a0add3
Merge branch 'master' into revolyssup/support-multipart-content
Revolyssup e374a62
Update apisix/plugins/body-transformer.lua
Revolyssup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,269 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
use t::APISIX 'no_plan'; | ||
|
||
no_long_string(); | ||
no_shuffle(); | ||
no_root_location(); | ||
|
||
add_block_preprocessor(sub { | ||
my ($block) = @_; | ||
|
||
if (!$block->request) { | ||
$block->set_value("request", "GET /t"); | ||
} | ||
}); | ||
|
||
run_tests; | ||
|
||
__DATA__ | ||
|
||
=== TEST 1: multipart request body to json request body conversion | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local t = require("lib.test_admin") | ||
local core = require("apisix.core") | ||
|
||
local code, body = t.test('/apisix/admin/routes/1', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"uri": "/echo", | ||
"plugins": { | ||
"body-transformer": { | ||
"request": { | ||
"template": "{\"foo\":\"{{name .. \" world\"}}\",\"bar\":{{age+10}}}" | ||
} | ||
} | ||
}, | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
} | ||
} | ||
}]] | ||
) | ||
|
||
if code >= 300 then | ||
ngx.status = code | ||
return | ||
end | ||
ngx.sleep(0.5) | ||
|
||
local http = require("resty.http") | ||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/echo" | ||
|
||
local body = ([[ | ||
--AaB03x | ||
Content-Disposition: form-data; name="name" | ||
|
||
Larry | ||
--AaB03x | ||
Content-Disposition: form-data; name="age" | ||
|
||
10 | ||
--AaB03x--]]) | ||
|
||
local opt = {method = "POST", body = body, headers = {["Content-Type"] = "multipart/related; boundary=AaB03x"}} | ||
local httpc = http.new() | ||
local res = httpc:request_uri(uri, opt) | ||
|
||
ngx.status = res.status | ||
ngx.say(res.body or res.reason) | ||
} | ||
} | ||
--- response_body | ||
{"foo":"Larry world","bar":20} | ||
|
||
|
||
|
||
=== TEST 2: multipart response body to json response body conversion | ||
--- config | ||
location /demo { | ||
content_by_lua_block { | ||
ngx.header["Content-Type"] = "multipart/related; boundary=AaB03x" | ||
ngx.say([[ | ||
--AaB03x | ||
Content-Disposition: form-data; name="name" | ||
|
||
Larry | ||
--AaB03x | ||
Content-Disposition: form-data; name="age" | ||
|
||
10 | ||
--AaB03x--]]) | ||
} | ||
} | ||
location /t { | ||
content_by_lua_block { | ||
local t = require("lib.test_admin") | ||
local core = require("apisix.core") | ||
|
||
local code, body = t.test('/apisix/admin/routes/1', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"uri": "/hello", | ||
"plugins": { | ||
"proxy-rewrite": { | ||
"uri": "/demo" | ||
}, | ||
"body-transformer": { | ||
"response": { | ||
"template": "{\"foo\":\"{{name .. \" world\"}}\",\"bar\":{{age+10}}}" | ||
} | ||
} | ||
}, | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": { | ||
"127.0.0.1:1984": 1 | ||
} | ||
} | ||
}]] | ||
) | ||
|
||
if code >= 300 then | ||
ngx.status = code | ||
return | ||
end | ||
ngx.sleep(0.5) | ||
|
||
local http = require("resty.http") | ||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello" | ||
|
||
local opt = {method = "GET"} | ||
local httpc = http.new() | ||
local res = httpc:request_uri(uri, opt) | ||
|
||
ngx.status = res.status | ||
ngx.say(res.body or res.reason) | ||
} | ||
} | ||
--- response_body | ||
{"foo":"Larry world","bar":20} | ||
|
||
|
||
|
||
=== TEST 3: multipart parse result accessible to template renderer | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local t = require("lib.test_admin") | ||
local core = require("apisix.core") | ||
|
||
local req_template = ngx.encode_base64[[ | ||
{% | ||
local core = require 'apisix.core' | ||
local cjson = require 'cjson' | ||
if tonumber(context.age) > 18 then | ||
context._multipart:set_simple("status", "major") | ||
else | ||
context._multipart:set_simple("status", "minor") | ||
end | ||
local body = context._multipart:tostring() | ||
%}{* body *} | ||
]] | ||
|
||
local code, body = t.test('/apisix/admin/routes/1', | ||
ngx.HTTP_PUT, | ||
string.format([[{ | ||
"uri": "/echo", | ||
"plugins": { | ||
"body-transformer": { | ||
"response": { | ||
"template": "%s" | ||
} | ||
} | ||
}, | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
} | ||
} | ||
}]], req_template) | ||
) | ||
|
||
if code >= 300 then | ||
ngx.status = code | ||
return | ||
end | ||
ngx.sleep(0.5) | ||
|
||
------------------------#######################------------------- | ||
|
||
local http = require("resty.http") | ||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/echo" | ||
|
||
local body_minor = ([[ | ||
--AaB03x | ||
Content-Disposition: form-data; name="name" | ||
|
||
Larry | ||
--AaB03x | ||
Content-Disposition: form-data; name="age" | ||
|
||
10 | ||
--AaB03x--]]) | ||
|
||
|
||
local opt = {method = "POST", body = body_minor, headers = {["Content-Type"] = "multipart/related; boundary=AaB03x"}} | ||
local httpc = http.new() | ||
local res = httpc:request_uri(uri, opt) | ||
assert(res.status == 200) | ||
|
||
ngx.say(res.body) | ||
|
||
} | ||
} | ||
--- response_body eval | ||
qr/.*Content-Disposition: form-data; name=\"status\"\r\n\r\nminor.*/ | ||
|
||
|
||
|
||
=== TEST 4: multipart parse response accessible to template renderer (test with age == 19) | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
|
||
local http = require("resty.http") | ||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/echo" | ||
|
||
local body_major = ([[ | ||
--AaB03x | ||
Content-Disposition: form-data; name="name" | ||
|
||
Larry | ||
--AaB03x | ||
Content-Disposition: form-data; name="age" | ||
|
||
19 | ||
--AaB03x--]]) | ||
|
||
|
||
local opt = {method = "POST", body = body_major, headers = {["Content-Type"] = "multipart/related; boundary=AaB03x"}} | ||
local httpc = http.new() | ||
local res = httpc:request_uri(uri, opt) | ||
assert(res.status == 200) | ||
|
||
ngx.say(res.body) | ||
|
||
} | ||
} | ||
--- response_body eval | ||
qr/.*Content-Disposition: form-data; name=\"status\"\r\n\r\nmajor.*/ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
performance: we should enable
plain mode