blob: 7666e1c08c5f4a96b41d228773a28b121faad3f4 [file] [log] [blame]
Sergey Kandaurov97ea6a22013-10-10 13:35:55 +04001#!/usr/bin/perl
2
3# (C) Sergey Kandaurov
4# (C) Nginx, Inc.
5
6# Tests for split_client module.
7
8###############################################################################
9
10use warnings;
11use strict;
12
13use Test::More;
14
15BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17use lib 'lib';
18use Test::Nginx;
19
20###############################################################################
21
22select STDERR; $| = 1;
23select STDOUT; $| = 1;
24
25my $t = Test::Nginx->new()->has(qw/http split_clients/)->plan(1);
26
27$t->write_file_expand('nginx.conf', <<'EOF');
28
29%%TEST_GLOBALS%%
30
31daemon off;
32
33events {
34}
35
36http {
37 %%TEST_GLOBALS_HTTP%%
38
39 split_clients $connection $variant {
40 51.2% ".one";
41 10% ".two";
42 * ".three";
43 }
44
45 server {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030046 listen 127.0.0.1:8080;
Sergey Kandaurov97ea6a22013-10-10 13:35:55 +040047 server_name localhost;
48
49 location / {
50 index index${variant}.html;
51 }
52 }
53}
54
55EOF
56
57$t->write_file('index.one.html', 'first');
58$t->write_file('index.two.html', 'second');
59$t->write_file('index.three.html', 'third');
60
61$t->run();
62
63###############################################################################
64
65# NB: split_clients distribution is a subject to implementation details
66
67like(many('/', 20), qr/first: 12, second: 2, third: 6/, 'split');
68
69###############################################################################
70
71sub many {
72 my ($uri, $count) = @_;
73 my %dist;
74
75 for (1 .. $count) {
76 if (http_get($uri) =~ /(first|second|third)/) {
77 $dist{$1} = 0 unless defined $dist{$1};
78 $dist{$1}++;
79 }
80 }
81
82 return join ', ', map { $_ . ": " . $dist{$_} } sort keys %dist;
83}
84
85###############################################################################