forked from tailscale/tailscale-client-go
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow for parsing json parsing when timestamps are empty (tailsc…
…ale#15) * fix: allow for parsing json parsing when timestamps are empty The tailscale API returns blank strings for non existing timestamps, such as the "created" timestamp for Tailscale managed devices. One example is the "hello" device * Apply suggestions from code review Co-authored-by: David Bond <davidsbond@users.noreply.github.com> * refactor: changes MaybeEmptyTime to Time Co-authored-by: David Bond <davidsbond@users.noreply.github.com>
- Loading branch information
1 parent
aeb163a
commit 931531d
Showing
4 changed files
with
254 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"devices": [ | ||
{ | ||
"addresses": [ | ||
"100.101.102.103", | ||
"fd7a:115c:a1e0:ab12:4843:cd96:6265:6667" | ||
], | ||
"authorized": true, | ||
"blocksIncomingConnections": false, | ||
"clientVersion": "", | ||
"created": "", | ||
"expires": "0001-01-01T00:00:00Z", | ||
"hostname": "hello", | ||
"id": "50052", | ||
"isExternal": true, | ||
"keyExpiryDisabled": true, | ||
"lastSeen": "2022-04-15T13:24:40Z", | ||
"machineKey": "", | ||
"name": "hello.tailscale.com", | ||
"nodeKey": "nodekey:30dc3c061ac8b33fdc6d88a4a67b053b01b56930d78cae0cf7a164411d424c0d", | ||
"os": "linux", | ||
"updateAvailable": false, | ||
"user": "services@tailscale.com" | ||
}, | ||
{ | ||
"addresses": [ | ||
"100.121.200.21", | ||
"fd7a:115c:a1e0:ab12:4843:cd96:6265:e618" | ||
], | ||
"authorized": true, | ||
"blocksIncomingConnections": false, | ||
"clientVersion": "1.22.2-t60b671955-gecc5d9846", | ||
"created": "2022-03-05T17:10:27Z", | ||
"expires": "2022-09-01T17:10:27Z", | ||
"hostname": "foo", | ||
"id": "50053", | ||
"isExternal": false, | ||
"keyExpiryDisabled": true, | ||
"lastSeen": "2022-04-15T13:25:21Z", | ||
"machineKey": "mkey:30dc3c061ac8b33fdc6d88a4a67b053b01b56930d78cae0cf7a164411d424c0d", | ||
"name": "foo.example.com", | ||
"nodeKey": "nodekey:30dc3c061ac8b33fdc6d88a4a67b053b01b56930d78cae0cf7a164411d424c0d", | ||
"os": "linux", | ||
"updateAvailable": false, | ||
"user": "foo@example.com" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package tailscale_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/davidsbond/tailscale-client-go/tailscale" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWrapsStdTime(t *testing.T) { | ||
expectedTime := tailscale.Time{} | ||
newTime := time.Time{} | ||
assert.Equal(t, expectedTime.Time.UTC(), newTime.UTC()) | ||
} | ||
|
||
func TestFailsToParseInvalidTimestamps(t *testing.T) { | ||
ti := tailscale.Time{} | ||
invalidIso8601Date := []byte("\"2022-13-05T17:10:27Z\"") | ||
assert.Error(t, ti.UnmarshalJSON(invalidIso8601Date)) | ||
} | ||
|
||
func TestMarshalingTimestamps(t *testing.T) { | ||
t.Parallel() | ||
utcMinusFour := time.FixedZone("UTC-4", -60*60*4) | ||
|
||
tt := []struct { | ||
Name string | ||
Expected time.Time | ||
TimeContent string | ||
}{ | ||
{ | ||
Name: "It should handle empty strings as null-value times", | ||
Expected: time.Time{}, | ||
TimeContent: "\"\"", | ||
}, | ||
{ | ||
Name: "It should parse timestamp strings", | ||
Expected: time.Date(2022, 3, 5, 17, 10, 27, 0, time.UTC), | ||
TimeContent: "\"2022-03-05T17:10:27Z\"", | ||
}, | ||
{ | ||
Name: "It should handle different timezones", | ||
TimeContent: "\"2006-01-02T15:04:05-04:00\"", | ||
Expected: time.Date(2006, 1, 2, 15, 04, 5, 0, utcMinusFour), | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
actual := tailscale.Time{} | ||
|
||
assert.NoError(t, actual.UnmarshalJSON([]byte(tc.TimeContent))) | ||
assert.Equal(t, tc.Expected.UTC(), actual.Time.UTC()) | ||
}) | ||
} | ||
} |