istio.io/istio@v0.0.0-20240520182934-d79c90f27776/samples/jwt-server/src/main_test.go (about)

     1  // Copyright Istio Authors
     2  //
     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 main
    16  
    17  import (
    18  	"crypto/tls"
    19  	"crypto/x509"
    20  	"fmt"
    21  	"net/http"
    22  	"os"
    23  	"testing"
    24  )
    25  
    26  func TestJwtHTTPServer(t *testing.T) {
    27  	server := NewJwtServer("", "")
    28  	// Start the test server on random port.
    29  	go server.runHTTP("localhost:0")
    30  	// Prepare the HTTP request.
    31  	httpClient := &http.Client{}
    32  	httpReq, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://localhost:%d/jwtkeys", <-server.httpPort), nil)
    33  	if err != nil {
    34  		t.Fatalf(err.Error())
    35  	}
    36  	resp, err := httpClient.Do(httpReq)
    37  	if err != nil {
    38  		t.Fatalf(err.Error())
    39  	}
    40  	defer resp.Body.Close()
    41  	if resp.StatusCode != http.StatusOK {
    42  		t.Fatalf("Expected to get %d, got %d", http.StatusOK, resp.StatusCode)
    43  	}
    44  }
    45  
    46  func TestJwtHTTPSServer(t *testing.T) {
    47  	var (
    48  		serverKey  = "../testdata/server.key"
    49  		serverCert = "../testdata/server.crt"
    50  	)
    51  
    52  	caCert, err := os.ReadFile(serverCert)
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	caCertPool := x509.NewCertPool()
    57  	caCertPool.AppendCertsFromPEM(caCert)
    58  
    59  	// creating https client with client certificate and certificate authority
    60  	httpsClient := &http.Client{
    61  		Transport: &http.Transport{
    62  			TLSClientConfig: &tls.Config{
    63  				RootCAs:    caCertPool,
    64  				MinVersion: tls.VersionTLS12,
    65  			},
    66  		},
    67  	}
    68  
    69  	server := NewJwtServer(serverCert, serverKey)
    70  
    71  	// Start the test server on port 8443.
    72  	go server.runHTTPS(":8443")
    73  
    74  	httpsReq, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://localhost:%d/jwtkeys", <-server.httpsPort), nil)
    75  	if err != nil {
    76  		t.Fatalf(err.Error())
    77  	}
    78  
    79  	resp, err := httpsClient.Do(httpsReq)
    80  	if err != nil {
    81  		t.Fatalf(err.Error())
    82  	}
    83  	defer resp.Body.Close()
    84  	if resp.StatusCode != http.StatusOK {
    85  		t.Fatalf("Expected to get %d, got %d", http.StatusOK, resp.StatusCode)
    86  	}
    87  }