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

     1  package dns
     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  				Status:     "OK",
    31  				StatusCode: http.StatusServiceUnavailable,
    32  				Body:       ioutil.NopCloser(strings.NewReader(`<HTML><HEAD>...</HEAD><BODY>...</BODY></HTML>`))},
    33  			expected: &Error{
    34  				Type:       "",
    35  				Title:      "Failed to unmarshal error body. DNS API failed. Check details for more information.",
    36  				Detail:     "<HTML><HEAD>...</HEAD><BODY>...</BODY></HTML>",
    37  				StatusCode: http.StatusServiceUnavailable,
    38  			},
    39  		},
    40  		"API failure with plain text response": {
    41  			input: &http.Response{
    42  				Request:    req,
    43  				Status:     "OK",
    44  				StatusCode: http.StatusServiceUnavailable,
    45  				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"))},
    46  			expected: &Error{
    47  				Type:       "",
    48  				Title:      "Failed to unmarshal error body. DNS API failed. Check details for more information.",
    49  				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",
    50  				StatusCode: http.StatusServiceUnavailable,
    51  			},
    52  		},
    53  		"API failure with XML response": {
    54  			input: &http.Response{
    55  				Request:    req,
    56  				Status:     "OK",
    57  				StatusCode: http.StatusServiceUnavailable,
    58  				Body:       ioutil.NopCloser(strings.NewReader(`<Root><Item id="1" name="Example" /></Root>`))},
    59  			expected: &Error{
    60  				Type:       "",
    61  				Title:      "Failed to unmarshal error body. DNS API failed. Check details for more information.",
    62  				Detail:     "<Root><Item id=\"1\" name=\"Example\" /></Root>",
    63  				StatusCode: http.StatusServiceUnavailable,
    64  			},
    65  		},
    66  	}
    67  
    68  	for name, test := range tests {
    69  		t.Run(name, func(t *testing.T) {
    70  			sess, _ := session.New()
    71  			d := dns{
    72  				Session: sess,
    73  			}
    74  			assert.Equal(t, test.expected, d.Error(test.input))
    75  		})
    76  	}
    77  }