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"
22
var urlTestCases = []struct {
25
wantQueryString string
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:/@$'()*,;",
113
"/aa%20bb?q=go%20language",
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",
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",
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"}
129
m = mosn.StartMosn(ConfigSimpleHTTP1)
131
time.Sleep(2 * time.Second) // wait mosn start
133
Case("client-mosn-server", func() {
134
var serverPath, serverQueryString string
137
goHttp.HandleFunc("/", func(writer goHttp.ResponseWriter, request *goHttp.Request) {
138
serverPath = request.URL.Path
139
serverQueryString = request.URL.RawQuery
141
_ = server.ListenAndServe()
144
time.Sleep(time.Second)
146
for _, tc := range urlTestCases {
147
serverPath, serverQueryString = "", ""
149
_, err := goHttp.Get("http://localhost:2046" + tc.inputURL)
151
Verify(err, Equal, nil)
152
Verify(serverPath, Equal, tc.wantPATH)
153
Verify(serverQueryString, Equal, tc.wantQueryString)
158
_ = server.Shutdown(context.TODO())
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
168
m = mosn.StartMosn(ConfigSimpleHTTP2)
170
time.Sleep(2 * time.Second) // wait mosn start
172
Case("client-mosn-server", func() {
173
var serverPath, serverQueryString string
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
181
server = &goHttp.Server{
182
Addr: "0.0.0.0:8080",
183
Handler: h2c.NewHandler(handler, h2s),
185
_ = server.ListenAndServe()
188
time.Sleep(time.Second)
190
for _, tc := range urlTestCases {
191
serverPath, serverQueryString = "", ""
193
client := goHttp.Client{
194
Transport: &http2.Transport{
196
DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
197
return net.Dial(network, addr)
201
_, err := client.Get("http://localhost:2046" + tc.inputURL)
203
Verify(err, Equal, nil)
204
Verify(serverPath, Equal, tc.wantPATH)
205
Verify(serverQueryString, Equal, tc.wantQueryString)
210
_ = server.Shutdown(context.TODO())
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"},
227
ExpectedBody: []byte("default-http1"),
230
Verify(client.SyncCall(), Equal, true)
231
stats := client.Stats()
232
Verify(stats.Requests(), Equal, 1)
233
Verify(stats.ExpectedResponseCount(), Equal, 1)
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"},
243
ExpectedBody: []byte("default-http1"),
246
Verify(client.SyncCall(), Equal, true)
247
stats := client.Stats()
248
Verify(stats.Requests(), Equal, 1)
249
Verify(stats.ExpectedResponseCount(), Equal, 1)
252
Case("server-verify", func() {
255
Verify(stats.ConnectionTotal(), Equal, 1)
256
Verify(stats.ConnectionActive(), Equal, 1)
257
Verify(stats.ConnectionClosed(), Equal, 0)
258
Verify(stats.Requests(), Equal, 2)
264
const MockHttpServerConfig = `{
267
"address": "127.0.0.1:8080"
271
const ConfigSimpleHTTP1 = `{
274
"default_log_path":"stdout",
275
"default_log_level": "ERROR",
278
"router_config_name":"router_to_mosn",
284
"match":{"prefix":"/"},
285
"route":{"cluster_name":"mosn_cluster"}
291
"router_config_name":"router_to_server",
293
"name":"server_hosts",
297
"match":{"prefix":"/"},
298
"route":{"cluster_name":"server_cluster"}
306
"address":"127.0.0.1:2045",
313
"downstream_protocol": "Http1",
314
"upstream_protocol": "Http1",
315
"router_config_name":"router_to_mosn"
322
"address":"127.0.0.1:2046",
329
"downstream_protocol": "Http1",
330
"upstream_protocol": "Http1",
331
"router_config_name":"router_to_server"
343
"name": "mosn_cluster",
345
"lb_type": "LB_RANDOM",
347
{"address":"127.0.0.1:2046"}
351
"name": "server_cluster",
353
"lb_type": "LB_RANDOM",
355
{"address":"127.0.0.1:8080"}
362
const ConfigSimpleHTTP2 = `{
365
"default_log_path":"stdout",
366
"default_log_level": "ERROR",
369
"router_config_name":"router_to_server",
371
"name":"server_hosts",
375
"match":{"prefix":"/"},
376
"route":{"cluster_name":"server_cluster"}
384
"address":"127.0.0.1:2046",
391
"downstream_protocol": "Http2",
392
"upstream_protocol": "Http2",
393
"router_config_name":"router_to_server"
405
"name": "server_cluster",
407
"lb_type": "LB_RANDOM",
409
{"address":"127.0.0.1:8080"}