Repository navigation
Enhance Response with body handlers - #4
Merged
Merged
Conversation
Signed-off-by: Chen Su <ghosind@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR enhances the web.Response type by adding body-introspection and string-writing helpers, and refactors body-size enforcement into a shared helper to support these new write paths.
Changes:
- Add
Response.WriteString(string)to write string content directly into the buffered response body. - Add
Response.Size()andResponse.Written()to inspect buffered body size/content. - Refactor max-body-size checking into
checkBodySize(...)and reuse it from bothWriteandWriteString.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| response.go | Adds WriteString, Size, Written, and centralizes body-size enforcement via checkBodySize. |
| response_test.go | Adds tests covering WriteString, Size, and Written behaviors and max-size enforcement. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+284
to
+299
| func TestResponse_WriteString_Unlimited(t *testing.T) { | ||
| if testing.Short() { | ||
| t.Skip("skipping test in short mode") | ||
| } | ||
|
|
||
| app := New(WithMaxResponseBodyBytes(MaxResponseBodyBytesUnlimited)) | ||
| rr := httptest.NewRecorder() | ||
| resp := newResponse(app, rr) | ||
|
|
||
| largeStr := string(make([]byte, MaxResponseBodyBytesDefault+1)) | ||
|
|
||
| n, err := resp.WriteString(largeStr) | ||
| if err != nil || n != len(largeStr) { | ||
| t.Fatalf("WriteString unlimited: n=%d err=%v", n, err) | ||
| } | ||
| } |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR extends the Response with
WriteString,Size, andWrittenmethods.WriteStringallows writing a string directly to the response body, which can be more efficient than converting the string to bytes first.Sizereturns the total size of the response body that has been written so far, which can be useful for logging or monitoring purposes.Writtenreturns the copy of the response body that has been written so far, allowing for inspection or further processing of the response content.