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.
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
30 "github.com/stretchr/testify/suite"
33 type HTTPServerTestSuite struct {
36 chSupervision chan chan string
40 // suite setup creates the HTTP server
41 func (suite *HTTPServerTestSuite) SetupSuite() {
42 os.Unsetenv("http_proxy")
43 os.Unsetenv("HTTP_PROXY")
44 suite.chNotif = make(chan []byte)
45 suite.chSupervision = make(chan chan string)
46 suite.server = HTTPServer{}
47 suite.server.init(":0")
48 suite.server.start("/vesmgr_notif/", suite.chNotif, suite.chSupervision)
51 func (suite *HTTPServerTestSuite) TestHtppServerSupervisionInvalidOperation() {
52 resp, reply := suite.doPost("http://"+suite.server.addr().String()+SupervisionURL, "supervision")
53 suite.Equal("405 method not allowed\n", reply)
54 suite.Equal(405, resp.StatusCode)
55 suite.Equal("405 Method Not Allowed", resp.Status)
58 func (suite *HTTPServerTestSuite) doGet(url string) (*http.Response, string) {
59 resp, err := http.Get(url)
62 defer resp.Body.Close()
63 contents, err := ioutil.ReadAll(resp.Body)
65 return resp, string(contents)
68 func (suite *HTTPServerTestSuite) doPost(serverURL string, msg string) (*http.Response, string) {
69 resp, err := http.Post(serverURL, "data", strings.NewReader(msg))
72 defer resp.Body.Close()
73 contents, err := ioutil.ReadAll(resp.Body)
75 return resp, string(contents)
78 func replySupervision(chSupervision chan chan string, reply string) {
79 chSupervisionAck := <-chSupervision
80 chSupervisionAck <- reply
83 func (suite *HTTPServerTestSuite) TestHttpServerSupervision() {
85 // start the "main loop" to reply to the supervision to the HTTPServer
86 go replySupervision(suite.chSupervision, "I'm just fine")
88 resp, reply := suite.doGet("http://" + suite.server.addr().String() + SupervisionURL)
90 suite.Equal("I'm just fine", reply)
91 suite.Equal(200, resp.StatusCode)
92 suite.Equal("200 OK", resp.Status)
95 func (suite *HTTPServerTestSuite) TestHttpServerInvalidUrl() {
96 resp, reply := suite.doPost("http://"+suite.server.addr().String()+"/invalid_url", "foo")
97 suite.Equal("404 page not found\n", reply)
98 suite.Equal(404, resp.StatusCode)
99 suite.Equal("404 Not Found", resp.Status)
102 func readXAppNotification(chNotif chan []byte, ch chan []byte) {
103 notification := <-chNotif
107 func (suite *HTTPServerTestSuite) TestHttpServerXappNotif() {
108 // start the "main loop" to receive the xAppNotification message from the HTTPServer
109 ch := make(chan []byte)
110 go readXAppNotification(suite.chNotif, ch)
112 resp, reply := suite.doPost("http://"+suite.server.addr().String()+"/vesmgr_notif/", "test data")
113 suite.Equal("", reply)
114 suite.Equal(200, resp.StatusCode)
115 suite.Equal("200 OK", resp.Status)
117 suite.Equal([]byte("test data"), notification)
120 func (suite *HTTPServerTestSuite) TestHttpServerXappNotifInvalidOperation() {
121 resp, reply := suite.doGet("http://" + suite.server.addr().String() + "/vesmgr_notif/")
122 suite.Equal("405 method not allowed\n", reply)
123 suite.Equal(405, resp.StatusCode)
124 suite.Equal("405 Method Not Allowed", resp.Status)
127 func TestHttpServerSuite(t *testing.T) {
128 suite.Run(t, new(HTTPServerTestSuite))