agones.dev/agones@v1.53.0/cmd/sdk-server/main_test.go (about)

     1  // Copyright 2020 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 main
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"io"
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  	"google.golang.org/grpc"
    27  )
    28  
    29  // TestRegisterTestSdkServer - test to verify
    30  func TestRegisterTestSdkServer(t *testing.T) {
    31  	t.Parallel()
    32  	ctlConf := parseEnvFlags()
    33  	grpcServer := grpc.NewServer()
    34  	_, err := registerTestSdkServer(grpcServer, ctlConf)
    35  	assert.NoError(t, err)
    36  	ctx, cancel := context.WithCancel(context.Background())
    37  	defer cancel()
    38  	ctx.Done()
    39  	ctlConf.LocalFile = "@@"
    40  	_, err = registerLocal(grpcServer, ctlConf)
    41  	assert.Error(t, err, "Wrong file name should produce an error")
    42  }
    43  
    44  func TestHealthCheckWrapper(t *testing.T) {
    45  	t.Parallel()
    46  	testCases := []struct {
    47  		name     string
    48  		body     io.Reader
    49  		expected int
    50  	}{
    51  		{"empty body", nil, http.StatusBadRequest},
    52  		{"non-empty body", bytes.NewBuffer([]byte(`{}`)), http.StatusOK},
    53  	}
    54  
    55  	testWrapper := healthCheckWrapper(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
    56  		w.WriteHeader(http.StatusOK)
    57  	}))
    58  
    59  	for _, tc := range testCases {
    60  		t.Run(tc.name, func(t *testing.T) {
    61  			testResponse := httptest.NewRecorder()
    62  			testWrapper.ServeHTTP(testResponse, httptest.NewRequest("POST", "http://testServer/health", tc.body))
    63  			assert.Equal(t, tc.expected, testResponse.Code)
    64  		})
    65  	}
    66  }