Add response helpers - #6
Merged
Merged
Conversation
Signed-off-by: Chen Su <ghosind@gmail.com>
Signed-off-by: Chen Su <ghosind@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR adds convenience response helpers to engine.Context to simplify writing common response types (plain text, JSON, redirects) from handlers.
Changes:
- Added
Context.String,Context.JSON, andContext.Redirecthelper methods. - Added unit tests covering default behavior and header-preservation behavior for the new helpers.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| context.go | Adds response helper methods for text, JSON, and redirects on Context. |
| context_test.go | Adds test coverage for the new Context response helper methods. |
Comments suppressed due to low confidence (1)
context_test.go:771
- The failure message says "unmarshalable" but the code path under test is json.Marshal. This is confusing when diagnosing failures; use wording that matches marshaling.
_, err := ctx.JSON(make(chan int))
if err == nil {
t.Fatalf("Expected JSON to return error for unmarshalable type")
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+336
to
+353
| func (ctx *Context) Redirect(link string, code ...int) error { | ||
| statusCode := http.StatusFound | ||
| if len(code) > 0 { | ||
| statusCode = code[0] | ||
| } | ||
| switch statusCode { | ||
| case http.StatusMultipleChoices, http.StatusMovedPermanently, http.StatusFound, http.StatusSeeOther, | ||
| http.StatusTemporaryRedirect, http.StatusPermanentRedirect: | ||
| // the status code is always valid | ||
| _ = ctx.Status(statusCode) | ||
| default: | ||
| return fmt.Errorf("invalid redirect status code: %d", statusCode) | ||
| } | ||
|
|
||
| ctx.SetHeader("Location", link) | ||
|
|
||
| return nil | ||
| } |
| } | ||
| } | ||
|
|
||
| func TestContext_Redirect_BodyOnStatus(t *testing.T) { |
Comment on lines
+713
to
+716
| _, err := ctx.JSON(make(chan int)) | ||
| if err == nil { | ||
| t.Fatalf("Expected JSON to return error for unmarshalable type") | ||
| } |
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 introduces String, JSON, and Redirect to the Context for handling responses