Repository: cloudwego/hertz
Pull request #1333 (2025-04-15 10:02:26)

fix(protocol): unexpected set resp.bodyStream
#### What type of PR is this?
fix

<!--
Add one of the following kinds:

build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
docs: Documentation only changes
feat: A new feature
optimize: A new optimization
fix: A bug fix
perf: A code change that improves performance
refactor: A code change that neither fixes a bug nor adds a feature
style: Changes that do not affect the meaning of the code (white space, formatting, missing semi-colons, etc)
test: Adding missing tests or correcting existing tests
chore: Changes to the build process or auxiliary tools and libraries such as documentation generation
-->

#### Check the PR title.
<!--
The description of the title will be attached in Release Notes, 
so please describe it from user-oriented, what this PR does / why we need it.
Please check your PR title with the below requirements:
-->
- [ ] This PR title match the format: \<type\>(optional scope): \<description\>
- [ ] The description of this PR title is user-oriented and clear enough for others to understand.
- [ ] Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. [User docs repo](https://github.com/cloudwego/cloudwego.github.io)


#### (Optional) Translate the PR title into Chinese.


#### (Optional) More detailed description for this PR(en: English/zh: Chinese).
<!--
Provide more detailed info for review(e.g., it's recommended to provide perf data if this is a perf type PR).
-->
en:
zh(optional):

#### (Optional) Which issue(s) this PR fixes:
<!--
Automatically closes linked issue when PR is merged.
Eg: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->

#### (Optional) The PR that updates user documentation:
<!--
If the current PR requires user awareness at the usage level, please submit a PR to update user docs. [User docs repo](https://github.com/cloudwego/cloudwego.github.io)
-->

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/cloudwego/hertz/pull/1333?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 72.90%. Comparing base [(`a4cc1c5`)](https://app.codecov.io/gh/cloudwego/hertz/commit/a4cc1c518989de854d2fa0f3e78ec2060f35448a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) to head [(`6c56579`)](https://app.codecov.io/gh/cloudwego/hertz/commit/6c565792b459b2f2c38d996668bbb933c2ac44fe?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff             @@
##           develop    #1333      +/-   ##
===========================================
+ Coverage    72.89%   72.90%   +0.01%     
===========================================
  Files          139      139              
  Lines        16264    16264              
===========================================
+ Hits         11855    11858       +3     
+ Misses        3816     3814       -2     
+ Partials       593      592       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/cloudwego/hertz/pull/1333?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego).

<details><summary> :rocket: New features to boost your workflow: </summary>

- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
</details>

Patch:
diff --git a/pkg/protocol/request.go b/pkg/protocol/request.go
index 9c20233..d7ddf2c 100644
--- a/pkg/protocol/request.go
+++ b/pkg/protocol/request.go
@@ -638,7 +638,7 @@ func (req *Request) IsBodyStream() bool {
 
 func (req *Request) BodyStream() io.Reader {
 	if req.bodyStream == nil {
-		req.bodyStream = NoBody
+		return NoBody
 	}
 	return req.bodyStream
 }
diff --git a/pkg/protocol/response.go b/pkg/protocol/response.go
index 174ae4f..7aed602 100644
--- a/pkg/protocol/response.go
+++ b/pkg/protocol/response.go
@@ -342,7 +342,7 @@ func (resp *Response) SetBody(body []byte) {
 
 func (resp *Response) BodyStream() io.Reader {
 	if resp.bodyStream == nil {
-		resp.bodyStream = NoResponseBody
+		return NoResponseBody
 	}
 	return resp.bodyStream
 }


Test patch:
diff --git a/pkg/protocol/response_test.go b/pkg/protocol/response_test.go
index 5ecf5a0..a3b85bd 100644
--- a/pkg/protocol/response_test.go
+++ b/pkg/protocol/response_test.go
@@ -250,6 +250,8 @@ func TestResponseAcquireResponse(t *testing.T) {
 		resp1 := AcquireResponse()
 		assert.NotNil(t, resp1)
 		assert.Nil(t, resp1.body)
+		assert.Assert(t, resp1.BodyStream() == NoResponseBody)
+		assert.Assert(t, resp1.IsBodyStream() == false)
 
 		resp1.SetBody([]byte("test"))
 		resp1.SetStatusCode(consts.StatusOK)