[Task] Support form-urlencoded request bodies in the mock plugin's MockRequest so mock expressions can read form fields through #req.
Description
Feature Request
Support form-urlencoded request bodies in the mock plugin's MockRequest so mock expressions can read form fields through #req.
Is your feature request related to a problem? Please describe
The mock plugin exposes the current request to expression generators as the #req variable:
ExpressionGenerator#doGeneratecallscontext.setVariable("req", mockRequest).MockPluginbuildsMockRequestwith headers, method, query parameters, URI, and the raw request body.
MockRequest already exposes getForms(), but the method is currently unimplemented:
public Map<String, String> getForms() {
//todo
throw new UnsupportedOperationException();
}As a result, a mock response expression that needs a field from an application/x-www-form-urlencoded request cannot use the request abstraction already exposed to SpEL.
Calling #req.forms / #req.getForms() currently fails with UnsupportedOperationException.
JSON request bodies do not have this limitation because MockRequest#getJson() already parses the raw body lazily.
Describe the solution you'd like
Implement MockRequest#getForms() for application/x-www-form-urlencoded request bodies.
A possible implementation could:
- Lazily parse the raw
bodyinto aMap<String, String>, similar togetJson(). - Decode percent-encoded keys and values as UTF-8.
- Handle an empty body without throwing.
- Cache the parsed form map on the
MockRequestinstance. - Add unit coverage for normal, encoded, and empty form bodies.
- Add an expression-generator test proving that a form field can be read from
#req.
Example expected usage:
POST body:
name=shenyu&message=hello%20world
expression|#req.forms['name']
=> "shenyu"Describe alternatives you've considered
The raw body could theoretically be parsed by callers, but MockRequest does not expose the raw request body and already defines a dedicated getForms() API.
Implementing the existing API would also keep form-body access consistent with getJson().
Additional context
Relevant files:
shenyu-plugin/shenyu-plugin-mock/src/main/java/org/apache/shenyu/plugin/mock/api/MockRequest.javashenyu-plugin/shenyu-plugin-mock/src/main/java/org/apache/shenyu/plugin/mock/MockPlugin.javashenyu-plugin/shenyu-plugin-mock/src/main/java/org/apache/shenyu/plugin/mock/generator/ExpressionGenerator.javashenyu-plugin/shenyu-plugin-mock/src/main/java/org/apache/shenyu/plugin/mock/generator/StandardExpressionGenerator.java
I searched the current open and closed issues for MockRequest, getForms, and mock-plugin form-body support and did not find an existing issue covering this request.
I am willing to submit a PR for this.
Task List
No response
Source: apache/shenyu