|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Dmitry Volyntsev. |
| 4 | +# (C) Nginx, Inc. |
| 5 | + |
| 6 | +# Tests for http JavaScript module, req.body method. |
| 7 | + |
| 8 | +############################################################################### |
| 9 | + |
| 10 | +use warnings; |
| 11 | +use strict; |
| 12 | + |
| 13 | +use Test::More; |
| 14 | + |
| 15 | +use Socket qw/ CRLF /; |
| 16 | + |
| 17 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 18 | + |
| 19 | +use lib 'lib'; |
| 20 | +use Test::Nginx; |
| 21 | + |
| 22 | +############################################################################### |
| 23 | + |
| 24 | +select STDERR; $| = 1; |
| 25 | +select STDOUT; $| = 1; |
| 26 | + |
| 27 | +my $t = Test::Nginx->new()->has(qw/http/) |
| 28 | + ->write_file_expand('nginx.conf', <<'EOF'); |
| 29 | +
|
| 30 | +%%TEST_GLOBALS%% |
| 31 | +
|
| 32 | +daemon off; |
| 33 | +
|
| 34 | +events { |
| 35 | +} |
| 36 | +
|
| 37 | +http { |
| 38 | + %%TEST_GLOBALS_HTTP%% |
| 39 | +
|
| 40 | + js_include test.js; |
| 41 | +
|
| 42 | + server { |
| 43 | + listen 127.0.0.1:8080; |
| 44 | + server_name localhost; |
| 45 | +
|
| 46 | + location /njs { |
| 47 | + js_content test_njs; |
| 48 | + } |
| 49 | +
|
| 50 | + location /body { |
| 51 | + js_content test_body; |
| 52 | + } |
| 53 | +
|
| 54 | + location /in_file { |
| 55 | + client_body_in_file_only on; |
| 56 | + js_content test_body; |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | +
|
| 61 | +EOF |
| 62 | + |
| 63 | +$t->write_file('test.js', <<EOF); |
| 64 | + function test_njs(req, res) { |
| 65 | + res.return(200, njs.version); |
| 66 | + } |
| 67 | +
|
| 68 | + function test_body(req) { |
| 69 | + try { |
| 70 | + var body = req.body; |
| 71 | + req.response.return(200, body); |
| 72 | +
|
| 73 | + } catch (e) { |
| 74 | + req.response.return(500, e.message); |
| 75 | + } |
| 76 | + } |
| 77 | +
|
| 78 | +EOF |
| 79 | + |
| 80 | +$t->try_run('no njs request body')->plan(3); |
| 81 | + |
| 82 | +############################################################################### |
| 83 | + |
| 84 | +TODO: { |
| 85 | +local $TODO = 'not yet' |
| 86 | + unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.2.1'; |
| 87 | +like(http_post('/body'), qr/REQ-BODY/, 'request body'); |
| 88 | +like(http_post('/in_file'), qr/request body is in a file/, |
| 89 | + 'request body in file'); |
| 90 | +like(http_post_big('/body'), qr/200.*^(1234567890){1024}$/ms, |
| 91 | + 'request body big'); |
| 92 | +} |
| 93 | + |
| 94 | +############################################################################### |
| 95 | + |
| 96 | +sub http_post { |
| 97 | + my ($url, %extra) = @_; |
| 98 | + |
| 99 | + my $p = "POST $url HTTP/1.0" . CRLF . |
| 100 | + "Host: localhost" . CRLF . |
| 101 | + "Content-Length: 8" . CRLF . |
| 102 | + CRLF . |
| 103 | + "REQ-BODY"; |
| 104 | + |
| 105 | + return http($p, %extra); |
| 106 | +} |
| 107 | + |
| 108 | +sub http_post_big { |
| 109 | + my ($url, %extra) = @_; |
| 110 | + |
| 111 | + my $p = "POST $url HTTP/1.0" . CRLF . |
| 112 | + "Host: localhost" . CRLF . |
| 113 | + "Content-Length: 10240" . CRLF . |
| 114 | + CRLF . |
| 115 | + ("1234567890" x 1024); |
| 116 | + |
| 117 | + return http($p, %extra); |
| 118 | +} |
0 commit comments