summaryrefslogtreecommitdiffstats
path: root/debian/vendor-h2o/t/40virtual-host.t
blob: aef1b6566c0c0c00c383a1383a3b73e3ae05292b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use strict;
use warnings;
use Digest::MD5 qw(md5_hex);
use Net::EmptyPort qw(check_port);
use File::Temp qw(tempfile);
use Test::More;
use t::Util;

plan skip_all => 'curl not found'
    unless prog_exists('curl');

subtest "basic" => sub {
    # create config
    my ($global_port, $alternate_port) = empty_ports(2);
    my ($conffh, $conffn) = tempfile(UNLINK => 1);
    print $conffh <<"EOT";
listen:
  host: 127.0.0.1
  port: $global_port
hosts:
  "default:$global_port":
    paths:
      /:
        file.dir: examples/doc_root
  "alternate:$global_port":
    listen:
      host: 127.0.0.1
      port: $alternate_port
    paths:
      /:
        file.dir: examples/doc_root.alternate
EOT

    # start server
    my ($guard, $pid) = spawn_server(
        argv     => [ bindir() . "/h2o", "-c", $conffn ],
        is_ready => sub {
            check_port($global_port) && check_port($alternate_port);
        },
    );

    my $resp = `curl --silent --resolve default:$global_port:127.0.0.1 http://default:$global_port/`;
    is md5_hex($resp), md5_file("examples/doc_root/index.html"), "'host: default' against default port";

    $resp = `curl --silent --resolve alternate:$global_port:127.0.0.1 http://alternate:$global_port/`;
    is md5_hex($resp), md5_file("examples/doc_root.alternate/index.txt"), "'host: alternate' against default port";

    $resp = `curl --silent --resolve default:$alternate_port:127.0.0.1 http://default:$alternate_port/`;
    is md5_hex($resp), md5_file("examples/doc_root.alternate/index.txt"), "'host: default' against alternate port";

    $resp = `curl --silent --resolve alternate:$alternate_port:127.0.0.1 http://alternate:$alternate_port/`;
    is md5_hex($resp), md5_file("examples/doc_root.alternate/index.txt"), "'host: alternate' against alternate port";
};

subtest "wildcard" => sub {
    my $server = spawn_h2o(sub {
        my ($port, $tls_port) = @_;
        return << "EOT";
hosts:
  default:
    paths:
      /:
        file.dir: examples/doc_root
  "*.example.com:$port":
    paths:
      /:
        file.dir: examples/doc_root.alternate
EOT
    });

    my $fetch = sub {
        my $host = shift;
        my $resp = `curl --silent --resolve $host:$server->{port}:127.0.0.1 http://$host:$server->{port}/`;
        md5_hex($resp);
    };

    is $fetch->("www.example.com"), md5_file("examples/doc_root.alternate/index.txt"), "www.example.com";
    is $fetch->("xxx.example.com"), md5_file("examples/doc_root.alternate/index.txt"), "xxx.example.com";
    is $fetch->("example.com"), md5_file("examples/doc_root/index.html"), "example.com";
    is $fetch->("example.org"), md5_file("examples/doc_root/index.html"), "example.org";
};

done_testing();