Skip to content

Commit

Permalink
feat: Create Ctx.Res API
Browse files Browse the repository at this point in the history
  • Loading branch information
nickajacks1 committed May 13, 2024
1 parent 9d0e669 commit d801659
Show file tree
Hide file tree
Showing 27 changed files with 884 additions and 740 deletions.
4 changes: 2 additions & 2 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")
}
return nil
}})
Expand Down
2 changes: 1 addition & 1 deletion bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 27 additions & 27 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,21 +540,21 @@ func Test_Bind_RespHeader(t *testing.T) {
c.Context().Request.SetBody([]byte(``))
c.Context().Request.Header.SetContentType("")

c.Response().Header.Add("id", "1")
c.Response().Header.Add("Name", "John Doe")
c.Response().Header.Add("Hobby", "golang,fiber")
c.Context().Response.Header.Add("id", "1")
c.Context().Response.Header.Add("Name", "John Doe")
c.Context().Response.Header.Add("Hobby", "golang,fiber")
q := new(Header)
require.NoError(t, c.Bind().RespHeader(q))
require.Len(t, q.Hobby, 2)

c.Response().Header.Del("hobby")
c.Response().Header.Add("Hobby", "golang,fiber,go")
c.Context().Response.Header.Del("hobby")
c.Context().Response.Header.Add("Hobby", "golang,fiber,go")
q = new(Header)
require.NoError(t, c.Bind().RespHeader(q))
require.Len(t, q.Hobby, 3)

empty := new(Header)
c.Response().Header.Del("hobby")
c.Context().Response.Header.Del("hobby")
require.NoError(t, c.Bind().Query(empty))
require.Empty(t, empty.Hobby)

Expand All @@ -569,13 +569,13 @@ func Test_Bind_RespHeader(t *testing.T) {
No []int64
}

c.Response().Header.Add("id", "2")
c.Response().Header.Add("Name", "Jane Doe")
c.Response().Header.Del("hobby")
c.Response().Header.Add("Hobby", "go,fiber")
c.Response().Header.Add("favouriteDrinks", "milo,coke,pepsi")
c.Response().Header.Add("alloc", "")
c.Response().Header.Add("no", "1")
c.Context().Response.Header.Add("id", "2")
c.Context().Response.Header.Add("Name", "Jane Doe")
c.Context().Response.Header.Del("hobby")
c.Context().Response.Header.Add("Hobby", "go,fiber")
c.Context().Response.Header.Add("favouriteDrinks", "milo,coke,pepsi")
c.Context().Response.Header.Add("alloc", "")
c.Context().Response.Header.Add("no", "1")

h2 := new(Header2)
h2.Bool = true
Expand All @@ -594,7 +594,7 @@ func Test_Bind_RespHeader(t *testing.T) {
Name string `respHeader:"name,required"`
}
rh := new(RequiredHeader)
c.Response().Header.Del("name")
c.Context().Response.Header.Del("name")
require.Equal(t, "name is empty", c.Bind().RespHeader(rh).Error())
}

Expand All @@ -608,21 +608,21 @@ func Test_Bind_RespHeader_Map(t *testing.T) {
c.Context().Request.SetBody([]byte(``))
c.Context().Request.Header.SetContentType("")

c.Response().Header.Add("id", "1")
c.Response().Header.Add("Name", "John Doe")
c.Response().Header.Add("Hobby", "golang,fiber")
c.Context().Response.Header.Add("id", "1")
c.Context().Response.Header.Add("Name", "John Doe")
c.Context().Response.Header.Add("Hobby", "golang,fiber")
q := make(map[string][]string, 0)
require.NoError(t, c.Bind().RespHeader(&q))
require.Len(t, q["Hobby"], 2)

c.Response().Header.Del("hobby")
c.Response().Header.Add("Hobby", "golang,fiber,go")
c.Context().Response.Header.Del("hobby")
c.Context().Response.Header.Add("Hobby", "golang,fiber,go")
q = make(map[string][]string, 0)
require.NoError(t, c.Bind().RespHeader(&q))
require.Len(t, q["Hobby"], 3)

empty := make(map[string][]string, 0)
c.Response().Header.Del("hobby")
c.Context().Response.Header.Del("hobby")
require.NoError(t, c.Bind().Query(&empty))
require.Empty(t, empty["Hobby"])
}
Expand Down Expand Up @@ -789,9 +789,9 @@ func Benchmark_Bind_RespHeader(b *testing.B) {
c.Context().Request.SetBody([]byte(``))
c.Context().Request.Header.SetContentType("")

c.Response().Header.Add("id", "1")
c.Response().Header.Add("Name", "John Doe")
c.Response().Header.Add("Hobby", "golang,fiber")
c.Context().Response.Header.Add("id", "1")
c.Context().Response.Header.Add("Name", "John Doe")
c.Context().Response.Header.Add("Hobby", "golang,fiber")

q := new(ReqHeader)
b.ReportAllocs()
Expand All @@ -811,9 +811,9 @@ func Benchmark_Bind_RespHeader_Map(b *testing.B) {
c.Context().Request.SetBody([]byte(``))
c.Context().Request.Header.SetContentType("")

c.Response().Header.Add("id", "1")
c.Response().Header.Add("Name", "John Doe")
c.Response().Header.Add("Hobby", "golang,fiber")
c.Context().Response.Header.Add("id", "1")
c.Context().Response.Header.Add("Name", "John Doe")
c.Context().Response.Header.Add("Hobby", "golang,fiber")

q := make(map[string][]string)
b.ReportAllocs()
Expand Down Expand Up @@ -1540,7 +1540,7 @@ func Test_Bind_Must(t *testing.T) {
rq := new(RequiredQuery)
c.Context().URI().SetQueryString("")
err := c.Bind().Must().Query(rq)
require.Equal(t, StatusBadRequest, c.Response().StatusCode())
require.Equal(t, StatusBadRequest, c.Context().Response.StatusCode())
require.Equal(t, "Bad request: name is empty", err.Error())
}

Expand Down
2 changes: 1 addition & 1 deletion client/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func Test_Response_Header(t *testing.T) {

server := startTestServer(t, func(app *fiber.App) {
app.Get("/", func(c fiber.Ctx) error {
c.Response().Header.Add("foo", "bar")
c.Set("foo", "bar")
return c.SendString("helo world")
})
})
Expand Down
Loading

0 comments on commit d801659

Please sign in to comment.