c2946aaa3629dac266f0da9e67adebf646df16fb
[ric-plt/vespamgr.git] / cmd / vesmgr / httpserver_test.go
1 /*
2  *  Copyright (c) 2019 AT&T Intellectual Property.
3  *  Copyright (c) 2018-2019 Nokia.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 package main
18
19 import (
20         "io/ioutil"
21         "net/http"
22         "os"
23         "strings"
24         "testing"
25
26         "github.com/stretchr/testify/suite"
27 )
28
29 type HTTPServerTestSuite struct {
30         suite.Suite
31         chNotif       chan []byte
32         chSupervision chan chan string
33         server        HTTPServer
34 }
35
36 // suite setup creates the HTTP server
37 func (suite *HTTPServerTestSuite) SetupSuite() {
38         os.Unsetenv("http_proxy")
39         os.Unsetenv("HTTP_PROXY")
40         suite.chNotif = make(chan []byte)
41         suite.chSupervision = make(chan chan string)
42         suite.server = HTTPServer{}
43         suite.server.init(":0")
44         suite.server.start("/vesmgr_notif/", suite.chNotif, suite.chSupervision)
45 }
46
47 func (suite *HTTPServerTestSuite) TestHtppServerSupervisionInvalidOperation() {
48         resp, reply := suite.doPost("http://"+suite.server.addr().String()+SupervisionURL, "supervision")
49         suite.Equal("405 method not allowed\n", reply)
50         suite.Equal(405, resp.StatusCode)
51         suite.Equal("405 Method Not Allowed", resp.Status)
52 }
53
54 func (suite *HTTPServerTestSuite) doGet(url string) (*http.Response, string) {
55         resp, err := http.Get(url)
56         suite.Nil(err)
57
58         defer resp.Body.Close()
59         contents, err := ioutil.ReadAll(resp.Body)
60         suite.Nil(err)
61         return resp, string(contents)
62 }
63
64 func (suite *HTTPServerTestSuite) doPost(serverURL string, msg string) (*http.Response, string) {
65         resp, err := http.Post(serverURL, "data", strings.NewReader(msg))
66         suite.Nil(err)
67
68         defer resp.Body.Close()
69         contents, err := ioutil.ReadAll(resp.Body)
70         suite.Nil(err)
71         return resp, string(contents)
72 }
73
74 func replySupervision(chSupervision chan chan string, reply string) {
75         chSupervisionAck := <-chSupervision
76         chSupervisionAck <- reply
77 }
78
79 func (suite *HTTPServerTestSuite) TestHttpServerSupervision() {
80
81         // start the "main loop" to reply to the supervision to the HTTPServer
82         go replySupervision(suite.chSupervision, "I'm just fine")
83
84         resp, reply := suite.doGet("http://" + suite.server.addr().String() + SupervisionURL)
85
86         suite.Equal("I'm just fine", reply)
87         suite.Equal(200, resp.StatusCode)
88         suite.Equal("200 OK", resp.Status)
89 }
90
91 func (suite *HTTPServerTestSuite) TestHttpServerInvalidUrl() {
92         resp, reply := suite.doPost("http://"+suite.server.addr().String()+"/invalid_url", "foo")
93         suite.Equal("404 page not found\n", reply)
94         suite.Equal(404, resp.StatusCode)
95         suite.Equal("404 Not Found", resp.Status)
96 }
97
98 func readXAppNotification(chNotif chan []byte, ch chan []byte) {
99         notification := <-chNotif
100         ch <- notification
101 }
102
103 func (suite *HTTPServerTestSuite) TestHttpServerXappNotif() {
104         // start the "main loop" to receive the xAppNotification message from the HTTPServer
105         ch := make(chan []byte)
106         go readXAppNotification(suite.chNotif, ch)
107
108         resp, reply := suite.doPost("http://"+suite.server.addr().String()+"/vesmgr_notif/", "test data")
109         suite.Equal("", reply)
110         suite.Equal(200, resp.StatusCode)
111         suite.Equal("200 OK", resp.Status)
112         notification := <-ch
113         suite.Equal([]byte("test data"), notification)
114 }
115
116 func (suite *HTTPServerTestSuite) TestHttpServerXappNotifInvalidOperation() {
117         resp, reply := suite.doGet("http://" + suite.server.addr().String() + "/vesmgr_notif/")
118         suite.Equal("405 method not allowed\n", reply)
119         suite.Equal(405, resp.StatusCode)
120         suite.Equal("405 Method Not Allowed", resp.Status)
121 }
122
123 func TestHttpServerSuite(t *testing.T) {
124         suite.Run(t, new(HTTPServerTestSuite))
125 }