55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package response
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNormalizeTimeUsesSecondPrecisionCST(t *testing.T) {
|
|
value := time.Date(2026, 7, 30, 9, 53, 58, 147355000, time.UTC)
|
|
got := NormalizeTime(map[string]interface{}{
|
|
"created_at": value,
|
|
"nested": []interface{}{
|
|
&value,
|
|
json.RawMessage(`{"kept":true}`),
|
|
},
|
|
})
|
|
|
|
raw, err := json.Marshal(got)
|
|
if err != nil {
|
|
t.Fatalf("marshal normalized response: %v", err)
|
|
}
|
|
text := string(raw)
|
|
if !json.Valid(raw) {
|
|
t.Fatalf("normalized response should stay valid json: %s", text)
|
|
}
|
|
if text != `{"created_at":"2026-07-30T17:53:58+08:00","nested":["2026-07-30T17:53:58+08:00",{"kept":true}]}` {
|
|
t.Fatalf("unexpected normalized response: %s", text)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeTimeStructKeepsJSONTags(t *testing.T) {
|
|
type sample struct {
|
|
CreatedAt time.Time `json:"created_at"`
|
|
Optional string `json:"optional,omitempty"`
|
|
Hidden string `json:"-"`
|
|
DeletedAt *time.Time `json:"deleted_at,omitempty"`
|
|
}
|
|
|
|
value := time.Date(2026, 7, 30, 9, 53, 58, 147355000, time.UTC)
|
|
got := NormalizeTime(sample{
|
|
CreatedAt: value,
|
|
Hidden: "secret",
|
|
})
|
|
|
|
raw, err := json.Marshal(got)
|
|
if err != nil {
|
|
t.Fatalf("marshal normalized struct: %v", err)
|
|
}
|
|
text := string(raw)
|
|
if text != `{"created_at":"2026-07-30T17:53:58+08:00"}` {
|
|
t.Fatalf("unexpected normalized struct: %s", text)
|
|
}
|
|
}
|