File Coverage: t/5-expire.t

# File Coverage

File: | t/5-expire.t
Coverage: | 86.4%

line | stmt | bran | cond | sub | pod | time | code
--- | --- | --- | --- | --- | --- | --- | ---
1 | 1 1 1 |  |  | 1 |  | 1272034660669971 12923 8 | use Test::More;
2 | 1 1 1 |  |  | 1 |  | 454 2095 6 | use Test::Exception;
3 | 1 1 1 |  |  | 1 |  | 360 931638 19 | use Catalyst ();
4 | 1 1 1 |  |  | 1 |  | 217 935 31 | use FindBin;
5 | 1 1 1 |  |  | 1 |  | 174 130959 20 | use DateTime;
6 | 1 1 1 |  |  | 1 |  | 4 1 27 | use DateTime::Duration;
7 |
8 |
9 |
10 |  |  |  |  |  |  | # a simple package
11 |  |  |  |  |  |  | {
12 | 1 |  |  |  |  | 441067 | package MyApp::Controller::Js;
13 | 1 1 1 |  |  | 1 |  | 3 1 9 | use Moose;
14 | 1 |  |  |  |  | 8 | extends 'Catalyst::Controller::Combine';
15 |
16 | 1 |  |  |  |  | 2815 | __PACKAGE__->config(
17 |  |  |  |  |  |  | # expire => 1,
18 |  |  |  |  |  |  | # expire_in => 60 * 60, # 1 hour
19 |  |  |  |  |  |  | );
20 |  |  |  |  |  |  | }
21 |
22 |
23 |  |  |  |  |  |  | #
24 |  |  |  |  |  |  | # test start...
25 |  |  |  |  |  |  | #
26 |
27 |  |  |  |  |  |  | # setup our Catalyst :-)
28 | 1 |  |  |  |  | 213 | my $c = Catalyst->new();
29 | 1 |  |  |  |  | 380 | $c->setup_log();
30 | 1 |  |  |  |  | 901 | $c->setup_home("$FindBin::Bin");
31 |
32 | 1 |  |  |  |  | 696 | my $controller;
33 | 1 1 |  |  | 1 |  | 11 106 | lives_ok { $controller = $c->setup_component('MyApp::Controller::Js') } 'setup component worked';
34 |
35 |
36 |  |  |  |  |  |  | #
37 |  |  |  |  |  |  | # check if expires header is sent, if feature isn't turned on
38 |  |  |  |  |  |  | #
39 | 1 |  |  |  |  | 332 | $controller->do_combine($c, 'js1');
40 | 1 |  |  |  |  | 54 | ok(!$c->response->header('expires'), "expires header not sent, if feature not active");
41 |
42 |
43 |  |  |  |  |  |  | # okay, let's check the real stuff, turn this feature one
44 | 1 |  |  |  |  | 318 | MyApp::Controller::Js->config->{expire} = 1;
45 | 1 |  |  |  |  | 104 | $controller = $c->setup_component('MyApp::Controller::Js');
46 |
47 |
48 |
49 |  |  |  |  |  |  | #
50 |  |  |  |  |  |  | # combine and check if expire header is set and correct (no expire_in is explicitly set)
51 |  |  |  |  |  |  | #
52 | 1 |  |  |  |  | 33 | $controller->do_combine($c, 'js1');
53 | 1 |  | 0 |  |  | 40 | my $expected_date_str = (DateTime->now + DateTime::Duration->new(seconds => $controller->{expire_in} || 0))->strftime( "%a, %d %b %Y %H:%M:%S GMT" );
54 | 1 |  | 0 |  |  | 1633 | ok($c->response->header('expires') && $c->response->header('expires') eq $expected_date_str,
55 |  |  |  |  |  |  | 'expires in "standard expire delta"');
56 |
57 |
58 |
59 |  |  |  |  |  |  | #
60 |  |  |  |  |  |  | # combine and check if expire header is set and correct (expire_in = 60 minutes)
61 |  |  |  |  |  |  | #
62 | 1 |  |  |  |  | 352 | MyApp::Controller::Js->config->{expire_in} = 60 * 60; # one hour
63 | 1 |  |  |  |  | 102 | $controller = $c->setup_component('MyApp::Controller::Js');
64 | 1 |  |  |  |  | 33 | $controller->do_combine($c, 'js1');
65 | 1 |  |  |  |  | 37 | $expected_date_str = (DateTime->now + DateTime::Duration->new(seconds => MyApp::Controller::Js->config->{expire_in}))->strftime( "%a, %d %b %Y %H:%M:%S GMT" );
66 | 1 |  | 0 |  |  | 1399 | ok($c->response->header('expires') && $c->response->header('expires') eq $expected_date_str,
67 |  |  |  |  |  |  | 'expires in one hour');
68 |
69 |
70 |
71 | 1 |  |  |  |  | 332 | done_testing;