921321651386a648309312d92fe2d1fea16ad5f5
[nonrtric.git] / dmaap-mediator-producer / internal / server / server_test.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: Nordix Foundation
6 //   %%
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
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
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===================================
19 //
20
21 package server
22
23 import (
24         "io"
25         "net/http"
26         "net/http/httptest"
27         "testing"
28
29         "github.com/stretchr/testify/require"
30 )
31
32 func TestStatusHandler(t *testing.T) {
33         assertions := require.New(t)
34         type args struct {
35                 responseRecorder *httptest.ResponseRecorder
36                 r                *http.Request
37         }
38         tests := []struct {
39                 name         string
40                 args         args
41                 wantedStatus int
42                 wantedBody   string
43         }{
44                 {
45                         name: "StatusHandler with correct path and method, should return OK",
46                         args: args{
47                                 responseRecorder: httptest.NewRecorder(),
48                                 r:                newRequest("GET", "/", nil, t),
49                         },
50                         wantedStatus: http.StatusOK,
51                         wantedBody:   "All is well!",
52                 },
53                 {
54                         name: "StatusHandler with incorrect path, should return NotFound",
55                         args: args{
56                                 responseRecorder: httptest.NewRecorder(),
57                                 r:                newRequest("GET", "/wrong", nil, t),
58                         },
59                         wantedStatus: http.StatusNotFound,
60                         wantedBody:   "404 not found.\n",
61                 },
62                 {
63                         name: "StatusHandler with incorrect method, should return NotFound",
64                         args: args{
65                                 responseRecorder: httptest.NewRecorder(),
66                                 r:                newRequest("PUT", "/", nil, t),
67                         },
68                         wantedStatus: http.StatusNotFound,
69                         wantedBody:   "Method is not supported.\n",
70                 },
71         }
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)
77
78                         assertions.Equal(tt.wantedBody, tt.args.responseRecorder.Body.String())
79                 })
80         }
81 }
82
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 {
85                 return req
86         } else {
87                 t.Fatalf("Could not create request due to: %v", err)
88                 return nil
89         }
90 }