Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔥 Feature!: Add Req and Res API #2894

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,8 +981,8 @@ func Test_App_Static_Custom_CacheControl(t *testing.T) {
app := New()

app.Static("/", "./.github", Static{ModifyResponse: func(c Ctx) error {
if strings.Contains(c.GetRespHeader("Content-Type"), "text/html") {
c.Response().Header.Set("Cache-Control", "no-cache, no-store, must-revalidate")
if strings.Contains(c.Res().Get("Content-Type"), "text/html") {
c.Set("Cache-Control", "no-cache, no-store, must-revalidate")
Comment on lines +984 to +985
Copy link
Contributor

@coderabbitai coderabbitai bot May 13, 2024

Choose a reason for hiding this comment

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

Ensure the content type check is case-insensitive.

- if strings.Contains(c.Res().Get("Content-Type"), "text/html") {
+ if strings.Contains(strings.ToLower(c.Res().Get("Content-Type")), "text/html") {

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if strings.Contains(c.Res().Get("Content-Type"), "text/html") {
c.Set("Cache-Control", "no-cache, no-store, must-revalidate")
if strings.Contains(strings.ToLower(c.Res().Get("Content-Type")), "text/html") {
c.Set("Cache-Control", "no-cache, no-store, must-revalidate")

Copy link
Member

Choose a reason for hiding this comment

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

Here we could use constant MIMETextHTML

Copy link
Contributor

Choose a reason for hiding this comment

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

@renanbastos93, using the constant MIMETextHTML instead of the hardcoded string "text/html" is indeed a good practice for maintainability and reducing the risk of typos. Here's how you can update the code:

if strings.Contains(strings.ToLower(c.Res().Get("Content-Type")), MIMETextHTML) {
    c.Set("Cache-Control", "no-cache, no-store, must-revalidate")
}

This change ensures that the code utilizes the predefined constants, making it cleaner and more consistent. Thank you for the suggestion!

}
return nil
}})
Expand Down Expand Up @@ -1666,7 +1666,7 @@ func Test_App_ReadBodyStream(t *testing.T) {
app := New(Config{StreamRequestBody: true})
app.Post("/", func(c Ctx) error {
// Calling c.Body() automatically reads the entire stream.
return c.SendString(fmt.Sprintf("%v %s", c.Request().IsBodyStream(), c.Body()))
return c.SendString(fmt.Sprintf("%v %s", c.Context().Request.IsBodyStream(), c.Body()))
})
testString := "this is a test"
resp, err := app.Test(httptest.NewRequest(MethodPost, "/", bytes.NewBufferString(testString)))
Expand All @@ -1683,7 +1683,7 @@ func Test_App_DisablePreParseMultipartForm(t *testing.T) {

app := New(Config{DisablePreParseMultipartForm: true, StreamRequestBody: true})
app.Post("/", func(c Ctx) error {
req := c.Request()
req := &c.Context().Request
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure the file is closed after usage to prevent resource leaks.

+ defer file.Close()

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
req := &c.Context().Request
req := &c.Context().Request
defer file.Close()

mpf, err := req.MultipartForm()
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (b *Bind) Custom(name string, dest any) error {

// Header binds the request header strings into the struct, map[string]string and map[string][]string.
func (b *Bind) Header(out any) error {
if err := b.returnErr(binder.HeaderBinder.Bind(b.ctx.Request(), out)); err != nil {
if err := b.returnErr(binder.HeaderBinder.Bind(&b.ctx.Context().Request, out)); err != nil {
return err
}

Expand All @@ -85,7 +85,7 @@ func (b *Bind) Header(out any) error {

// RespHeader binds the response header strings into the struct, map[string]string and map[string][]string.
func (b *Bind) RespHeader(out any) error {
if err := b.returnErr(binder.RespHeaderBinder.Bind(b.ctx.Response(), out)); err != nil {
if err := b.returnErr(binder.RespHeaderBinder.Bind(&b.ctx.Context().Response, out)); err != nil {
return err
}

Expand Down
Loading
Loading