Debian Codegraph @

pkg/server/mux/pathrecorder_test.go

raw
1 /*
2 Copyright 2016 The Kubernetes 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 */
16
17 package mux
18
19 import (
20 "net/http"
21 "net/http/httptest"
22 "testing"
23
24 "github.com/stretchr/testify/assert"
25 )
26
27 func TestSecretHandlers(t *testing.T) {
28 c := NewPathRecorderMux("test")
29 c.UnlistedHandleFunc("/secret", func(http.ResponseWriter, *http.Request) {})
30 c.HandleFunc("/nonswagger", func(http.ResponseWriter, *http.Request) {})
31 assert.NotContains(t, c.ListedPaths(), "/secret")
32 assert.Contains(t, c.ListedPaths(), "/nonswagger")
33 }
34
35 func TestUnregisterHandlers(t *testing.T) {
36 first := 0
37 second := 0
38
39 c := NewPathRecorderMux("test")
40 s := httptest.NewServer(c)
41 defer s.Close()
42
43 c.UnlistedHandleFunc("/secret", func(http.ResponseWriter, *http.Request) {})
44 c.HandleFunc("/nonswagger", func(http.ResponseWriter, *http.Request) {
45 first = first + 1
46 })
47 assert.NotContains(t, c.ListedPaths(), "/secret")
48 assert.Contains(t, c.ListedPaths(), "/nonswagger")
49
50 resp, _ := http.Get(s.URL + "/nonswagger")
51 assert.Equal(t, 1, first)
52 assert.Equal(t, http.StatusOK, resp.StatusCode)
53
54 c.Unregister("/nonswagger")
55 assert.NotContains(t, c.ListedPaths(), "/nonswagger")
56
57 resp, _ = http.Get(s.URL + "/nonswagger")
58 assert.Equal(t, 1, first)
59 assert.Equal(t, http.StatusNotFound, resp.StatusCode)
60
61 c.HandleFunc("/nonswagger", func(http.ResponseWriter, *http.Request) {
62 second = second + 1
63 })
64 assert.Contains(t, c.ListedPaths(), "/nonswagger")
65 resp, _ = http.Get(s.URL + "/nonswagger")
66 assert.Equal(t, 1, first)
67 assert.Equal(t, 1, second)
68 assert.Equal(t, http.StatusOK, resp.StatusCode)
69 }
70
71 func TestPrefixHandlers(t *testing.T) {
72 c := NewPathRecorderMux("test")
73 s := httptest.NewServer(c)
74 defer s.Close()
75
76 secretPrefixCount := 0
77 c.UnlistedHandlePrefix("/secretPrefix/", http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
78 secretPrefixCount = secretPrefixCount + 1
79 }))
80 publicPrefixCount := 0
81 c.HandlePrefix("/publicPrefix/", http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
82 publicPrefixCount = publicPrefixCount + 1
83 }))
84 precisePrefixCount := 0
85 c.HandlePrefix("/publicPrefix/but-more-precise/", http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
86 precisePrefixCount = precisePrefixCount + 1
87 }))
88 exactMatchCount := 0
89 c.Handle("/publicPrefix/exactmatch", http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
90 exactMatchCount = exactMatchCount + 1
91 }))
92 slashMatchCount := 0
93 c.Handle("/otherPublic/exactmatchslash/", http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
94 slashMatchCount = slashMatchCount + 1
95 }))
96 fallThroughCount := 0
97 c.NotFoundHandler(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
98 fallThroughCount = fallThroughCount + 1
99 }))
100
101 assert.NotContains(t, c.ListedPaths(), "/secretPrefix/")
102 assert.Contains(t, c.ListedPaths(), "/publicPrefix/")
103
104 resp, _ := http.Get(s.URL + "/fallthrough")
105 assert.Equal(t, 1, fallThroughCount)
106 assert.Equal(t, http.StatusOK, resp.StatusCode)
107 resp, _ = http.Get(s.URL + "/publicPrefix")
108 assert.Equal(t, 2, fallThroughCount)
109 assert.Equal(t, http.StatusOK, resp.StatusCode)
110
111 http.Get(s.URL + "/publicPrefix/")
112 assert.Equal(t, 1, publicPrefixCount)
113 http.Get(s.URL + "/publicPrefix/something")
114 assert.Equal(t, 2, publicPrefixCount)
115 http.Get(s.URL + "/publicPrefix/but-more-precise")
116 assert.Equal(t, 3, publicPrefixCount)
117 http.Get(s.URL + "/publicPrefix/but-more-precise/")
118 assert.Equal(t, 1, precisePrefixCount)
119 http.Get(s.URL + "/publicPrefix/but-more-precise/more-stuff")
120 assert.Equal(t, 2, precisePrefixCount)
121
122 http.Get(s.URL + "/publicPrefix/exactmatch")
123 assert.Equal(t, 1, exactMatchCount)
124 http.Get(s.URL + "/publicPrefix/exactmatch/")
125 assert.Equal(t, 4, publicPrefixCount)
126 http.Get(s.URL + "/otherPublic/exactmatchslash")
127 assert.Equal(t, 3, fallThroughCount)
128 http.Get(s.URL + "/otherPublic/exactmatchslash/")
129 assert.Equal(t, 1, slashMatchCount)
130
131 http.Get(s.URL + "/secretPrefix/")
132 assert.Equal(t, 1, secretPrefixCount)
133 http.Get(s.URL + "/secretPrefix/something")
134 assert.Equal(t, 2, secretPrefixCount)
135 }