...

Source file src/github.com/docker/distribution/registry/client/errors_test.go

Documentation: github.com/docker/distribution/registry/client

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"net/http"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  type nopCloser struct {
    12  	io.Reader
    13  }
    14  
    15  func (nopCloser) Close() error { return nil }
    16  
    17  func TestHandleErrorResponse401ValidBody(t *testing.T) {
    18  	json := `{"errors":[{"code":"UNAUTHORIZED","message":"action requires authentication"}]}`
    19  	response := &http.Response{
    20  		Status:     "401 Unauthorized",
    21  		StatusCode: 401,
    22  		Body:       nopCloser{bytes.NewBufferString(json)},
    23  		Header:     http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
    24  	}
    25  	err := HandleErrorResponse(response)
    26  
    27  	expectedMsg := "unauthorized: action requires authentication"
    28  	if !strings.Contains(err.Error(), expectedMsg) {
    29  		t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
    30  	}
    31  }
    32  
    33  func TestHandleErrorResponse401WithInvalidBody(t *testing.T) {
    34  	json := "{invalid json}"
    35  	response := &http.Response{
    36  		Status:     "401 Unauthorized",
    37  		StatusCode: 401,
    38  		Body:       nopCloser{bytes.NewBufferString(json)},
    39  		Header:     http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
    40  	}
    41  	err := HandleErrorResponse(response)
    42  
    43  	expectedMsg := "unauthorized: authentication required"
    44  	if !strings.Contains(err.Error(), expectedMsg) {
    45  		t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
    46  	}
    47  }
    48  
    49  func TestHandleErrorResponseExpectedStatusCode400ValidBody(t *testing.T) {
    50  	json := `{"errors":[{"code":"DIGEST_INVALID","message":"provided digest does not match"}]}`
    51  	response := &http.Response{
    52  		Status:     "400 Bad Request",
    53  		StatusCode: 400,
    54  		Body:       nopCloser{bytes.NewBufferString(json)},
    55  		Header:     http.Header{"Content-Type": []string{"application/json"}},
    56  	}
    57  	err := HandleErrorResponse(response)
    58  
    59  	expectedMsg := "digest invalid: provided digest does not match"
    60  	if !strings.Contains(err.Error(), expectedMsg) {
    61  		t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
    62  	}
    63  }
    64  
    65  func TestHandleErrorResponseExpectedStatusCode404EmptyErrorSlice(t *testing.T) {
    66  	json := `{"randomkey": "randomvalue"}`
    67  	response := &http.Response{
    68  		Status:     "404 Not Found",
    69  		StatusCode: 404,
    70  		Body:       nopCloser{bytes.NewBufferString(json)},
    71  		Header:     http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
    72  	}
    73  	err := HandleErrorResponse(response)
    74  
    75  	expectedMsg := `error parsing HTTP 404 response body: no error details found in HTTP response body: "{\"randomkey\": \"randomvalue\"}"`
    76  	if !strings.Contains(err.Error(), expectedMsg) {
    77  		t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
    78  	}
    79  }
    80  
    81  func TestHandleErrorResponseExpectedStatusCode404InvalidBody(t *testing.T) {
    82  	json := "{invalid json}"
    83  	response := &http.Response{
    84  		Status:     "404 Not Found",
    85  		StatusCode: 404,
    86  		Body:       nopCloser{bytes.NewBufferString(json)},
    87  		Header:     http.Header{"Content-Type": []string{"application/json"}},
    88  	}
    89  	err := HandleErrorResponse(response)
    90  
    91  	expectedMsg := "error parsing HTTP 404 response body: invalid character 'i' looking for beginning of object key string: \"{invalid json}\""
    92  	if !strings.Contains(err.Error(), expectedMsg) {
    93  		t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
    94  	}
    95  }
    96  
    97  func TestHandleErrorResponseUnexpectedStatusCode501(t *testing.T) {
    98  	response := &http.Response{
    99  		Status:     "501 Not Implemented",
   100  		StatusCode: 501,
   101  		Body:       nopCloser{bytes.NewBufferString("{\"Error Encountered\" : \"Function not implemented.\"}")},
   102  		Header:     http.Header{"Content-Type": []string{"application/json"}},
   103  	}
   104  	err := HandleErrorResponse(response)
   105  
   106  	expectedMsg := "received unexpected HTTP status: 501 Not Implemented"
   107  	if !strings.Contains(err.Error(), expectedMsg) {
   108  		t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
   109  	}
   110  }
   111  
   112  func TestHandleErrorResponseInsufficientPrivileges403(t *testing.T) {
   113  	json := `{"details":"requesting higher privileges than access token allows"}`
   114  	response := &http.Response{
   115  		Status:     "403 Forbidden",
   116  		StatusCode: 403,
   117  		Body:       nopCloser{bytes.NewBufferString(json)},
   118  		Header:     http.Header{"Content-Type": []string{"application/json"}},
   119  	}
   120  	err := HandleErrorResponse(response)
   121  
   122  	expectedMsg := "denied: requesting higher privileges than access token allows"
   123  	if !strings.Contains(err.Error(), expectedMsg) {
   124  		t.Errorf("Expected %q, got: %q", expectedMsg, err.Error())
   125  	}
   126  }
   127  
   128  func TestHandleErrorResponseNonJson(t *testing.T) {
   129  	msg := `{"details":"requesting higher privileges than access token allows"}`
   130  	response := &http.Response{
   131  		Status:     "403 Forbidden",
   132  		StatusCode: 403,
   133  		Body:       nopCloser{bytes.NewBufferString(msg)},
   134  	}
   135  	err := HandleErrorResponse(response)
   136  
   137  	if !strings.Contains(err.Error(), msg) {
   138  		t.Errorf("Expected %q, got: %q", msg, err.Error())
   139  	}
   140  }
   141  

View as plain text