Skip to content

Commit 7caa8e4

Browse files
player-twopatryk
authored andcommitted
Fix unmarshalling of OriginCACertificate (#387)
* Fix unmarshalling of OriginCACertificate * Linter and test fixes * Use aliasing to override just a single field for unmarshalling
1 parent d6d38f7 commit 7caa8e4

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

origin_ca.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,38 @@ type OriginCACertificate struct {
2222
CSR string `json:"csr"`
2323
}
2424

25+
// UnmarshalJSON handles custom parsing from an API response to an OriginCACertificate
26+
// http://choly.ca/post/go-json-marshalling/
27+
func (c *OriginCACertificate) UnmarshalJSON(data []byte) error {
28+
type alias OriginCACertificate
29+
30+
aux := &struct {
31+
ExpiresOn string `json:"expires_on"`
32+
*alias
33+
}{
34+
alias: (*alias)(c),
35+
}
36+
37+
var err error
38+
39+
if err = json.Unmarshal(data, &aux); err != nil {
40+
return err
41+
}
42+
43+
// This format comes from time.Time.String() source
44+
c.ExpiresOn, err = time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", aux.ExpiresOn)
45+
46+
if err != nil {
47+
c.ExpiresOn, err = time.Parse(time.RFC3339, aux.ExpiresOn)
48+
}
49+
50+
if err != nil {
51+
return err
52+
}
53+
54+
return nil
55+
}
56+
2557
// OriginCACertificateListOptions represents the parameters used to list Cloudflare-issued certificates.
2658
type OriginCACertificateListOptions struct {
2759
ZoneID string

origin_ca_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cloudflare
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"net/http"
67
"testing"
@@ -9,6 +10,40 @@ import (
910
"github.com/stretchr/testify/assert"
1011
)
1112

13+
var (
14+
payloadTemplate = `{"expires_on":"%s"}`
15+
unmarshalTime = time.Now().UTC().Round(time.Second)
16+
)
17+
18+
func TestOriginCA_UnmarshalRFC3339(t *testing.T) {
19+
payload := fmt.Sprintf(payloadTemplate, unmarshalTime.Format(time.RFC3339))
20+
21+
var cert OriginCACertificate
22+
err := json.Unmarshal([]byte(payload), &cert)
23+
if assert.NoError(t, err) {
24+
assert.Equal(t, unmarshalTime, cert.ExpiresOn)
25+
}
26+
}
27+
28+
func TestOriginCA_UnmarshalString(t *testing.T) {
29+
payload := fmt.Sprintf(payloadTemplate, unmarshalTime.String())
30+
31+
var cert OriginCACertificate
32+
err := json.Unmarshal([]byte(payload), &cert)
33+
if assert.NoError(t, err) {
34+
assert.Equal(t, unmarshalTime, cert.ExpiresOn)
35+
}
36+
}
37+
38+
func TestOriginCA_UnmarshalOther(t *testing.T) {
39+
payload := fmt.Sprintf(payloadTemplate, unmarshalTime.Format(time.RFC1123))
40+
41+
var cert OriginCACertificate
42+
err := json.Unmarshal([]byte(payload), &cert)
43+
assert.Error(t, err)
44+
assert.Equal(t, OriginCACertificate{}, cert)
45+
}
46+
1247
func TestOriginCA_CreateOriginCertificate(t *testing.T) {
1348
setup()
1449
defer teardown()

0 commit comments

Comments
 (0)