-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
routeralias.go
192 lines (174 loc) · 4.58 KB
/
routeralias.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
Copyright 2023 The Dapr Authors
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 implieh.
See the License for the specific language governing permissions and
limitations under the License.
*/
package app
import (
"context"
"fmt"
"io"
nethttp "net/http"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/dapr/dapr/tests/integration/framework"
"github.com/dapr/dapr/tests/integration/framework/process/daprd"
prochttp "github.com/dapr/dapr/tests/integration/framework/process/http"
"github.com/dapr/dapr/tests/integration/framework/util"
"github.com/dapr/dapr/tests/integration/suite"
)
func init() {
suite.Register(new(routeralias))
}
type routeralias struct {
daprd1 *daprd.Daprd
daprd2 *daprd.Daprd
resDir string
}
func (r *routeralias) Setup(t *testing.T) []framework.Option {
configFile := filepath.Join(t.TempDir(), "config.yaml")
require.NoError(t, os.WriteFile(configFile, []byte(`
apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: hotreloading
spec:
features:
- name: HotReload
enabled: true
appHttpPipeline:
handlers:
- name: routeralias1
type: middleware.http.routeralias
- name: routeralias2
type: middleware.http.routeralias
- name: routeralias3
type: middleware.http.routeralias
`), 0o600))
r.resDir = t.TempDir()
srv := func(id string) *prochttp.HTTP {
handler := nethttp.NewServeMux()
handler.HandleFunc("/", func(w nethttp.ResponseWriter, r *nethttp.Request) {
fmt.Fprintf(w, "%s:%s", id, r.URL.Path)
})
return prochttp.New(t, prochttp.WithHandler(handler))
}
srv1 := srv("daprd1")
srv2 := srv("daprd2")
r.daprd1 = daprd.New(t,
daprd.WithAppPort(srv1.Port()),
)
r.daprd2 = daprd.New(t,
daprd.WithConfigs(configFile),
daprd.WithResourcesDir(r.resDir),
daprd.WithAppPort(srv2.Port()),
)
require.NoError(t, os.WriteFile(filepath.Join(r.resDir, "res.yaml"), []byte(`
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: routeralias1
spec:
type: middleware.http.routeralias
version: v1
metadata:
- name: routes
value: '{
"/helloworld": "/foobar",
"/xyz": "/abc"
}'
---
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: routeralias2
spec:
type: middleware.http.routeralias
version: v1
metadata:
- name: routes
value: '{
"/helloworld": "/xyz",
"/foobar": "/abc"
}'
`), 0o600))
return []framework.Option{
framework.WithProcesses(srv1, srv2, r.daprd1, r.daprd2),
}
}
func (r *routeralias) Run(t *testing.T, ctx context.Context) {
r.daprd1.WaitUntilAppHealth(t, ctx)
r.daprd2.WaitUntilAppHealth(t, ctx)
client := util.HTTPClient(t)
r.doReq(t, ctx, client, fmt.Sprintf("/v1.0/invoke/%s/method/helloworld", r.daprd2.AppID()), "daprd2:/abc")
require.NoError(t, os.WriteFile(filepath.Join(r.resDir, "res.yaml"), []byte(`
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: routeralias1
spec:
type: middleware.http.routeralias
version: v1
metadata:
- name: routes
value: '{
"/abc": "/barfoo",
"/xyz": "/abc"
}'
---
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: routeralias2
spec:
type: middleware.http.routeralias
version: v1
metadata:
- name: routes
value: '{
"/helloworld": "/xyz",
"/foobar": "/abc"
}'
---
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: routeralias3
spec:
type: middleware.http.routeralias
version: v1
metadata:
- name: routes
value: '{
"/helloworld": "/eee",
"/foobar": "/fff",
"/xyz": "/aaa"
}'
`), 0o600))
assert.EventuallyWithT(t, func(c *assert.CollectT) {
r.doReq(c, ctx, client, fmt.Sprintf("/v1.0/invoke/%s/method/helloworld", r.daprd2.AppID()), "daprd2:/aaa")
}, time.Second*10, time.Millisecond*10)
}
func (r *routeralias) doReq(t require.TestingT, ctx context.Context, client *nethttp.Client, path, expect string) {
url := fmt.Sprintf("http://localhost:%d/%s", r.daprd1.HTTPPort(), path)
req, err := nethttp.NewRequestWithContext(ctx, nethttp.MethodPost, url, nil)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, nethttp.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, expect, string(body))
}