r44@latte: adam | 2006-12-01 00:12:17 -0800
[catagits/Catalyst-Action-REST.git] / t / catalyst-action-rest.t
CommitLineData
d34c067a 1package Test::Catalyst::Action::REST;
2
3use FindBin;
4
5use lib ("$FindBin::Bin/../lib");
6
7use strict;
8use warnings;
9
10use Catalyst::Runtime '5.70';
11
12use Catalyst;
13
14__PACKAGE__->config( name => 'Test::Catalyst::Action::REST' );
15__PACKAGE__->setup;
16
17sub test :Local :ActionClass('REST') {
18 my ($self, $c) = @_;
19 $c->stash->{'entity'} = 'something';
20}
21
22sub test_GET :Local :ActionClass('REST') {
23 my ($self, $c) = @_;
24
25 $c->stash->{'entity'} .= " GET";
26 $c->forward('ok');
27}
28
29sub test_POST :Local :ActionClass('REST') {
30 my ($self, $c) = @_;
31
32 $c->stash->{'entity'} .= " POST";
33 $c->forward('ok');
34}
35
36sub test_PUT :Local :ActionClass('REST') {
37 my ($self, $c) = @_;
38
39 $c->stash->{'entity'} .= " PUT";
40 $c->forward('ok');
41}
42
43sub test_DELETE :Local :ActionClass('REST') {
44 my ($self, $c) = @_;
45
46 $c->stash->{'entity'} .= " DELETE";
47 $c->forward('ok');
48}
49
50sub test_OPTIONS :Local :ActionClass('REST') {
51 my ($self, $c) = @_;
52
53 $c->stash->{'entity'} .= " OPTIONS";
54 $c->forward('ok');
55}
56
57sub notreally :Local :ActionClass('REST') { }
58
59sub notreally_GET {
60 my ($self, $c) = @_;
61
62 $c->stash->{'entity'} = "notreally GET";
63 $c->forward('ok');
64}
65
66sub not_implemented :Local :ActionClass('REST') {}
67
68sub not_implemented_GET {
69 my ($self, $c) = @_;
70
71 $c->stash->{'entity'} = "not_implemented GET";
72 $c->forward('ok');
73}
74
75sub not_implemented_not_implemented {
76 my ($self, $c) = @_;
77
78 $c->stash->{'entity'} = "Not Implemented Handler";
79 $c->forward('ok');
80}
81
82sub ok :Private {
83 my ($self, $c) = @_;
84
85 $c->res->content_type('text/plain');
86 $c->res->body($c->stash->{'entity'});
87}
88
89package main;
90
91use strict;
92use warnings;
93use Test::More qw(no_plan);
94use FindBin;
95use Data::Dump qw(dump);
96
97use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib");
98use Test::Rest;
99
100# Should use the default serializer, YAML
101my $t = Test::Rest->new('content_type' => 'text/plain');
102
103use_ok 'Catalyst::Test', 'Test::Catalyst::Action::REST';
104
105foreach my $method (qw(GET DELETE POST PUT OPTIONS)) {
106 my $run_method = lc($method);
107 my $result = "something $method";
108 my $res;
109 if (grep /$method/, qw(GET DELETE OPTIONS)) {
110 $res = request($t->$run_method(url => '/test'));
111 } else {
112 $res = request($t->$run_method(
113 url => '/test',
114 data => { foo => 'bar' }
115 )
116 );
117 }
118 ok( $res->is_success, "$method request succeeded" );
119 is( $res->content, "something $method", "$method request had proper response");
120}
121
122my $fail_res = request($t->delete(url => '/notreally'));
123is( $fail_res->code, 405, "Request to bad method gets 405 Not Implemented");
124is( $fail_res->header('allow'), "GET", "405 allow header properly set.");
125
126my $options_res = request($t->options(url => '/notreally'));
127is( $options_res->code, 200, "OPTIONS request handler succeeded");
128is( $options_res->header('allow'), "GET", "OPTIONS request allow header properly set.");
129
130my $ni_res = request($t->delete(url => '/not_implemented'));
131is( $ni_res->code, 200, "Custom not_implemented handler succeeded");
132is ($ni_res->content, "Not Implemented Handler", "not_implemented handler had proper response");
133
1341;