storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/rest/client_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2020 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package rest
    18  
    19  import (
    20  	"errors"
    21  	"net"
    22  	"net/url"
    23  	"testing"
    24  )
    25  
    26  func TestNetworkError_Unwrap(t *testing.T) {
    27  	tests := []struct {
    28  		name   string
    29  		err    error
    30  		target interface{}
    31  		want   bool
    32  	}{
    33  		{
    34  			name:   "url.Error",
    35  			err:    &url.Error{Op: "PUT", URL: "http://localhost/1234", Err: restError("remote server offline")},
    36  			target: &url.Error{},
    37  			want:   true,
    38  		},
    39  		{
    40  			name: "net.Error",
    41  			err:  &url.Error{Op: "PUT", URL: "http://localhost/1234", Err: restError("remote server offline")},
    42  			want: true,
    43  		},
    44  		{
    45  			name: "net.Error-unmatched",
    46  			err:  errors.New("something"),
    47  			want: false,
    48  		},
    49  	}
    50  	for _, tt := range tests {
    51  		t.Run(tt.name, func(t *testing.T) {
    52  			// Wrap error
    53  			n := &NetworkError{
    54  				Err: tt.err,
    55  			}
    56  			if tt.target == nil {
    57  				var netErrInterface net.Error
    58  				if errors.As(n, &netErrInterface) != tt.want {
    59  					t.Errorf("errors.As(n, &tt.target) != tt.want, n: %#v, target: %#v, want:%v, got: %v", n, tt.target, tt.want, !tt.want)
    60  				}
    61  			} else {
    62  				if errors.As(n, &tt.target) != tt.want {
    63  					t.Errorf("errors.As(n, &tt.target) != tt.want, n: %#v, target: %#v, want:%v, got: %v", n, tt.target, tt.want, !tt.want)
    64  				}
    65  			}
    66  		})
    67  	}
    68  }