comparison proxy_cookie_flags.t @ 1594:2083b4f183e7

Tests: proxy_cookie_flags tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Sat, 26 Sep 2020 18:09:28 +0100
parents
children 3b8a9f02d141
comparison
equal deleted inserted replaced
1593:a328109be893 1594:2083b4f183e7
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for the proxy_cookie_flags directive.
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 proxy rewrite/);
26
27 $t->write_file_expand('nginx.conf', <<'EOF');
28
29 %%TEST_GLOBALS%%
30
31 daemon off;
32
33 events {
34 }
35
36 http {
37 %%TEST_GLOBALS_HTTP%%
38
39 server {
40 listen 127.0.0.1:8080;
41 server_name localhost;
42
43 location / {
44 proxy_pass http://127.0.0.1:8081;
45
46 proxy_cookie_flags a secure httponly samesite=none;
47 proxy_cookie_flags b secure httponly samesite=lax;
48 proxy_cookie_flags c secure httponly samesite=strict;
49 proxy_cookie_flags d nosecure nohttponly nosamesite;
50
51 proxy_cookie_flags $arg_complex secure;
52 proxy_cookie_flags ~BAR httponly;
53
54 location /off/ {
55 proxy_pass http://127.0.0.1:8081;
56 proxy_cookie_flags off;
57 }
58 }
59 }
60
61 server {
62 listen 127.0.0.1:8081;
63 server_name localhost;
64
65 location / {
66 set $c "$arg_v$arg_complex=path=domain=; Domain=example.org$arg_f";
67 add_header Set-Cookie $c;
68 return 200 OK;
69 }
70 }
71 }
72
73 EOF
74
75 $t->try_run('no proxy_cookie_flags')->plan(11);
76
77 ###############################################################################
78
79 is(http_get_set_cookie('/?v=a'),
80 'a=path=domain=; Domain=example.org; Secure; HttpOnly; SameSite=None',
81 'flags set all');
82 is(http_get_set_cookie('/?v=b'),
83 'b=path=domain=; Domain=example.org; Secure; HttpOnly; SameSite=Lax',
84 'flags set lax');
85 is(http_get_set_cookie('/?v=c'),
86 'c=path=domain=; Domain=example.org; Secure; HttpOnly; SameSite=Strict',
87 'flags set strict');
88
89 # edit already set flags
90
91 is(http_get_set_cookie('/?v=a&f=;Secure;HttpOnly;SameSite=Lax'),
92 'a=path=domain=; Domain=example.org; Secure; HttpOnly; SameSite=None',
93 'flags reset all');
94 is(http_get_set_cookie('/?v=b&f=;Secure;HttpOnly;SameSite=None'),
95 'b=path=domain=; Domain=example.org; Secure; HttpOnly; SameSite=Lax',
96 'flags reset lax');
97 is(http_get_set_cookie('/?v=c&f=;Secure;HttpOnly;SameSite=None'),
98 'c=path=domain=; Domain=example.org; Secure; HttpOnly; SameSite=Strict',
99 'flags reset strict');
100
101 is(http_get_set_cookie('/?v=d&f=;secure;httponly;samesite=lax'),
102 'd=path=domain=; Domain=example.org',
103 'flags remove');
104
105 is(http_get_set_cookie('/?v=nx&f=;samesite=none'),
106 'nx=path=domain=; Domain=example.org;samesite=none', 'flags no match');
107
108 is(http_get_set_cookie('/?complex=v'),
109 'v=path=domain=; Domain=example.org; Secure', 'flags variable');
110 is(http_get_set_cookie('/?v=foobarbaz'),
111 'foobarbaz=path=domain=; Domain=example.org; HttpOnly', 'flags regex');
112
113 is(http_get_set_cookie('/off/?v=a'), 'a=path=domain=; Domain=example.org',
114 'flags off');
115
116 ###############################################################################
117
118 sub http_get_set_cookie {
119 my ($uri) = @_;
120 http_get($uri) =~ /^Set-Cookie:\s(.+?)\x0d?$/mi;
121 return $1;
122 }
123
124 ###############################################################################