github.com/aiven/aiven-go-client@v1.36.0/api_test.go (about) 1 package aiven 2 3 import "testing" 4 5 func Test_checkAPIResponse(t *testing.T) { 6 type args struct { 7 bts []byte 8 r Response 9 } 10 tests := []struct { 11 name string 12 args args 13 wantErr bool 14 }{ 15 { 16 "invalid-json", 17 args{ 18 bts: []byte(`Invalid JSON`), 19 r: nil, 20 }, 21 true, 22 }, 23 { 24 "error-response", 25 args{ 26 bts: []byte(`{ 27 "message": "Authentication failed", 28 "errors": [ 29 { 30 "status": "403", 31 "message": "Authentication failed" 32 } 33 ] 34 }`), 35 r: nil, 36 }, 37 true, 38 }, 39 { 40 "error-response", 41 args{ 42 bts: []byte(`{ 43 "message": "", 44 "errors": [ 45 ] 46 }`), 47 r: nil, 48 }, 49 false, 50 }, 51 } 52 for _, tt := range tests { 53 t.Run(tt.name, func(t *testing.T) { 54 if err := checkAPIResponse(tt.args.bts, tt.args.r); (err != nil) != tt.wantErr { 55 t.Errorf("checkAPIResponse() error = %v, wantErr %v", err, tt.wantErr) 56 } 57 }) 58 } 59 } 60 61 func TestAPIResponse_GetError(t *testing.T) { 62 type fields struct { 63 Errors []Error 64 Message string 65 } 66 tests := []struct { 67 name string 68 fields fields 69 wantErr bool 70 }{ 71 { 72 "empty", 73 fields{ 74 Errors: nil, 75 Message: "", 76 }, 77 false, 78 }, 79 { 80 "has-error", 81 fields{ 82 Errors: []Error{ 83 { 84 Message: "error-message", 85 MoreInfo: "some-info", 86 Status: 500, 87 }, 88 }, 89 Message: "error-message", 90 }, 91 true, 92 }, 93 } 94 for _, tt := range tests { 95 t.Run(tt.name, func(t *testing.T) { 96 r := APIResponse{ 97 Errors: tt.fields.Errors, 98 Message: tt.fields.Message, 99 } 100 if err := r.GetError(); (err != nil) != tt.wantErr { 101 t.Errorf("GetError() error = %v, wantErr %v", err, tt.wantErr) 102 } 103 }) 104 } 105 }