-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
63 lines (54 loc) · 2.73 KB
/
errors_test.go
File metadata and controls
63 lines (54 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package errors
import (
"bytes"
"context"
"io"
_http "net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/ARM-software/golang-utils/utils/commonerrors"
"github.com/ARM-software/golang-utils/utils/commonerrors/errortest"
)
func TestFetchAPIErrorDescription(t *testing.T) {
t.Run("error response exists", func(t *testing.T) {
resp := _http.Response{Body: io.NopCloser(bytes.NewReader([]byte("{\"message\": \"client error\",\"requestId\": \"761761721\"}")))}
actualMessage := FetchAPIErrorDescription(&resp)
expectedMessage := "API call error [request-id: 761761721] client error"
assert.Equal(t, expectedMessage, actualMessage)
})
t.Run("error response exists with status", func(t *testing.T) {
resp := _http.Response{Body: io.NopCloser(bytes.NewReader([]byte("{\"message\": \"client error\",\"requestId\": \"761761721\", \"httpStatusCode\": 406}")))}
actualMessage := FetchAPIErrorDescription(&resp)
expectedMessage := "API call error (406) [request-id: 761761721] client error"
assert.Equal(t, expectedMessage, actualMessage)
})
t.Run("error response has fields", func(t *testing.T) {
resp := _http.Response{Body: io.NopCloser(bytes.NewReader([]byte("{\"message\":\"client error\",\"requestId\":\"761761721\",\"fields\":[{\"fieldName\":\"client request error\",\"fieldPath\":\"https://foo.bar\",\"message\":\"client error\"}]}")))}
actualMessage := FetchAPIErrorDescription(&resp)
expectedMessage := "API call error [request-id: 761761721] client error [client request error: client error (https://foo.bar)]"
assert.Equal(t, expectedMessage, actualMessage)
})
t.Run("error response empty", func(t *testing.T) {
respBody := _http.Response{Body: io.NopCloser(bytes.NewReader(nil))}
actualMessage := FetchAPIErrorDescription(&respBody)
expectedMessage := ""
assert.Equal(t, expectedMessage, actualMessage)
})
t.Run("error response is not JSON", func(t *testing.T) {
resp := _http.Response{Body: io.NopCloser(bytes.NewReader([]byte("<html><head><title>403 Forbidden</title></head></html>")))}
actualMessage := FetchAPIErrorDescription(&resp)
expectedMessage := "<html><head><title>403 Forbidden</title></head></html>"
assert.Equal(t, expectedMessage, actualMessage)
})
}
func TestFetchAPIErrorDescriptionInterrupt(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
resp := _http.Response{Body: io.NopCloser(bytes.NewReader([]byte("{\"message\": \"client error\",\"requestId\": \"761761721\"}")))}
_, err := FetchAPIErrorDescriptionWithContext(ctx, &resp)
errortest.AssertError(t, err, commonerrors.ErrTimeout, commonerrors.ErrCancelled)
}