d52f4b2d359070d8aa8a6495e8b6a16a39cd30e8
[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         "github.com/stretchr/testify/suite"
21         "io/ioutil"
22         "net"
23         "net/http"
24         "os"
25         "strings"
26         "testing"
27 )
28
29 type HttpServerTestSuite struct {
30         suite.Suite
31         listener       net.Listener
32         ch_notif       chan []byte
33         ch_supervision chan chan string
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         var err error
41         suite.listener, err = net.Listen("tcp", ":0")
42         suite.Nil(err)
43         suite.ch_notif = make(chan []byte)
44         suite.ch_supervision = make(chan chan string)
45         startHttpServer(suite.listener, "/vesmgr_notif/", suite.ch_notif, suite.ch_supervision)
46 }
47
48 func (suite *HttpServerTestSuite) TestHtppServerSupervisionInvalidOperation() {
49         resp, reply := suite.doPost("http://"+suite.listener.Addr().String()+SupervisionUrl, "supervision")
50         suite.Equal("405 method not allowed\n", reply)
51         suite.Equal(405, resp.StatusCode)
52         suite.Equal("405 Method Not Allowed", resp.Status)
53 }
54
55 func (suite *HttpServerTestSuite) doGet(url string) (*http.Response, string) {
56         resp, err := http.Get(url)
57         suite.Nil(err)
58
59         defer resp.Body.Close()
60         contents, err := ioutil.ReadAll(resp.Body)
61         suite.Nil(err)
62         return resp, string(contents)
63 }
64
65 func (suite *HttpServerTestSuite) doPost(serverUrl string, msg string) (*http.Response, string) {
66         resp, err := http.Post(serverUrl, "data", strings.NewReader(msg))
67         suite.Nil(err)
68
69         defer resp.Body.Close()
70         contents, err := ioutil.ReadAll(resp.Body)
71         suite.Nil(err)
72         return resp, string(contents)
73 }
74
75 func replySupervision(ch_supervision chan chan string, reply string) {
76         ch_supervision_ack := <-ch_supervision
77         ch_supervision_ack <- reply
78 }
79
80 func (suite *HttpServerTestSuite) TestHttpServerSupervision() {
81
82         // start the "main loop" to reply to the supervision to the HTTPServer
83         go replySupervision(suite.ch_supervision, "I'm just fine")
84
85         resp, reply := suite.doGet("http://" + suite.listener.Addr().String() + SupervisionUrl)
86
87         suite.Equal("I'm just fine", reply)
88         suite.Equal(200, resp.StatusCode)
89         suite.Equal("200 OK", resp.Status)
90 }
91
92 func (suite *HttpServerTestSuite) TestHttpServerInvalidUrl() {
93         resp, reply := suite.doPost("http://"+suite.listener.Addr().String()+"/invalid_url", "foo")
94         suite.Equal("404 page not found\n", reply)
95         suite.Equal(404, resp.StatusCode)
96         suite.Equal("404 Not Found", resp.Status)
97 }
98
99 func readXAppNotification(ch_notif chan []byte, ch chan []byte) {
100         notification := <-ch_notif
101         ch <- notification
102 }
103
104 func (suite *HttpServerTestSuite) TestHttpServerXappNotif() {
105         // start the "main loop" to receive the xAppNotification message from the HTTPServer
106         ch := make(chan []byte)
107         go readXAppNotification(suite.ch_notif, ch)
108
109         resp, reply := suite.doPost("http://"+suite.listener.Addr().String()+"/vesmgr_notif/", "test data")
110         suite.Equal("", reply)
111         suite.Equal(200, resp.StatusCode)
112         suite.Equal("200 OK", resp.Status)
113         notification := <-ch
114         suite.Equal([]byte("test data"), notification)
115 }
116
117 func (suite *HttpServerTestSuite) TestHttpServerXappNotifInvalidOperation() {
118         resp, reply := suite.doGet("http://" + suite.listener.Addr().String() + "/vesmgr_notif/")
119         suite.Equal("405 method not allowed\n", reply)
120         suite.Equal(405, resp.StatusCode)
121         suite.Equal("405 Method Not Allowed", resp.Status)
122 }
123
124 func TestHttpServerSuite(t *testing.T) {
125         suite.Run(t, new(HttpServerTestSuite))
126 }