Commit | Line | Data |
ec4d7259 |
1 | use warnings; |
2 | use strict; |
3 | use HTTP::Request::Common; |
4 | use Test::More; |
5 | |
6 | { |
7 | package Local::Controller::Errors; |
8 | |
9 | use Moose; |
10 | use MooseX::MethodAttributes; |
11 | |
12 | extends 'Catalyst::Controller'; |
13 | |
14 | has ['a', 'b'] => (is=>'ro', required=>1); |
15 | |
16 | sub not_found :Local { pop->res->from_psgi_response(404, [], ['Not Found']) } |
17 | |
18 | package MyApp::Model::User; |
19 | $INC{'MyApp/Model/User.pm'} = __FILE__; |
20 | |
21 | use base 'Catalyst::Model'; |
22 | |
23 | our %users = ( |
24 | 1 => { name => 'john', age => 46 }, |
25 | 2 => { name => 'mary', age => 36 }, |
26 | 3 => { name => 'ian', age => 25 }, |
27 | 4 => { name => 'visha', age => 18 }, |
28 | ); |
29 | |
30 | sub find { |
31 | my ($self, $id) = @_; |
32 | my $user = $users{$id} || return; |
33 | return bless $user, "MyApp::Model::User::user"; |
34 | } |
35 | |
36 | package MyApp::Controller::Root; |
37 | $INC{'MyApp/Controller/Root.pm'} = __FILE__; |
38 | |
39 | use Moose; |
40 | use MooseX::MethodAttributes; |
41 | |
42 | extends 'Catalyst::Controller'; |
43 | |
44 | sub user :Local Args(1) { |
45 | my ($self, $c, $int) = @_; |
46 | my $user = $c->model("User")->find($int); |
47 | $c->res->body("name: $user->{name}, age: $user->{age}"); |
48 | } |
49 | |
50 | sub default :Default { |
51 | my ($self, $c, $int) = @_; |
52 | $c->res->body('default'); |
53 | } |
54 | |
55 | MyApp::Controller::Root->config(namespace=>''); |
56 | |
57 | package MyApp; |
58 | use Catalyst; |
59 | |
60 | MyApp->config({ |
61 | 'Controller::Err' => { |
62 | component => 'Local::Controller::Errors' |
63 | } |
64 | }); |
65 | |
66 | MyApp->setup; |
67 | } |
68 | |
69 | use Catalyst::Test 'MyApp'; |
70 | |
71 | { |
72 | my $res = request '/user/1'; |
73 | is $res->content, 'name: john, age: 46'; |
74 | } |
75 | |
76 | done_testing; |