|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Dmitry Volyntsev |
| 4 | +# (C) Nginx, Inc. |
| 5 | + |
| 6 | +# Tests for http njs module, working with headers. |
| 7 | + |
| 8 | +############################################################################### |
| 9 | + |
| 10 | +use warnings; |
| 11 | +use strict; |
| 12 | + |
| 13 | +use Test::More; |
| 14 | + |
| 15 | +use Config; |
| 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 charset/) |
| 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 /content_length { |
| 51 | + js_content content_length; |
| 52 | + } |
| 53 | +
|
| 54 | + location /content_type { |
| 55 | + charset windows-1251; |
| 56 | +
|
| 57 | + default_type text/plain; |
| 58 | + js_content content_type; |
| 59 | + } |
| 60 | +
|
| 61 | + location /content_encoding { |
| 62 | + js_content content_encoding; |
| 63 | + } |
| 64 | +
|
| 65 | + location /headers_list { |
| 66 | + js_content headers_list; |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +
|
| 71 | +EOF |
| 72 | + |
| 73 | +$t->write_file('test.js', <<EOF); |
| 74 | + function test_njs(r) { |
| 75 | + r.return(200, njs.version); |
| 76 | + } |
| 77 | +
|
| 78 | + function content_length(r) { |
| 79 | + r.headersOut['Content-Length'] = ''; |
| 80 | + r.headersOut['Content-Length'] = 3; |
| 81 | + delete r.headersOut['Content-Length']; |
| 82 | + r.headersOut['Content-Length'] = 3; |
| 83 | + r.sendHeader(); |
| 84 | + r.send('XXX'); |
| 85 | + r.finish(); |
| 86 | + } |
| 87 | +
|
| 88 | + function content_type(r) { |
| 89 | + r.headersOut['Content-Type'] = 'text/xml'; |
| 90 | + r.headersOut['Content-Type'] = ''; |
| 91 | + r.headersOut['Content-Type'] = 'text/xml; charset='; |
| 92 | + delete r.headersOut['Content-Type']; |
| 93 | + r.headersOut['Content-Type'] = 'text/xml; charset=utf-8'; |
| 94 | + r.headersOut['Content-Type'] = 'text/xml; charset="utf-8"'; |
| 95 | + r.return(200); |
| 96 | + } |
| 97 | +
|
| 98 | + function content_encoding(r) { |
| 99 | + r.headersOut['Content-Encoding'] = ''; |
| 100 | + r.headersOut['Content-Encoding'] = 'test'; |
| 101 | + delete r.headersOut['Content-Encoding']; |
| 102 | + r.headersOut['Content-Encoding'] = 'gzip'; |
| 103 | + r.return(200); |
| 104 | + } |
| 105 | +
|
| 106 | + function headers_list(r) { |
| 107 | + for (var h in {a:1, b:2, c:3}) { |
| 108 | + r.headersOut[h] = h; |
| 109 | + } |
| 110 | +
|
| 111 | + delete r.headersOut.b; |
| 112 | + r.headersOut.d = 'd'; |
| 113 | +
|
| 114 | + var out = ""; |
| 115 | + for (var h in r.headersOut) { |
| 116 | + out += h + ":"; |
| 117 | + } |
| 118 | +
|
| 119 | + r.return(200, out); |
| 120 | + } |
| 121 | +
|
| 122 | +EOF |
| 123 | + |
| 124 | +$t->try_run('no njs')->plan(5); |
| 125 | + |
| 126 | +############################################################################### |
| 127 | + |
| 128 | + |
| 129 | +TODO: { |
| 130 | +local $TODO = 'not yet' |
| 131 | + unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.2.8'; |
| 132 | + |
| 133 | +like(http_get('/content_length'), qr/Content-Length: 3/, |
| 134 | + 'set Content-Length'); |
| 135 | +like(http_get('/content_type'), qr/Content-Type: text\/xml; charset="utf-8"\r/, |
| 136 | + 'set Content-Type'); |
| 137 | +unlike(http_get('/content_type'), qr/Content-Type: text\/plain/, |
| 138 | + 'set Content-Type 2'); |
| 139 | +like(http_get('/content_encoding'), qr/Content-Encoding: gzip/, |
| 140 | + 'set Content-Encoding'); |
| 141 | +like(http_get('/headers_list'), qr/a:c:d/, 'headers list'); |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +############################################################################### |
0 commit comments