-
Notifications
You must be signed in to change notification settings - Fork 3
/
package.go
176 lines (161 loc) · 5.7 KB
/
package.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2023 Cisco Systems, Inc. and its affiliates
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0
package webtest
import (
"context"
"fmt"
"github.com/cisco-open/go-lanai/pkg/log"
"github.com/cisco-open/go-lanai/pkg/web"
webinit "github.com/cisco-open/go-lanai/pkg/web/init"
"github.com/cisco-open/go-lanai/test"
"github.com/cisco-open/go-lanai/test/apptest"
"go.uber.org/fx"
"testing"
)
const (
DefaultContextPath = "/test"
)
// WithRealServer start a real web server at random port with context-path as DefaultContextPath.
// NewRequest(), Exec() and MustExec() can be used to create/send request and verifying result
// By default, the server doesn't allow CORS and have no security configured.
// Actual server port can be retrieved via CurrentPort()
// When using this mode, *web.Registrar became available to inject
func WithRealServer(opts ...TestServerOptions) test.Options {
conf := TestServerConfig{
ContextPath: DefaultContextPath,
LogLevel: log.LevelInfo,
}
for _, fn := range opts {
fn(&conf)
}
props := toProperties(&conf)
di := realSvrDI{}
return test.WithOptions(
apptest.WithModules(webinit.Module),
apptest.WithProperties(props...),
apptest.WithDI(&di),
test.SubTestSetup(testSetupAddrExtractor(&conf, &di)),
)
}
// WithMockedServer initialize web package without starting an actual web server.
// NewRequest(), Exec() and MustExec() can be used to create/send request and verifying result without creating an actual http connection.
// By default, the server doesn't allow CORS and have no security configured.
// When using this mode, *web.Registrar became available to inject
// Note: In this mode, httptest package is used internally and http.Handler (*web.Engine in our case) is invoked directly
func WithMockedServer(opts ...TestServerOptions) test.Options {
conf := TestServerConfig{
ContextPath: DefaultContextPath,
LogLevel: log.LevelInfo,
}
for _, fn := range opts {
fn(&conf)
}
props := toProperties(&conf)
di := mockedSvrDI{}
return test.WithOptions(
apptest.WithModules(mockedWebModule),
apptest.WithProperties(props...),
apptest.WithDI(&di),
test.SubTestSetup(testSetupEngineExtractor(&conf, &di)),
)
}
// WithUtilities DOES NOT initialize web package, it only provide properties and setup utilities (e.g. MustExec)
// Important: this mode is mostly for go-lanai internal tests. DO NOT use it in microservices
//
// NewRequest(), Exec() and MustExec() can be used to create/send request and verifying result without creating an actual http connection.
// Note: In this mode, httptest package is used internally and http.Handler (*web.Engine in our case) is invoked directly
func WithUtilities(opts ...TestServerOptions) test.Options {
conf := TestServerConfig{
ContextPath: DefaultContextPath,
LogLevel: log.LevelInfo,
}
for _, fn := range opts {
fn(&conf)
}
props := toProperties(&conf)
di := mockedSvrDI{}
return test.WithOptions(
apptest.WithProperties(props...),
apptest.WithFxOptions(
fx.Provide(
web.BindServerProperties,
),
),
apptest.WithDI(&di),
test.SubTestSetup(testSetupEngineExtractor(&conf, &di)),
)
}
// UsePort returns a TestServerOptions that use given port.
// Note: using fixed port might cause issues when run in CI/CD
func UsePort(port int) TestServerOptions {
return func(conf *TestServerConfig) {
conf.Port = port
}
}
// UseContextPath returns a TestServerOptions that overwrite the context-path of the test server
func UseContextPath(contextPath string) TestServerOptions {
return func(conf *TestServerConfig) {
conf.ContextPath = contextPath
}
}
// UseLogLevel returns a TestServerOptions that overwrite the default log level of the test server
func UseLogLevel(lvl log.LoggingLevel) TestServerOptions {
return func(conf *TestServerConfig) {
conf.LogLevel = lvl
}
}
// AddDefaultRequestOptions returns a TestServerOptions that add default RequestOptions on every request
// created via NewRequest.
func AddDefaultRequestOptions(opts...RequestOptions) TestServerOptions {
return func(conf *TestServerConfig) {
conf.RequestOptions = append(conf.RequestOptions, opts...)
}
}
type realSvrDI struct {
fx.In
Registrar *web.Registrar
Engine *web.Engine
}
type mockedSvrDI struct {
fx.In
Engine *web.Engine `optional:"true"`
}
func toProperties(conf *TestServerConfig) []string {
return []string{
fmt.Sprintf("server.port: %d", conf.Port),
fmt.Sprintf("server.context-path: %s", conf.ContextPath),
fmt.Sprintf("server.logging.default-level: %s", conf.LogLevel.String()),
"server.logging.enabled: true",
}
}
func testSetupAddrExtractor(conf *TestServerConfig, di *realSvrDI) test.SetupFunc {
return func(ctx context.Context, t *testing.T) (context.Context, error) {
info := serverInfo{
hostname: "127.0.0.1",
port: di.Registrar.ServerPort(),
contextPath: conf.ContextPath,
}
return newWebTestContext(ctx, conf, &info, nil), nil
}
}
func testSetupEngineExtractor(conf *TestServerConfig, di *mockedSvrDI) test.SetupFunc {
return func(ctx context.Context, t *testing.T) (context.Context, error) {
info := serverInfo{
contextPath: conf.ContextPath,
}
return newWebTestContext(ctx, conf, &info, di.Engine), nil
}
}