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

     1  package gtm
     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  
    13  	"github.com/tj/assert"
    14  )
    15  
    16  func TestJSONErrorUnmarshalling(t *testing.T) {
    17  	req, err := http.NewRequestWithContext(
    18  		context.TODO(),
    19  		http.MethodHead,
    20  		"/",
    21  		nil)
    22  	require.NoError(t, err)
    23  	tests := map[string]struct {
    24  		input    *http.Response
    25  		expected *Error
    26  	}{
    27  		"API failure with HTML response": {
    28  			input: &http.Response{
    29  				Request:    req,
    30  				StatusCode: http.StatusServiceUnavailable,
    31  				Body:       ioutil.NopCloser(strings.NewReader(`<HTML><HEAD>...</HEAD><BODY>...</BODY></HTML>`))},
    32  			expected: &Error{
    33  				Type:       "",
    34  				Title:      "Failed to unmarshal error body. GTM API failed. Check details for more information.",
    35  				Detail:     "<HTML><HEAD>...</HEAD><BODY>...</BODY></HTML>",
    36  				StatusCode: http.StatusServiceUnavailable,
    37  			},
    38  		},
    39  		"API failure with plain text response": {
    40  			input: &http.Response{
    41  				Request:    req,
    42  				StatusCode: http.StatusServiceUnavailable,
    43  				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"))},
    44  			expected: &Error{
    45  				Type:       "",
    46  				Title:      "Failed to unmarshal error body. GTM API failed. Check details for more information.",
    47  				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",
    48  				StatusCode: http.StatusServiceUnavailable,
    49  			},
    50  		},
    51  		"API failure with XML response": {
    52  			input: &http.Response{
    53  				Request:    req,
    54  				StatusCode: http.StatusServiceUnavailable,
    55  				Body:       ioutil.NopCloser(strings.NewReader(`<Root><Item id="1" name="Example" /></Root>`))},
    56  			expected: &Error{
    57  				Type:       "",
    58  				Title:      "Failed to unmarshal error body. GTM API failed. Check details for more information.",
    59  				Detail:     "<Root><Item id=\"1\" name=\"Example\" /></Root>",
    60  				StatusCode: http.StatusServiceUnavailable,
    61  			},
    62  		},
    63  		"API failure nested error": {
    64  			input: &http.Response{
    65  				Request:    req,
    66  				StatusCode: http.StatusBadRequest,
    67  				Body: ioutil.NopCloser(strings.NewReader(`
    68  {
    69   	"type": "https://problems.luna.akamaiapis.net/config-gtm/v1/propertyValidationFailed",
    70   	"title": "Property Validation Failure",
    71   	"detail": "",
    72   	"instance": "https://akaa-ouijhfns55qwgfuc-knsod5nrjl2w2gmt.luna-dev.akamaiapis.net/config-gtm-api/v1/domains/ddzh-test-1.akadns.net/properties/property_test#d290ddf7-53da-4509-be5a-ba582614f883",
    73   	"errors": [
    74   		{
    75   			"type": "https://problems.luna.akamaiapis.net/config-gtm/v1/propertyValidationError",
    76   			"title": "Property Validation Error",
    77   			"detail": "In Property \"property_test\", there are no enabled traffic targets that have any traffic allowed to go to them",
    78   			"errors": null
    79   		}
    80   	]
    81  }`))},
    82  			expected: &Error{
    83  				Type:       "https://problems.luna.akamaiapis.net/config-gtm/v1/propertyValidationFailed",
    84  				Title:      "Property Validation Failure",
    85  				Detail:     "",
    86  				StatusCode: http.StatusBadRequest,
    87  				Instance:   "https://akaa-ouijhfns55qwgfuc-knsod5nrjl2w2gmt.luna-dev.akamaiapis.net/config-gtm-api/v1/domains/ddzh-test-1.akadns.net/properties/property_test#d290ddf7-53da-4509-be5a-ba582614f883",
    88  				Errors: []Error{
    89  					{
    90  						Type:   "https://problems.luna.akamaiapis.net/config-gtm/v1/propertyValidationError",
    91  						Title:  "Property Validation Error",
    92  						Detail: "In Property \"property_test\", there are no enabled traffic targets that have any traffic allowed to go to them",
    93  						Errors: nil,
    94  					},
    95  				},
    96  			},
    97  		},
    98  	}
    99  
   100  	for name, test := range tests {
   101  		t.Run(name, func(t *testing.T) {
   102  			sess, _ := session.New()
   103  			g := gtm{
   104  				Session: sess,
   105  			}
   106  			assert.Equal(t, test.expected, g.Error(test.input))
   107  		})
   108  	}
   109  }