github.com/fastly/go-fastly/v6@v6.8.0/fastly/errors_test.go (about)

     1  package fastly
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"net/http"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/google/jsonapi"
    11  )
    12  
    13  func TestNewHTTPError(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	t.Run("legacy", func(t *testing.T) {
    17  		resp := &http.Response{
    18  			StatusCode: 404,
    19  			Body: io.NopCloser(bytes.NewBufferString(
    20  				`{"msg": "hello", "detail": "nope"}`)),
    21  		}
    22  		e := NewHTTPError(resp)
    23  
    24  		if e.StatusCode != 404 {
    25  			t.Errorf("bad status code: %d", e.StatusCode)
    26  		}
    27  
    28  		expected := strings.TrimSpace(`
    29  404 - Not Found:
    30  
    31      Title:  hello
    32      Detail: nope
    33  `)
    34  		if e.Error() != expected {
    35  			t.Errorf("expected \n\n%q\n\n to be \n\n%q\n\n", e.Error(), expected)
    36  		}
    37  		if e.String() != expected {
    38  			t.Errorf("expected \n\n%q\n\n to be \n\n%q\n\n", e.String(), expected)
    39  		}
    40  
    41  		if !e.IsNotFound() {
    42  			t.Error("not not found")
    43  		}
    44  	})
    45  
    46  	t.Run("jsonapi", func(t *testing.T) {
    47  		resp := &http.Response{
    48  			StatusCode: 404,
    49  			Header:     http.Header(map[string][]string{"Content-Type": {jsonapi.MediaType}}),
    50  			Body: io.NopCloser(bytes.NewBufferString(
    51  				`{"errors":[{"id":"abc123", "title":"Not found", "detail":"That resource does not exist"}]}`)),
    52  		}
    53  		e := NewHTTPError(resp)
    54  
    55  		if e.StatusCode != 404 {
    56  			t.Errorf("expected %d to be %d", e.StatusCode, 404)
    57  		}
    58  
    59  		expected := strings.TrimSpace(`
    60  404 - Not Found:
    61  
    62      ID:     abc123
    63      Title:  Not found
    64      Detail: That resource does not exist
    65  `)
    66  		if e.Error() != expected {
    67  			t.Errorf("expected \n\n%q\n\n to be \n\n%q\n\n", e.Error(), expected)
    68  		}
    69  		if e.String() != expected {
    70  			t.Errorf("expected \n\n%q\n\n to be \n\n%q\n\n", e.String(), expected)
    71  		}
    72  
    73  		if !e.IsNotFound() {
    74  			t.Error("not not found")
    75  		}
    76  	})
    77  
    78  	t.Run("problem detail", func(t *testing.T) {
    79  		resp := &http.Response{
    80  			StatusCode: 404,
    81  			Header:     http.Header(map[string][]string{"Content-Type": {"application/problem+json"}}),
    82  			Body: io.NopCloser(bytes.NewBufferString(
    83  				`{"title": "Error", "detail": "this was an error", "status": 404}`,
    84  			)),
    85  		}
    86  		e := NewHTTPError(resp)
    87  
    88  		if e.StatusCode != 404 {
    89  			t.Errorf("expected %d to be %d", e.StatusCode, 404)
    90  		}
    91  
    92  		expected := strings.TrimSpace(`
    93  404 - Not Found:
    94  
    95      Title:  Error
    96      Detail: this was an error
    97  `)
    98  		if e.Error() != expected {
    99  			t.Errorf("expected \n\n%q\n\n to be \n\n%q\n\n", e.Error(), expected)
   100  		}
   101  		if e.String() != expected {
   102  			t.Errorf("expected \n\n%q\n\n to be \n\n%q\n\n", e.String(), expected)
   103  		}
   104  
   105  		if !e.IsNotFound() {
   106  			t.Error("not not found")
   107  		}
   108  	})
   109  }