Merge 'trunk' into 'param_filtering'
[catagits/Catalyst-Runtime.git] / t / unit_core_debug_filtering.t
CommitLineData
10f204e1 1use strict;
2use warnings;
3use Test::More tests=>15;
4
5use Catalyst;
6use HTTP::Headers;
7my $c = Catalyst->new( {} );
8$c->config(Debug => {param_filters => 'simple_str'});
9
10isa_ok( $c, 'Catalyst' );
11my $params = $c->_apply_parameter_debug_filters( 'query', {} );
12is_deeply( $params, {}, 'empty param list' );
13my $filter_str = '[FILTERED]';
14
15$params = $c->_apply_parameter_debug_filters( 'body', { simple_str => 1, other_str => 2 } );
16is( $params->{simple_str}, $filter_str, 'filtered simple_str' );
17is( $params->{other_str}, '2', "didn't filter other_str" );
18
19$c->config( Debug => { param_filters => [qw(a b)] } );
20$params = $c->_apply_parameter_debug_filters( 'query', { a => 1, b => 2, c => 3 }, );
21
22is_deeply( $params, { a => $filter_str, b => $filter_str, c => 3 }, 'list of simple param names' );
23
24$c->config( Debug => { param_filters => qr/^foo/ } );
25$params = $c->_apply_parameter_debug_filters( 'query', { foo => 1, foobar => 2, c => 3 }, );
26is_deeply( $params, { foo => $filter_str, foobar => $filter_str, c => 3 }, 'single regex' );
27
28$c->config(Debug => {param_filters => [qr/^foo/, qr/bar/, 'simple']});
29$params = $c->_apply_parameter_debug_filters( 'query', { foo => 1, foobar => 2, bar => 3, c => 3, simple => 4 }, );
30is_deeply( $params, { foo => $filter_str, foobar => $filter_str, bar => $filter_str, c => 3, simple => $filter_str }, 'array of regexes and a simple filter' );
31
32$c->config(
33 Debug => {
34 param_filters => sub { return unless shift eq 'password'; return '*' x 8 }
35 }
36);
37$params = $c->_apply_parameter_debug_filters( 'query', { password => 'secret', other => 'public' }, );
38is_deeply( $params, { other => 'public', password => '********' }, 'single CODE ref' );
39
40$c->config( Debug => { param_filters => { body => qr// } } );
41$params = $c->_apply_parameter_debug_filters( 'query', { a=>1, b=>2 } );
42is_deeply( $params, { a=>1, b=>2 }, 'body filters do not modify query params' );
43$params = $c->_apply_parameter_debug_filters( 'body', { a=>1, b=>2 } );
44is_deeply( $params, { a => $filter_str, b => $filter_str }, 'all body params filtered' );
45
46$c->config( Debug => { param_filters => undef } );
47$c->config( Debug => { param_filters => { all => [qw(foo bar)] } } );
48$params = $c->_apply_parameter_debug_filters( 'body', { foo=>1, bar=>2, baz=>3 } );
49is_deeply( $params, { foo => $filter_str, bar => $filter_str, baz => 3 }, 'using the "all" type filter on body params' );
50$params = $c->_apply_parameter_debug_filters( 'query', { foo=>1, bar=>2, baz=>3 } );
51is_deeply( $params, { foo => $filter_str, bar => $filter_str, baz => 3 }, 'using the "all" type filter on query params' );
52
53my $headers = HTTP::Headers->new(
54 Content_type => 'text/html',
55 Set_Cookie => 'session_id=abc123; expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; domain=.example.org.',
56 Set_Cookie => 'something_else=xyz890; expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; domain=.example.org.',
57);
58$c->config(
59 Debug => {
60 response_header_filters => sub {
61 my ( $n, $v ) = @_;
62 return unless $n eq 'Set-Cookie';
63 $v =~ s/session_id=.*?;/session_id=SECRET/;
64 return $v;
65 },
66 }
67);
68my $filtered = $c->_apply_header_debug_filters(response => $headers);
69is($filtered->header('Content-Type'), 'text/html', 'Content-Type header left alone');
70like($filtered->as_string, qr/session_id=SECRET/, 'Set-Cookie value filtered');
71like($filtered->as_string, qr/something_else=xyz890/, 'non-session_id cookie not filtered');