File: response_test.go

package info (click to toggle)
golang-github-kong-go-kong 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 620 kB
  • sloc: sh: 18; makefile: 4
file content (75 lines) | stat: -rw-r--r-- 1,627 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package kong

import (
	"io/ioutil"
	"net/http"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestHasError(T *testing.T) {
	for _, tt := range []struct {
		name     string
		response http.Response
		want     error
	}{
		{
			name: "code 200",
			response: http.Response{
				StatusCode: 200,
				Body:       ioutil.NopCloser(strings.NewReader("")),
			},
		},
		{
			name: "code 404",
			response: http.Response{
				StatusCode: 404,
				Body:       ioutil.NopCloser(strings.NewReader(`{"message": "potayto pohtato", "some": "other field"}`)),
			},
			want: &APIError{
				httpCode: 404,
				message:  "potayto pohtato",
			},
		},
		{
			name: "code 404, message field missing",
			response: http.Response{
				StatusCode: 404,
				Body:       ioutil.NopCloser(strings.NewReader(`{"nothing": "nothing"}`)),
			},
			want: &APIError{
				httpCode: 404,
				message:  "",
			},
		},
		{
			name: "code 404, empty body",
			response: http.Response{
				StatusCode: 404,
				Body:       ioutil.NopCloser(strings.NewReader(``)),
			},
			want: &APIError{
				httpCode: 404,
				message:  "<failed to parse response body: unexpected end of JSON input>",
			},
		},
		{
			name: "code 404, unparseable json",
			response: http.Response{
				StatusCode: 404,
				Body:       ioutil.NopCloser(strings.NewReader(`This is not json`)),
			},
			want: &APIError{
				httpCode: 404,
				message:  "<failed to parse response body: invalid character 'T' looking for beginning of value>",
			},
		},
	} {
		T.Run(tt.name, func(T *testing.T) {
			got := hasError(&tt.response)
			assert.Equal(T, tt.want, got)
		})
	}
}