github.com/GoogleContainerTools/kaniko@v1.23.0/pkg/buildcontext/https_test.go (about)

     1  /*
     2  Copyright 2018 Google LLC
     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 buildcontext
    18  
    19  import (
    20  	"net/http"
    21  	"net/http/httptest"
    22  	"testing"
    23  )
    24  
    25  func TestBuildWithHttpsTar(t *testing.T) {
    26  
    27  	tests := []struct {
    28  		name          string
    29  		serverHandler http.HandlerFunc
    30  	}{
    31  		{
    32  			name: "test http bad status",
    33  			serverHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    34  				w.WriteHeader(http.StatusBadRequest)
    35  				_, err := w.Write([]byte("corrupted message"))
    36  				if err != nil {
    37  					t.Fatalf("Error sending response: %v", err)
    38  				}
    39  			}),
    40  		},
    41  		{
    42  			name: "test http bad data",
    43  			serverHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    44  				w.WriteHeader(http.StatusOK)
    45  				_, err := w.Write([]byte("corrupted message"))
    46  				if err != nil {
    47  					t.Fatalf("Error sending response: %v", err)
    48  				}
    49  			}),
    50  		},
    51  	}
    52  
    53  	for _, tcase := range tests {
    54  		t.Run(tcase.name, func(t *testing.T) {
    55  			server := httptest.NewServer(tcase.serverHandler)
    56  			defer server.Close()
    57  
    58  			context := &HTTPSTar{
    59  				context: server.URL + "/data.tar.gz",
    60  			}
    61  
    62  			_, err := context.UnpackTarFromBuildContext()
    63  			if err == nil {
    64  				t.Fatalf("Error expected but not returned: %s", err)
    65  			}
    66  		})
    67  	}
    68  }