comparison js_return.t @ 1317:a05f377bf0ca

Tests: added njs return tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Wed, 04 Apr 2018 18:01:58 +0300
parents
children 8bd4b88fcac5
comparison
equal deleted inserted replaced
1316:d51cc2526066 1317:a05f377bf0ca
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for http JavaScript module, return method.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http/)
26 ->write_file_expand('nginx.conf', <<'EOF');
27
28 %%TEST_GLOBALS%%
29
30 daemon off;
31
32 events {
33 }
34
35 http {
36 %%TEST_GLOBALS_HTTP%%
37
38 js_include test.js;
39
40 server {
41 listen 127.0.0.1:8080;
42 server_name localhost;
43
44 location / {
45 js_content test_return;
46 }
47 }
48 }
49
50 EOF
51
52 $t->write_file('test.js', <<EOF);
53 function test_return(req, res) {
54 njs.version;
55 res.return(Number(req.args.c), req.args.t);
56 }
57
58 EOF
59
60 $t->try_run('no njs return')->plan(5);
61
62 ###############################################################################
63
64 like(http_get('/?c=200'), qr/200 OK.*\x0d\x0a?\x0d\x0a?$/s, 'return code');
65 like(http_get('/?c=200&t=SEE-THIS'), qr/200 OK.*^SEE-THIS$/ms, 'return text');
66 like(http_get('/?c=301&t=path'), qr/ 301 .*Location: path/s, 'return redirect');
67 like(http_get('/?c=404'), qr/404 Not.*html/s, 'return error page');
68 like(http_get('/?c=inv'), qr/ 500 /, 'return invalid');
69
70 ###############################################################################