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).
33 "github.com/stretchr/testify/suite"
36 type do func(w http.ResponseWriter)
38 type QueryXAppsConfigTestSuite struct {
44 // suite setup creates the HTTP server
45 func (suite *QueryXAppsConfigTestSuite) SetupSuite() {
46 os.Unsetenv("http_proxy")
47 os.Unsetenv("HTTP_PROXY")
49 suite.listener, err = net.Listen("tcp", ":0")
51 go runXAppMgr(suite.listener, "/test_url/", suite)
54 func runXAppMgr(listener net.Listener, url string, suite *QueryXAppsConfigTestSuite) {
56 http.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
62 http.Serve(listener, nil)
65 func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigFailsWithTimeout() {
66 doSleep := func(w http.ResponseWriter) {
67 time.Sleep(time.Second * 2)
69 suite.xAppMgrFunc = doSleep
71 data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1)
72 suite.Equal([]byte("{}"), data)
74 e, ok := err.(*url.Error)
76 suite.Equal(e.Timeout(), true)
79 func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigFailsWithAnErrorReply() {
80 doReplyWithErr := func(w http.ResponseWriter) {
81 http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed)
83 suite.xAppMgrFunc = doReplyWithErr
85 data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1)
86 suite.Equal([]byte("{}"), data)
88 suite.Equal("405 Method Not Allowed", err.Error())
91 func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigOk() {
92 doReply := func(w http.ResponseWriter) {
93 fmt.Fprintf(w, "reply message")
95 suite.xAppMgrFunc = doReply
97 data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1)
100 suite.Equal(data, []byte("reply message"))
103 func TestQueryXAppsConfigTestSuite(t *testing.T) {
104 suite.Run(t, new(QueryXAppsConfigTestSuite))