mosn

Форк
0
/
http1_test.go 
414 строк · 12.7 Кб
1
//go:build MOSNTest
2
// +build MOSNTest
3

4
package simple
5

6
import (
7
	"context"
8
	"crypto/tls"
9
	"net"
10
	goHttp "net/http"
11
	"testing"
12
	"time"
13

14
	"golang.org/x/net/http2"
15
	"mosn.io/mosn/pkg/module/http2/h2c"
16
	. "mosn.io/mosn/test/framework"
17
	"mosn.io/mosn/test/lib"
18
	"mosn.io/mosn/test/lib/http"
19
	"mosn.io/mosn/test/lib/mosn"
20
)
21

22
var urlTestCases = []struct {
23
	inputURL        string
24
	wantPATH        string
25
	wantQueryString string
26
}{
27
	{
28
		"/",
29
		"/",
30
		"",
31
	},
32
	{
33
		"/abc",
34
		"/abc",
35
		"",
36
	},
37
	{
38
		"/aa%20bb",
39
		"/aa bb",
40
		"",
41
	},
42
	{
43
		"/aa%20bb%26cc",
44
		"/aa bb&cc",
45
		"",
46
	},
47
	{
48
		"/abc+def",
49
		"/abc+def",
50
		"",
51
	},
52
	{
53
		"/10%25",
54
		"/10%",
55
		"",
56
	},
57
	{
58
		"/1%41",
59
		"/1A",
60
		"",
61
	},
62
	{
63
		"/1%41%42%43",
64
		"/1ABC",
65
		"",
66
	},
67
	{
68
		"/%4a",
69
		"/J",
70
		"",
71
	},
72
	{
73
		"/%6F",
74
		"/o",
75
		"",
76
	},
77
	{
78
		"/%20%3F&=%23+%25%21%3C%3E%23%22%7B%7D%7C%5C%5E%5B%5D%60%E2%98%BA%09:%2F@$%27%28%29%2A%2C%3B",
79
		"/ ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;",
80
		"",
81
	},
82
	{
83
		"/home/;some/sample",
84
		"/home/;some/sample",
85
		"",
86
	},
87
	{
88
		"/aa?bb=cc",
89
		"/aa",
90
		"bb=cc",
91
	},
92
	{
93
		"/?",
94
		"/",
95
		"",
96
	},
97
	{
98
		"/?foo=bar?",
99
		"/",
100
		"foo=bar?",
101
	},
102
	{
103
		"/?q=go+language",
104
		"/",
105
		"q=go+language",
106
	},
107
	{
108
		"/?q=go%20language",
109
		"/",
110
		"q=go%20language",
111
	},
112
	{
113
		"/aa%20bb?q=go%20language",
114
		"/aa bb",
115
		"q=go%20language",
116
	},
117
	{
118
		"/aa?+%3F%26%3D%23%2B%25%21%3C%3E%23%22%7B%7D%7C%5C%5E%5B%5D%60%E2%98%BA%09%3A%2F%40%24%27%28%29%2A%2C%3B",
119
		"/aa",
120
		"+%3F%26%3D%23%2B%25%21%3C%3E%23%22%7B%7D%7C%5C%5E%5B%5D%60%E2%98%BA%09%3A%2F%40%24%27%28%29%2A%2C%3B",
121
	},
122
}
123

124
func TestHTTP1UrlPathQuery(t *testing.T) {
125
	Scenario(t, "http1 url path and query string", func() {
126
		var m *mosn.MosnOperator
127
		server := goHttp.Server{Addr: ":8080"}
128
		Setup(func() {
129
			m = mosn.StartMosn(ConfigSimpleHTTP1)
130
			Verify(m, NotNil)
131
			time.Sleep(2 * time.Second) // wait mosn start
132
		})
133
		Case("client-mosn-server", func() {
134
			var serverPath, serverQueryString string
135

136
			go func() {
137
				goHttp.HandleFunc("/", func(writer goHttp.ResponseWriter, request *goHttp.Request) {
138
					serverPath = request.URL.Path
139
					serverQueryString = request.URL.RawQuery
140
				})
141
				_ = server.ListenAndServe()
142
			}()
143

144
			time.Sleep(time.Second)
145

146
			for _, tc := range urlTestCases {
147
				serverPath, serverQueryString = "", ""
148

149
				_, err := goHttp.Get("http://localhost:2046" + tc.inputURL)
150

151
				Verify(err, Equal, nil)
152
				Verify(serverPath, Equal, tc.wantPATH)
153
				Verify(serverQueryString, Equal, tc.wantQueryString)
154
			}
155
		})
156
		TearDown(func() {
157
			m.Stop()
158
			_ = server.Shutdown(context.TODO())
159
		})
160
	})
161
}
162

