2023-08-29 18:03:40 +01:00
|
|
|
package aghhttp_test
|
2022-10-04 14:02:55 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-08-29 18:03:40 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2022-10-04 14:02:55 +01:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// testJSONTime is the JSON time for tests.
|
2023-08-29 18:03:40 +01:00
|
|
|
var testJSONTime = aghhttp.JSONTime(time.Unix(1_234_567_890, 123_456_000).UTC())
|
2022-10-04 14:02:55 +01:00
|
|
|
|
|
|
|
// testJSONTimeStr is the string with the JSON encoding of testJSONTime.
|
|
|
|
const testJSONTimeStr = "1234567890123.456"
|
|
|
|
|
|
|
|
func TestJSONTime_MarshalJSON(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
wantErrMsg string
|
2023-08-29 18:03:40 +01:00
|
|
|
in aghhttp.JSONTime
|
2022-10-04 14:02:55 +01:00
|
|
|
want []byte
|
|
|
|
}{{
|
|
|
|
name: "unix_zero",
|
|
|
|
wantErrMsg: "",
|
2023-08-29 18:03:40 +01:00
|
|
|
in: aghhttp.JSONTime(time.Unix(0, 0)),
|
2022-10-04 14:02:55 +01:00
|
|
|
want: []byte("0"),
|
|
|
|
}, {
|
|
|
|
name: "empty",
|
|
|
|
wantErrMsg: "",
|
2023-08-29 18:03:40 +01:00
|
|
|
in: aghhttp.JSONTime{},
|
2022-10-04 14:02:55 +01:00
|
|
|
want: []byte("-6795364578871.345"),
|
|
|
|
}, {
|
|
|
|
name: "time",
|
|
|
|
wantErrMsg: "",
|
|
|
|
in: testJSONTime,
|
|
|
|
want: []byte(testJSONTimeStr),
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
got, err := tc.in.MarshalJSON()
|
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
in := &struct {
|
2023-08-29 18:03:40 +01:00
|
|
|
A aghhttp.JSONTime
|
2022-10-04 14:02:55 +01:00
|
|
|
}{
|
|
|
|
A: testJSONTime,
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := json.Marshal(in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, []byte(`{"A":`+testJSONTimeStr+`}`), got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONTime_UnmarshalJSON(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
wantErrMsg string
|
2023-08-29 18:03:40 +01:00
|
|
|
want aghhttp.JSONTime
|
2022-10-04 14:02:55 +01:00
|
|
|
data []byte
|
|
|
|
}{{
|
|
|
|
name: "time",
|
|
|
|
wantErrMsg: "",
|
|
|
|
want: testJSONTime,
|
|
|
|
data: []byte(testJSONTimeStr),
|
|
|
|
}, {
|
|
|
|
name: "bad",
|
|
|
|
wantErrMsg: `parsing json time: strconv.ParseFloat: parsing "{}": ` +
|
|
|
|
`invalid syntax`,
|
2023-08-29 18:03:40 +01:00
|
|
|
want: aghhttp.JSONTime{},
|
2022-10-04 14:02:55 +01:00
|
|
|
data: []byte(`{}`),
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-08-29 18:03:40 +01:00
|
|
|
var got aghhttp.JSONTime
|
2022-10-04 14:02:55 +01:00
|
|
|
err := got.UnmarshalJSON(tc.data)
|
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
2023-08-29 18:03:40 +01:00
|
|
|
err := (*aghhttp.JSONTime)(nil).UnmarshalJSON([]byte("0"))
|
2022-10-04 14:02:55 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
msg := err.Error()
|
|
|
|
assert.Equal(t, "json time is nil", msg)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
want := testJSONTime
|
|
|
|
var got struct {
|
2023-08-29 18:03:40 +01:00
|
|
|
A aghhttp.JSONTime
|
2022-10-04 14:02:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(`{"A":`+testJSONTimeStr+`}`), &got)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, want, got.A)
|
|
|
|
})
|
|
|
|
}
|