Debian Codegraph @

fastly/errors_test.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "bytes"
5 "io/ioutil"
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: ioutil.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%s\n\n to be \n\n%s\n\n", e.Error(), expected)
36 }
37 if e.String() != expected {
38 t.Errorf("expected \n\n%s\n\n to be \n\n%s\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": []string{jsonapi.MediaType}}),
50 Body: ioutil.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%s\n\n to be \n\n%s\n\n", e.Error(), expected)
68 }
69 if e.String() != expected {
70 t.Errorf("expected \n\n%s\n\n to be \n\n%s\n\n", e.String(), expected)
71 }
72
73 if !e.IsNotFound() {
74 t.Error("not not found")
75 }
76 })
77 }