2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
31 "github.com/deepmap/oapi-codegen/pkg/testutil"
32 "github.com/getkin/kin-openapi/openapi3"
33 "github.com/labstack/echo/v4"
34 "github.com/stretchr/testify/assert"
35 "oransc.org/nonrtric/capifcore/internal/common29122"
40 func Test_routing(t *testing.T) {
56 returnStatus: http.StatusOK,
61 name: "Provider path",
63 url: "/api-provider-management/v1/registrations/provider",
64 returnStatus: http.StatusNoContent,
71 url: "/published-apis/v1/apfId/service-apis/serviceId",
72 returnStatus: http.StatusNotFound,
77 name: "Discover path",
79 url: "/service-apis/v1/allServiceAPIs?api-invoker-id=api_invoker_id",
80 returnStatus: http.StatusNotFound,
87 url: "/api-invoker-management/v1/onboardedInvokers/invoker",
88 returnStatus: http.StatusNoContent,
95 url: "/capif-events/v1/subscriberId/subscriptions/subId",
96 returnStatus: http.StatusNoContent,
101 name: "Security path",
103 url: "/capif-security/v1/trustedInvokers/apiInvokerId",
104 returnStatus: http.StatusNotImplemented,
109 for _, tt := range tests {
110 t.Run(tt.name, func(t *testing.T) {
111 var result *testutil.CompletedRequest
112 if tt.args.method == "GET" {
113 result = testutil.NewRequest().Get(tt.args.url).Go(t, e)
114 } else if tt.args.method == "DELETE" {
115 result = testutil.NewRequest().Delete(tt.args.url).Go(t, e)
118 assert.Equal(t, tt.args.returnStatus, result.Code(), tt.name)
123 func TestGetSwagger(t *testing.T) {
135 name: "Provider api",
163 name: "Discover api",
170 name: "Security api",
177 for _, tt := range tests {
178 t.Run(tt.name, func(t *testing.T) {
179 result := testutil.NewRequest().Get("/swagger/"+tt.args.apiPath).Go(t, e)
180 assert.Equal(t, http.StatusOK, result.Code())
181 var swaggerResponse openapi3.T
182 err := result.UnmarshalJsonToObject(&swaggerResponse)
184 assert.Contains(t, swaggerResponse.Info.Title, tt.args.apiName)
187 invalidApi := "foobar"
188 result := testutil.NewRequest().Get("/swagger/"+invalidApi).Go(t, e)
189 assert.Equal(t, http.StatusBadRequest, result.Code())
190 var errorResponse common29122.ProblemDetails
191 err := result.UnmarshalJsonToObject(&errorResponse)
193 assert.Contains(t, *errorResponse.Cause, "Invalid API")
194 assert.Contains(t, *errorResponse.Cause, invalidApi)
197 func TestHTTPSServer(t *testing.T) {
200 go startHttpsWebServer(e, 44333, "certs/cert.pem", "certs/key.pem") //"certs/test/cert.pem", "certs/test/key.pem"
202 time.Sleep(100 * time.Millisecond)
204 tr := &http.Transport{
205 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
208 client := &http.Client{Transport: tr}
209 res, err := client.Get(fmt.Sprintf("https://localhost:%d", port))
214 defer res.Body.Close()
215 assert.Equal(t, res.StatusCode, res.StatusCode)
217 body, err := io.ReadAll(res.Body)
222 expected := []byte("Hello, World!")
223 assert.Equal(t, expected, body)