-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
test.go
129 lines (112 loc) · 3.46 KB
/
test.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
package test
import (
"context"
"encoding/json"
"io"
"net/http"
"os"
"sync"
"time"
"github.com/plgd-dev/hub/v2/grpc-gateway/pb"
"github.com/plgd-dev/hub/v2/http-gateway/service"
"github.com/plgd-dev/hub/v2/http-gateway/uri"
"github.com/plgd-dev/hub/v2/pkg/fsnotify"
"github.com/plgd-dev/hub/v2/pkg/log"
pkgHttp "github.com/plgd-dev/hub/v2/pkg/net/http"
"github.com/plgd-dev/hub/v2/test/config"
testHttp "github.com/plgd-dev/hub/v2/test/http"
"github.com/plgd-dev/kit/v2/codec/cbor"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protojson"
)
func MakeWebConfigurationConfig() service.WebConfiguration {
return service.WebConfiguration{
Authority: testHttp.HTTPS_SCHEME + config.OAUTH_SERVER_HOST,
HTTPGatewayAddress: testHttp.HTTPS_SCHEME + config.HTTP_GW_HOST,
DeviceProvisioningService: testHttp.HTTPS_SCHEME + config.HTTP_GW_HOST,
WebOAuthClient: service.BasicOAuthClient{
ClientID: config.OAUTH_MANAGER_CLIENT_ID,
Audience: config.OAUTH_MANAGER_AUDIENCE,
Scopes: []string{"openid", "offline_access"},
},
DeviceOAuthClient: service.DeviceOAuthClient{
BasicOAuthClient: service.BasicOAuthClient{
ClientID: config.OAUTH_MANAGER_CLIENT_ID,
Audience: config.OAUTH_MANAGER_AUDIENCE,
Scopes: []string{"profile", "openid", "offline_access"},
},
ProviderName: config.DEVICE_PROVIDER,
},
}
}
func MakeConfig(t require.TestingT, enableUI bool) service.Config {
var cfg service.Config
cfg.Log = log.MakeDefaultConfig()
cfg.APIs.HTTP.Authorization = config.MakeAuthorizationConfig()
cfg.APIs.HTTP.Connection = config.MakeListenerConfig(config.HTTP_GW_HOST)
cfg.APIs.HTTP.Connection.TLS.ClientCertificateRequired = false
cfg.APIs.HTTP.WebSocket.StreamBodyLimit = 256 * 1024
cfg.APIs.HTTP.WebSocket.PingFrequency = 10 * time.Second
cfg.APIs.HTTP.Server = config.MakeHttpServerConfig()
cfg.Clients.GrpcGateway.Connection = config.MakeGrpcClientConfig(config.GRPC_GW_HOST)
cfg.Clients.OpenTelemetryCollector = pkgHttp.OpenTelemetryCollectorConfig{
Config: config.MakeOpenTelemetryCollectorClient(),
}
if enableUI {
cfg.UI.Enabled = true
cfg.UI.Directory = os.Getenv("TEST_HTTP_GW_WWW_ROOT")
cfg.UI.WebConfiguration = MakeWebConfigurationConfig()
}
err := cfg.Validate()
require.NoError(t, err)
return cfg
}
func SetUp(t require.TestingT) (tearDown func()) {
return New(t, MakeConfig(t, false))
}
func New(t require.TestingT, cfg service.Config) func() {
ctx := context.Background()
logger := log.NewLogger(cfg.Log)
fileWatcher, err := fsnotify.NewWatcher(logger)
require.NoError(t, err)
s, err := service.New(ctx, cfg, fileWatcher, logger)
require.NoError(t, err)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
_ = s.Serve()
}()
return func() {
_ = s.Close()
wg.Wait()
err = fileWatcher.Close()
require.NoError(t, err)
}
}
func GetContentData(content *pb.Content, desiredContentType string) ([]byte, error) {
if desiredContentType == uri.ApplicationProtoJsonContentType {
data, err := protojson.Marshal(content)
if err != nil {
return nil, err
}
return data, err
}
v, err := cbor.ToJSON(content.GetData())
if err != nil {
return nil, err
}
return []byte(v), err
}
func UnmarshalJson(code int, input io.Reader, v any) error {
var data json.RawMessage
err := json.NewDecoder(input).Decode(&data)
if err != nil {
return err
}
if code != http.StatusOK {
return UnmarshalError(data)
}
err = json.Unmarshal(data, v)
return err
}