github.com/mika/distribution@v2.2.2-0.20160108133430-a75790e3d8e0+incompatible/registry/client/errors_test.go (about)

     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  	}
    24  	err := HandleErrorResponse(response)
    25  
    26  	expectedMsg := "unauthorized: action requires authentication"
    27  	if !strings.Contains(err.Error(), expectedMsg) {
    28  		t.Errorf("Expected \"%s\", got: \"%s\"", expectedMsg, err.Error())
    29  	}
    30  }
    31  
    32  func TestHandleErrorResponse401WithInvalidBody(t *testing.T) {
    33  	json := "{invalid json}"
    34  	response := &http.Response{
    35  		Status:     "401 Unauthorized",
    36  		StatusCode: 401,
    37  		Body:       nopCloser{bytes.NewBufferString(json)},
    38  	}
    39  	err := HandleErrorResponse(response)
    40  
    41  	expectedMsg := "unauthorized: authentication required"
    42  	if !strings.Contains(err.Error(), expectedMsg) {
    43  		t.Errorf("Expected \"%s\", got: \"%s\"", expectedMsg, err.Error())
    44  	}
    45  }
    46  
    47  func TestHandleErrorResponseExpectedStatusCode400ValidBody(t *testing.T) {
    48  	json := "{\"errors\":[{\"code\":\"DIGEST_INVALID\",\"message\":\"provided digest does not match\"}]}"
    49  	response := &http.Response{
    50  		Status:     "400 Bad Request",
    51  		StatusCode: 400,
    52  		Body:       nopCloser{bytes.NewBufferString(json)},
    53  	}
    54  	err := HandleErrorResponse(response)
    55  
    56  	expectedMsg := "digest invalid: provided digest does not match"
    57  	if !strings.Contains(err.Error(), expectedMsg) {
    58  		t.Errorf("Expected \"%s\", got: \"%s\"", expectedMsg, err.Error())
    59  	}
    60  }
    61  
    62  func TestHandleErrorResponseExpectedStatusCode404InvalidBody(t *testing.T) {
    63  	json := "{invalid json}"
    64  	response := &http.Response{
    65  		Status:     "404 Not Found",
    66  		StatusCode: 404,
    67  		Body:       nopCloser{bytes.NewBufferString(json)},
    68  	}
    69  	err := HandleErrorResponse(response)
    70  
    71  	expectedMsg := "Error parsing HTTP response: invalid character 'i' looking for beginning of object key string: \"{invalid json}\""
    72  	if !strings.Contains(err.Error(), expectedMsg) {
    73  		t.Errorf("Expected \"%s\", got: \"%s\"", expectedMsg, err.Error())
    74  	}
    75  }
    76  
    77  func TestHandleErrorResponseUnexpectedStatusCode501(t *testing.T) {
    78  	response := &http.Response{
    79  		Status:     "501 Not Implemented",
    80  		StatusCode: 501,
    81  		Body:       nopCloser{bytes.NewBufferString("{\"Error Encountered\" : \"Function not implemented.\"}")},
    82  	}
    83  	err := HandleErrorResponse(response)
    84  
    85  	expectedMsg := "Received unexpected HTTP status: 501 Not Implemented"
    86  	if !strings.Contains(err.Error(), expectedMsg) {
    87  		t.Errorf("Expected \"%s\", got: \"%s\"", expectedMsg, err.Error())
    88  	}
    89  }