Skip to content

Commit aa344bf

Browse files
committed
Tests: moved njs http headers test to a separate file.
1 parent ff5944e commit aa344bf

File tree

2 files changed

+71
-115
lines changed

2 files changed

+71
-115
lines changed

js.t

Lines changed: 1 addition & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ http {
4141
js_set $test_version test_version;
4242
js_set $test_addr test_addr;
4343
js_set $test_uri test_uri;
44-
js_set $test_hdr_in test_hdr_in;
45-
js_set $test_ihdr_in test_ihdr_in;
4644
js_set $test_arg test_arg;
4745
js_set $test_iarg test_iarg;
4846
js_set $test_var test_var;
@@ -76,14 +74,6 @@ http {
7674
return 200 $test_uri;
7775
}
7876
79-
location /hdr_in {
80-
return 200 $test_hdr_in;
81-
}
82-
83-
location /ihdr_in {
84-
return 200 $test_ihdr_in;
85-
}
86-
8777
location /arg {
8878
return 200 $test_arg;
8979
}
@@ -113,22 +103,6 @@ http {
113103
js_content status;
114104
}
115105
116-
location /ctype {
117-
js_content ctype;
118-
}
119-
120-
location /clen {
121-
js_content clen;
122-
}
123-
124-
location /hdr_out {
125-
js_content hdr_out;
126-
}
127-
128-
location /ihdr_out {
129-
js_content ihdr_out;
130-
}
131-
132106
location /request_body {
133107
js_content request_body;
134108
}
@@ -141,10 +115,6 @@ http {
141115
js_content return_method;
142116
}
143117
144-
location /return_headers {
145-
js_content return_headers;
146-
}
147-
148118
location /log {
149119
return 200 $test_log;
150120
}
@@ -188,20 +158,6 @@ $t->write_file('test.js', <<EOF);
188158
return 'uri=' + r.uri;
189159
}
190160
191-
function test_hdr_in(r) {
192-
return 'hdr=' + r.headersIn.foo;
193-
}
194-
195-
function test_ihdr_in(r) {
196-
var s = '', h;
197-
for (h in r.headersIn) {
198-
if (h.substr(0, 3) == 'foo') {
199-
s += r.headersIn[h];
200-
}
201-
}
202-
return s;
203-
}
204-
205161
function test_arg(r) {
206162
return 'arg=' + r.args.foo;
207163
}
@@ -230,49 +186,6 @@ $t->write_file('test.js', <<EOF);
230186
r.finish();
231187
}
232188
233-
function ctype(r) {
234-
r.status = 200;
235-
r.headersOut['Content-Type'] = 'application/foo';
236-
r.sendHeader();
237-
r.finish();
238-
}
239-
240-
function clen(r) {
241-
r.status = 200;
242-
r.headersOut['Content-Length'] = 5;
243-
r.sendHeader();
244-
r.send('foo12');
245-
r.finish();
246-
}
247-
248-
function hdr_out(r) {
249-
r.status = 200;
250-
r.headersOut['Foo'] = r.args.fOO;
251-
252-
if (r.args.bar) {
253-
r.headersOut['Bar'] =
254-
r.headersOut[(r.args.bar == 'empty' ? 'Baz' :'Foo')]
255-
}
256-
257-
r.sendHeader();
258-
r.finish();
259-
}
260-
261-
function ihdr_out(r) {
262-
r.status = 200;
263-
r.headersOut['a'] = r.args.a;
264-
r.headersOut['b'] = r.args.b;
265-
266-
var s = '', h;
267-
for (h in r.headersOut) {
268-
s += r.headersOut[h];
269-
}
270-
271-
r.sendHeader();
272-
r.send(s);
273-
r.finish();
274-
}
275-
276189
function request_body(r) {
277190
try {
278191
var body = r.requestBody;
@@ -300,11 +213,6 @@ $t->write_file('test.js', <<EOF);
300213
r.return(Number(r.args.c), r.args.t);
301214
}
302215
303-
function return_headers(r) {
304-
r.headersOut.Foo = 'bar';
305-
r.return(200);
306-
}
307-
308216
function test_log(r) {
309217
r.log('SEE-THIS');
310218
}
@@ -324,7 +232,7 @@ $t->write_file('test.js', <<EOF);
324232
325233
EOF
326234

327-
$t->try_run('no njs available')->plan(35);
235+
$t->try_run('no njs available')->plan(23);
328236

329237
###############################################################################
330238

@@ -333,29 +241,10 @@ like(http_get('/method'), qr/method=GET/, 'r.method');
333241
like(http_get('/version'), qr/version=1.0/, 'r.httpVersion');
334242
like(http_get('/addr'), qr/addr=127.0.0.1/, 'r.remoteAddress');
335243
like(http_get('/uri'), qr/uri=\/uri/, 'r.uri');
336-
like(http_get_hdr('/hdr_in'), qr/hdr=12345/, 'r.headersIn');
337-
like(http_get_ihdr('/ihdr_in'), qr/12345barz/, 'r.headersIn iteration');
338244
like(http_get('/arg?foO=12345'), qr/arg=12345/, 'r.args');
339245
like(http_get('/iarg?foo=12345&foo2=bar&nn=22&foo-3=z'), qr/12345barz/,
340246
'r.args iteration');
341247
like(http_get('/status'), qr/204 No Content/, 'r.status');
342-
like(http_get('/ctype'), qr/Content-Type: application\/foo/,
343-
'r.headersOut.contentType');
344-
like(http_get('/clen'), qr/Content-Length: 5/, 'r.headersOut.contentLength');
345-
like(http_get('/hdr_out?foo=12345'), qr/Foo: 12345/, 'r.headersOut');
346-
like(http_get('/hdr_out?foo=123&bar=copy'), qr/Bar: 123/, 'r.headersOut get');
347-
like(http_get('/ihdr_out?a=12&b=34'), qr/^1234$/m, 'r.headersOut iteration');
348-
like(http_get('/ihdr_out'), qr/\x0d\x0a?\x0d\x0a?$/m, 'r.send zero');
349-
350-
TODO: {
351-
local $TODO = 'not yet'
352-
unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.2.8';
353-
354-
unlike(http_get('/hdr_out?bar=empty'), qr/Bar:/, 'r.headersOut empty');
355-
unlike(http_get('/hdr_out?foo='), qr/Foo:/, 'r.headersOut no value');
356-
unlike(http_get('/hdr_out?foo'), qr/Foo:/, 'r.headersOut no value 2');
357-
358-
}
359248

360249
like(http_post('/body'), qr/REQ-BODY/, 'request body');
361250
like(http_post('/in_file'), qr/request body is in a file/,
@@ -375,8 +264,6 @@ like(http_get('/return_method?c=301&t=path'), qr/ 301 .*Location: path/s,
375264
like(http_get('/return_method?c=404'), qr/404 Not.*html/s, 'return error page');
376265
like(http_get('/return_method?c=inv'), qr/ 500 /, 'return invalid');
377266

378-
like(http_get('/return_headers'), qr/Foo: bar/, 'return headers');
379-
380267
like(http_get('/var'), qr/variable=127.0.0.1/, 'r.variables');
381268
like(http_get('/global'), qr/global=njs/, 'global code');
382269
like(http_get('/log'), qr/200 OK/, 'r.log');

js_headers.t

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ events {
3737
http {
3838
%%TEST_GLOBALS_HTTP%%
3939
40+
js_set $test_hdr_in test_hdr_in;
41+
js_set $test_ihdr_in test_ihdr_in;
42+
4043
js_include test.js;
4144
4245
server {
@@ -65,6 +68,22 @@ http {
6568
location /headers_list {
6669
js_content headers_list;
6770
}
71+
72+
location /hdr_in {
73+
return 200 $test_hdr_in;
74+
}
75+
76+
location /ihdr_in {
77+
return 200 $test_ihdr_in;
78+
}
79+
80+
location /hdr_out {
81+
js_content hdr_out;
82+
}
83+
84+
location /ihdr_out {
85+
js_content ihdr_out;
86+
}
6887
}
6988
}
7089
@@ -119,9 +138,52 @@ $t->write_file('test.js', <<EOF);
119138
r.return(200, out);
120139
}
121140
141+
function test_hdr_in(r) {
142+
return 'hdr=' + r.headersIn.foo;
143+
}
144+
145+
function test_ihdr_in(r) {
146+
var s = '', h;
147+
for (h in r.headersIn) {
148+
if (h.substr(0, 3) == 'foo') {
149+
s += r.headersIn[h];
150+
}
151+
}
152+
return s;
153+
}
154+
155+
function hdr_out(r) {
156+
r.status = 200;
157+
r.headersOut['Foo'] = r.args.fOO;
158+
159+
if (r.args.bar) {
160+
r.headersOut['Bar'] =
161+
r.headersOut[(r.args.bar == 'empty' ? 'Baz' :'Foo')]
162+
}
163+
164+
r.sendHeader();
165+
r.finish();
166+
}
167+
168+
function ihdr_out(r) {
169+
r.status = 200;
170+
r.headersOut['a'] = r.args.a;
171+
r.headersOut['b'] = r.args.b;
172+
173+
var s = '', h;
174+
for (h in r.headersOut) {
175+
s += r.headersOut[h];
176+
}
177+
178+
r.sendHeader();
179+
r.send(s);
180+
r.finish();
181+
}
182+
183+
122184
EOF
123185

124-
$t->try_run('no njs')->plan(5);
186+
$t->try_run('no njs')->plan(12);
125187

126188
###############################################################################
127189

@@ -140,6 +202,13 @@ like(http_get('/content_encoding'), qr/Content-Encoding: gzip/,
140202
'set Content-Encoding');
141203
like(http_get('/headers_list'), qr/a:c:d/, 'headers list');
142204

205+
like(http_get('/ihdr_out?a=12&b=34'), qr/^1234$/m, 'r.headersOut iteration');
206+
like(http_get('/ihdr_out'), qr/\x0d\x0a?\x0d\x0a?$/m, 'r.send zero');
207+
like(http_get('/hdr_out?foo=12345'), qr/Foo: 12345/, 'r.headersOut');
208+
like(http_get('/hdr_out?foo=123&bar=copy'), qr/Bar: 123/, 'r.headersOut get');
209+
unlike(http_get('/hdr_out?bar=empty'), qr/Bar:/, 'r.headersOut empty');
210+
unlike(http_get('/hdr_out?foo='), qr/Foo:/, 'r.headersOut no value');
211+
unlike(http_get('/hdr_out?foo'), qr/Foo:/, 'r.headersOut no value 2');
143212
}
144213

145214
###############################################################################

0 commit comments

Comments
 (0)