...

Source file src/oras.land/oras-go/pkg/registry/remote/internal/errutil/errors_test.go

Documentation: oras.land/oras-go/pkg/registry/remote/internal/errutil

     1  /*
     2  Copyright The ORAS Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  package errutil
    16  
    17  import (
    18  	"net/http"
    19  	"net/http/httptest"
    20  	"strings"
    21  	"testing"
    22  )
    23  
    24  func Test_ParseErrorResponse(t *testing.T) {
    25  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    26  		msg := `{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":[{"Type":"repository","Class":"","Name":"library/hello-world","Action":"pull"}]}]}`
    27  		w.WriteHeader(http.StatusUnauthorized)
    28  		if _, err := w.Write([]byte(msg)); err != nil {
    29  			t.Errorf("failed to write %q: %v", r.URL, err)
    30  		}
    31  	}))
    32  	defer ts.Close()
    33  
    34  	resp, err := http.Get(ts.URL)
    35  	if err != nil {
    36  		t.Fatalf("failed to do request: %v", err)
    37  	}
    38  	err = ParseErrorResponse(resp)
    39  	if err == nil {
    40  		t.Errorf("ParseErrorResponse() error = %v, wantErr %v", err, true)
    41  	}
    42  	errmsg := err.Error()
    43  	if want := "401"; !strings.Contains(errmsg, want) {
    44  		t.Errorf("ParseErrorResponse() error = %v, want err message %v", err, want)
    45  	}
    46  	if want := "unauthorized"; !strings.Contains(errmsg, want) {
    47  		t.Errorf("ParseErrorResponse() error = %v, want err message %v", err, want)
    48  	}
    49  	if want := "authentication required"; !strings.Contains(errmsg, want) {
    50  		t.Errorf("ParseErrorResponse() error = %v, want err message %v", err, want)
    51  	}
    52  }
    53  
    54  func Test_ParseErrorResponse_plain(t *testing.T) {
    55  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    56  		w.WriteHeader(http.StatusUnauthorized)
    57  	}))
    58  	defer ts.Close()
    59  
    60  	resp, err := http.Get(ts.URL)
    61  	if err != nil {
    62  		t.Fatalf("failed to do request: %v", err)
    63  	}
    64  	err = ParseErrorResponse(resp)
    65  	if err == nil {
    66  		t.Errorf("ParseErrorResponse() error = %v, wantErr %v", err, true)
    67  	}
    68  	errmsg := err.Error()
    69  	if want := "401"; !strings.Contains(errmsg, want) {
    70  		t.Errorf("ParseErrorResponse() error = %v, want err message %v", err, want)
    71  	}
    72  	if want := http.StatusText(http.StatusUnauthorized); !strings.Contains(errmsg, want) {
    73  		t.Errorf("ParseErrorResponse() error = %v, want err message %v", err, want)
    74  	}
    75  }
    76  

View as plain text