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

     1  package datastream
     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/assert"
    12  	"github.com/stretchr/testify/require"
    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. DataStream2 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).(*ds).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  				StatusCode: http.StatusServiceUnavailable,
    86  				Body:       ioutil.NopCloser(strings.NewReader(`<HTML><HEAD>...</HEAD><BODY>...</BODY></HTML>`))},
    87  			expected: &Error{
    88  				Type:       "",
    89  				Title:      "Failed to unmarshal error body. DataStream2 API failed. Check details for more information.",
    90  				Detail:     "<HTML><HEAD>...</HEAD><BODY>...</BODY></HTML>",
    91  				StatusCode: http.StatusServiceUnavailable,
    92  			},
    93  		},
    94  		"API failure with plain text response": {
    95  			input: &http.Response{
    96  				Request:    req,
    97  				Status:     "OK",
    98  				StatusCode: http.StatusServiceUnavailable,
    99  				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"))},
   100  			expected: &Error{
   101  				Type:       "",
   102  				Title:      "Failed to unmarshal error body. DataStream2 API failed. Check details for more information.",
   103  				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",
   104  				StatusCode: http.StatusServiceUnavailable,
   105  			},
   106  		},
   107  		"API failure with XML response": {
   108  			input: &http.Response{
   109  				Request:    req,
   110  				Status:     "OK",
   111  				StatusCode: http.StatusServiceUnavailable,
   112  				Body:       ioutil.NopCloser(strings.NewReader(`<Root><Item id="1" name="Example" /></Root>`))},
   113  			expected: &Error{
   114  				Type:       "",
   115  				Title:      "Failed to unmarshal error body. DataStream2 API failed. Check details for more information.",
   116  				Detail:     "<Root><Item id=\"1\" name=\"Example\" /></Root>",
   117  				StatusCode: http.StatusServiceUnavailable,
   118  			},
   119  		},
   120  	}
   121  
   122  	for name, test := range tests {
   123  		t.Run(name, func(t *testing.T) {
   124  			sess, _ := session.New()
   125  			d := ds{
   126  				Session: sess,
   127  			}
   128  			assert.Equal(t, test.expected, d.Error(test.input))
   129  		})
   130  	}
   131  }