Skip to content

Commit 8a80d77

Browse files
Merge pull request #11 from speakeasy-api/fix-time-precision
fix: time precision
2 parents d33fb77 + d8659ed commit 8a80d77

16 files changed

+112
-28
lines changed

capture.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (s *speakeasy) buildHarFile(ctx context.Context, cw *captureWriter, r *http
133133
Comment: "request capture for " + r.URL.String(),
134134
Entries: []*har.Entry{
135135
{
136-
StartedDateTime: startTime.Format(time.RFC3339),
136+
StartedDateTime: startTime.Format(time.RFC3339Nano),
137137
Time: float64(timeSince(startTime).Milliseconds()),
138138
Request: s.getHarRequest(ctx, cw, r),
139139
Response: s.getHarResponse(ctx, cw, r, startTime),
@@ -286,9 +286,9 @@ func getHarCookies(cookies []*http.Cookie, startTime time.Time) []*har.Cookie {
286286
}
287287

288288
if cookie.MaxAge != 0 {
289-
harCookie.Expires = startTime.Add(time.Duration(cookie.MaxAge) * time.Second).Format(time.RFC3339)
289+
harCookie.Expires = startTime.Add(time.Duration(cookie.MaxAge) * time.Second).Format(time.RFC3339Nano)
290290
} else if (cookie.Expires != time.Time{}) {
291-
harCookie.Expires = cookie.Expires.Format(time.RFC3339)
291+
harCookie.Expires = cookie.Expires.Format(time.RFC3339Nano)
292292
}
293293

294294
harCookies = append(harCookies, harCookie)

middleware_test.go

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ type fields struct {
3838
MaxCaptureSize int `json:"max_capture_size,omitempty"`
3939
}
4040
type args struct {
41-
Method string `json:"method"`
42-
URL string `json:"url"`
43-
Headers map[string][]string `json:"headers,omitempty"`
44-
Body string `json:"body,omitempty"`
45-
ResponseStatus int `json:"response_status,omitempty"`
46-
ResponseBody string `json:"response_body,omitempty"`
47-
ResponseHeaders map[string][]string `json:"response_headers,omitempty"`
41+
Method string `json:"method"`
42+
URL string `json:"url"`
43+
Headers map[string][]string `json:"headers"`
44+
Body string `json:"body"`
45+
RequestStartTime time.Time `json:"request_start_time"`
46+
ElapsedTime int `json:"elapsed_time"`
47+
ResponseStatus int `json:"response_status"`
48+
ResponseBody string `json:"response_body"`
49+
ResponseHeaders map[string][]string `json:"response_headers"`
4850
}
4951
type test struct {
5052
Name string `json:"name"`
@@ -96,8 +98,16 @@ func TestSpeakeasy_Middleware_Capture_Success(t *testing.T) {
9698
captured := false
9799
handled := false
98100

99-
speakeasy.ExportSetTimeNow(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
100-
speakeasy.ExportSetTimeSince(1 * time.Millisecond)
101+
if tt.Args.RequestStartTime.IsZero() {
102+
speakeasy.ExportSetTimeNow(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
103+
} else {
104+
speakeasy.ExportSetTimeNow(tt.Args.RequestStartTime)
105+
}
106+
if tt.Args.ElapsedTime == 0 {
107+
speakeasy.ExportSetTimeSince(1 * time.Millisecond)
108+
} else {
109+
speakeasy.ExportSetTimeSince(time.Duration(tt.Args.ElapsedTime) * time.Millisecond)
110+
}
101111

102112
wg := &sync.WaitGroup{}
103113
wg.Add(1)
@@ -386,8 +396,16 @@ func TestSpeakeasy_GinMiddleware_Success(t *testing.T) {
386396
captured := false
387397
handled := false
388398

389-
speakeasy.ExportSetTimeNow(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
390-
speakeasy.ExportSetTimeSince(1 * time.Millisecond)
399+
if tt.Args.RequestStartTime.IsZero() {
400+
speakeasy.ExportSetTimeNow(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
401+
} else {
402+
speakeasy.ExportSetTimeNow(tt.Args.RequestStartTime)
403+
}
404+
if tt.Args.ElapsedTime == 0 {
405+
speakeasy.ExportSetTimeSince(1 * time.Millisecond)
406+
} else {
407+
speakeasy.ExportSetTimeSince(time.Duration(tt.Args.ElapsedTime) * time.Millisecond)
408+
}
391409

392410
wg := &sync.WaitGroup{}
393411
wg.Add(1)
@@ -552,8 +570,16 @@ func TestSpeakeasy_EchoMiddleware_Success(t *testing.T) {
552570
captured := false
553571
handled := false
554572

555-
speakeasy.ExportSetTimeNow(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
556-
speakeasy.ExportSetTimeSince(1 * time.Millisecond)
573+
if tt.Args.RequestStartTime.IsZero() {
574+
speakeasy.ExportSetTimeNow(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
575+
} else {
576+
speakeasy.ExportSetTimeNow(tt.Args.RequestStartTime)
577+
}
578+
if tt.Args.ElapsedTime == 0 {
579+
speakeasy.ExportSetTimeSince(1 * time.Millisecond)
580+
} else {
581+
speakeasy.ExportSetTimeSince(time.Duration(tt.Args.ElapsedTime) * time.Millisecond)
582+
}
557583

558584
wg := &sync.WaitGroup{}
559585
wg.Add(1)

speakeasy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const (
1515
)
1616

1717
var (
18-
speakeasyVersion = "0.0.2" // TODO get this from CI
18+
speakeasyVersion = "0.0.3" // TODO get this from CI
1919
serverURL = "https://api.speakeasyapi.dev"
2020

2121
defaultInstance *speakeasy

testdata/captures_basic_request_and_no_response_body_output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.2",
44
"creator": {
55
"name": "speakeasy-go-sdk",
6-
"version": "0.0.2"
6+
"version": "0.0.3"
77
},
88
"entries": [
99
{

testdata/captures_basic_request_and_response_output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.2",
44
"creator": {
55
"name": "speakeasy-go-sdk",
6-
"version": "0.0.2"
6+
"version": "0.0.3"
77
},
88
"entries": [
99
{

testdata/captures_basic_request_and_response_with_different_content_types_output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.2",
44
"creator": {
55
"name": "speakeasy-go-sdk",
6-
"version": "0.0.2"
6+
"version": "0.0.3"
77
},
88
"entries": [
99
{

testdata/captures_basic_request_and_response_with_no_response_header_set_output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.2",
44
"creator": {
55
"name": "speakeasy-go-sdk",
6-
"version": "0.0.2"
6+
"version": "0.0.3"
77
},
88
"entries": [
99
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "captures basic request with nano precision",
3+
"fields": {
4+
"max_capture_size": 9437184
5+
},
6+
"args": {
7+
"method": "GET",
8+
"url": "http://test.com/test",
9+
"request_start_time": "2020-01-01T00:00:00.0000005Z",
10+
"elapsed_time": 2,
11+
"response_status": 200,
12+
"response_body": "test"
13+
}
14+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"log": {
3+
"version": "1.2",
4+
"creator": {
5+
"name": "speakeasy-go-sdk",
6+
"version": "0.0.3"
7+
},
8+
"entries": [
9+
{
10+
"startedDateTime": "2020-01-01T00:00:00.0000005Z",
11+
"time": 2,
12+
"request": {
13+
"method": "GET",
14+
"url": "http://test.com/test",
15+
"httpVersion": "HTTP/1.1",
16+
"cookies": [],
17+
"headers": [],
18+
"queryString": [],
19+
"headersSize": 0,
20+
"bodySize": 0
21+
},
22+
"response": {
23+
"status": 200,
24+
"statusText": "OK",
25+
"httpVersion": "HTTP/1.1",
26+
"cookies": [],
27+
"headers": [],
28+
"content": {
29+
"size": 4,
30+
"mimeType": "application/octet-stream",
31+
"text": "test"
32+
},
33+
"redirectURL": "",
34+
"headersSize": 0,
35+
"bodySize": 4
36+
},
37+
"cache": null,
38+
"timings": null,
39+
"serverIPAddress": "test.com"
40+
}
41+
],
42+
"comment": "request capture for http://test.com/test"
43+
}
44+
}

testdata/captures_body_size_zero_when_cached_output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.2",
44
"creator": {
55
"name": "speakeasy-go-sdk",
6-
"version": "0.0.2"
6+
"version": "0.0.3"
77
},
88
"entries": [
99
{

0 commit comments

Comments
 (0)