blob: 434c4c440c9f9a1b6cf73b649e6f8cea9ebb13e2 [file] [log] [blame]
Katri Turunen412df962019-09-16 08:48:18 +03001/*
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.
Roni Riska6ffba082019-11-27 10:59:54 +020016 *
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
19 *
Katri Turunen412df962019-09-16 08:48:18 +030020 */
21package main
22
23import (
Katri Turunen412df962019-09-16 08:48:18 +030024 "io/ioutil"
Katri Turunen412df962019-09-16 08:48:18 +030025 "net/http"
26 "os"
27 "strings"
28 "testing"
Roni Riskafc77ebb2019-09-26 08:20:44 +030029
30 "github.com/stretchr/testify/suite"
Katri Turunen412df962019-09-16 08:48:18 +030031)
32
Roni Riskafc77ebb2019-09-26 08:20:44 +030033type HTTPServerTestSuite struct {
Katri Turunen412df962019-09-16 08:48:18 +030034 suite.Suite
Roni Riskafc77ebb2019-09-26 08:20:44 +030035 chNotif chan []byte
36 chSupervision chan chan string
37 server HTTPServer
Katri Turunen412df962019-09-16 08:48:18 +030038}
39
40// suite setup creates the HTTP server
Roni Riskafc77ebb2019-09-26 08:20:44 +030041func (suite *HTTPServerTestSuite) SetupSuite() {
Katri Turunen412df962019-09-16 08:48:18 +030042 os.Unsetenv("http_proxy")
43 os.Unsetenv("HTTP_PROXY")
Roni Riskafc77ebb2019-09-26 08:20:44 +030044 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)
Katri Turunen412df962019-09-16 08:48:18 +030049}
50
Roni Riskafc77ebb2019-09-26 08:20:44 +030051func (suite *HTTPServerTestSuite) TestHtppServerSupervisionInvalidOperation() {
52 resp, reply := suite.doPost("http://"+suite.server.addr().String()+SupervisionURL, "supervision")
Katri Turunen412df962019-09-16 08:48:18 +030053 suite.Equal("405 method not allowed\n", reply)
54 suite.Equal(405, resp.StatusCode)
55 suite.Equal("405 Method Not Allowed", resp.Status)
56}
57
Roni Riskafc77ebb2019-09-26 08:20:44 +030058func (suite *HTTPServerTestSuite) doGet(url string) (*http.Response, string) {
Katri Turunen412df962019-09-16 08:48:18 +030059 resp, err := http.Get(url)
60 suite.Nil(err)
61
62 defer resp.Body.Close()
63 contents, err := ioutil.ReadAll(resp.Body)
64 suite.Nil(err)
65 return resp, string(contents)
66}
67
Roni Riskafc77ebb2019-09-26 08:20:44 +030068func (suite *HTTPServerTestSuite) doPost(serverURL string, msg string) (*http.Response, string) {
69 resp, err := http.Post(serverURL, "data", strings.NewReader(msg))
Katri Turunen412df962019-09-16 08:48:18 +030070 suite.Nil(err)
71
72 defer resp.Body.Close()
73 contents, err := ioutil.ReadAll(resp.Body)
74 suite.Nil(err)
75 return resp, string(contents)
76}
77
Roni Riskafc77ebb2019-09-26 08:20:44 +030078func replySupervision(chSupervision chan chan string, reply string) {
79 chSupervisionAck := <-chSupervision
80 chSupervisionAck <- reply
Katri Turunen412df962019-09-16 08:48:18 +030081}
82
Roni Riskafc77ebb2019-09-26 08:20:44 +030083func (suite *HTTPServerTestSuite) TestHttpServerSupervision() {
Katri Turunen412df962019-09-16 08:48:18 +030084
85 // start the "main loop" to reply to the supervision to the HTTPServer
Roni Riskafc77ebb2019-09-26 08:20:44 +030086 go replySupervision(suite.chSupervision, "I'm just fine")
Katri Turunen412df962019-09-16 08:48:18 +030087
Roni Riskafc77ebb2019-09-26 08:20:44 +030088 resp, reply := suite.doGet("http://" + suite.server.addr().String() + SupervisionURL)
Katri Turunen412df962019-09-16 08:48:18 +030089
90 suite.Equal("I'm just fine", reply)
91 suite.Equal(200, resp.StatusCode)
92 suite.Equal("200 OK", resp.Status)
93}
94
Roni Riskafc77ebb2019-09-26 08:20:44 +030095func (suite *HTTPServerTestSuite) TestHttpServerInvalidUrl() {
96 resp, reply := suite.doPost("http://"+suite.server.addr().String()+"/invalid_url", "foo")
Katri Turunen412df962019-09-16 08:48:18 +030097 suite.Equal("404 page not found\n", reply)
98 suite.Equal(404, resp.StatusCode)
99 suite.Equal("404 Not Found", resp.Status)
100}
101
Roni Riskafc77ebb2019-09-26 08:20:44 +0300102func readXAppNotification(chNotif chan []byte, ch chan []byte) {
103 notification := <-chNotif
Katri Turunen412df962019-09-16 08:48:18 +0300104 ch <- notification
105}
106
Roni Riskafc77ebb2019-09-26 08:20:44 +0300107func (suite *HTTPServerTestSuite) TestHttpServerXappNotif() {
Katri Turunen412df962019-09-16 08:48:18 +0300108 // start the "main loop" to receive the xAppNotification message from the HTTPServer
109 ch := make(chan []byte)
Roni Riskafc77ebb2019-09-26 08:20:44 +0300110 go readXAppNotification(suite.chNotif, ch)
Katri Turunen412df962019-09-16 08:48:18 +0300111
Roni Riskafc77ebb2019-09-26 08:20:44 +0300112 resp, reply := suite.doPost("http://"+suite.server.addr().String()+"/vesmgr_notif/", "test data")
Katri Turunen412df962019-09-16 08:48:18 +0300113 suite.Equal("", reply)
114 suite.Equal(200, resp.StatusCode)
115 suite.Equal("200 OK", resp.Status)
116 notification := <-ch
117 suite.Equal([]byte("test data"), notification)
118}
119
Roni Riskafc77ebb2019-09-26 08:20:44 +0300120func (suite *HTTPServerTestSuite) TestHttpServerXappNotifInvalidOperation() {
121 resp, reply := suite.doGet("http://" + suite.server.addr().String() + "/vesmgr_notif/")
Katri Turunen412df962019-09-16 08:48:18 +0300122 suite.Equal("405 method not allowed\n", reply)
123 suite.Equal(405, resp.StatusCode)
124 suite.Equal("405 Method Not Allowed", resp.Status)
125}
126
127func TestHttpServerSuite(t *testing.T) {
Roni Riskafc77ebb2019-09-26 08:20:44 +0300128 suite.Run(t, new(HTTPServerTestSuite))
Katri Turunen412df962019-09-16 08:48:18 +0300129}