mirror of
https://github.com/MontFerret/ferret.git
synced 2025-03-05 15:16:07 +02:00
Fixed JSON serialization for HTTPHeader type (#323)
This commit is contained in:
parent
a5d422ccdc
commit
35bfc5e71d
@ -38,6 +38,17 @@ const (
|
||||
SameSiteStrictMode
|
||||
)
|
||||
|
||||
func (s SameSite) String() string {
|
||||
switch s {
|
||||
case SameSiteLaxMode:
|
||||
return "Lax"
|
||||
case SameSiteStrictMode:
|
||||
return "Strict"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (c HTTPCookie) Type() core.Type {
|
||||
return HTTPCookieType
|
||||
}
|
||||
@ -119,7 +130,7 @@ func (c HTTPCookie) Hash() uint64 {
|
||||
h.Write([]byte(strconv.Itoa(c.MaxAge)))
|
||||
h.Write([]byte(fmt.Sprintf("%t", c.Secure)))
|
||||
h.Write([]byte(fmt.Sprintf("%t", c.HTTPOnly)))
|
||||
h.Write([]byte(strconv.Itoa(int(c.SameSite))))
|
||||
h.Write([]byte(c.SameSite.String()))
|
||||
|
||||
return h.Sum64()
|
||||
}
|
||||
@ -138,7 +149,7 @@ func (c HTTPCookie) MarshalJSON() ([]byte, error) {
|
||||
"max_age": c.MaxAge,
|
||||
"secure": c.Secure,
|
||||
"http_only": c.HTTPOnly,
|
||||
"same_site": c.SameSite,
|
||||
"same_site": c.SameSite.String(),
|
||||
}
|
||||
|
||||
out, err := json.Marshal(v)
|
||||
@ -181,14 +192,7 @@ func (c HTTPCookie) GetIn(_ context.Context, path []core.Value) (core.Value, err
|
||||
case "httpOnly":
|
||||
return values.NewBoolean(c.HTTPOnly), nil
|
||||
case "sameSite":
|
||||
switch c.SameSite {
|
||||
case SameSiteLaxMode:
|
||||
return values.NewString("Lax"), nil
|
||||
case SameSiteStrictMode:
|
||||
return values.NewString("Strict"), nil
|
||||
default:
|
||||
return values.EmptyString, nil
|
||||
}
|
||||
return values.NewString(c.SameSite.String()), nil
|
||||
default:
|
||||
return values.None, nil
|
||||
}
|
||||
|
33
pkg/drivers/cookie_test.go
Normal file
33
pkg/drivers/cookie_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package drivers_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/drivers"
|
||||
)
|
||||
|
||||
func TestHTTPCookie(t *testing.T) {
|
||||
Convey("HTTPCookie", t, func() {
|
||||
Convey(".MarshalJSON", func() {
|
||||
Convey("Should serialize cookie values", func() {
|
||||
cookie := &drivers.HTTPCookie{}
|
||||
|
||||
cookie.Name = "test_cookie"
|
||||
cookie.Value = "test_value"
|
||||
cookie.Domain = "montferret.dev"
|
||||
cookie.HTTPOnly = true
|
||||
cookie.MaxAge = 320
|
||||
cookie.Path = "/"
|
||||
cookie.SameSite = drivers.SameSiteLaxMode
|
||||
cookie.Secure = true
|
||||
|
||||
out, err := cookie.MarshalJSON()
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
So(string(out), ShouldEqual, `{"domain":"montferret.dev","expires":"0001-01-01T00:00:00Z","http_only":true,"max_age":320,"name":"test_cookie","path":"/","same_site":"Lax","secure":true,"value":"test_value"}`)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
@ -15,7 +15,7 @@ import (
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
||||
)
|
||||
|
||||
// HTTPCookie HTTPCookie object
|
||||
// HTTPHeader HTTP header object
|
||||
type HTTPHeader map[string][]string
|
||||
|
||||
func (h HTTPHeader) Type() core.Type {
|
||||
@ -101,7 +101,7 @@ func (h HTTPHeader) Copy() core.Value {
|
||||
}
|
||||
|
||||
func (h HTTPHeader) MarshalJSON() ([]byte, error) {
|
||||
out, err := json.Marshal(h)
|
||||
out, err := json.Marshal(map[string][]string(h))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
26
pkg/drivers/header_test.go
Normal file
26
pkg/drivers/header_test.go
Normal file
@ -0,0 +1,26 @@
|
||||
package drivers_test
|
||||
|
||||
import (
|
||||
"github.com/MontFerret/ferret/pkg/drivers"
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestHTTPHeader(t *testing.T) {
|
||||
Convey("HTTPHeader", t, func() {
|
||||
Convey(".MarshalJSON", func() {
|
||||
Convey("Should serialize header values", func() {
|
||||
headers := make(drivers.HTTPHeader)
|
||||
|
||||
headers["content-encoding"] = []string{"gzip"}
|
||||
headers["content-type"] = []string{"text/html", "charset=utf-8"}
|
||||
|
||||
out, err := headers.MarshalJSON()
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
So(string(out), ShouldEqual, `{"content-encoding":["gzip"],"content-type":["text/html","charset=utf-8"]}`)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user