-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent_test.go
More file actions
150 lines (121 loc) · 3.56 KB
/
component_test.go
File metadata and controls
150 lines (121 loc) · 3.56 KB
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
package platform_component
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"github.com/nats-io/nkeys"
)
type testData struct {
Test string
Ing int
}
func startAPIServer(t testing.TB) (*server.Server, *httptest.Server) {
t.Helper()
natsServer, err := startNatsServer(t)
if err != nil {
t.Fatalf("failed to start nats server: %v", err)
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "only POST allowed", http.StatusMethodNotAllowed)
return
}
defer r.Body.Close()
req_body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "failed to read body", http.StatusInternalServerError)
return
}
req_data := ConnectRequest{}
err = json.Unmarshal(req_body, &req_data)
if err != nil {
http.Error(w, "failed to unmarshal body", http.StatusInternalServerError)
return
}
connect := &testData{}
err = json.Unmarshal(req_data.Data, connect)
if err != nil {
http.Error(w, "failed to unmarshal test config", http.StatusInternalServerError)
return
}
if !nkeys.IsValidPublicUserKey(req_data.NKey) {
http.Error(w, "failed to verify public user nkey", http.StatusInternalServerError)
return
}
if connect.Test != "testing" {
http.Error(w, "unexpected platform config", http.StatusInternalServerError)
return
}
if connect.Ing != -1 {
http.Error(w, "unexpected platform config", http.StatusInternalServerError)
return
}
pc, err := json.Marshal(map[string]string{"Bucket": "bucket"})
if err != nil {
http.Error(w, "failed to marshall platform config", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
response := ConnectResponse{
JWT: "JWT123",
Account: "CACCOUNT",
Server: natsServer.ClientURL(),
Config: pc,
}
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
return natsServer, httptest.NewServer(handler)
}
func startNatsServer(t testing.TB) (*server.Server, error) {
t.Helper()
require := require.New(t)
s, err := server.NewServer(&server.Options{
Port: -1,
JetStream: true,
StoreDir: t.TempDir(),
})
if err != nil {
t.Fatalf("failed to create server: %v", err)
}
s.Start()
time.Sleep(500 * time.Millisecond)
nc, err := nats.Connect(s.ClientURL())
require.NoError(err, "failed to connect to server")
js, err := jetstream.New(nc)
require.NoError(err, "failed to create jetstream context")
_, err = js.CreateKeyValue(context.TODO(), jetstream.KeyValueConfig{
Bucket: "bucket",
})
require.NoError(err, "failed to create kv")
_ = nc.Drain()
return s, nil
}
func TestRegister(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
natsserver, apiServer := startAPIServer(t)
defer apiServer.Close()
defer natsserver.Shutdown()
c := Component("test", nil)
err := c.Register("asdf", WithURL(apiServer.URL))
assert.Contains(err.Error(), "failed to unmarshal test config")
err = c.Register("asdf", WithURL(apiServer.URL), WithData(&testData{Test: "testing", Ing: -1}))
require.Nil(err)
require.NotEmpty(c.Config().Server)
require.Equal("JWT123", c.Config().JWT)
require.Equal("CACCOUNT", c.Config().Account)
require.NotNil(c.Config().Config)
require.NoError(c.Start(context.Background()))
require.NoError(c.Stop())
}