agones.dev/agones@v1.53.0/pkg/util/https/server_test.go (about)

     1  // Copyright 2019 Google LLC All Rights Reserved.
     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 https
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  type testServer struct {
    27  	server *httptest.Server
    28  }
    29  
    30  func (ts *testServer) Shutdown(_ context.Context) error {
    31  	ts.server.Close()
    32  	return nil
    33  }
    34  
    35  // ListenAndServeTLS(certFile, keyFile string) error
    36  func (ts *testServer) ListenAndServeTLS(_, _ string) error {
    37  	ts.server.StartTLS()
    38  	return nil
    39  }
    40  
    41  func TestServerRun(t *testing.T) {
    42  	t.Parallel()
    43  
    44  	s := NewServer("", "", "")
    45  	ts := &testServer{server: httptest.NewUnstartedServer(s.Mux)}
    46  	s.tls = ts
    47  
    48  	ctx, cancel := context.WithCancel(context.Background())
    49  	defer cancel()
    50  
    51  	err := s.Run(ctx, 0)
    52  	assert.Nil(t, err)
    53  
    54  	client := ts.server.Client()
    55  	resp, err := client.Get(ts.server.URL + "/test")
    56  	assert.Nil(t, err)
    57  	defer resp.Body.Close() // nolint: errcheck
    58  	assert.Equal(t, http.StatusNotFound, resp.StatusCode)
    59  
    60  	resp, err = client.Get(ts.server.URL + "/")
    61  	assert.Nil(t, err)
    62  	defer resp.Body.Close() // nolint: errcheck
    63  	assert.Equal(t, http.StatusOK, resp.StatusCode)
    64  }