2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: Nordix Foundation. All rights reserved.
6 // Copyright (C) 2023 OpenInfra Foundation Europe. All rights reserved.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 // ========================LICENSE_END===================================
32 "github.com/deepmap/oapi-codegen/pkg/testutil"
33 "github.com/getkin/kin-openapi/openapi3"
34 "github.com/labstack/echo/v4"
35 "github.com/stretchr/testify/assert"
36 "oransc.org/nonrtric/capifcore"
37 "oransc.org/nonrtric/capifcore/internal/common29122"
40 func Test_routing(t *testing.T) {
42 capifcore.RegisterHandlers(e, nil, nil)
57 returnStatus: http.StatusOK,
62 name: "Provider path",
64 url: "/api-provider-management/v1/registrations/provider",
65 returnStatus: http.StatusNoContent,
72 url: "/published-apis/v1/apfId/service-apis/serviceId",
73 returnStatus: http.StatusNotFound,
78 name: "Discover path",
80 url: "/service-apis/v1/allServiceAPIs?api-invoker-id=api_invoker_id",
81 returnStatus: http.StatusNotFound,
88 url: "/api-invoker-management/v1/onboardedInvokers/invoker",
89 returnStatus: http.StatusNoContent,
96 url: "/capif-events/v1/subscriberId/subscriptions/subId",
97 returnStatus: http.StatusNoContent,
102 name: "Security path",
104 url: "/capif-security/v1/trustedInvokers/apiInvokerId",
105 returnStatus: http.StatusNotFound,
110 for _, tt := range tests {
111 t.Run(tt.name, func(t *testing.T) {
112 var result *testutil.CompletedRequest
113 if tt.args.method == "GET" {
114 result = testutil.NewRequest().Get(tt.args.url).Go(t, e)
115 } else if tt.args.method == "DELETE" {
116 result = testutil.NewRequest().Delete(tt.args.url).Go(t, e)
119 assert.Equal(t, tt.args.returnStatus, result.Code(), tt.name)
124 func TestGetSwagger(t *testing.T) {
126 capifcore.RegisterHandlers(e, nil, nil)
137 name: "Provider api",
165 name: "Discover api",
172 name: "Security api",
179 for _, tt := range tests {
180 t.Run(tt.name, func(t *testing.T) {
181 result := testutil.NewRequest().Get("/swagger/"+tt.args.apiPath).Go(t, e)
182 assert.Equal(t, http.StatusOK, result.Code())
183 var swaggerResponse openapi3.T
184 err := result.UnmarshalJsonToObject(&swaggerResponse)
186 assert.Contains(t, swaggerResponse.Info.Title, tt.args.apiName)
189 invalidApi := "foobar"
190 result := testutil.NewRequest().Get("/swagger/"+invalidApi).Go(t, e)
191 assert.Equal(t, http.StatusBadRequest, result.Code())
192 var errorResponse common29122.ProblemDetails
193 err := result.UnmarshalJsonToObject(&errorResponse)
195 assert.Contains(t, *errorResponse.Cause, "Invalid API")
196 assert.Contains(t, *errorResponse.Cause, invalidApi)
199 func TestHTTPSServer(t *testing.T) {
201 capifcore.RegisterHandlers(e, nil, nil)
204 go startHttpsWebServer(e, 44333, "../certs/cert.pem", "../certs/key.pem") //"certs/test/cert.pem", "certs/test/key.pem"
206 time.Sleep(100 * time.Millisecond)
208 tr := &http.Transport{
209 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
212 client := &http.Client{Transport: tr}
213 res, err := client.Get(fmt.Sprintf("https://localhost:%d", port))
218 defer res.Body.Close()
219 assert.Equal(t, res.StatusCode, res.StatusCode)
221 body, err := io.ReadAll(res.Body)
226 expected := []byte("Hello, World!")
227 assert.Equal(t, expected, body)