mosn

Форк
0
/
route_extend_handler_test.go 
184 строки · 6.1 Кб
1
//go:build MOSNTest
2
// +build MOSNTest
3

4
package route
5

6
import (
7
	"fmt"
8
	"testing"
9

10
	. "mosn.io/mosn/test/framework"
11
	"mosn.io/mosn/test/lib"
12
	"mosn.io/mosn/test/lib/http"
13
)
14

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"},
26
					},
27
				},
28
				Verify: &http.VerifyConfig{
29
					ExpectedStatusCode: 200, // x-first contains no host, fallback to x-test-route
30
				},
31
			})
32
			Verify(client.SyncCall(), Equal, true)
33
		})
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"},
40
					},
41
				},
42
				Verify: &http.VerifyConfig{
43
					ExpectedStatusCode: 404, // no route matched
44
				},
45
			})
46
			Verify(client.SyncCall(), Equal, true)
47
		})
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"},
55
					},
56
				},
57
				Verify: &http.VerifyConfig{
58
					ExpectedStatusCode: 502, // choose a cluster, but no hosts
59
				},
60
			})
61
			Verify(client.SyncCall(), Equal, true)
62
		})
63
	})
64
}
65

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"},
77
					},
78
				},
79
				Verify: &http.VerifyConfig{
80
					ExpectedStatusCode: 502, // connection failed
81
				},
82
			})
83
			Verify(client.SyncCall(), Equal, true)
84
		})
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"},
92
					},
93
				},
94
				Verify: &http.VerifyConfig{
95
					ExpectedStatusCode: 502,
96
				},
97
			})
98
			Verify(client.SyncCall(), Equal, true)
99
		})
100
	})
101
}
102

103
const ConfigExtendRouteTmpl = `{
104
        "servers":[
105
                {
106
                        "default_log_path":"stdout",
107
                        "default_log_level": "ERROR",
108
                        "routers": [
109
                                {
110
                                        "router_config_name":"router_to_server",
111
                                        "virtual_hosts":[{
112
                                                "name":"server_hosts",
113
                                                "domains": ["*"],
114
                                                "routers": [
115
							{
116
								"match":{"headers":[{"name":"x-first","value":"test"}]},
117
								"route":{"cluster_name":"priority_cluster"}
118
							},
119
                                                        {
120
                                                                "match":{"headers":[{"name":"x-test-route","value":"paas"}]},
121
                                                                "route":{"cluster_name":"server_cluster"}
122
                                                        }
123
                                                ]
124
                                        }]
125
                                }
126
                        ],
127
                        "listeners":[
128
                                {
129
                                        "address":"127.0.0.1:2046",
130
                                        "bind_port": true,
131
                                        "filter_chains": [{
132
                                                "filters": [
133
                                                        {
134
                                                                "type": "proxy",
135
                                                                "config": {
136
                                                                        "downstream_protocol": "Http1",
137
                                                                        "upstream_protocol": "Http1",
138
                                                                        "router_config_name":"router_to_server",
139
									"router_handler_name": "check-handler"
140
                                                                }
141
                                                        }
142
                                                ]
143
                                        }]
144
                                },
145
				{
146
					"address":"127.0.0.1:2045",
147
					"bind_port": true,
148
					"filter_chains": [{
149
						"filters": [
150
							{
151
								"type": "proxy",
152
								"config": {
153
									"downstream_protocol": "Http1",
154
									"upstream_protocol": "Http1",
155
									"router_config_name":"router_to_server"
156
								}
157
							}
158
						]
159
					}]
160
				}
161
                        ]
162
                }
163
        ],
164
        "cluster_manager":{
165
                "clusters":[
166
                        {
167
                                "name": "server_cluster",
168
                                "type": "SIMPLE",
169
                                "lb_type": "LB_RANDOM",
170
                                "hosts":[
171
                                        {"address":"127.0.0.1:8080"}
172
                                ]
173
                        },
174
			{
175
				"name": "priority_cluster",
176
				"type": "SIMPLE",
177
				"lb_type": "LB_RANDOM",
178
				"hosts":[
179
					%s
180
				]
181
			}
182
                ]
183
        }
184
}`
185

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.