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