mosn

Форк
0
/
variable_router_test.go 
225 строк · 8.7 Кб
1
//go:build MOSNTest
2
// +build MOSNTest
3

4
package simple
5

6
import (
7
	"encoding/json"
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 TestVariableRoute1(t *testing.T) {
16
	Scenario(t, "variable route test", func() {
17
		// servers is invalid in `Case`
18
		_, servers := lib.InitMosn(VariableRouterConfigHTTP1, lib.CreateConfig(genMockHttpServerConfig("server1", "127.0.0.1:8080")),
19
			lib.CreateConfig(genMockHttpServerConfig("server2", "127.0.0.1:8081")))
20
		Case("client-mosn-mosn-server1", func() {
21
			client := lib.CreateClient("Http1", &http.HttpClientConfig{
22
				TargetAddr: "127.0.0.1:2045",
23
				Verify: &http.VerifyConfig{
24
					ExpectedStatusCode: 200,
25
					ExpectedHeader: map[string][]string{
26
						"server_address": []string{"127.0.0.1:8080"},
27
					},
28
					ExpectedBody: []byte("default-http1"),
29
				},
30
				Request: &http.RequestConfig{
31
					Method: "GET",
32
				},
33
			})
34
			Verify(client.SyncCall(), Equal, true)
35
			stats := client.Stats()
36
			Verify(stats.Requests(), Equal, 1)
37
			Verify(stats.ExpectedResponseCount(), Equal, 1)
38
		})
39
		Case("client-mosn-mosn-server2", func() {
40
			client := lib.CreateClient("Http1", &http.HttpClientConfig{
41
				TargetAddr: "127.0.0.1:2045",
42
				Verify: &http.VerifyConfig{
43
					ExpectedStatusCode: 200,
44
					ExpectedHeader: map[string][]string{
45
						"server_address": []string{"127.0.0.1:8081"},
46
					},
47
					ExpectedBody: []byte("default-http1"),
48
				},
49
				Request: &http.RequestConfig{
50
					Method: "POST",
51
				},
52
			})
53
			Verify(client.SyncCall(), Equal, true)
54
			stats := client.Stats()
55
			Verify(stats.Requests(), Equal, 1)
56
			Verify(stats.ExpectedResponseCount(), Equal, 1)
57
		})
58
		Case("client-mosn-server", func() {
59
			client := lib.CreateClient("Http1", &http.HttpClientConfig{
60
				TargetAddr: "127.0.0.1:2046",
61
				Verify: &http.VerifyConfig{
62
					ExpectedStatusCode: 200,
63
					ExpectedHeader: map[string][]string{
64
						"server_address": []string{"127.0.0.1:8080"},
65
					},
66
					ExpectedBody: []byte("default-http1"),
67
				},
68
			})
69
			Verify(client.SyncCall(), Equal, true)
70
			stats := client.Stats()
71
			Verify(stats.Requests(), Equal, 1)
72
			Verify(stats.ExpectedResponseCount(), Equal, 1)
73

74
		})
75
		Case("server-verify", func() {
76
			srv := servers[0]
77
			stats := srv.Stats()
78
			Verify(stats.ConnectionTotal(), Equal, 1)
79
			Verify(stats.ConnectionActive(), Equal, 1)
80
			Verify(stats.ConnectionClosed(), Equal, 0)
81
			Verify(stats.Requests(), Equal, 2)
82

83
		})
84
	})
85
}
86

87
func genMockHttpServerConfig(serverName string, addr string) string {
88
	config := lib.Config{
89
		Protocol: "Http1",
90
		Config: &http.HttpServerConfig{
91
			Addr: addr,
92
			Configs: map[string]*http.ResonseConfig{
93
				"/": &http.ResonseConfig{
94
					CommonBuilder: &http.ResponseBuilder{
95
						StatusCode: 200,
96
						Header: map[string][]string{
97
							"server_address": []string{addr},
98
						},
99
						Body: "default-http1",
100
					},
101
					ErrorBuilder: &http.ResponseBuilder{
102
						StatusCode: 500, // 500
103
					},
104
				},
105
			},
106
		},
107
	}
108
	s, _ := json.Marshal(config)
109
	return string(s)
110
}
111

112
const VariableRouterConfigHTTP1 = `{
113
        "servers":[
114
                {
115
                        "default_log_path":"stdout",
116
                        "default_log_level": "ERROR",
117
                        "routers": [
118
                                {
119
                                        "router_config_name":"router_to_mosn",
120
                                        "virtual_hosts":[{
121
                                                "name":"mosn_hosts",
122
                                                "domains": ["*"],
123
                                                 "routers": [
124
                                                        {
125
                                                                "match":{"prefix":"/"},
126
                                                                "route":{"cluster_name":"mosn_cluster"}
127
                                                        }
128
                                                ]
129
                                        }]
130
                                },
131
                                {
132
                                        "router_config_name":"router_to_server",
133
                                        "virtual_hosts":[{
134
                                                "name":"server_hosts",
135
                                                "domains": ["*"],
136
                                                "routers": [
137
                                                        {
138
                                                                "match":{
139
																	"variables":[{
140
																		"name":"x-mosn-method",
141
																		"value":"GET",
142
																		"model":"and"
143
																	}]
144
																},
145
                                                                "route":{"cluster_name":"server_cluster1"}
146
                                                        },
147
 														{
148
                                                                "match":{
149
																	"variables":[{
150
																		"name":"x-mosn-method",
151
																		"value":"POST",
152
																		"model":"and"
153
																	}]
154
																},
155
                                                                "route":{"cluster_name":"server_cluster2"}
156
                                                        }
157
                                                ]
158
                                        }]
159
                                }
160
                        ],
161
                        "listeners":[
162
                                {
163
                                        "address":"127.0.0.1:2045",
164
                                        "bind_port": true,
165
                                        "filter_chains": [{
166
                                                "filters": [
167
                                                        {
168
                                                                "type": "proxy",
169
                                                                "config": {
170
                                                                        "downstream_protocol": "Http1",
171
                                                                        "upstream_protocol": "Http1",
172
                                                                        "router_config_name":"router_to_mosn"
173
                                                                }
174
                                                        }
175
                                                ]
176
                                        }]
177
                                },
178
                                {
179
                                        "address":"127.0.0.1:2046",
180
                                        "bind_port": true,
181
                                        "filter_chains": [{
182
                                                "filters": [
183
                                                        {
184
                                                                "type": "proxy",
185
                                                                "config": {
186
                                                                        "downstream_protocol": "Http1",
187
                                                                        "upstream_protocol": "Http1",
188
                                                                        "router_config_name":"router_to_server"
189
                                                                }
190
                                                        }
191
                                                ]
192
                                        }]
193
                                }
194
                        ]
195
                }
196
        ],
197
        "cluster_manager":{
198
                "clusters":[
199
                        {
200
                                "name": "mosn_cluster",
201
                                "type": "SIMPLE",
202
                                "lb_type": "LB_RANDOM",
203
                                "hosts":[
204
                                        {"address":"127.0.0.1:2046"}
205
                                ]
206
                        },
207
                        {
208
                                "name": "server_cluster1",
209
                                "type": "SIMPLE",
210
                                "lb_type": "LB_RANDOM",
211
                                "hosts":[
212
                                        {"address":"127.0.0.1:8080"}
213
                                ]
214
                        },
215
						 {
216
                                "name": "server_cluster2",
217
                                "type": "SIMPLE",
218
                                "lb_type": "LB_RANDOM",
219
                                "hosts":[
220
                                        {"address":"127.0.0.1:8081"}
221
                                ]
222
                        }
223
                ]
224
        }
225
}`
226

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

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

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

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