163
func TestHTTP2UrlPathQuery(t *testing.T) {
164
	Scenario(t, "http2 url path and query string", func() {
165
		var m *mosn.MosnOperator
166
		var server *goHttp.Server
167
		Setup(func() {
168
			m = mosn.StartMosn(ConfigSimpleHTTP2)
169
			Verify(m, NotNil)
170
			time.Sleep(2 * time.Second) // wait mosn start
171
		})
172
		Case("client-mosn-server", func() {
173
			var serverPath, serverQueryString string
174

175
			go func() {
176
				h2s := &http2.Server{}
177
				handler := goHttp.HandlerFunc(func(w goHttp.ResponseWriter, r *goHttp.Request) {
178
					serverPath = r.URL.Path
179
					serverQueryString = r.URL.RawQuery
180
				})
181
				server = &goHttp.Server{
182
					Addr:    "0.0.0.0:8080",
183
					Handler: h2c.NewHandler(handler, h2s),
184
				}
185
				_ = server.ListenAndServe()
186
			}()
187

188
			time.Sleep(time.Second)
189

190
			for _, tc := range urlTestCases {
191
				serverPath, serverQueryString = "", ""
192

193
				client := goHttp.Client{
194
					Transport: &http2.Transport{
195
						AllowHTTP: true,
196
						DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
197
							return net.Dial(network, addr)
198
						},
199
					},
200
				}
201
				_, err := client.Get("http://localhost:2046" + tc.inputURL)
202

203
				Verify(err, Equal, nil)
204
				Verify(serverPath, Equal, tc.wantPATH)
205
				Verify(serverQueryString, Equal, tc.wantQueryString)
206
			}
207
		})
208
		TearDown(func() {
209
			m.Stop()
210
			_ = server.Shutdown(context.TODO())
211
		})
212
	})
213
}
214

215
func TestSimpleHTTP1(t *testing.T) {
216
	Scenario(t, "simple http1 proxy used mosn", func() {
217
		// servers is invalid in `Case`
218
		_, servers := lib.InitMosn(ConfigSimpleHTTP1, lib.CreateConfig(MockHttpServerConfig))
219
		Case("client-mosn-mosn-server", func() {
220
			client := lib.CreateClient("Http1", &http.HttpClientConfig{
221
				TargetAddr: "127.0.0.1:2045",
222
				Verify: &http.VerifyConfig{
223
					ExpectedStatusCode: 200,
224
					ExpectedHeader: map[string][]string{
225
						"mosn-test-default": []string{"http1"},
226
					},
227
					ExpectedBody: []byte("default-http1"),
228
				},
229
			})
230
			Verify(client.SyncCall(), Equal, true)
231
			stats := client.Stats()
232
			Verify(stats.Requests(), Equal, 1)
233
			Verify(stats.ExpectedResponseCount(), Equal, 1)
234
		})
235
		Case("client-mosn-server", func() {
236
			client := lib.CreateClient("Http1", &http.HttpClientConfig{
237
				TargetAddr: "127.0.0.1:2046",
238
				Verify: &http.VerifyConfig{
239
					ExpectedStatusCode: 200,
240
					ExpectedHeader: map[string][]string{
241
						"mosn-test-default": []string{"http1"},
242
					},
243
					ExpectedBody: []byte("default-http1"),
244
				},
245
			})
246
			Verify(client.SyncCall(), Equal, true)
247
			stats := client.Stats()
248
			Verify(stats.Requests(), Equal, 1)
249
			Verify(stats.ExpectedResponseCount(), Equal, 1)
250

251
		})
252
		Case("server-verify", func() {
253
			srv := servers[0]
254
			stats := srv.Stats()
255
			Verify(stats.ConnectionTotal(), Equal, 1)
256
			Verify(stats.ConnectionActive(), Equal, 1)
257
			Verify(stats.ConnectionClosed(), Equal, 0)
258
			Verify(stats.Requests(), Equal, 2)
259

260
		})
261
	})
262
}
263

264
const MockHttpServerConfig = `{
265
	"protocol":"Http1",
266
	"config": {
267
		"address": "127.0.0.1:8080"
268
	}
269
}`
270

