10
. "mosn.io/mosn/test/framework"
11
"mosn.io/mosn/test/lib"
12
"mosn.io/mosn/test/lib/http"
15
func TestRouteExtendHandler(t *testing.T) {
16
Scenario(t, "mosn route handler with check, no host cluster will be ignore", func() {
17
ConfigExtendRoute := fmt.Sprintf(ConfigExtendRouteTmpl, "")
18
_, _ = lib.InitMosn(ConfigExtendRoute, lib.CreateConfig(MockHttpServerConfig))
19
Case("route extend", func() {
20
client := lib.CreateClient("Http1", &http.HttpClientConfig{
21
TargetAddr: "127.0.0.1:2046",
22
Request: &http.RequestConfig{
23
Header: map[string][]string{
24
"x-test-route": []string{"paas"},
25
"x-first": []string{"test"},
28
Verify: &http.VerifyConfig{
29
ExpectedStatusCode: 200, // x-first contains no host, fallback to x-test-route
32
Verify(client.SyncCall(), Equal, true)
34
Case("route only matched", func() {
35
client := lib.CreateClient("Http1", &http.HttpClientConfig{
36
TargetAddr: "127.0.0.1:2046",
37
Request: &http.RequestConfig{
38
Header: map[string][]string{
39
"x-first": []string{"test"},
42
Verify: &http.VerifyConfig{
43
ExpectedStatusCode: 404, // no route matched
46
Verify(client.SyncCall(), Equal, true)
48
Case("route default", func() {
49
client := lib.CreateClient("Http1", &http.HttpClientConfig{
50
TargetAddr: "127.0.0.1:2045",
51
Request: &http.RequestConfig{
52
Header: map[string][]string{
53
"x-test-route": []string{"paas"},
54
"x-first": []string{"test"},
57
Verify: &http.VerifyConfig{
58
ExpectedStatusCode: 502, // choose a cluster, but no hosts
61
Verify(client.SyncCall(), Equal, true)
66
func TestRouteExtendHandler2(t *testing.T) {
67
Scenario(t, "mosn route handler withcheck, ", func() {
68
ConfigExtendRoute := fmt.Sprintf(ConfigExtendRouteTmpl, `{"address":"127.0.0.1:8081"}`) // a host that no listened
69
_, _ = lib.InitMosn(ConfigExtendRoute, lib.CreateConfig(MockHttpServerConfig))
70
Case("route extend", func() {
71
client := lib.CreateClient("Http1", &http.HttpClientConfig{
72
TargetAddr: "127.0.0.1:2046",
73
Request: &http.RequestConfig{
74
Header: map[string][]string{
75
"x-test-route": []string{"paas"},
76
"x-first": []string{"test"},
79
Verify: &http.VerifyConfig{
80
ExpectedStatusCode: 502, // connection failed
83
Verify(client.SyncCall(), Equal, true)
85
Case("route default", func() {
86
client := lib.CreateClient("Http1", &http.HttpClientConfig{
87
TargetAddr: "127.0.0.1:2045",
88
Request: &http.RequestConfig{
89
Header: map[string][]string{
90
"x-test-route": []string{"paas"},
91
"x-first": []string{"test"},
94
Verify: &http.VerifyConfig{
95
ExpectedStatusCode: 502,
98
Verify(client.SyncCall(), Equal, true)
103
const ConfigExtendRouteTmpl = `{
106
"default_log_path":"stdout",
107
"default_log_level": "ERROR",
110
"router_config_name":"router_to_server",
112
"name":"server_hosts",
116
"match":{"headers":[{"name":"x-first","value":"test"}]},
117
"route":{"cluster_name":"priority_cluster"}
120
"match":{"headers":[{"name":"x-test-route","value":"paas"}]},
121
"route":{"cluster_name":"server_cluster"}
129
"address":"127.0.0.1:2046",
136
"downstream_protocol": "Http1",
137
"upstream_protocol": "Http1",
138
"router_config_name":"router_to_server",
139
"router_handler_name": "check-handler"
146
"address":"127.0.0.1:2045",
153
"downstream_protocol": "Http1",
154
"upstream_protocol": "Http1",
155
"router_config_name":"router_to_server"
167
"name": "server_cluster",
169
"lb_type": "LB_RANDOM",
171
{"address":"127.0.0.1:8080"}
175
"name": "priority_cluster",
177
"lb_type": "LB_RANDOM",