| 1 |
/* |
| 2 |
Copyright 2021 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 notfoundhandler |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"io" |
| 22 |
"net/http/httptest" |
| 23 |
"strings" |
| 24 |
"testing" |
| 25 |
|
| 26 |
"k8s.io/apimachinery/pkg/runtime" |
| 27 |
"k8s.io/apimachinery/pkg/runtime/serializer" |
| 28 |
) |
| 29 |
|
| 30 |
func TestNotFoundHandler(t *testing.T) { |
| 31 |
isMuxAndDiscoveryCompleteGlobalValue := true |
| 32 |
isMuxAndDiscoveryCompleteTestFn := func(ctx context.Context) bool { return isMuxAndDiscoveryCompleteGlobalValue } |
| 33 |
serializer := serializer.NewCodecFactory(runtime.NewScheme()).WithoutConversion() |
| 34 |
target := New(serializer, isMuxAndDiscoveryCompleteTestFn) |
| 35 |
|
| 36 |
// scenario 1: pretend the request has been made after the signal has been ready |
| 37 |
req := httptest.NewRequest("GET", "http://apiserver.com/apis/flowcontrol.apiserver.k8s.io/v1beta1", nil) |
| 38 |
rw := httptest.NewRecorder() |
| 39 |
|
| 40 |
target.ServeHTTP(rw, req) |
| 41 |
resp := rw.Result() |
| 42 |
body, err := io.ReadAll(resp.Body) |
| 43 |
if err != nil { |
| 44 |
t.Fatal(err) |
| 45 |
} |
| 46 |
bodyStr := strings.TrimSuffix(string(body), "\n") |
| 47 |
|
| 48 |
if resp.StatusCode != 404 { |
| 49 |
t.Fatalf("unexpected status code %d, expected 503", resp.StatusCode) |
| 50 |
} |
| 51 |
expectedMsg := "404 page not found" |
| 52 |
if bodyStr != expectedMsg { |
| 53 |
t.Fatalf("unexpected response: %v, expected: %v", bodyStr, expectedMsg) |
| 54 |
} |
| 55 |
|
| 56 |
// scenario 2: pretend the request has been made before the signal has been ready |
| 57 |
isMuxAndDiscoveryCompleteGlobalValue = false |
| 58 |
rw = httptest.NewRecorder() |
| 59 |
|
| 60 |
target.ServeHTTP(rw, req) |
| 61 |
resp = rw.Result() |
| 62 |
body, err = io.ReadAll(resp.Body) |
| 63 |
if err != nil { |
| 64 |
t.Fatal(err) |
| 65 |
} |
| 66 |
bodyStr = strings.TrimSuffix(string(body), "\n") |
| 67 |
if resp.StatusCode != 503 { |
| 68 |
t.Fatalf("unexpected status code %d, expected 503", resp.StatusCode) |
| 69 |
} |
| 70 |
expectedMsg = `{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"the request has been made before all known HTTP paths have been installed, please try again","reason":"ServiceUnavailable","details":{"retryAfterSeconds":5},"code":503}` |
| 71 |
if bodyStr != expectedMsg { |
| 72 |
t.Fatalf("unexpected response: %v, expected: %v", bodyStr, expectedMsg) |
| 73 |
} |
| 74 |
} |