yangjj-iso opened a new issue, #7369:
URL: https://github.com/apache/shenyu/issues/7369
### 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#doGenerate` calls `context.setVariable("req",
mockRequest)`.
- `MockPlugin` builds `MockRequest` with headers, method, query parameters,
URI, and the raw request body.
`MockRequest` already exposes `getForms()`, but the method is currently
unimplemented:
```java
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:
1. Lazily parse the raw `body` into a `Map<String, String>`, similar to
`getJson()`.
2. Decode percent-encoded keys and values as UTF-8.
3. Handle an empty body without throwing.
4. Cache the parsed form map on the `MockRequest` instance.
5. Add unit coverage for normal, encoded, and empty form bodies.
6. Add an expression-generator test proving that a form field can be read
from `#req`.
Example expected usage:
```text
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.java`
-
`shenyu-plugin/shenyu-plugin-mock/src/main/java/org/apache/shenyu/plugin/mock/MockPlugin.java`
-
`shenyu-plugin/shenyu-plugin-mock/src/main/java/org/apache/shenyu/plugin/mock/generator/ExpressionGenerator.java`
-
`shenyu-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_
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscr...@shenyu.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org