271
const ConfigSimpleHTTP1 = `{
272
        "servers":[
273
                {
274
                        "default_log_path":"stdout",
275
                        "default_log_level": "ERROR",
276
                        "routers": [
277
                                {
278
                                        "router_config_name":"router_to_mosn",
279
                                        "virtual_hosts":[{
280
                                                "name":"mosn_hosts",
281
                                                "domains": ["*"],
282
                                                "routers": [
283
                                                        {
284
                                                                "match":{"prefix":"/"},
285
                                                                "route":{"cluster_name":"mosn_cluster"}
286
                                                        }
287
                                                ]
288
                                        }]
289
                                },
290
                                {
291
                                        "router_config_name":"router_to_server",
292
                                        "virtual_hosts":[{
293
                                                "name":"server_hosts",
294
                                                "domains": ["*"],
295
                                                "routers": [
296
                                                        {
297
                                                                "match":{"prefix":"/"},
298
                                                                "route":{"cluster_name":"server_cluster"}
299
                                                        }
300
                                                ]
301
                                        }]
302
                                }
303
                        ],
304
                        "listeners":[
305
                                {
306
                                        "address":"127.0.0.1:2045",
307
                                        "bind_port": true,
308
                                        "filter_chains": [{
309
                                                "filters": [
310
                                                        {
311
                                                                "type": "proxy",
312
                                                                "config": {
313
                                                                        "downstream_protocol": "Http1",
314
                                                                        "upstream_protocol": "Http1",
315
                                                                        "router_config_name":"router_to_mosn"
316
                                                                }
317
                                                        }
318
                                                ]
319
                                        }]
320
                                },
321
                                {
322
                                        "address":"127.0.0.1:2046",
323
                                        "bind_port": true,
324
                                        "filter_chains": [{
325
                                                "filters": [
326
                                                        {
327
                                                                "type": "proxy",
328
                                                                "config": {
329
                                                                        "downstream_protocol": "Http1",
330
                                                                        "upstream_protocol": "Http1",
331
                                                                        "router_config_name":"router_to_server"
332
                                                                }
333
                                                        }
334
                                                ]
335
                                        }]
336
                                }
337
                        ]
338
                }
339
        ],
340
        "cluster_manager":{
341
                "clusters":[
342
                        {
343
                                "name": "mosn_cluster",
344
                                "type": "SIMPLE",
345
                                "lb_type": "LB_RANDOM",
346
                                "hosts":[
347
                                        {"address":"127.0.0.1:2046"}
348
                                ]
349
                        },
350
                        {
351
                                "name": "server_cluster",
352
                                "type": "SIMPLE",
353
                                "lb_type": "LB_RANDOM",
354
                                "hosts":[
355
                                        {"address":"127.0.0.1:8080"}
356
                                ]
357
                        }
358
                ]
359
        }
360
}`
361

362
const ConfigSimpleHTTP2 = `{
363
        "servers":[
364
                {
365
                        "default_log_path":"stdout",
366
                        "default_log_level": "ERROR",
367
                        "routers": [
368
                                {
369
                                        "router_config_name":"router_to_server",
370
                                        "virtual_hosts":[{
371
                                                "name":"server_hosts",
372
                                                "domains": ["*"],
373
                                                "routers": [
374
                                                        {
375
                                                                "match":{"prefix":"/"},
376
                                                                "route":{"cluster_name":"server_cluster"}
377
                                                        }
378
                                                ]
379
                                        }]
380
                                }
381
                        ],
382
                        "listeners":[
383
                                {
384
                                        "address":"127.0.0.1:2046",
385
                                        "bind_port": true,
386
                                        "filter_chains": [{
387
                                                "filters": [
388
                                                        {
389
                                                                "type": "proxy",
390
                                                                "config": {
391
                                                                        "downstream_protocol": "Http2",
392
                                                                        "upstream_protocol": "Http2",
393
                                                                        "router_config_name":"router_to_server"
394
                                                                }
395
                                                        }
396
                                                ]
397
                                        }]
398
                                }
399
                        ]
400
                }
401
        ],
402
        "cluster_manager":{
403
                "clusters":[
404
                        {
405
                                "name": "server_cluster",
406
                                "type": "SIMPLE",
407
                                "lb_type": "LB_RANDOM",
408
                                "hosts":[
409
                                        {"address":"127.0.0.1:8080"}
410
                                ]
411
                        }
412
                ]
413
        }
414
}`
415

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

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

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

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