Mercurial > hg > nginx-tests
annotate auth_delay.t @ 1943:1786f49cca17
Tests: fixed spurious http_resolver_ipv4.t failures.
Previously, to get resolved addresses, the test relied on cached random
rotation in resolver, such that a chosen address is eventually changed,
which may not happen after several client requests. For this reason, the
test is rewritten to get all addresses by switching to the next upstream.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Mon, 22 Jan 2024 14:11:12 +0400 |
parents | 5ac6efbe5552 |
children |
rev | line source |
---|---|
1560
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
1 #!/usr/bin/perl |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
2 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
3 # (C) Sergey Kandaurov |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
4 # (C) Nginx, Inc. |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
5 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
6 # Tests for auth_delay directive using auth basic module. |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
7 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
8 ############################################################################### |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
9 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
10 use warnings; |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
11 use strict; |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
12 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
13 use Test::More; |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
14 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
15 use MIME::Base64; |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
16 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
17 BEGIN { use FindBin; chdir($FindBin::Bin); } |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
18 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
19 use lib 'lib'; |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
20 use Test::Nginx; |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
21 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
22 ############################################################################### |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
23 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
24 select STDERR; $| = 1; |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
25 select STDOUT; $| = 1; |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
26 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
27 my $t = Test::Nginx->new()->has(qw/http auth_basic/) |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
28 ->write_file_expand('nginx.conf', <<'EOF'); |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
29 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
30 %%TEST_GLOBALS%% |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
31 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
32 daemon off; |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
33 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
34 events { |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
35 } |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
36 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
37 http { |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
38 %%TEST_GLOBALS_HTTP%% |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
39 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
40 server { |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
41 listen 127.0.0.1:8080; |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
42 server_name localhost; |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
43 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
44 location / { |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
45 auth_delay 2s; |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
46 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
47 auth_basic "closed site"; |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
48 auth_basic_user_file %%TESTDIR%%/htpasswd; |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
49 } |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
50 } |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
51 } |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
52 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
53 EOF |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
54 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
55 $t->write_file('index.html', ''); |
1561
75fb32094392
Tests: unbreak auth_delay tests on win32.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1560
diff
changeset
|
56 $t->write_file('htpasswd', 'user:' . '{PLAIN}good' . "\n"); |
1560
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
57 |
1693
5ac6efbe5552
Tests: removed TODO and try_run() checks for legacy versions.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1561
diff
changeset
|
58 $t->run()->plan(4); |
1560
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
59 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
60 ############################################################################### |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
61 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
62 my $t1 = time(); |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
63 like(http_get_auth('/', 'user', 'bad'), qr/401 Unauthorize/, 'not authorized'); |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
64 cmp_ok(time() - $t1, '>=', 2, 'auth delay'); |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
65 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
66 $t1 = time(); |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
67 like(http_get_auth('/', 'user', 'good'), qr/200 OK/, 'authorized'); |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
68 cmp_ok(time() - $t1, '<', 2, 'no delay'); |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
69 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
70 ############################################################################### |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
71 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
72 sub http_get_auth { |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
73 my ($url, $user, $password) = @_; |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
74 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
75 my $auth = encode_base64($user . ':' . $password, ''); |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
76 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
77 return http(<<EOF); |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
78 GET $url HTTP/1.0 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
79 Host: localhost |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
80 Authorization: Basic $auth |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
81 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
82 EOF |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
83 } |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
84 |
0f385f8922b5
Tests: auth_delay directive.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
85 ############################################################################### |