2 * Copyright (c) 2019 AT&T Intellectual Property.
3 * Copyright (c) 2018-2019 Nokia.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 "github.com/stretchr/testify/suite"
29 type HttpServerTestSuite struct {
33 ch_supervision chan chan string
36 // suite setup creates the HTTP server
37 func (suite *HttpServerTestSuite) SetupSuite() {
38 os.Unsetenv("http_proxy")
39 os.Unsetenv("HTTP_PROXY")
41 suite.listener, err = net.Listen("tcp", ":0")
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)
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)
55 func (suite *HttpServerTestSuite) doGet(url string) (*http.Response, string) {
56 resp, err := http.Get(url)
59 defer resp.Body.Close()
60 contents, err := ioutil.ReadAll(resp.Body)
62 return resp, string(contents)
65 func (suite *HttpServerTestSuite) doPost(serverUrl string, msg string) (*http.Response, string) {
66 resp, err := http.Post(serverUrl, "data", strings.NewReader(msg))
69 defer resp.Body.Close()
70 contents, err := ioutil.ReadAll(resp.Body)
72 return resp, string(contents)
75 func replySupervision(ch_supervision chan chan string, reply string) {
76 ch_supervision_ack := <-ch_supervision
77 ch_supervision_ack <- reply
80 func (suite *HttpServerTestSuite) TestHttpServerSupervision() {
82 // start the "main loop" to reply to the supervision to the HTTPServer
83 go replySupervision(suite.ch_supervision, "I'm just fine")
85 resp, reply := suite.doGet("http://" + suite.listener.Addr().String() + SupervisionUrl)
87 suite.Equal("I'm just fine", reply)
88 suite.Equal(200, resp.StatusCode)
89 suite.Equal("200 OK", resp.Status)
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)
99 func readXAppNotification(ch_notif chan []byte, ch chan []byte) {
100 notification := <-ch_notif
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)
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)
114 suite.Equal([]byte("test data"), notification)
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)
124 func TestHttpServerSuite(t *testing.T) {
125 suite.Run(t, new(HttpServerTestSuite))