Skip to content

Add response helpers - #6

Merged
ghosind merged 2 commits into
go-amwk:mainfrom
ghosind:feat/resp-helper
Jul 25, 2026
Merged

Add response helpers#6
ghosind merged 2 commits into
go-amwk:mainfrom
ghosind:feat/resp-helper

Conversation

@ghosind

@ghosind ghosind commented Jul 25, 2026

Copy link
Copy Markdown
Member

This PR introduces String, JSON, and Redirect to the Context for handling responses

ghosind added 2 commits July 24, 2026 09:52
Signed-off-by: Chen Su <ghosind@gmail.com>
Signed-off-by: Chen Su <ghosind@gmail.com>
@ghosind
ghosind requested a review from Copilot July 25, 2026 15:13
@ghosind ghosind added the enhancement New feature or request label Jul 25, 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 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, and Context.Redirect helper 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 thread context.go
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
}
Comment thread context_test.go
}
}

func TestContext_Redirect_BodyOnStatus(t *testing.T) {
Comment thread context_test.go
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")
}
@ghosind
ghosind merged commit fbc9df1 into go-amwk:main Jul 25, 2026
9 checks passed
@ghosind
ghosind deleted the feat/resp-helper branch July 25, 2026 15:22
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