github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/errors_test.go (about) 1 package appsec 2 3 import ( 4 "context" 5 "io/ioutil" 6 "net/http" 7 "strings" 8 "testing" 9 10 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/session" 11 "github.com/stretchr/testify/require" 12 "github.com/tj/assert" 13 ) 14 15 func TestNewError(t *testing.T) { 16 sess, err := session.New() 17 require.NoError(t, err) 18 19 req, err := http.NewRequestWithContext( 20 context.TODO(), 21 http.MethodHead, 22 "/", 23 nil) 24 require.NoError(t, err) 25 26 tests := map[string]struct { 27 response *http.Response 28 expected *Error 29 }{ 30 "valid response, status code 500": { 31 response: &http.Response{ 32 Status: "Internal Server Error", 33 StatusCode: http.StatusInternalServerError, 34 Body: ioutil.NopCloser(strings.NewReader( 35 `{"type":"a","title":"b","detail":"c"}`), 36 ), 37 Request: req, 38 }, 39 expected: &Error{ 40 Type: "a", 41 Title: "b", 42 Detail: "c", 43 StatusCode: http.StatusInternalServerError, 44 }, 45 }, 46 "invalid response body, assign status code": { 47 response: &http.Response{ 48 Status: "Internal Server Error", 49 StatusCode: http.StatusInternalServerError, 50 Body: ioutil.NopCloser(strings.NewReader( 51 `test`), 52 ), 53 Request: req, 54 }, 55 expected: &Error{ 56 Title: "Failed to unmarshal error body. Application Security API failed. Check details for more information.", 57 Detail: "test", 58 StatusCode: http.StatusInternalServerError, 59 }, 60 }, 61 } 62 for name, test := range tests { 63 t.Run(name, func(t *testing.T) { 64 res := Client(sess).(*appsec).Error(test.response) 65 assert.Equal(t, test.expected, res) 66 }) 67 } 68 } 69 70 func TestJsonErrorUnmarshalling(t *testing.T) { 71 req, err := http.NewRequestWithContext( 72 context.TODO(), 73 http.MethodHead, 74 "/", 75 nil) 76 require.NoError(t, err) 77 tests := map[string]struct { 78 input *http.Response 79 expected *Error 80 }{ 81 "API failure with HTML response": { 82 input: &http.Response{ 83 Request: req, 84 Status: "OK", 85 Body: ioutil.NopCloser(strings.NewReader(`<HTML><HEAD>...</HEAD><BODY>...</BODY></HTML>`))}, 86 expected: &Error{ 87 Type: "", 88 Title: "Failed to unmarshal error body. Application Security API failed. Check details for more information.", 89 Detail: "<HTML><HEAD>...</HEAD><BODY>...</BODY></HTML>", 90 }, 91 }, 92 "API failure with plain text response": { 93 input: &http.Response{ 94 Request: req, 95 Status: "OK", 96 Body: ioutil.NopCloser(strings.NewReader("Your request did not succeed as this operation has reached the limit for your account. Please try after 2024-01-16T15:20:55.945Z"))}, 97 expected: &Error{ 98 Type: "", 99 Title: "Failed to unmarshal error body. Application Security API failed. Check details for more information.", 100 Detail: "Your request did not succeed as this operation has reached the limit for your account. Please try after 2024-01-16T15:20:55.945Z", 101 }, 102 }, 103 "API failure with XML response": { 104 input: &http.Response{ 105 Request: req, 106 Status: "OK", 107 Body: ioutil.NopCloser(strings.NewReader(`<Root><Item id="1" name="Example" /></Root>`))}, 108 expected: &Error{ 109 Type: "", 110 Title: "Failed to unmarshal error body. Application Security API failed. Check details for more information.", 111 Detail: "<Root><Item id=\"1\" name=\"Example\" /></Root>", 112 }, 113 }, 114 } 115 116 for name, test := range tests { 117 t.Run(name, func(t *testing.T) { 118 sess, _ := session.New() 119 as := appsec{ 120 Session: sess, 121 } 122 assert.Equal(t, test.expected, as.Error(test.input)) 123 }) 124 } 125 }