Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 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. |
Roni Riska | 6ffba08 | 2019-11-27 10:59:54 +0200 | [diff] [blame^] | 16 | * |
| 17 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 18 | * platform project (RICP). |
| 19 | * |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 20 | */ |
| 21 | package main |
| 22 | |
| 23 | import ( |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 24 | "io/ioutil" |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 25 | "net/http" |
| 26 | "os" |
| 27 | "strings" |
| 28 | "testing" |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 29 | |
| 30 | "github.com/stretchr/testify/suite" |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 31 | ) |
| 32 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 33 | type HTTPServerTestSuite struct { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 34 | suite.Suite |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 35 | chNotif chan []byte |
| 36 | chSupervision chan chan string |
| 37 | server HTTPServer |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // suite setup creates the HTTP server |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 41 | func (suite *HTTPServerTestSuite) SetupSuite() { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 42 | os.Unsetenv("http_proxy") |
| 43 | os.Unsetenv("HTTP_PROXY") |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 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) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 49 | } |
| 50 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 51 | func (suite *HTTPServerTestSuite) TestHtppServerSupervisionInvalidOperation() { |
| 52 | resp, reply := suite.doPost("http://"+suite.server.addr().String()+SupervisionURL, "supervision") |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 53 | 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 Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 58 | func (suite *HTTPServerTestSuite) doGet(url string) (*http.Response, string) { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 59 | 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 Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 68 | func (suite *HTTPServerTestSuite) doPost(serverURL string, msg string) (*http.Response, string) { |
| 69 | resp, err := http.Post(serverURL, "data", strings.NewReader(msg)) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 70 | 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 Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 78 | func replySupervision(chSupervision chan chan string, reply string) { |
| 79 | chSupervisionAck := <-chSupervision |
| 80 | chSupervisionAck <- reply |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 81 | } |
| 82 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 83 | func (suite *HTTPServerTestSuite) TestHttpServerSupervision() { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 84 | |
| 85 | // start the "main loop" to reply to the supervision to the HTTPServer |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 86 | go replySupervision(suite.chSupervision, "I'm just fine") |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 87 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 88 | resp, reply := suite.doGet("http://" + suite.server.addr().String() + SupervisionURL) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 89 | |
| 90 | suite.Equal("I'm just fine", reply) |
| 91 | suite.Equal(200, resp.StatusCode) |
| 92 | suite.Equal("200 OK", resp.Status) |
| 93 | } |
| 94 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 95 | func (suite *HTTPServerTestSuite) TestHttpServerInvalidUrl() { |
| 96 | resp, reply := suite.doPost("http://"+suite.server.addr().String()+"/invalid_url", "foo") |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 97 | 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 Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 102 | func readXAppNotification(chNotif chan []byte, ch chan []byte) { |
| 103 | notification := <-chNotif |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 104 | ch <- notification |
| 105 | } |
| 106 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 107 | func (suite *HTTPServerTestSuite) TestHttpServerXappNotif() { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 108 | // start the "main loop" to receive the xAppNotification message from the HTTPServer |
| 109 | ch := make(chan []byte) |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 110 | go readXAppNotification(suite.chNotif, ch) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 111 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 112 | resp, reply := suite.doPost("http://"+suite.server.addr().String()+"/vesmgr_notif/", "test data") |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 113 | 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 Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 120 | func (suite *HTTPServerTestSuite) TestHttpServerXappNotifInvalidOperation() { |
| 121 | resp, reply := suite.doGet("http://" + suite.server.addr().String() + "/vesmgr_notif/") |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 122 | 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 | |
| 127 | func TestHttpServerSuite(t *testing.T) { |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 128 | suite.Run(t, new(HTTPServerTestSuite)) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 129 | } |