File: tls_lite_app.t

package info (click to toggle)
libmojolicious-perl 2.98%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,968 kB
  • sloc: perl: 10,178; sh: 48; makefile: 8
file content (116 lines) | stat: -rw-r--r-- 3,291 bytes parent folder | download
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use Mojo::Base -strict;

# Disable IPv6 and libev
BEGIN {
  $ENV{MOJO_NO_IPV6} = 1;
  $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More;
use Mojo::IOLoop::Server;
plan skip_all => 'set TEST_TLS to enable this test (developer only!)'
  unless $ENV{TEST_TLS};
plan skip_all => 'IO::Socket::SSL 1.37 required for this test!'
  unless Mojo::IOLoop::Server::TLS;
plan tests => 40;

# "Look at these low, low prices on famous brand-name electronics!
#  Don't be a sap, Dad. These are just crappy knockoffs.
#  Pfft. I know a genuine Panaphonics when I see it.
#  And look, there's a Magnetbox and Sorny."
use Mojo::IOLoop;
use Mojo::UserAgent;
use Mojolicious::Lite;
use Test::Mojo;

# Silence
app->log->level('fatal');

# Secure sessions
app->sessions->secure(1);

# GET /login
get '/login' => sub {
  my $self = shift;
  my $name = $self->param('name') || 'anonymous';
  $self->session(name => $name);
  $self->render_text("Welcome $name!");
};

# GET /again
get '/again' => sub {
  my $self = shift;
  my $name = $self->session('name') || 'anonymous';
  $self->render_text("Welcome back $name!");
};

# GET /logout
get '/logout' => sub {
  my $self = shift;
  $self->session(expires => 1);
  $self->redirect_to('login');
};

# Use HTTPS
my $t = Test::Mojo->new;
$t->ua->max_redirects(5);
$t->reset_session->ua->app_url('https');

# GET /login
$t->get_ok('/login?name=sri' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
  ->content_is('Welcome sri!');
ok $t->tx->res->cookie('mojolicious')->expires, 'session cookie expires';
ok $t->tx->res->cookie('mojolicious')->secure,  'session cookie is secure';

# GET /again
$t->get_ok('/again' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
  ->content_is('Welcome back sri!');

# GET /logout
$t->get_ok('/logout' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
  ->content_is('Welcome anonymous!');

# GET /again (expired session)
$t->get_ok('/again' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
  ->content_is('Welcome back anonymous!');

# GET /logout (no session)
$t->get_ok('/logout' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
  ->content_is('Welcome anonymous!');

# Use HTTP
$t->reset_session->ua->app_url('http');

# GET /login
$t->reset_session->get_ok('/login?name=sri')->status_is(200)
  ->content_is('Welcome sri!');

# GET /again
$t->get_ok('/again')->status_is(200)->content_is('Welcome back anonymous!');

# Use HTTPS again (without expiration)
$t->reset_session->ua->app_url('https');
app->sessions->default_expiration(0);

# GET /login
$t->get_ok('/login?name=sri' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
  ->content_is('Welcome sri!');
ok !$t->tx->res->cookie('mojolicious')->expires,
  'session cookie does not expire';
ok $t->tx->res->cookie('mojolicious')->secure, 'session cookie is secure';

# GET /again
$t->get_ok('/again' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
  ->content_is('Welcome back sri!');

# GET /logout
$t->get_ok('/logout' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
  ->content_is('Welcome anonymous!');

# GET /again (expired session)
$t->get_ok('/again' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
  ->content_is('Welcome back anonymous!');

# GET /logout (no session)
$t->get_ok('/logout' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
  ->content_is('Welcome anonymous!');