forked from scaleway/scaleway-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
60 lines (45 loc) · 1.5 KB
/
errors_test.go
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
package scw
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"testing"
"github.com/scaleway/scaleway-sdk-go/internal/testhelpers"
)
func TestHasResponseErrorWithStatus200(t *testing.T) {
res := &http.Response{StatusCode: 200}
newErr := hasResponseError(res)
testhelpers.AssertNoError(t, newErr)
}
func TestHasResponseErrorWithoutBody(t *testing.T) {
res := &http.Response{StatusCode: 400}
newErr := hasResponseError(res)
testhelpers.Assert(t, newErr != nil, "Should have error")
}
func TestHasResponseErrorWithValidError(t *testing.T) {
var (
errorMessage = "some message"
errorType = "some type"
errorFields = map[string][]string{"some_field": {"some_value"}}
errorStatusCode = 400
errorStatus = "400 Bad Request"
)
// Create expected error response
testErrorReponse := &ResponseError{
Message: errorMessage,
Type: errorType,
Fields: errorFields,
StatusCode: errorStatusCode,
Status: errorStatus,
RawBody: []byte(`{"message":"some message","type":"some type","fields":{"some_field":["some_value"]}}`),
}
// Create response body with marshalled error response
bodyBytes, err := json.Marshal(testErrorReponse)
testhelpers.AssertNoError(t, err)
res := &http.Response{Status: errorStatus, StatusCode: errorStatusCode, Body: ioutil.NopCloser(bytes.NewReader(bodyBytes))}
// Test hasResponseError()
newErr := hasResponseError(res)
testhelpers.Assert(t, newErr != nil, "Should have error")
testhelpers.Equals(t, testErrorReponse, newErr)
}