Skip to content
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: e2e test support http body check #733

Merged
merged 13 commits into from
Jan 12, 2024

Conversation

Uncle-Justice
Copy link
Contributor

@Uncle-Justice Uncle-Justice commented Dec 23, 2023

Ⅰ. Describe what this PR did

  1. 支持e2e test发出的http请求携带body,支持json,text/plain等类型的body,需手动指明Content-Type
  2. 支持ExpectedRequestbody的断言
  3. 支持ExpectedResponsebody的断言
  4. 增加AssertionMeta.CompareTarget字段,即测试目标,可设定为RequestResponse,默认为Request模式。
  5. Request模式下,相比于以前,只是增加了ExpectedRequest body的校验,其他均未做改动,但是不能校验ExpectedResponse body
  6. Response模式下,无法对request做断言

Ⅱ. Does this pull request fix one issue?

fixes #551
fixes #724

Ⅲ. Why don't you add test cases (unit test/integration test)?

  1. 已增加request-block go-wasm插件的测试用例,验证e2e框架能够发送带body的http请求,并在测试目标为RequestResponse下校验text/plain类型的body
  2. 已增加transformer go-wasm插件的测试用例,验证e2e框架能够发送带body的http请求,并在测试目标为RequestResponse下校验json类型的body

Ⅳ. Describe how to verify it

运行所有的e2e测试

Ⅴ. Special notes for reviews

之前的代码逻辑是,可以同时对ExpectedResponse.HeaderExpectedRequest.Header进行断言,目前默认情况下仍保留该逻辑。

@codecov-commenter
Copy link

codecov-commenter commented Dec 23, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (c647ab3) 33.66% compared to head (ed99539) 38.02%.
Report is 15 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #733      +/-   ##
==========================================
+ Coverage   33.66%   38.02%   +4.35%     
==========================================
  Files          78       61      -17     
  Lines       11204    10323     -881     
==========================================
+ Hits         3772     3925     +153     
+ Misses       7142     6098    -1044     
- Partials      290      300      +10     

see 26 files with indirect coverage changes

@johnlanni
Copy link
Collaborator

cc @2456868764

err = json.Unmarshal(body, cReq)
if err != nil {
return nil, nil, fmt.Errorf("unexpected error reading response: %w", err)
if request.Method == "GET" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里建议是否 GET / POST 都返回同样 CapturedRequest 内容结构,如果 为POST, cReq 里body为空。这里要保持一致,而不是要区分不同method, method为POST时也可以做比如 Header等判断

@2456868764
Copy link
Collaborator

cc @2456868764

我看了这个PR,主要在于支持POST请求时,请求返回 cReq 只包含 content-type 和 返回body, 而GET请求返回 cReq 包含 Path, Host,Method, Headers 等内容, 这样造成 POST 只能用于 body和cotent-type 断言,不能包含其他比如 Headers, Path, Host等断言,我建议 POST请求也要支持GET请求里包含内容。

@Uncle-Justice @johnlanni

return nil, nil, fmt.Errorf("unexpected error reading response: %w", err)
}
}
} else if request.Method == "POST" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e2e框架不应该根据Method做特殊判断,增加对于rawbody,rawheady的校验即可

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

},
},
Response: http.AssertionResponse{
ExpectedResponse: http.Response{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

比如实现一个 ExpectedRawResponse

@Uncle-Justice
Copy link
Contributor Author

@johnlanni @2456868764

总结:backend服务增加了可选为echo-body之后,最核心的问题在于捕获请求CapturedRequest的赋值方式可能发生变化

至于测试后续的比较过程CompareRequest主要比较的就是CapturedRequestExpectedResponse里面的header&body,比较的部分的确可以不分是GET还是POST,只追加对body部分的比较即可,没有怎么破坏原有的逻辑

但是以下情况应该是需要增加二选一判断的(不区分GET或POST):

  1. 当实际发出的请求中不含body时,后端应为echo-serverCapturedRequest的赋值方式应为client实际收到的response body中直接提取

  2. 当实际发出的请求中含body时,后端应为echo-bodyCapturedRequest的赋值方式应为client实际收到的response中直接提取,header对header,body对body


因此我计划的改动为:根据ActualRequest中有无body数据来确定CapturedRequest的赋值方式。

以上最大的问题在于,测试人员可能在ActualRequest中增加了body数据,但是backend误选为了echo-server,我能想到的补救措施是做一点检查,比如此情况下检测到client收到的response body中含有"headers"字段,就弹一个warn,提醒可能是后端服务选错了。

@johnlanni
Copy link
Collaborator

@Uncle-Justice 不是的 echo sever会将请求body塞在响应的json中,依然可以用于校验网关是否对request body做了预期的修改,请求body的校验只能基于echo server。相比之下echo sever是个更通用的测试后端,而echo body目前只是用于插件需要特定的响应body来触发特定测试case。

@Uncle-Justice
Copy link
Contributor Author

详细沟通后总结:

  1. echo-server backend增加返回body["body"]的功能,对于Request的校验,目前只支持backend为echo-server这种返回body["headers"], body["body"]的情况
  2. echo-body backend支持返回raw header以及raw body
  3. 对于Response的校验,追加body读取和校验的逻辑,不限定具体的backend,只需要测试人员写明期望的body和headers形式即可
  4. 不支持测试人员在同一个测试用例中,同时要求ExpectedRequestExpectedRepsonse
  5. 对于Request和Reponse,均支持同时对Headers,body等内容的断言

@2456868764
Copy link
Collaborator

LGTM

@johnlanni johnlanni merged commit b825f91 into alibaba:main Jan 12, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants