gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/go-control-plane/pkg/server/v3/gateway_test.go (about)

     1  // Copyright 2018 Envoyproxy 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 server_test
    16  
    17  import (
    18  	"context"
    19  	"io"
    20  	"net/http"
    21  	"strings"
    22  	"testing"
    23  	"testing/iotest"
    24  
    25  	discovery "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/discovery/v3"
    26  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/cache/types"
    27  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/cache/v3"
    28  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/resource/v3"
    29  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/server/v3"
    30  )
    31  
    32  type logger struct {
    33  	t *testing.T
    34  }
    35  
    36  func (log logger) Debugf(format string, args ...interface{}) { log.t.Logf(format, args...) }
    37  func (log logger) Infof(format string, args ...interface{})  { log.t.Logf(format, args...) }
    38  func (log logger) Warnf(format string, args ...interface{})  { log.t.Logf(format, args...) }
    39  func (log logger) Errorf(format string, args ...interface{}) { log.t.Logf(format, args...) }
    40  
    41  func TestGateway(t *testing.T) {
    42  	config := makeMockConfigWatcher()
    43  	config.responses = map[string][]cache.Response{
    44  		resource.ClusterType: {
    45  			&cache.RawResponse{
    46  				Version:   "2",
    47  				Resources: []types.ResourceWithTTL{{Resource: cluster}},
    48  				Request:   &discovery.DiscoveryRequest{TypeUrl: resource.ClusterType},
    49  			},
    50  		},
    51  		resource.RouteType: {
    52  			&cache.RawResponse{
    53  				Version:   "3",
    54  				Resources: []types.ResourceWithTTL{{Resource: route}},
    55  				Request:   &discovery.DiscoveryRequest{TypeUrl: resource.RouteType},
    56  			},
    57  		},
    58  		resource.ListenerType: {
    59  			&cache.RawResponse{
    60  				Version:   "4",
    61  				Resources: []types.ResourceWithTTL{{Resource: listener}},
    62  				Request:   &discovery.DiscoveryRequest{TypeUrl: resource.ListenerType},
    63  			},
    64  		},
    65  	}
    66  	gtw := server.HTTPGateway{Log: logger{t: t}, Server: server.NewServer(context.Background(), config, nil)}
    67  
    68  	failCases := []struct {
    69  		path   string
    70  		body   io.Reader
    71  		expect int
    72  	}{
    73  		{
    74  			path:   "/hello/",
    75  			expect: http.StatusNotFound,
    76  		},
    77  		{
    78  			path:   resource.FetchEndpoints,
    79  			expect: http.StatusBadRequest,
    80  		},
    81  		{
    82  			path:   resource.FetchEndpoints,
    83  			body:   iotest.TimeoutReader(strings.NewReader("hello")),
    84  			expect: http.StatusBadRequest,
    85  		},
    86  		{
    87  			path:   resource.FetchEndpoints,
    88  			body:   strings.NewReader("hello"),
    89  			expect: http.StatusBadRequest,
    90  		},
    91  		{
    92  			// missing response
    93  			path:   resource.FetchEndpoints,
    94  			body:   strings.NewReader("{\"node\": {\"id\": \"test\"}}"),
    95  			expect: http.StatusInternalServerError,
    96  		},
    97  	}
    98  	for _, cs := range failCases {
    99  		req, err := http.NewRequest(http.MethodPost, cs.path, cs.body)
   100  		if err != nil {
   101  			t.Fatal(err)
   102  		}
   103  		resp, code, err := gtw.ServeHTTP(req)
   104  		if err == nil {
   105  			t.Errorf("ServeHTTP succeeded, but should have failed")
   106  		}
   107  		if resp != nil {
   108  			t.Errorf("handler returned wrong response")
   109  		}
   110  		if status := code; status != cs.expect {
   111  			t.Errorf("handler returned wrong status: %d, want %d", status, cs.expect)
   112  		}
   113  	}
   114  
   115  	for _, path := range []string{resource.FetchClusters, resource.FetchRoutes, resource.FetchListeners} {
   116  		req, err := http.NewRequest(http.MethodPost, path, strings.NewReader("{\"node\": {\"id\": \"test\"}}"))
   117  		if err != nil {
   118  			t.Fatal(err)
   119  		}
   120  		resp, code, err := gtw.ServeHTTP(req)
   121  		if err != nil {
   122  			t.Fatal(err)
   123  		}
   124  		if resp == nil {
   125  			t.Errorf("handler returned wrong response")
   126  		}
   127  		if status := code; status != 200 {
   128  			t.Errorf("handler returned wrong status: %d, want %d", status, 200)
   129  		}
   130  	}
   131  }