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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
// Copyright 2018 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package transport
import (
"bytes"
"errors"
"io"
"net/http"
"net/url"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestTemporary(t *testing.T) {
tests := []struct {
error *Error
retry bool
}{{
error: &Error{},
retry: false,
}, {
error: &Error{
Errors: []Diagnostic{{
Code: BlobUploadInvalidErrorCode,
}},
},
retry: true,
}, {
error: &Error{
Errors: []Diagnostic{{
Code: BlobUploadInvalidErrorCode,
}, {
Code: DeniedErrorCode,
}},
},
retry: false,
}, {
error: &Error{
Errors: []Diagnostic{{
Code: TooManyRequestsErrorCode,
}},
},
retry: true,
}, {
error: &Error{
Errors: []Diagnostic{{
Code: UnavailableErrorCode,
}},
},
retry: true,
}, {
error: &Error{
StatusCode: http.StatusInternalServerError,
},
retry: true,
}}
for _, test := range tests {
retry := test.error.Temporary()
if test.retry != retry {
t.Errorf("Temporary(%s) = %t, wanted %t", test.error, retry, test.retry)
}
}
}
func TestCheckErrorNil(t *testing.T) {
tests := []int{
http.StatusOK,
http.StatusAccepted,
http.StatusCreated,
http.StatusMovedPermanently,
http.StatusInternalServerError,
}
for _, code := range tests {
resp := &http.Response{StatusCode: code}
if err := CheckError(resp, code); err != nil {
t.Errorf("CheckError(%d) = %v", code, err)
}
}
}
func TestCheckErrorNotError(t *testing.T) {
tests := []struct {
code int
body string
msg string
request *http.Request
}{{
code: http.StatusBadRequest,
body: "",
msg: "unexpected status code 400 Bad Request",
}, {
code: http.StatusUnauthorized,
// Valid JSON, but not a structured error -- we should still print the body.
body: `{"details":"incorrect username or password"}`,
msg: `unexpected status code 401 Unauthorized: {"details":"incorrect username or password"}`,
}, {
code: http.StatusUnauthorized,
body: "Not JSON",
msg: "GET https://example.com/somepath?access_token=REDACTED&scope=foo&service=bar: unexpected status code 401 Unauthorized: Not JSON",
request: &http.Request{
Method: http.MethodGet,
URL: &url.URL{
Scheme: "https",
Host: "example.com",
Path: "somepath",
RawQuery: url.Values{
"scope": []string{"foo"},
"service": []string{"bar"},
"access_token": []string{"hunter2"},
}.Encode(),
},
},
}, {
code: http.StatusUnauthorized,
body: "",
msg: "HEAD https://example.com/somepath: unexpected status code 401 Unauthorized (HEAD responses have no body, use GET for details)",
request: &http.Request{
Method: http.MethodHead,
URL: &url.URL{
Scheme: "https",
Host: "example.com",
Path: "somepath",
},
},
}}
for _, test := range tests {
resp := &http.Response{
StatusCode: test.code,
Body: io.NopCloser(bytes.NewBufferString(test.body)),
Request: test.request,
}
err := CheckError(resp, http.StatusOK)
if err == nil {
t.Fatalf("CheckError(%d, %s) = nil, wanted error", test.code, test.body)
}
var terr *Error
if !errors.As(err, &terr) {
t.Fatalf("CheckError(%d, %s) = %v, wanted error type", test.code, test.body, err)
}
if terr.StatusCode != test.code {
t.Errorf("Incorrect status code, got %d, want %d", terr.StatusCode, test.code)
}
if terr.Error() != test.msg {
t.Errorf("Incorrect message, got %q, want %q", terr.Error(), test.msg)
}
}
}
func TestCheckErrorWithError(t *testing.T) {
tests := []struct {
name string
code int
errorBody string
msg string
}{{
name: "Invalid name error",
code: http.StatusBadRequest,
errorBody: `{"errors":[{"code":"NAME_INVALID","message":"a message for you"}],"StatusCode":400}`,
msg: "NAME_INVALID: a message for you",
}, {
name: "Only status code is provided",
code: http.StatusBadRequest,
errorBody: `{"StatusCode":400}`,
msg: "unexpected status code 400 Bad Request: {\"StatusCode\":400}",
}, {
name: "Multiple diagnostics",
code: http.StatusBadRequest,
errorBody: `{"errors":[{"code":"NAME_INVALID","message":"a message for you"}, {"code":"SIZE_INVALID","message":"another message for you", "detail": "with some details"}],"StatusCode":400,"Request":null}`,
msg: "multiple errors returned: NAME_INVALID: a message for you; SIZE_INVALID: another message for you; with some details",
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
resp := &http.Response{
StatusCode: test.code,
Body: io.NopCloser(bytes.NewBuffer([]byte(test.errorBody))),
}
var terr *Error
if err := CheckError(resp, http.StatusOK); err == nil {
t.Errorf("CheckError(%d, %s) = nil, wanted error", test.code, test.errorBody)
} else if !errors.As(err, &terr) {
t.Errorf("CheckError(%d, %s) = %T, wanted *transport.Error", test.code, test.errorBody, err)
} else if diff := cmp.Diff(test.msg, err.Error()); diff != "" {
t.Errorf("CheckError(%d, %s).Error(); (-want +got) %s", test.code, test.errorBody, diff)
}
})
}
}
func TestBodyError(t *testing.T) {
expectedErr := errors.New("whoops")
resp := &http.Response{
StatusCode: http.StatusOK,
Body: &errReadCloser{expectedErr},
}
if err := CheckError(resp, http.StatusNotFound); err == nil {
t.Errorf("CheckError() = nil, wanted error %v", expectedErr)
} else if !errors.Is(err, expectedErr) {
t.Errorf("CheckError() = %v, wanted %v", err, expectedErr)
}
}
type errReadCloser struct {
err error
}
func (e *errReadCloser) Read(_ []byte) (int, error) {
return 0, e.err
}
func (e *errReadCloser) Close() error {
return e.err
}
|