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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
package scw
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"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 TestNonStandardError(t *testing.T) {
type testCase struct {
resStatus string
resStatusCode int
resBody string
contentType string
expectedError SdkError
}
run := func(c *testCase) func(t *testing.T) {
return func(t *testing.T) {
res := &http.Response{
Status: c.resStatus,
StatusCode: c.resStatusCode,
Body: ioutil.NopCloser(strings.NewReader(c.resBody)),
Header: http.Header{
"Content-Type": []string{c.contentType},
},
}
// Test that hasResponseError converts the response to the expected SdkError.
newErr := hasResponseError(res)
testhelpers.Assert(t, newErr != nil, "Should have error")
testhelpers.Equals(t, c.expectedError, newErr)
}
}
t.Run("invalid_request_error type with fields", run(&testCase{
resStatus: "400 Bad Request",
resStatusCode: http.StatusBadRequest,
contentType: "application/json",
resBody: `{"fields":{"volumes.5.id":["92 is not a valid UUID."],"volumes.5.name":["required key not provided"]},"message":"Validation Error","type":"invalid_request_error"}`,
expectedError: &InvalidArgumentsError{
Details: []InvalidArgumentsErrorDetail{
{
ArgumentName: "volumes.5.id",
Reason: "constraint",
HelpMessage: "92 is not a valid UUID.",
},
{
ArgumentName: "volumes.5.name",
Reason: "constraint",
HelpMessage: "required key not provided",
},
},
RawBody: []byte(`{"fields":{"volumes.5.id":["92 is not a valid UUID."],"volumes.5.name":["required key not provided"]},"message":"Validation Error","type":"invalid_request_error"}`),
},
}))
t.Run("invalid_request_error type with message", run(&testCase{
resStatus: "400 Bad Request",
resStatusCode: http.StatusBadRequest,
contentType: "application/json",
resBody: `{"message": "server should be running", "type": "invalid_request_error"}`,
expectedError: &ResponseError{
Status: "400 Bad Request",
StatusCode: http.StatusBadRequest,
Message: "server should be running",
Type: "invalid_request_error",
RawBody: []byte(`{"message": "server should be running", "type": "invalid_request_error"}`),
},
}))
t.Run("invalid_request_error quota exceeded", run(&testCase{
resStatus: "403 Forbidden",
resStatusCode: http.StatusForbidden,
contentType: "application/json",
resBody: `{"type": "invalid_request_error", "message": "Quota exceeded for this resource.", "resource": "compute_snapshots_type_b_ssd_available"}`,
expectedError: &QuotasExceededError{
Details: []QuotasExceededErrorDetail{
{
Resource: "compute_snapshots_type_b_ssd_available",
Current: 0,
Quota: 0,
},
},
RawBody: []byte(`{"type": "invalid_request_error", "message": "Quota exceeded for this resource.", "resource": "compute_snapshots_type_b_ssd_available"}`),
},
}))
t.Run("unknown_resource ", run(&testCase{
resStatus: "404 Not Found",
resStatusCode: http.StatusNotFound,
contentType: "application/json",
resBody: `{"type": "unknown_resource", "message": "\"11111111-1111-4111-8111-111111111111\" not found"}`,
expectedError: &ResourceNotFoundError{
ResourceID: "11111111-1111-4111-8111-111111111111",
RawBody: []byte(`{"type": "unknown_resource", "message": "\"11111111-1111-4111-8111-111111111111\" not found"}`),
},
}))
t.Run("unknown_resource bis", run(&testCase{
resStatus: "404 Not Found",
resStatusCode: http.StatusNotFound,
contentType: "application/json",
resBody: `{"type": "unknown_resource", "message": "Security group \"11111111-1111-4111-8111-111111111111\" not found"}`,
expectedError: &ResourceNotFoundError{
ResourceID: "11111111-1111-4111-8111-111111111111",
Resource: "security_group",
RawBody: []byte(`{"type": "unknown_resource", "message": "Security group \"11111111-1111-4111-8111-111111111111\" not found"}`),
},
}))
t.Run("unknown_resource single qupte", run(&testCase{
resStatus: "404 Not Found",
resStatusCode: http.StatusNotFound,
contentType: "application/json",
resBody: `{"type": "unknown_resource", "message": "Volume '11111111-1111-4111-8111-111111111111' not found"}`,
expectedError: &ResourceNotFoundError{
ResourceID: "11111111-1111-4111-8111-111111111111",
Resource: "volume",
RawBody: []byte(`{"type": "unknown_resource", "message": "Volume '11111111-1111-4111-8111-111111111111' not found"}`),
},
}))
t.Run("conflict type", run(&testCase{
resStatus: "409 Conflict",
resStatusCode: http.StatusConflict,
contentType: "application/json",
resBody: `{"message": "Group is in use. You cannot delete it.", "type": "conflict"}`,
expectedError: &ResponseError{
Status: "409 Conflict",
StatusCode: http.StatusConflict,
Message: "group is in use. you cannot delete it.",
Type: "conflict",
RawBody: []byte(`{"message": "Group is in use. You cannot delete it.", "type": "conflict"}`),
},
}))
t.Run("text/plain content type", run(&testCase{
resStatus: "404 Not Found",
resStatusCode: http.StatusNotFound,
contentType: "text/plain",
resBody: ``,
expectedError: &ResponseError{
Status: "404 Not Found",
StatusCode: http.StatusNotFound,
Message: "404 Not Found",
RawBody: []byte(""),
},
}))
}
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,
Header: map[string][]string{
"Content-Type": []string{"application/json"},
},
Body: ioutil.NopCloser(bytes.NewReader(bodyBytes)),
}
// Test hasResponseError()
newErr := hasResponseError(res)
testhelpers.Assert(t, newErr != nil, "Should have error")
testhelpers.Equals(t, testErrorReponse, newErr)
}
|