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.
26 "github.com/stretchr/testify/suite"
29 type HTTPServerTestSuite struct {
32 chSupervision 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")
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)
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)
54 func (suite *HTTPServerTestSuite) doGet(url string) (*http.Response, string) {
55 resp, err := http.Get(url)
58 defer resp.Body.Close()
59 contents, err := ioutil.ReadAll(resp.Body)
61 return resp, string(contents)
64 func (suite *HTTPServerTestSuite) doPost(serverURL string, msg string) (*http.Response, string) {
65 resp, err := http.Post(serverURL, "data", strings.NewReader(msg))
68 defer resp.Body.Close()
69 contents, err := ioutil.ReadAll(resp.Body)
71 return resp, string(contents)
74 func replySupervision(chSupervision chan chan string, reply string) {
75 chSupervisionAck := <-chSupervision
76 chSupervisionAck <- reply
79 func (suite *HTTPServerTestSuite) TestHttpServerSupervision() {
81 // start the "main loop" to reply to the supervision to the HTTPServer
82 go replySupervision(suite.chSupervision, "I'm just fine")
84 resp, reply := suite.doGet("http://" + suite.server.addr().String() + SupervisionURL)
86 suite.Equal("I'm just fine", reply)
87 suite.Equal(200, resp.StatusCode)
88 suite.Equal("200 OK", resp.Status)
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)
98 func readXAppNotification(chNotif chan []byte, ch chan []byte) {
99 notification := <-chNotif
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)
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)
113 suite.Equal([]byte("test data"), notification)
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)
123 func TestHttpServerSuite(t *testing.T) {
124 suite.Run(t, new(HTTPServerTestSuite))