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

feat: The HTTP module supports set Cookie #49

Merged
merged 7 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 7 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ jobs:
with:
go-version: 'stable'
cache: false
- name: go mod pakcage cache
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('go.mod') }}
- name: Install dependencies
run: go mod tidy
- name: Lint
uses: golangci/golangci-lint-action@v3
with:
Expand Down
4 changes: 4 additions & 0 deletions context_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (r *ContextRequest) Bind(obj any) error {
return r.instance.BodyParser(obj)
}

func (r *ContextRequest) Cookie(key string, defaultValue ...string) string {
return r.instance.Cookies(key, defaultValue...)
}

func (r *ContextRequest) Form(key string, defaultValue ...string) string {
if len(defaultValue) == 0 {
return r.instance.FormValue(key)
Expand Down
63 changes: 56 additions & 7 deletions context_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ func TestRequest(t *testing.T) {
ConfigFacade = mockConfig
}
tests := []struct {
name string
method string
url string
setup func(method, url string) error
expectCode int
expectBody string
expectBodyJson string
name string
method string
url string
cookieName string
setup func(method, url string) error
expectCode int
expectBody string
expectBodyJson string
expectCookieValue string
}{
{
name: "All when Get and query is empty",
Expand Down Expand Up @@ -1461,6 +1463,44 @@ func TestRequest(t *testing.T) {
expectCode: http.StatusBadRequest,
expectBody: "Validate error: error",
},
{
name: "Cookie",
method: "GET",
url: "/cookie",
cookieName: "user",
setup: func(method, url string) error {
fiber.Get("/cookie", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Cookie(contractshttp.Cookie{
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved
Name: "cook",
Value: "Krishan",
}).Success().Json(nil)
})

req, _ = http.NewRequest(method, url, nil)

return nil
},
expectCode: http.StatusOK,
expectCookieValue: "Krishan",
},
{
name: "Cookie - default value",
method: "GET",
url: "/cookie",
setup: func(method, url string) error {
fiber.Get("/cookie", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Success().Json(contractshttp.Json{
"cookie": ctx.Request().Cookie("cookie", "value"),
})
})

req, _ = http.NewRequest(method, url, nil)

return nil
},
expectCode: http.StatusOK,
expectBodyJson: "{\"cookie\":\"value\"}",
},
}

for _, test := range tests {
Expand Down Expand Up @@ -1496,6 +1536,15 @@ func TestRequest(t *testing.T) {
assert.Equal(t, exceptBodyMap, bodyMap)
}

if test.cookieName != "" {
cookies := resp.Cookies()
for _, cookie := range cookies {
if cookie.Name == test.cookieName {
assert.Equal(t, test.expectCookieValue, cookie.Value)
}
}
}

assert.Equal(t, test.expectCode, resp.StatusCode)

mockConfig.AssertExpectations(t)
Expand Down
25 changes: 25 additions & 0 deletions context_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ func NewContextResponse(instance *fiber.Ctx, origin contractshttp.ResponseOrigin
return &ContextResponse{instance, origin}
}

func (r *ContextResponse) Cookie(cookie contractshttp.Cookie) contractshttp.ContextResponse {
r.instance.Cookie(&fiber.Cookie{
Name: cookie.Name,
Value: cookie.Value,
Path: cookie.Path,
Domain: cookie.Domain,
Expires: cookie.Expires,
Secure: cookie.Secure,
HTTPOnly: cookie.HttpOnly,
MaxAge: cookie.MaxAge,
SameSite: cookie.SameSite,
})

return r
}

func (r *ContextResponse) Data(code int, contentType string, data []byte) contractshttp.Response {
return &DataResponse{code, contentType, data, r.instance}
}
Expand Down Expand Up @@ -67,6 +83,15 @@ func (r *ContextResponse) Flush() {
r.instance.Fresh()
}

func (r *ContextResponse) WithoutCookie(name string) contractshttp.ContextResponse {
r.instance.Cookie(&fiber.Cookie{
Name: name,
MaxAge: -1,
})

return r
}

func (r *ContextResponse) Writer() http.ResponseWriter {
return &WriterAdapter{r.instance}
}
Expand Down
78 changes: 70 additions & 8 deletions context_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ func TestResponse(t *testing.T) {
ConfigFacade = mockConfig
}
tests := []struct {
name string
method string
url string
setup func(method, url string) error
expectCode int
expectBody string
expectBodyJson string
expectHeader string
name string
method string
url string
cookieName string
setup func(method, url string) error
expectCode int
expectBody string
expectBodyJson string
expectHeader string
expectedCookieValue string
}{
{
name: "Data",
Expand Down Expand Up @@ -299,6 +301,57 @@ func TestResponse(t *testing.T) {
expectCode: http.StatusOK,
expectBody: "Goravel",
},
{
name: "WithoutCookie",
method: "GET",
url: "/without/cookie",
cookieName: "goravel",
setup: func(method, url string) error {
fiber.Get("/without/cookie", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().WithoutCookie("goravel").String(http.StatusOK, "Goravel")
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved
})

var err error
req, err = http.NewRequest(method, url, nil)
if err != nil {
return err
}
req.AddCookie(&http.Cookie{
Name: "goravel",
Value: "goravel",
})

return nil
},
expectCode: http.StatusOK,
expectBody: "Goravel",
expectedCookieValue: "",
},
{
name: "Cookie",
method: "GET",
url: "/cookie",
cookieName: "goravel",
setup: func(method, url string) error {
fiber.Get("/cookie", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Cookie(contractshttp.Cookie{
Name: "goravel",
Value: "goravel",
}).String(http.StatusOK, "Goravel")
})

var err error
req, err = http.NewRequest(method, url, nil)
if err != nil {
return err
}

return nil
},
expectCode: http.StatusOK,
expectBody: "Goravel",
expectedCookieValue: "goravel",
},
}

for _, test := range tests {
Expand Down Expand Up @@ -335,6 +388,15 @@ func TestResponse(t *testing.T) {
assert.Equal(t, test.expectHeader, strings.Join(resp.Header.Values("Hello"), ""))
}

if test.cookieName != "" {
cookies := resp.Cookies()
for _, cookie := range cookies {
if cookie.Name == test.cookieName {
assert.Equal(t, test.expectedCookieValue, cookie.Value)
}
}
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved
}

assert.Equal(t, test.expectCode, resp.StatusCode)

mockConfig.AssertExpectations(t)
Expand Down
56 changes: 27 additions & 29 deletions go.mod
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ require (
github.com/gofiber/template/html/v2 v2.0.5
github.com/gookit/color v1.5.4
github.com/gookit/validate v1.5.1
github.com/goravel/framework v1.13.1-0.20231121034700-e91df530a997
github.com/goravel/framework v1.13.1-0.20231214150751-ea46f55b93db
github.com/savioxavier/termlink v1.3.0
github.com/spf13/cast v1.6.0
github.com/stretchr/testify v1.8.4
github.com/valyala/fasthttp v1.51.0
)

require (
cloud.google.com/go v0.110.7 // indirect
cloud.google.com/go/compute v1.23.0 // indirect
cloud.google.com/go v0.110.10 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.1 // indirect
cloud.google.com/go/iam v1.1.5 // indirect
cloud.google.com/go/pubsub v1.33.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.16 // indirect
Expand All @@ -37,7 +37,7 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/glebarez/go-sqlite v1.21.2 // indirect
github.com/glebarez/sqlite v1.10.0 // indirect
Expand All @@ -47,24 +47,23 @@ require (
github.com/gofiber/template v1.8.2 // indirect
github.com/gofiber/utils v1.1.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang-jwt/jwt/v5 v5.1.0 // indirect
github.com/golang-jwt/jwt/v5 v5.2.0 // indirect
github.com/golang-migrate/migrate/v4 v4.16.2 // indirect
github.com/golang-module/carbon/v2 v2.2.13 // indirect
github.com/golang-module/carbon/v2 v2.2.14 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gomodule/redigo v2.0.0+incompatible // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/google/wire v0.5.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.1 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/gookit/filter v1.2.0 // indirect
github.com/gookit/goutil v0.6.12 // indirect
github.com/goravel/file-rotatelogs v0.0.0-20211215053220-2ab31dd9575c // indirect
github.com/goravel/file-rotatelogs/v2 v2.4.1 // indirect
github.com/goravel/file-rotatelogs/v2 v2.4.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
Expand All @@ -75,11 +74,9 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible // indirect
github.com/kelseyhightower/envconfig v1.4.0 // indirect
github.com/klauspost/compress v1.17.3 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/lestrrat-go/strftime v1.0.5 // indirect
github.com/lib/pq v1.10.2 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand All @@ -99,18 +96,18 @@ require (
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rotisserie/eris v0.5.4 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.17.0 // indirect
github.com/spf13/viper v1.18.1 // indirect
github.com/streadway/amqp v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/urfave/cli/v2 v2.25.7 // indirect
github.com/urfave/cli/v2 v2.26.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
Expand All @@ -124,19 +121,20 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/api v0.143.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230913181813-007df8e322eb // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 // indirect
google.golang.org/grpc v1.59.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/api v0.153.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.60.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand All @@ -145,7 +143,7 @@ require (
gorm.io/driver/postgres v1.5.4 // indirect
gorm.io/driver/sqlserver v1.5.2 // indirect
gorm.io/gorm v1.25.5 // indirect
gorm.io/plugin/dbresolver v1.4.7 // indirect
gorm.io/plugin/dbresolver v1.5.0 // indirect
modernc.org/libc v1.22.5 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.5.0 // indirect
Expand Down
Loading