Skip to content

Enhance Response with body handlers - #4

Merged
ghosind merged 1 commit into
go-amwk:mainfrom
ghosind:feat/resp-writer
Jul 27, 2026
Merged

ghosind merged 1 commit into
go-amwk:mainfrom
ghosind:feat/resp-writer

Conversation

@ghosind

@ghosind ghosind commented Jul 27, 2026

Copy link
Copy Markdown
Member

This PR extends the Response with WriteString, Size, and Written methods.

  • WriteString allows writing a string directly to the response body, which can be more efficient than converting the string to bytes first.
  • Size returns the total size of the response body that has been written so far, which can be useful for logging or monitoring purposes.
  • Written returns the copy of the response body that has been written so far, allowing for inspection or further processing of the response content.

Signed-off-by: Chen Su <ghosind@gmail.com>
@ghosind
ghosind requested a review from Copilot July 27, 2026 08:05
@ghosind ghosind added the enhancement New feature or request label Jul 27, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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() and Response.Written() to inspect buffered body size/content.
  • Refactor max-body-size checking into checkBodySize(...) and reuse it from both Write and WriteString.

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 thread response_test.go
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)
}
}
@ghosind
ghosind merged commit 6913b72 into go-amwk:main Jul 27, 2026
9 checks passed
@ghosind
ghosind deleted the feat/resp-writer branch July 27, 2026 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants