2015-12-30 00:21:03 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type nopCloser struct {
|
|
|
|
io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nopCloser) Close() error { return nil }
|
|
|
|
|
2023-09-08 12:33:46 +00:00
|
|
|
func TestHandleHTTPResponseError200ValidBody(t *testing.T) {
|
|
|
|
response := &http.Response{
|
|
|
|
Status: "200 OK",
|
|
|
|
StatusCode: 200,
|
|
|
|
}
|
|
|
|
err := HandleHTTPResponseError(response)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no error, got: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleHTTPResponseError401ValidBody(t *testing.T) {
|
2023-08-24 07:08:04 +00:00
|
|
|
json := `{"errors":[{"code":"UNAUTHORIZED","message":"action requires authentication"}]}`
|
2015-12-30 00:21:03 +00:00
|
|
|
response := &http.Response{
|
|
|
|
Status: "401 Unauthorized",
|
|
|
|
StatusCode: 401,
|
|
|
|
Body: nopCloser{bytes.NewBufferString(json)},
|
2023-08-24 07:08:04 +00:00
|
|
|
Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
|
2015-12-30 00:21:03 +00:00
|
|
|
}
|
2023-09-08 12:33:46 +00:00
|
|
|
err := HandleHTTPResponseError(response)
|
2015-12-30 00:21:03 +00:00
|
|
|
|
|
|
|
expectedMsg := "unauthorized: action requires authentication"
|
|
|
|
if !strings.Contains(err.Error(), expectedMsg) {
|
2023-08-24 07:08:04 +00:00
|
|
|
t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
|
2015-12-30 00:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:33:46 +00:00
|
|
|
func TestHandleHTTPResponseError401WithInvalidBody(t *testing.T) {
|
2015-12-30 00:21:03 +00:00
|
|
|
json := "{invalid json}"
|
|
|
|
response := &http.Response{
|
|
|
|
Status: "401 Unauthorized",
|
|
|
|
StatusCode: 401,
|
|
|
|
Body: nopCloser{bytes.NewBufferString(json)},
|
2023-08-24 07:08:04 +00:00
|
|
|
Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
|
2015-12-30 00:21:03 +00:00
|
|
|
}
|
2023-09-08 12:33:46 +00:00
|
|
|
err := HandleHTTPResponseError(response)
|
2015-12-30 00:21:03 +00:00
|
|
|
|
|
|
|
expectedMsg := "unauthorized: authentication required"
|
|
|
|
if !strings.Contains(err.Error(), expectedMsg) {
|
2023-08-24 07:08:04 +00:00
|
|
|
t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
|
2015-12-30 00:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 10:44:24 +00:00
|
|
|
func TestHandleHTTPResponseError401WithNoBody(t *testing.T) {
|
|
|
|
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 := HandleHTTPResponseError(response)
|
|
|
|
|
|
|
|
expectedMsg := "unauthorized: "
|
|
|
|
if !strings.Contains(err.Error(), expectedMsg) {
|
|
|
|
t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:33:46 +00:00
|
|
|
func TestHandleHTTPResponseErrorExpectedStatusCode400ValidBody(t *testing.T) {
|
2023-08-24 07:08:04 +00:00
|
|
|
json := `{"errors":[{"code":"DIGEST_INVALID","message":"provided digest does not match"}]}`
|
2015-12-30 00:21:03 +00:00
|
|
|
response := &http.Response{
|
|
|
|
Status: "400 Bad Request",
|
|
|
|
StatusCode: 400,
|
|
|
|
Body: nopCloser{bytes.NewBufferString(json)},
|
2023-08-24 07:08:04 +00:00
|
|
|
Header: http.Header{"Content-Type": []string{"application/json"}},
|
2015-12-30 00:21:03 +00:00
|
|
|
}
|
2023-09-08 12:33:46 +00:00
|
|
|
err := HandleHTTPResponseError(response)
|
2015-12-30 00:21:03 +00:00
|
|
|
|
|
|
|
expectedMsg := "digest invalid: provided digest does not match"
|
|
|
|
if !strings.Contains(err.Error(), expectedMsg) {
|
2023-08-24 07:08:04 +00:00
|
|
|
t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
|
2015-12-30 00:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:33:46 +00:00
|
|
|
func TestHandleHTTPResponseErrorExpectedStatusCode404EmptyErrorSlice(t *testing.T) {
|
2016-03-14 17:06:30 +00:00
|
|
|
json := `{"randomkey": "randomvalue"}`
|
|
|
|
response := &http.Response{
|
|
|
|
Status: "404 Not Found",
|
|
|
|
StatusCode: 404,
|
|
|
|
Body: nopCloser{bytes.NewBufferString(json)},
|
2023-08-24 07:08:04 +00:00
|
|
|
Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
|
2016-03-14 17:06:30 +00:00
|
|
|
}
|
2023-09-08 12:33:46 +00:00
|
|
|
err := HandleHTTPResponseError(response)
|
2016-03-14 17:06:30 +00:00
|
|
|
|
2016-03-15 16:03:56 +00:00
|
|
|
expectedMsg := `error parsing HTTP 404 response body: no error details found in HTTP response body: "{\"randomkey\": \"randomvalue\"}"`
|
2016-03-14 17:06:30 +00:00
|
|
|
if !strings.Contains(err.Error(), expectedMsg) {
|
2023-08-24 07:08:04 +00:00
|
|
|
t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
|
2016-03-14 17:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:33:46 +00:00
|
|
|
func TestHandleHTTPResponseErrorExpectedStatusCode404InvalidBody(t *testing.T) {
|
2015-12-30 00:21:03 +00:00
|
|
|
json := "{invalid json}"
|
|
|
|
response := &http.Response{
|
|
|
|
Status: "404 Not Found",
|
|
|
|
StatusCode: 404,
|
|
|
|
Body: nopCloser{bytes.NewBufferString(json)},
|
2023-08-24 07:08:04 +00:00
|
|
|
Header: http.Header{"Content-Type": []string{"application/json"}},
|
2015-12-30 00:21:03 +00:00
|
|
|
}
|
2023-09-08 12:33:46 +00:00
|
|
|
err := HandleHTTPResponseError(response)
|
2015-12-30 00:21:03 +00:00
|
|
|
|
2016-03-15 16:03:56 +00:00
|
|
|
expectedMsg := "error parsing HTTP 404 response body: invalid character 'i' looking for beginning of object key string: \"{invalid json}\""
|
2015-12-30 00:21:03 +00:00
|
|
|
if !strings.Contains(err.Error(), expectedMsg) {
|
2023-08-24 07:08:04 +00:00
|
|
|
t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
|
2015-12-30 00:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:33:46 +00:00
|
|
|
func TestHandleHTTPResponseErrorUnexpectedStatusCode501(t *testing.T) {
|
2015-12-30 00:21:03 +00:00
|
|
|
response := &http.Response{
|
|
|
|
Status: "501 Not Implemented",
|
|
|
|
StatusCode: 501,
|
|
|
|
Body: nopCloser{bytes.NewBufferString("{\"Error Encountered\" : \"Function not implemented.\"}")},
|
2023-08-24 07:08:04 +00:00
|
|
|
Header: http.Header{"Content-Type": []string{"application/json"}},
|
2015-12-30 00:21:03 +00:00
|
|
|
}
|
2023-09-08 12:33:46 +00:00
|
|
|
err := HandleHTTPResponseError(response)
|
2015-12-30 00:21:03 +00:00
|
|
|
|
2016-03-14 17:06:30 +00:00
|
|
|
expectedMsg := "received unexpected HTTP status: 501 Not Implemented"
|
2015-12-30 00:21:03 +00:00
|
|
|
if !strings.Contains(err.Error(), expectedMsg) {
|
2023-08-24 07:08:04 +00:00
|
|
|
t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
|
2015-12-30 00:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-27 17:31:50 +00:00
|
|
|
|
2023-09-08 12:33:46 +00:00
|
|
|
func TestHandleHTTPResponseErrorInsufficientPrivileges403(t *testing.T) {
|
2023-04-27 17:31:50 +00:00
|
|
|
json := `{"details":"requesting higher privileges than access token allows"}`
|
|
|
|
response := &http.Response{
|
|
|
|
Status: "403 Forbidden",
|
|
|
|
StatusCode: 403,
|
|
|
|
Body: nopCloser{bytes.NewBufferString(json)},
|
2023-08-24 07:08:04 +00:00
|
|
|
Header: http.Header{"Content-Type": []string{"application/json"}},
|
2023-04-27 17:31:50 +00:00
|
|
|
}
|
2023-09-08 12:33:46 +00:00
|
|
|
err := HandleHTTPResponseError(response)
|
2023-04-27 17:31:50 +00:00
|
|
|
|
|
|
|
expectedMsg := "denied: requesting higher privileges than access token allows"
|
|
|
|
if !strings.Contains(err.Error(), expectedMsg) {
|
2023-08-24 07:08:04 +00:00
|
|
|
t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:33:46 +00:00
|
|
|
func TestHandleHTTPResponseErrorNonJson(t *testing.T) {
|
2023-08-24 07:08:04 +00:00
|
|
|
msg := `{"details":"requesting higher privileges than access token allows"}`
|
|
|
|
response := &http.Response{
|
|
|
|
Status: "403 Forbidden",
|
|
|
|
StatusCode: 403,
|
|
|
|
Body: nopCloser{bytes.NewBufferString(msg)},
|
|
|
|
}
|
2023-09-08 12:33:46 +00:00
|
|
|
err := HandleHTTPResponseError(response)
|
2023-08-24 07:08:04 +00:00
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), msg) {
|
|
|
|
t.Errorf("Expected %q, got: %q", msg, err.Error())
|
2023-04-27 17:31:50 +00:00
|
|
|
}
|
|
|
|
}
|