comparison auth_delay.t @ 1560:0f385f8922b5

Tests: auth_delay directive.
author Sergey Kandaurov <pluknet@nginx.com>
date Thu, 09 Apr 2020 20:38:10 +0300
parents
children 75fb32094392
comparison
equal deleted inserted replaced
1559:9e5d38da7651 1560:0f385f8922b5
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for auth_delay directive using auth basic module.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 use MIME::Base64;
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 auth_basic/)
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 server {
41 listen 127.0.0.1:8080;
42 server_name localhost;
43
44 location / {
45 auth_delay 2s;
46
47 auth_basic "closed site";
48 auth_basic_user_file %%TESTDIR%%/htpasswd;
49 }
50 }
51 }
52
53 EOF
54
55 $t->write_file('index.html', '');
56 $t->write_file('htpasswd', 'user:' . crypt('good', 'salt') . "\n");
57
58 $t->try_run('no auth_delay')->plan(4);
59
60 ###############################################################################
61
62 my $t1 = time();
63 like(http_get_auth('/', 'user', 'bad'), qr/401 Unauthorize/, 'not authorized');
64 cmp_ok(time() - $t1, '>=', 2, 'auth delay');
65
66 $t1 = time();
67 like(http_get_auth('/', 'user', 'good'), qr/200 OK/, 'authorized');
68 cmp_ok(time() - $t1, '<', 2, 'no delay');
69
70 ###############################################################################
71
72 sub http_get_auth {
73 my ($url, $user, $password) = @_;
74
75 my $auth = encode_base64($user . ':' . $password, '');
76
77 return http(<<EOF);
78 GET $url HTTP/1.0
79 Host: localhost
80 Authorization: Basic $auth
81
82 EOF
83 }
84
85 ###############################################################################