github.com/GoogleCloudPlatform/testgrid@v0.0.174/pkg/api/router_http_test.go (about) 1 /* 2 Copyright 2023 The TestGrid 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 api 18 19 import ( 20 "net/http" 21 "net/http/httptest" 22 "testing" 23 ) 24 25 func TestHealth(t *testing.T) { 26 tests := []struct { 27 name string 28 path string 29 expectedCode int 30 }{ 31 { 32 name: "Returns a healthiness check at '/'", 33 path: "/", 34 expectedCode: http.StatusOK, 35 }, 36 { 37 name: "Return 404 to nonsense", 38 path: "/ipa/v1/derp", 39 expectedCode: http.StatusNotFound, 40 }, 41 } 42 43 // Tests are run from the local directory, while the image is built from the repository root 44 healthCheckFile = "README.md" 45 46 for _, test := range tests { 47 t.Run(test.name, func(t *testing.T) { 48 router, _, err := GetRouters(RouterOptions{}, nil) 49 if err != nil { 50 t.Fatalf("Unexpected error: %v", err) 51 } 52 request, err := http.NewRequest("GET", test.path, nil) 53 if err != nil { 54 t.Fatalf("Can't form request: %v", err) 55 } 56 response := httptest.NewRecorder() 57 router.ServeHTTP(response, request) 58 if response.Code != test.expectedCode { 59 t.Errorf("Expected %d, but got %d", test.expectedCode, response.Code) 60 } 61 }) 62 } 63 }