~ubuntu-branches/ubuntu/quantal/nginx/quantal-updates

« back to all changes in this revision

Viewing changes to debian/modules/nginx-lua/t/023-rewrite/multi-capture.t

  • Committer: Bazaar Package Importer
  • Author(s): Kartik Mistry
  • Date: 2011-04-16 13:47:58 UTC
  • mfrom: (4.2.31 sid)
  • Revision ID: james.westby@ubuntu.com-20110416134758-yqca2qp5crh2hw2f
Tags: 1.0.0-2
* debian/rules:
  + Removed --with-file-aio support. Fixed FTBFS on kFreeBSD-* arch
    (Closes: #621882)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim:set ft= ts=4 sw=4 et fdm=marker:
 
2
 
 
3
use lib 'lib';
 
4
use Test::Nginx::Socket;
 
5
 
 
6
repeat_each(10);
 
7
 
 
8
plan tests => blocks() * repeat_each() * 2;
 
9
 
 
10
#$ENV{LUA_PATH} = $ENV{HOME} . '/work/JSON4Lua-0.9.30/json/?.lua';
 
11
$ENV{TEST_NGINX_MYSQL_PORT} ||= 3306;
 
12
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
 
13
 
 
14
no_long_string();
 
15
 
 
16
run_tests();
 
17
 
 
18
__DATA__
 
19
 
 
20
=== TEST 1: sanity
 
21
--- config
 
22
    location /foo {
 
23
        rewrite_by_lua '
 
24
            local res1, res2 = ngx.location.capture_multi{
 
25
                { "/a" },
 
26
                { "/b" },
 
27
            }
 
28
            ngx.say("res1.status = " .. res1.status)
 
29
            ngx.say("res1.body = " .. res1.body)
 
30
            ngx.say("res2.status = " .. res2.status)
 
31
            ngx.say("res2.body = " .. res2.body)
 
32
        ';
 
33
        content_by_lua return;
 
34
    }
 
35
    location /a {
 
36
        echo -n a;
 
37
    }
 
38
    location /b {
 
39
        echo -n b;
 
40
    }
 
41
--- request
 
42
    GET /foo
 
43
--- response_body
 
44
res1.status = 200
 
45
res1.body = a
 
46
res2.status = 200
 
47
res2.body = b
 
48
 
 
49
 
 
50
 
 
51
=== TEST 2: 4 concurrent requests
 
52
--- config
 
53
    location /foo {
 
54
        rewrite_by_lua '
 
55
            local res1, res2, res3, res4 = ngx.location.capture_multi{
 
56
                { "/a" },
 
57
                { "/b" },
 
58
                { "/c" },
 
59
                { "/d" },
 
60
            }
 
61
            ngx.say("res1.status = " .. res1.status)
 
62
            ngx.say("res1.body = " .. res1.body)
 
63
 
 
64
            ngx.say("res2.status = " .. res2.status)
 
65
            ngx.say("res2.body = " .. res2.body)
 
66
 
 
67
            ngx.say("res3.status = " .. res3.status)
 
68
            ngx.say("res3.body = " .. res3.body)
 
69
 
 
70
            ngx.say("res4.status = " .. res4.status)
 
71
            ngx.say("res4.body = " .. res4.body)
 
72
        ';
 
73
        content_by_lua return;
 
74
    }
 
75
    location ~ '^/([a-d])$' {
 
76
        echo -n $1;
 
77
    }
 
78
--- request
 
79
    GET /foo
 
80
--- response_body
 
81
res1.status = 200
 
82
res1.body = a
 
83
res2.status = 200
 
84
res2.body = b
 
85
res3.status = 200
 
86
res3.body = c
 
87
res4.status = 200
 
88
res4.body = d
 
89
 
 
90
 
 
91
 
 
92
=== TEST 3: capture multi in series
 
93
--- config
 
94
    location /foo {
 
95
        rewrite_by_lua '
 
96
            local res1, res2 = ngx.location.capture_multi{
 
97
                { "/a" },
 
98
                { "/b" },
 
99
            }
 
100
            ngx.say("res1.status = " .. res1.status)
 
101
            ngx.say("res1.body = " .. res1.body)
 
102
            ngx.say("res2.status = " .. res2.status)
 
103
            ngx.say("res2.body = " .. res2.body)
 
104
 
 
105
            res1, res2 = ngx.location.capture_multi{
 
106
                { "/a" },
 
107
                { "/b" },
 
108
            }
 
109
            ngx.say("2 res1.status = " .. res1.status)
 
110
            ngx.say("2 res1.body = " .. res1.body)
 
111
            ngx.say("2 res2.status = " .. res2.status)
 
112
            ngx.say("2 res2.body = " .. res2.body)
 
113
 
 
114
        ';
 
115
        content_by_lua return;
 
116
    }
 
117
    location /a {
 
118
        echo -n a;
 
119
    }
 
120
    location /b {
 
121
        echo -n b;
 
122
    }
 
123
--- request
 
124
    GET /foo
 
125
--- response_body
 
126
res1.status = 200
 
127
res1.body = a
 
128
res2.status = 200
 
129
res2.body = b
 
130
2 res1.status = 200
 
131
2 res1.body = a
 
132
2 res2.status = 200
 
133
2 res2.body = b
 
134
 
 
135
 
 
136
 
 
137
=== TEST 4: capture multi in subrequest
 
138
--- config
 
139
    location /foo {
 
140
        rewrite_by_lua '
 
141
            local res1, res2 = ngx.location.capture_multi{
 
142
                { "/a" },
 
143
                { "/b" },
 
144
            }
 
145
 
 
146
            local n = ngx.var.arg_n
 
147
 
 
148
            ngx.say(n .. " res1.status = " .. res1.status)
 
149
            ngx.say(n .. " res1.body = " .. res1.body)
 
150
            ngx.say(n .. " res2.status = " .. res2.status)
 
151
            ngx.say(n .. " res2.body = " .. res2.body)
 
152
        ';
 
153
        content_by_lua return;
 
154
    }
 
155
 
 
156
    location /main {
 
157
        rewrite_by_lua '
 
158
            res = ngx.location.capture("/foo?n=1")
 
159
            ngx.say("top res.status = " .. res.status)
 
160
            ngx.say("top res.body = [" .. res.body .. "]")
 
161
        ';
 
162
        content_by_lua return;
 
163
    }
 
164
 
 
165
    location /a {
 
166
        echo -n a;
 
167
    }
 
168
 
 
169
    location /b {
 
170
        echo -n b;
 
171
    }
 
172
--- request
 
173
    GET /main
 
174
--- response_body
 
175
top res.status = 200
 
176
top res.body = [1 res1.status = 200
 
177
1 res1.body = a
 
178
1 res2.status = 200
 
179
1 res2.body = b
 
180
]
 
181
 
 
182
 
 
183
 
 
184
=== TEST 5: capture multi in parallel
 
185
--- config
 
186
    location ~ '^/(foo|bar)$' {
 
187
        set $tag $1;
 
188
        rewrite_by_lua '
 
189
            local res1, res2
 
190
            if ngx.var.tag == "foo" then
 
191
                res1, res2 = ngx.location.capture_multi{
 
192
                    { "/a" },
 
193
                    { "/b" },
 
194
                }
 
195
            else
 
196
                res1, res2 = ngx.location.capture_multi{
 
197
                    { "/c" },
 
198
                    { "/d" },
 
199
                }
 
200
            end
 
201
 
 
202
            local n = ngx.var.arg_n
 
203
 
 
204
            ngx.say(n .. " res1.status = " .. res1.status)
 
205
            ngx.say(n .. " res1.body = " .. res1.body)
 
206
            ngx.say(n .. " res2.status = " .. res2.status)
 
207
            ngx.say(n .. " res2.body = " .. res2.body)
 
208
        ';
 
209
        content_by_lua return;
 
210
    }
 
211
 
 
212
    location /main {
 
213
        rewrite_by_lua '
 
214
            local res1, res2 = ngx.location.capture_multi{
 
215
                { "/foo?n=1" },
 
216
                { "/bar?n=2" },
 
217
            }
 
218
 
 
219
            ngx.say("top res1.status = " .. res1.status)
 
220
            ngx.say("top res1.body = [" .. res1.body .. "]")
 
221
            ngx.say("top res2.status = " .. res2.status)
 
222
            ngx.say("top res2.body = [" .. res2.body .. "]")
 
223
        ';
 
224
        content_by_lua return;
 
225
    }
 
226
 
 
227
    location ~ '^/([abcd])$' {
 
228
        echo -n $1;
 
229
    }
 
230
--- request
 
231
    GET /main
 
232
--- response_body
 
233
top res1.status = 200
 
234
top res1.body = [1 res1.status = 200
 
235
1 res1.body = a
 
236
1 res2.status = 200
 
237
1 res2.body = b
 
238
]
 
239
top res2.status = 200
 
240
top res2.body = [2 res1.status = 200
 
241
2 res1.body = c
 
242
2 res2.status = 200
 
243
2 res2.body = d
 
244
]
 
245
 
 
246
 
 
247
 
 
248
=== TEST 6: memc sanity
 
249
--- config
 
250
    location /foo {
 
251
        rewrite_by_lua '
 
252
            local res1, res2 = ngx.location.capture_multi{
 
253
                { "/a" },
 
254
                { "/b" },
 
255
            }
 
256
            ngx.say("res1.status = " .. res1.status)
 
257
            ngx.say("res1.body = " .. res1.body)
 
258
            ngx.say("res2.status = " .. res2.status)
 
259
            ngx.say("res2.body = " .. res2.body)
 
260
        ';
 
261
        content_by_lua return;
 
262
    }
 
263
    location ~ '^/[ab]$' {
 
264
        set $memc_key $uri;
 
265
        set $memc_value hello;
 
266
        set $memc_cmd set;
 
267
        memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
 
268
    }
 
269
--- request
 
270
    GET /foo
 
271
--- response_body eval
 
272
"res1.status = 201
 
273
res1.body = STORED\r
 
274
 
 
275
res2.status = 201
 
276
res2.body = STORED\r
 
277
 
 
278
"
 
279
 
 
280
 
 
281
 
 
282
=== TEST 7: memc muti + multi
 
283
--- config
 
284
    location /main {
 
285
        rewrite_by_lua '
 
286
            local res1, res2 = ngx.location.capture_multi{
 
287
                { "/foo?n=1" },
 
288
                { "/bar?n=2" },
 
289
            }
 
290
            ngx.say("res1.status = " .. res1.status)
 
291
            ngx.say("res1.body = [" .. res1.body .. "]")
 
292
            ngx.say("res2.status = " .. res2.status)
 
293
            ngx.say("res2.body = [" .. res2.body .. "]")
 
294
        ';
 
295
        content_by_lua return;
 
296
    }
 
297
    location ~ '^/(foo|bar)$' {
 
298
        set $tag $1;
 
299
        rewrite_by_lua '
 
300
            local res1, res2
 
301
            if ngx.var.tag == "foo" then
 
302
                res1, res2 = ngx.location.capture_multi{
 
303
                    { "/a" },
 
304
                    { "/b" },
 
305
                }
 
306
            else
 
307
                res1, res2 = ngx.location.capture_multi{
 
308
                    { "/c" },
 
309
                    { "/d" },
 
310
                }
 
311
            end
 
312
            print("args: " .. ngx.var.args)
 
313
            local n = ngx.var.arg_n
 
314
            ngx.say(n .. " res1.status = " .. res1.status)
 
315
            ngx.say(n .. " res1.body = " .. res1.body)
 
316
            ngx.say(n .. " res2.status = " .. res2.status)
 
317
            ngx.say(n .. " res2.body = " .. res2.body)
 
318
        ';
 
319
        content_by_lua return;
 
320
    }
 
321
    location ~ '^/[abcd]$' {
 
322
        set $memc_key $uri;
 
323
        set $memc_value hello;
 
324
        set $memc_cmd set;
 
325
        memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
 
326
    }
 
327
--- request
 
328
    GET /main
 
329
--- response_body eval
 
330
"res1.status = 200
 
331
res1.body = [1 res1.status = 201
 
332
1 res1.body = STORED\r
 
333
 
 
334
1 res2.status = 201
 
335
1 res2.body = STORED\r
 
336
 
 
337
]
 
338
res2.status = 200
 
339
res2.body = [2 res1.status = 201
 
340
2 res1.body = STORED\r
 
341
 
 
342
2 res2.status = 201
 
343
2 res2.body = STORED\r
 
344
 
 
345
]
 
346
"
 
347
 
 
348
 
 
349
 
 
350
=== TEST 8: memc 4 concurrent requests
 
351
--- config
 
352
    location /foo {
 
353
        rewrite_by_lua '
 
354
            local res1, res2, res3, res4 = ngx.location.capture_multi{
 
355
                { "/a" },
 
356
                { "/b" },
 
357
                { "/c" },
 
358
                { "/d" },
 
359
            }
 
360
            ngx.say("res1.status = " .. res1.status)
 
361
            ngx.say("res1.body = " .. res1.body)
 
362
 
 
363
            ngx.say("res2.status = " .. res2.status)
 
364
            ngx.say("res2.body = " .. res2.body)
 
365
 
 
366
            ngx.say("res3.status = " .. res3.status)
 
367
            ngx.say("res3.body = " .. res3.body)
 
368
 
 
369
            ngx.say("res4.status = " .. res4.status)
 
370
            ngx.say("res4.body = " .. res4.body)
 
371
        ';
 
372
        content_by_lua return;
 
373
    }
 
374
    location ~ '^/[a-d]$' {
 
375
        set $memc_key $uri;
 
376
        set $memc_value hello;
 
377
        set $memc_cmd set;
 
378
        memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
 
379
    }
 
380
--- request
 
381
    GET /foo
 
382
--- response_body eval
 
383
"res1.status = 201
 
384
res1.body = STORED\r
 
385
 
 
386
res2.status = 201
 
387
res2.body = STORED\r
 
388
 
 
389
res3.status = 201
 
390
res3.body = STORED\r
 
391
 
 
392
res4.status = 201
 
393
res4.body = STORED\r
 
394
 
 
395
"
 
396