Skip to content

Commit

Permalink
FIX: AuditInfo is now properley parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
gravataLonga committed Nov 22, 2020
1 parent 9c4c0b3 commit 1d459b5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# API CALL - Creative Code Solution
# API CALL - [Creative Code Solution](https://creativecodesolutions.pt)

![Test](https://github.com/gravataLonga/api-call/workflows/Test/badge.svg?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/gravataLonga/api-call/badge.svg?branch=master)](https://coveralls.io/github/gravataLonga/api-call?branch=master)
Expand Down
1 change: 0 additions & 1 deletion pkg/apicall.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ func formatResponse(baseResponse *BaseStandard, response *http.Response) error {
}

baseResponse.AuditInfo.StatusCode = response.StatusCode
baseResponse.AuditInfo.Duration = time.Since(baseResponse.Timestamp)

return nil
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/apicall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,33 @@ func TestSetProperlyHeader(t *testing.T) {

assert.NotEmpty(t, header)
}

func TestCanParseAuditInfo(t *testing.T) {
type ItemToken struct {
Token string `json:"token"`
}
ts := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
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":[]}`
_, _ = writer.Write([]byte(json))
}))
defer ts.Close()
apicall := NewApiCall(
WithBaseUrl(ts.URL),
)
response, err := apicall.Send("POST", "/", nil)
items := &ItemToken{}
errGetItems := response.GetItems(items)

assert.Nil(t, errGetItems)
assert.Nil(t, err)
assert.Equal(t, "token", items.Token)
assert.Equal(t, 0.027, response.AuditInfo.Duration)
assert.Equal(t, "test.com", response.AuditInfo.Host)
assert.Equal(t, "127.0.0.1", response.AuditInfo.ClientIP)
assert.Equal(t, true, response.AuditInfo.Ok)

assert.Empty(t, response.AuditInfo.Errors.Items)
assert.Empty(t, response.AuditInfo.Warning.Items)
assert.Empty(t, response.AuditInfo.Info.Items)
}
2 changes: 1 addition & 1 deletion pkg/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (i Items) String() string {
// request/response from server side
type AuditInfo struct {
// Duration of request
Duration time.Duration `json:"duration"`
Duration float64 `json:"duration"`
// Timestamp when the request started
Timestamp time.Time `json:"timestamp"`
// Host is hostname of made request
Expand Down

0 comments on commit 1d459b5

Please sign in to comment.