github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/edgeworkers/errors_test.go (about)

     1  package edgeworkers
     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.NewRequest(
    20  		http.MethodHead,
    21  		"/",
    22  		nil)
    23  	require.NoError(t, err)
    24  
    25  	tests := map[string]struct {
    26  		response *http.Response
    27  		expected *Error
    28  	}{
    29  		"valid response, status code 500": {
    30  			response: &http.Response{
    31  				Status:     "Internal Server Error",
    32  				StatusCode: http.StatusInternalServerError,
    33  				Body: ioutil.NopCloser(strings.NewReader(
    34  					`{"type":"a","title":"b","detail":"c","status":500}`),
    35  				),
    36  				Request: req,
    37  			},
    38  			expected: &Error{
    39  				Type:   "a",
    40  				Title:  "b",
    41  				Detail: "c",
    42  				Status: http.StatusInternalServerError,
    43  			},
    44  		},
    45  		"invalid response body, assign status code": {
    46  			response: &http.Response{
    47  				Status:     "Internal Server Error",
    48  				StatusCode: http.StatusInternalServerError,
    49  				Body: ioutil.NopCloser(strings.NewReader(
    50  					`test`),
    51  				),
    52  				Request: req,
    53  			},
    54  			expected: &Error{
    55  				Title:  "Failed to unmarshal error body. Edgeworkers API failed. Check details for more information.",
    56  				Detail: "test",
    57  				Status: http.StatusInternalServerError,
    58  			},
    59  		},
    60  	}
    61  	for name, test := range tests {
    62  		t.Run(name, func(t *testing.T) {
    63  			res := Client(sess).(*edgeworkers).Error(test.response)
    64  			assert.Equal(t, test.expected, res)
    65  		})
    66  	}
    67  }
    68  
    69  func TestIs(t *testing.T) {
    70  	tests := map[string]struct {
    71  		err      Error
    72  		target   Error
    73  		expected bool
    74  	}{
    75  		"different error code": {
    76  			err:      Error{Status: 404},
    77  			target:   Error{Status: 401},
    78  			expected: false,
    79  		},
    80  		"same error code": {
    81  			err:      Error{Status: 404},
    82  			target:   Error{Status: 404},
    83  			expected: true,
    84  		},
    85  		"same error code and title": {
    86  			err:      Error{Status: 404, Title: "some error"},
    87  			target:   Error{Status: 404, Title: "some error"},
    88  			expected: true,
    89  		},
    90  		"same error code and different error message": {
    91  			err:      Error{Status: 404, Title: "some error"},
    92  			target:   Error{Status: 404, Title: "other error"},
    93  			expected: false,
    94  		},
    95  	}
    96  
    97  	for name, test := range tests {
    98  		t.Run(name, func(t *testing.T) {
    99  			assert.Equal(t, test.err.Is(&test.target), test.expected)
   100  		})
   101  	}
   102  }
   103  
   104  func TestValidationErrorsParsing(t *testing.T) {
   105  	req, err := http.NewRequestWithContext(
   106  		context.TODO(),
   107  		http.MethodHead,
   108  		"/",
   109  		nil)
   110  	require.NoError(t, err)
   111  	tests := map[string]struct {
   112  		input    *http.Response
   113  		expected *Error
   114  	}{
   115  		"API failure with HTML response": {
   116  			input: &http.Response{
   117  				Request: req,
   118  				Status:  "OK",
   119  				Body:    ioutil.NopCloser(strings.NewReader(`<HTML><HEAD>...</HEAD><BODY>...</BODY></HTML>`))},
   120  			expected: &Error{
   121  				Type:   "",
   122  				Title:  "Failed to unmarshal error body. Edgeworkers API failed. Check details for more information.",
   123  				Detail: "<HTML><HEAD>...</HEAD><BODY>...</BODY></HTML>",
   124  			},
   125  		},
   126  		"API failure with plain text response": {
   127  			input: &http.Response{
   128  				Request: req,
   129  				Status:  "OK",
   130  				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"))},
   131  			expected: &Error{
   132  				Type:   "",
   133  				Title:  "Failed to unmarshal error body. Edgeworkers API failed. Check details for more information.",
   134  				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",
   135  			},
   136  		},
   137  		"API failure with XML response": {
   138  			input: &http.Response{
   139  				Request: req,
   140  				Status:  "OK",
   141  				Body:    ioutil.NopCloser(strings.NewReader(`<Root><Item id="1" name="Example" /></Root>`))},
   142  			expected: &Error{
   143  				Type:   "",
   144  				Title:  "Failed to unmarshal error body. Edgeworkers API failed. Check details for more information.",
   145  				Detail: "<Root><Item id=\"1\" name=\"Example\" /></Root>",
   146  			},
   147  		},
   148  	}
   149  
   150  	for name, test := range tests {
   151  		t.Run(name, func(t *testing.T) {
   152  			sess, _ := session.New()
   153  			e := edgeworkers{
   154  				Session: sess,
   155  			}
   156  			assert.Equal(t, test.expected, e.Error(test.input))
   157  		})
   158  	}
   159  }