Skip to content

Commit

Permalink
[bug] Fix DateTime-type objects not being URL-encoded for HTTP GET …
Browse files Browse the repository at this point in the history
…requests (#211)
  • Loading branch information
nwithan8 authored Mar 25, 2024
1 parent 2183713 commit 9252def
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Next Release

- Fix `DateTime`-type parameters not being included in GET requests

## v4.1.0 (2024-01-08)

- Add `ListChildUsers` and `GetNextChildUserPage` functions to `User` service
Expand Down
23 changes: 16 additions & 7 deletions datetime.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package easypost

import "time"
import (
"net/url"
"time"
)

type DateTime time.Time

Expand Down Expand Up @@ -93,16 +96,22 @@ func (dt *DateTime) UnmarshalJSON(b []byte) (err error) {
return nil
}

func (dt DateTime) MarshalJSON() ([]byte, error) {
return time.Time(dt).MarshalJSON()
func (dt *DateTime) MarshalJSON() ([]byte, error) {
return time.Time(*dt).MarshalJSON()
}

func (dt DateTime) String() string {
return time.Time(dt).String()
func (dt *DateTime) String() string {
return time.Time(*dt).String()
}

func (dt DateTime) AsTime() time.Time {
return time.Time(dt)
// EncodeValues implements the query.Encoder interface of how to encode the DateTime for a URL query string
func (dt *DateTime) EncodeValues(key string, values *url.Values) error {
values.Set(key, time.Time(*dt).Format(time.RFC3339)) // RFC3339 is the default format for time.Time
return nil
}

func (dt *DateTime) AsTime() time.Time {
return time.Time(*dt)
}

// NewDateTime returns the DateTime corresponding to
Expand Down
25 changes: 25 additions & 0 deletions tests/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/EasyPost/easypost-go/v4"
"reflect"
"time"

"github.com/google/go-querystring/query"
)

func (c *ClientTests) TestDateTime() {
Expand Down Expand Up @@ -59,3 +61,26 @@ func (c *ClientTests) TestDateTimeJSON() {
assert.NotNil(maxDatetimeString)
assert.NotNil(minDatetimeString)
}

// / TestDateTimeUrlQueryStringParameterInclusion tests that DateTime-type parameters are encoded properly (RFC3339) for use in HTTP payloads
func (c *ClientTests) TestDateTimeUrlQueryStringParameterInclusion() {
assert := c.Assert()

now := time.Now().UTC()
past := now.AddDate(0, -4, 0)

start := easypost.DateTimeFromTime(past)
end := easypost.DateTimeFromTime(now)

options := easypost.ListOptions{
StartDateTime: &start,
EndDateTime: &end,
PageSize: 100,
}

// This is the internals of the `convertOptsToURLValues` method in the `Client` struct (can't be used directly because it's private)
values, _ := query.Values(options)

assert.Equal(past.Format(time.RFC3339), values.Get("start_datetime"))
assert.Equal(now.Format(time.RFC3339), values.Get("end_datetime"))
}

0 comments on commit 9252def

Please sign in to comment.