Skip to content

Commit 1d459b5

Browse files
committed
FIX: AuditInfo is now properley parsed
1 parent 9c4c0b3 commit 1d459b5

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# API CALL - Creative Code Solution
1+
# API CALL - [Creative Code Solution](https://creativecodesolutions.pt)
22

33
![Test](https://github.com/gravataLonga/api-call/workflows/Test/badge.svg?branch=master)
44
[![Coverage Status](https://coveralls.io/repos/github/gravataLonga/api-call/badge.svg?branch=master)](https://coveralls.io/github/gravataLonga/api-call?branch=master)

pkg/apicall.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ func formatResponse(baseResponse *BaseStandard, response *http.Response) error {
135135
}
136136

137137
baseResponse.AuditInfo.StatusCode = response.StatusCode
138-
baseResponse.AuditInfo.Duration = time.Since(baseResponse.Timestamp)
139138

140139
return nil
141140
}

pkg/apicall_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,33 @@ func TestSetProperlyHeader(t *testing.T) {
337337

338338
assert.NotEmpty(t, header)
339339
}
340+
341+
func TestCanParseAuditInfo(t *testing.T) {
342+
type ItemToken struct {
343+
Token string `json:"token"`
344+
}
345+
ts := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
346+
writer.Header().Set("Content-Type", "application/json")
347+
json := `{"items":{"token":"token"},"auditInfo":{"duration":0.027,"host":"test.com","dbHost":"dbHost","clientIP":"127.0.0.1","ok":true,"errors":{"items":[]},"info":{"items":[]},"warning":{"items":[]},"total":1,"env":"local","version":"1.0.0 - 2020-01-01 00:00:00","tokenAudience":""},"interfaceSettings":[]}`
348+
_, _ = writer.Write([]byte(json))
349+
}))
350+
defer ts.Close()
351+
apicall := NewApiCall(
352+
WithBaseUrl(ts.URL),
353+
)
354+
response, err := apicall.Send("POST", "/", nil)
355+
items := &ItemToken{}
356+
errGetItems := response.GetItems(items)
357+
358+
assert.Nil(t, errGetItems)
359+
assert.Nil(t, err)
360+
assert.Equal(t, "token", items.Token)
361+
assert.Equal(t, 0.027, response.AuditInfo.Duration)
362+
assert.Equal(t, "test.com", response.AuditInfo.Host)
363+
assert.Equal(t, "127.0.0.1", response.AuditInfo.ClientIP)
364+
assert.Equal(t, true, response.AuditInfo.Ok)
365+
366+
assert.Empty(t, response.AuditInfo.Errors.Items)
367+
assert.Empty(t, response.AuditInfo.Warning.Items)
368+
assert.Empty(t, response.AuditInfo.Info.Items)
369+
}

pkg/response.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (i Items) String() string {
3939
// request/response from server side
4040
type AuditInfo struct {
4141
// Duration of request
42-
Duration time.Duration `json:"duration"`
42+
Duration float64 `json:"duration"`
4343
// Timestamp when the request started
4444
Timestamp time.Time `json:"timestamp"`
4545
// Host is hostname of made request

0 commit comments

Comments
 (0)