/
http.go
98 lines (86 loc) · 3.72 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2020 Red Hat, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package helpers
import (
"testing"
"github.com/RedHatInsights/insights-operator-utils/tests/helpers"
"github.com/RedHatInsights/insights-results-aggregator/server"
"github.com/RedHatInsights/insights-results-aggregator/storage"
)
// APIRequest is a type for APIRequest
type APIRequest = helpers.APIRequest
// APIResponse is a type for APIResponse
type APIResponse = helpers.APIResponse
var (
// ExecuteRequest executes request
ExecuteRequest = helpers.ExecuteRequest
// CheckResponseBodyJSON checks response body
CheckResponseBodyJSON = helpers.CheckResponseBodyJSON
// AssertReportResponsesEqual fails if report responses aren't equal
AssertReportResponsesEqual = helpers.AssertReportResponsesEqual
// AssertReportResponsesEqualCustomElementsChecker fails if report responses aren't equal
AssertReportResponsesEqualCustomElementsChecker = helpers.AssertReportResponsesEqualCustomElementsChecker
// AssertRuleResponsesEqual fails if rule responses aren't equal
AssertRuleResponsesEqual = helpers.AssertRuleResponsesEqual
// NewGockRequestMatcher returns a new matcher for github.com/h2non/gock to match requests
// with provided method, url and jsonBody
NewGockRequestMatcher = helpers.NewGockRequestMatcher
// GockExpectAPIRequest makes gock expect the request with the baseURL and sends back the response
GockExpectAPIRequest = helpers.GockExpectAPIRequest
// MakeXRHTokenString converts types.Token to a token string(base64 encoded)
MakeXRHTokenString = helpers.MakeXRHTokenString
)
// DefaultServerConfig is a default config used by AssertAPIRequest
var DefaultServerConfig = server.Configuration{
Address: ":8080",
APIPrefix: "/api/test/",
APISpecFile: "openapi.json",
Debug: true,
Auth: false,
MaximumFeedbackMessageLength: 255,
OrgOverviewLimitHours: 2,
}
// DefaultServerConfigAuth is a default config used by AssertAPIRequest with authentication set to true
var DefaultServerConfigAuth = server.Configuration{
Address: ":8080",
APIPrefix: "/api/test/",
APISpecFile: "openapi.json",
Debug: true,
Auth: true,
AuthType: "xrh",
MaximumFeedbackMessageLength: 255,
OrgOverviewLimitHours: 2,
}
// AssertAPIRequest creates new server with provided mockStorage
// (which you can keep nil so it will be created automatically)
// and provided serverConfig(you can leave it empty to use the default one)
// sends api request and checks api response (see docs for APIRequest and APIResponse)
func AssertAPIRequest(
t testing.TB,
mockStorage storage.OCPRecommendationsStorage,
serverConfig *server.Configuration,
request *helpers.APIRequest,
expectedResponse *helpers.APIResponse,
) {
if mockStorage == nil {
var closer func()
mockStorage, closer = MustGetPostgresStorage(t, true)
defer closer()
}
if serverConfig == nil {
serverConfig = &DefaultServerConfig
}
testServer := server.New(*serverConfig, mockStorage, nil)
helpers.AssertAPIRequest(t, testServer, serverConfig.APIPrefix, request, expectedResponse)
}