|
14 | 14 | package webrisk
|
15 | 15 |
|
16 | 16 | import (
|
| 17 | + "bytes" |
17 | 18 | "context"
|
18 | 19 | "encoding/base64"
|
| 20 | + "errors" |
| 21 | + "io" |
| 22 | + "io/ioutil" |
19 | 23 | "net/http"
|
20 | 24 | "net/http/httptest"
|
21 | 25 | "reflect"
|
22 | 26 | "testing"
|
23 | 27 |
|
| 28 | + "github.com/google/go-cmp/cmp" |
| 29 | + "github.com/google/go-cmp/cmp/cmpopts" |
24 | 30 | "google.golang.org/protobuf/encoding/protojson"
|
25 | 31 | "google.golang.org/protobuf/proto"
|
26 | 32 | pb "github.com/google/webrisk/internal/webrisk_proto"
|
@@ -182,3 +188,67 @@ func TestNetAPI(t *testing.T) {
|
182 | 188 | t.Errorf("unexpected HashLookup success, wanted malformed JSON error")
|
183 | 189 | }
|
184 | 190 | }
|
| 191 | + |
| 192 | +func createBody(j string) io.ReadCloser { |
| 193 | + return ioutil.NopCloser(bytes.NewReader([]byte(j))) |
| 194 | +} |
| 195 | + |
| 196 | +func TestParseError(t *testing.T) { |
| 197 | + tests := []struct { |
| 198 | + httpResp *http.Response |
| 199 | + wantErr error |
| 200 | + }{ |
| 201 | + { |
| 202 | + httpResp: &http.Response{ |
| 203 | + StatusCode: http.StatusBadRequest, |
| 204 | + Body: createBody(`{ |
| 205 | + "error": { |
| 206 | + "code": 400, |
| 207 | + "message": "API key not valid. Please pass a valid API key", |
| 208 | + "status": "INVALID_ARGUMENT"}}`), // Formatted Response |
| 209 | + }, |
| 210 | + wantErr: errors.New("webrisk: unexpected server response code: 400, status: INVALID_ARGUMENT, message: API key not valid. Please pass a valid API key"), |
| 211 | + }, |
| 212 | + { |
| 213 | + httpResp: &http.Response{ |
| 214 | + StatusCode: http.StatusForbidden, |
| 215 | + Body: createBody(`{ |
| 216 | + "error": { |
| 217 | + "code": 403, |
| 218 | + "message": "API Not Enabled", |
| 219 | + "status": "PERMISSION_DENIED", |
| 220 | + "details": [{ |
| 221 | + "@type": "type.googleapis.com/google.rpc.ErrorInfo", |
| 222 | + "reason": "PERMISSION_DENIED", |
| 223 | + "domain": "googleapis.com", |
| 224 | + "metadata": { |
| 225 | + "service": "webrisk.googleapis.com" |
| 226 | + }}]}}`), // Full Response |
| 227 | + }, |
| 228 | + wantErr: errors.New("webrisk: unexpected server response code: 403, status: PERMISSION_DENIED, message: API Not Enabled"), |
| 229 | + }, |
| 230 | + { |
| 231 | + httpResp: &http.Response{ |
| 232 | + StatusCode: http.StatusServiceUnavailable, |
| 233 | + Body: createBody(""), // Empty body |
| 234 | + }, |
| 235 | + wantErr: errors.New("webrisk: unknown error, response code: 503"), |
| 236 | + }, |
| 237 | + { |
| 238 | + httpResp: &http.Response{ |
| 239 | + StatusCode: http.StatusInternalServerError, |
| 240 | + Body: createBody(`{badjson}`), |
| 241 | + }, |
| 242 | + wantErr: errors.New("webrisk: unknown error, response code: 500"), |
| 243 | + }, |
| 244 | + } |
| 245 | + |
| 246 | + for _, tc := range tests { |
| 247 | + var a netAPI |
| 248 | + gotErr := a.parseError(tc.httpResp) |
| 249 | + |
| 250 | + if cmp.Equal(tc.wantErr, gotErr, cmpopts.EquateErrors()) { |
| 251 | + t.Errorf("parseError(%v) returned error: %v, want error: %v", tc.httpResp, gotErr, tc.wantErr) |
| 252 | + } |
| 253 | + } |
| 254 | +} |
0 commit comments