Debian Codegraph @

t/http-config.t

raw
1 use strict;
2 use warnings;
3
4 use Test::More;
5 plan tests => 30;
6
7 use HTTP::Config;
8
9 sub j { join("|", @_) }
10
11 my $conf = HTTP::Config->new;
12 ok($conf->empty);
13 is($conf->entries, 0);
14 $conf->add_item(42);
15 ok(!$conf->empty);
16 is($conf->entries, 1);
17 is(j($conf->matching_items("http://www.example.com/foo")), 42);
18 is(j($conf->remove_items), 42);
19 is(j($conf->remove_items), '');
20 is($conf->matching_items("http://www.example.com/foo"), 0);
21 is($conf->matching_items('foo', 'bar', 'baz'), 0);
22 $conf->add({item => "http://www.example.com/foo", m_uri__HEAD => undef});
23 is($conf->entries, 1);
24 is($conf->matching_items("http://www.example.com/foo"), 0);
25 SKIP: {
26 my $res;
27 eval { $res = $conf->matching_items(0); };
28 skip "can fails on non-object", 2 if $@;
29 is($res, 0);
30 eval { $res = $conf->matching(0); };
31 ok(!defined $res);
32 }
33
34 $conf = HTTP::Config->new;
35
36 $conf->add_item("always");
37 $conf->add_item("GET", m_method => ["GET", "HEAD"]);
38 $conf->add_item("POST", m_method => "POST");
39 $conf->add_item(".com", m_domain => ".com");
40 $conf->add_item("secure", m_secure => 1);
41 $conf->add_item("not secure", m_secure => 0);
42 $conf->add_item("slash", m_host_port => "www.example.com:80", m_path_prefix => "/");
43 $conf->add_item("u:p", m_host_port => "www.example.com:80", m_path_prefix => "/foo");
44 $conf->add_item("success", m_code => "2xx");
45 is($conf->find(m_domain => ".com")->{item}, '.com');
46 my @found = $conf->find(m_domain => ".com");
47 is($#found, 0);
48 is($found[0]->{item}, '.com');
49
50 use HTTP::Request;
51 my $request = HTTP::Request->new(HEAD => "http://www.example.com/foo/bar");
52 $request->header("User-Agent" => "Moz/1.0");
53
54 is(j($conf->matching_items($request)), "u:p|slash|.com|GET|not secure|always");
55
56 $request->method("HEAD");
57 $request->uri->scheme("https");
58
59 is(j($conf->matching_items($request)), ".com|GET|secure|always");
60
61 is(j($conf->matching_items("http://activestate.com")), ".com|not secure|always");
62
63 use HTTP::Response;
64 my $response = HTTP::Response->new(200 => "OK");
65 $response->content_type("text/plain");
66 $response->content("Hello, world!\n");
67 $response->request($request);
68
69 is(j($conf->matching_items($response)), ".com|success|GET|secure|always");
70
71 $conf->remove_items(m_secure => 1);
72 $conf->remove_items(m_domain => ".com");
73 is(j($conf->matching_items($response)), "success|GET|always");
74
75 $conf->remove_items; # start fresh
76 is(j($conf->matching_items($response)), "");
77
78 $conf->add_item("any", "m_media_type" => "*/*");
79 $conf->add_item("text", m_media_type => "text/*");
80 $conf->add_item("html", m_media_type => "html");
81 $conf->add_item("HTML", m_media_type => "text/html");
82 $conf->add_item("xhtml", m_media_type => "xhtml");
83
84 is(j($conf->matching_items($response)), "text|any");
85
86 $response->content_type("application/xhtml+xml");
87 is(j($conf->matching_items($response)), "xhtml|html|any");
88
89 $response->content_type("text/html");
90 is(j($conf->matching_items($response)), "HTML|html|text|any");
91
92 $response->request(undef);
93 is(j($conf->matching_items($response)), "HTML|html|text|any");
94
95 {
96 my @warnings;
97 local $SIG{__WARN__} = sub { push @warnings, grep { length } @_ };
98
99 my $conf = HTTP::Config->new;
100 $conf->add(owner => undef, callback => sub { 'bleah' });
101 $conf->remove(owner => undef);
102
103 ok(($conf->empty), 'found and removed the config entry');
104 is(scalar(@warnings), 0, 'no warnings')
105 or diag('got warnings: ', explain(\@warnings));
106
107 @warnings = ();
108 $conf->add_item("bond", m_header__user_agent => 'james/0.0.7');
109 my $request2 = HTTP::Request->new(HEAD => "http://www.example.com/foo/bar");
110 is(j($conf->matching_items($request2)), '');
111
112 is(scalar(@warnings), 0, 'no warnings')
113 or diag('got warnings: ', explain(\@warnings));
114
115 }