2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
29 "github.com/stretchr/testify/require"
32 func TestStatusHandler(t *testing.T) {
33 assertions := require.New(t)
35 responseRecorder *httptest.ResponseRecorder
45 name: "StatusHandler with correct path and method, should return OK",
47 responseRecorder: httptest.NewRecorder(),
48 r: newRequest("GET", "/", nil, t),
50 wantedStatus: http.StatusOK,
51 wantedBody: "All is well!",
54 name: "StatusHandler with incorrect path, should return NotFound",
56 responseRecorder: httptest.NewRecorder(),
57 r: newRequest("GET", "/wrong", nil, t),
59 wantedStatus: http.StatusNotFound,
60 wantedBody: "404 not found.\n",
63 name: "StatusHandler with incorrect method, should return NotFound",
65 responseRecorder: httptest.NewRecorder(),
66 r: newRequest("PUT", "/", nil, t),
68 wantedStatus: http.StatusNotFound,
69 wantedBody: "Method is not supported.\n",
72 for _, tt := range tests {
73 t.Run(tt.name, func(t *testing.T) {
74 handler := http.HandlerFunc(StatusHandler)
75 handler.ServeHTTP(tt.args.responseRecorder, tt.args.r)
76 assertions.Equal(tt.wantedStatus, tt.args.responseRecorder.Code)
78 assertions.Equal(tt.wantedBody, tt.args.responseRecorder.Body.String())
83 func newRequest(method string, url string, body io.Reader, t *testing.T) *http.Request {
84 if req, err := http.NewRequest(method, url, body); err == nil {
87 t.Fatalf("Could not create request due to: %v", err)