File: app.t

package info (click to toggle)
libmojolicious-perl 0.999926-1%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,672 kB
  • ctags: 949
  • sloc: perl: 17,391; makefile: 4
file content (110 lines) | stat: -rw-r--r-- 3,174 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
#!/usr/bin/env perl

# Copyright (C) 2008-2010, Sebastian Riedel.

package ContinueHandlerTest;

use strict;
use warnings;

use base 'Mojo::HelloWorld';

sub continue_handler {
    my ($self, $tx) = @_;
    $tx->res->code(417);
}

package main;

use strict;
use warnings;

use Mojo::IOLoop;
use Test::More;

# Make sure sockets are working
plan skip_all => 'working sockets required for this test!'
  unless Mojo::IOLoop->new->generate_port;
plan tests => 23;

# I was so bored I cut the pony tail off the guy in front of us.
# Look at me, I'm a grad student. I'm 30 years old and I made $600 last year.
# Bart, don't make fun of grad students.
# They've just made a terrible life choice.
use_ok('Mojo');
use_ok('Mojo::Client');
use_ok('Mojo::Transaction::HTTP');
use_ok('Mojo::HelloWorld');

# Logger
my $logger = Mojo::Log->new;
my $app = Mojo->new({log => $logger});
is($app->log, $logger, 'right logger');

$app = Mojo::HelloWorld->new;
my $client = Mojo::Client->new->app($app);

# Normal request
my $tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/1/');
$client->process($tx);
ok($tx->keep_alive, 'will be kept alive');
is($tx->res->code, 200, 'right status');
like($tx->res->body, qr/^Congratulations/, 'right content');

# Post request expecting a 100 Continue
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('POST');
$tx->req->url->parse('/2/');
$tx->req->headers->expect('100-continue');
$tx->req->body('foo bar baz' x 128);
$client->process($tx);
is($tx->res->code, 200, 'right status');
like($tx->res->body, qr/^Congratulations/, 'right content');

$client = Mojo::Client->new->app('ContinueHandlerTest');

# Continue handler not returning 100 Continue
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('POST');
$tx->req->url->parse('/3/');
$tx->req->headers->expect('100-continue');
$tx->req->body('bar baz foo' x 128);
$client->process($tx);
ok(defined $tx->connection, 'has connection id');
is($tx->res->code,                417,     'right status');
is($tx->res->headers->connection, 'Close', 'right content');

# Regular pipeline
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/4/');
my $tx2 = Mojo::Transaction::HTTP->new;
$tx2->req->method('GET');
$tx2->req->url->parse('/5/');
$client->process([$tx, $tx2]);
ok(defined $tx->connection,  'has connection id');
ok(defined $tx2->connection, 'has connection id');
ok($tx->is_done,             'state is done');
ok($tx2->is_done,            'state is done');

# Interrupted pipeline
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/6/');
$tx2 = Mojo::Transaction::HTTP->new;
$tx2->req->method('POST');
$tx2->req->url->parse('/7/');
$tx2->req->headers->expect('100-continue');
$tx2->req->body('bar baz foo' x 128);
my $tx3 = Mojo::Transaction::HTTP->new;
$tx3->req->method('GET');
$tx3->req->url->parse('/8/');
$client->process([$tx, $tx2, $tx3]);
ok($tx->is_finished,  'state is finished');
ok(!$tx->has_error,   'has no errors');
ok($tx2->is_finished, 'state is finished');
ok(!$tx2->has_error,  'has no errors');
ok($tx3->is_finished, 'state is finished');
ok($tx3->has_error,   'has error');