File: errors_test.go

package info (click to toggle)
docker-registry 2.8.3%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,148 kB
  • sloc: sh: 331; makefile: 83
file content (140 lines) | stat: -rw-r--r-- 4,692 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package client

import (
	"bytes"
	"io"
	"net/http"
	"strings"
	"testing"
)

type nopCloser struct {
	io.Reader
}

func (nopCloser) Close() error { return nil }

func TestHandleErrorResponse401ValidBody(t *testing.T) {
	json := `{"errors":[{"code":"UNAUTHORIZED","message":"action requires authentication"}]}`
	response := &http.Response{
		Status:     "401 Unauthorized",
		StatusCode: 401,
		Body:       nopCloser{bytes.NewBufferString(json)},
		Header:     http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
	}
	err := HandleErrorResponse(response)

	expectedMsg := "unauthorized: action requires authentication"
	if !strings.Contains(err.Error(), expectedMsg) {
		t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
	}
}

func TestHandleErrorResponse401WithInvalidBody(t *testing.T) {
	json := "{invalid json}"
	response := &http.Response{
		Status:     "401 Unauthorized",
		StatusCode: 401,
		Body:       nopCloser{bytes.NewBufferString(json)},
		Header:     http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
	}
	err := HandleErrorResponse(response)

	expectedMsg := "unauthorized: authentication required"
	if !strings.Contains(err.Error(), expectedMsg) {
		t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
	}
}

func TestHandleErrorResponseExpectedStatusCode400ValidBody(t *testing.T) {
	json := `{"errors":[{"code":"DIGEST_INVALID","message":"provided digest does not match"}]}`
	response := &http.Response{
		Status:     "400 Bad Request",
		StatusCode: 400,
		Body:       nopCloser{bytes.NewBufferString(json)},
		Header:     http.Header{"Content-Type": []string{"application/json"}},
	}
	err := HandleErrorResponse(response)

	expectedMsg := "digest invalid: provided digest does not match"
	if !strings.Contains(err.Error(), expectedMsg) {
		t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
	}
}

func TestHandleErrorResponseExpectedStatusCode404EmptyErrorSlice(t *testing.T) {
	json := `{"randomkey": "randomvalue"}`
	response := &http.Response{
		Status:     "404 Not Found",
		StatusCode: 404,
		Body:       nopCloser{bytes.NewBufferString(json)},
		Header:     http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
	}
	err := HandleErrorResponse(response)

	expectedMsg := `error parsing HTTP 404 response body: no error details found in HTTP response body: "{\"randomkey\": \"randomvalue\"}"`
	if !strings.Contains(err.Error(), expectedMsg) {
		t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
	}
}

func TestHandleErrorResponseExpectedStatusCode404InvalidBody(t *testing.T) {
	json := "{invalid json}"
	response := &http.Response{
		Status:     "404 Not Found",
		StatusCode: 404,
		Body:       nopCloser{bytes.NewBufferString(json)},
		Header:     http.Header{"Content-Type": []string{"application/json"}},
	}
	err := HandleErrorResponse(response)

	expectedMsg := "error parsing HTTP 404 response body: invalid character 'i' looking for beginning of object key string: \"{invalid json}\""
	if !strings.Contains(err.Error(), expectedMsg) {
		t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
	}
}

func TestHandleErrorResponseUnexpectedStatusCode501(t *testing.T) {
	response := &http.Response{
		Status:     "501 Not Implemented",
		StatusCode: 501,
		Body:       nopCloser{bytes.NewBufferString("{\"Error Encountered\" : \"Function not implemented.\"}")},
		Header:     http.Header{"Content-Type": []string{"application/json"}},
	}
	err := HandleErrorResponse(response)

	expectedMsg := "received unexpected HTTP status: 501 Not Implemented"
	if !strings.Contains(err.Error(), expectedMsg) {
		t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
	}
}

func TestHandleErrorResponseInsufficientPrivileges403(t *testing.T) {
	json := `{"details":"requesting higher privileges than access token allows"}`
	response := &http.Response{
		Status:     "403 Forbidden",
		StatusCode: 403,
		Body:       nopCloser{bytes.NewBufferString(json)},
		Header:     http.Header{"Content-Type": []string{"application/json"}},
	}
	err := HandleErrorResponse(response)

	expectedMsg := "denied: requesting higher privileges than access token allows"
	if !strings.Contains(err.Error(), expectedMsg) {
		t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
	}
}

func TestHandleErrorResponseNonJson(t *testing.T) {
	msg := `{"details":"requesting higher privileges than access token allows"}`
	response := &http.Response{
		Status:     "403 Forbidden",
		StatusCode: 403,
		Body:       nopCloser{bytes.NewBufferString(msg)},
	}
	err := HandleErrorResponse(response)

	if !strings.Contains(err.Error(), msg) {
		t.Errorf("Expected %q, got: %q", msg, err.Error())
	}
}