Skip to content

Commit

Permalink
Fixed JSON serialization for HTTPHeader type (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex authored Jul 3, 2019
1 parent a5d422c commit 35bfc5e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
24 changes: 14 additions & 10 deletions pkg/drivers/cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
33 changes: 33 additions & 0 deletions pkg/drivers/cookie_test.go
Original file line number Diff line number Diff line change
@@ -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"}`)
})
})
})
}
4 changes: 2 additions & 2 deletions pkg/drivers/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions pkg/drivers/header_test.go
Original file line number Diff line number Diff line change
@@ -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"]}`)
})
})
})
}

0 comments on commit 35bfc5e

Please sign in to comment.