github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/edge/pkg/edgehub/common/http/http_test.go (about)

     1  /*
     2  Copyright 2019 The KubeEdge Authors.
     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 http
    18  
    19  import (
    20  	"crypto/tls"
    21  	"crypto/x509"
    22  	"net/http"
    23  	"reflect"
    24  	"testing"
    25  
    26  	"github.com/kubeedge/kubeedge/edge/pkg/common/util"
    27  )
    28  
    29  const (
    30  	CertFile = "/tmp/kubeedge/testData/edge.crt"
    31  	KeyFile  = "/tmp/kubeedge/testData/edge.key"
    32  )
    33  
    34  //TestNewHttpClient() tests the creation of a new HTTP client
    35  func TestNewHttpClient(t *testing.T) {
    36  	httpClient := NewHTTPClient()
    37  	if httpClient == nil {
    38  		t.Fatal("Failed to build HTTP client")
    39  	}
    40  }
    41  
    42  //TestNewHTTPSclient() tests the creation of a new HTTPS client with proper values
    43  func TestNewHTTPSclient(t *testing.T) {
    44  	err := util.GenerateTestCertificate("/tmp/kubeedge/testData/", "edge", "edge")
    45  	if err != nil {
    46  		t.Errorf("Error in generating fake certificates: %v", err)
    47  		return
    48  	}
    49  	certificate, err := tls.LoadX509KeyPair(CertFile, KeyFile)
    50  	if err != nil {
    51  		t.Errorf("Error in loading key pair: %v", err)
    52  		return
    53  	}
    54  	type args struct {
    55  		certFile string
    56  		keyFile  string
    57  	}
    58  	tests := []struct {
    59  		name    string
    60  		args    args
    61  		want    *http.Client
    62  		wantErr bool
    63  	}{
    64  		{
    65  			name: "TestNewHTTPSclient: ",
    66  			args: args{
    67  				keyFile:  KeyFile,
    68  				certFile: CertFile,
    69  			},
    70  			want: &http.Client{
    71  				Transport: &http.Transport{
    72  					TLSClientConfig: &tls.Config{
    73  						RootCAs:      x509.NewCertPool(),
    74  						Certificates: []tls.Certificate{certificate},
    75  						MinVersion:   tls.VersionTLS12,
    76  						CipherSuites: []uint16{
    77  							tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
    78  						},
    79  						InsecureSkipVerify: true},
    80  				},
    81  				Timeout: connectTimeout,
    82  			},
    83  			wantErr: false,
    84  		},
    85  		{
    86  			name: "Wrong path given while getting HTTPS client",
    87  			args: args{
    88  				keyFile:  "WrongKeyFilePath",
    89  				certFile: "WrongCertFilePath",
    90  			},
    91  			want:    nil,
    92  			wantErr: true,
    93  		},
    94  	}
    95  	for _, tt := range tests {
    96  		t.Run(tt.name, func(t *testing.T) {
    97  			got, err := NewHTTPSclient(tt.args.certFile, tt.args.keyFile)
    98  			if (err != nil) != tt.wantErr {
    99  				t.Errorf("NewHTTPSclient() error = %v, expectedError = %v", err, tt.wantErr)
   100  				return
   101  			}
   102  			if !reflect.DeepEqual(got, tt.want) {
   103  				t.Errorf("NewHTTPSclient() = %v, want %v", got, tt.want)
   104  			}
   105  		})
   106  	}
   107  }