first pass test case
John Napiorkowski [Wed, 23 Oct 2013 17:30:08 +0000 (12:30 -0500)]
MANIFEST.SKIP
t/content_negotiation.t
t/lib/TestContentNegotiation/share/file.txt [new file with mode: 0644]

index b9ad8e0..18b2702 100644 (file)
@@ -1,2 +1,2 @@
-^(?!script/\w+\.pl$|TODO$|lib/.+(?<!ROADMAP)\.p(m|od)$|inc/|t/a(uthor|ggregate)/.*\.t$|t/([^/]+|.{1,2}|[^t][^m][^p].*)\.(gif|yml|pl|t)$|t/lib/.*\.pm$|t/something/(Makefile.PL|script/foo/bar/for_dist)$|t/conf/extra.conf.in$|Makefile.PL$|README$|MANIFEST$|Changes$|META.yml$|.+testappencodingsetinconfig.json|.+TestMiddleware/share.*)
+^(?!script/\w+\.pl$|TODO$|lib/.+(?<!ROADMAP)\.p(m|od)$|inc/|t/a(uthor|ggregate)/.*\.t$|t/([^/]+|.{1,2}|[^t][^m][^p].*)\.(gif|yml|pl|t)$|t/lib/.*\.pm$|t/something/(Makefile.PL|script/foo/bar/for_dist)$|t/conf/extra.conf.in$|Makefile.PL$|README$|MANIFEST$|Changes$|META.yml$|.+testappencodingsetinconfig.json|.+TestMiddleware/share.*|.+TestContentNegotiation/share.*)
 /cpanfile
index 38eb181..df6a3c5 100644 (file)
@@ -6,9 +6,38 @@ use strict;
 use FindBin;
 use Test::More;
 use HTTP::Request::Common;
+use JSON::MaybeXS;
 
 use lib "$FindBin::Bin/lib";
 use Catalyst::Test 'TestContentNegotiation';
 
+{
+  ok my $req = POST '/',
+     Content_Type => 'application/json',
+     Content => encode_json +{message=>'test'};
+
+  ok my $res = request $req;
+
+  is $res->content, 'is_json';
+}
+
+{
+  ok my $req = POST '/', [a=>1,b=>2];
+  ok my $res = request $req;
+
+  is $res->content, 'is_urlencoded';
+}
+
+{
+  ok my $path = TestContentNegotiation->path_to(qw/share file.txt/);
+  ok my $req = POST '/',
+    Content_Type => 'form-data',
+    Content =>  [a=>1, b=>2, file=>["$path"]];
+
+  ok my $res = request $req;
+
+  is $res->content, 'is_multipart';
+}
+
 
 done_testing;
diff --git a/t/lib/TestContentNegotiation/share/file.txt b/t/lib/TestContentNegotiation/share/file.txt
new file mode 100644 (file)
index 0000000..ab8e0eb
--- /dev/null
@@ -0,0 +1,50 @@
+package TestContentNegotiation::Controller::Root;
+
+use Moose;
+use MooseX::MethodAttributes;
+
+extends 'Catalyst::Controller';
+
+sub start :Chained(/) PathPrefix CaptureArgs(0) { }
+
+    sub is_json       : Chained('start') PathPart('') Consumes('application/json') Args(0) { pop->res->body('is_json') }
+    sub is_urlencoded : Chained('start') PathPart('') Consumes('application/x-www-form-urlencoded') Args(0) { pop->res->body('is_urlencoded') }
+    sub is_multipart  : Chained('start') PathPart('') Consumes('multipart/form-data') Args(0) { pop->res->body('is_multipart') }
+      
+    sub under :Chained('start') CaptureArgs(0) { }
+
+      sub is_json_under       : Chained('under') PathPart('') Consumes(JSON) Args(0) { pop->res->body('is_json') }
+      sub is_urlencoded_under : Chained('under') PathPart('') Consumes(UrlEncoded) Args(0) { pop->res->body('is_urlencoded') }
+      sub is_multipart_under  : Chained('under') PathPart('') Consumes(Multipart) Args(0) { pop->res->body('is_multipart') }
+
+      ## Or allow more than one type
+    
+    sub multi :Chained('start') CaptureArgs(0) { }
+      
+    sub is_more_than_one_1
+      : Chained('multi') PathPart('')
+      : Consumes('application/x-www-form-urlencoded')
+      : Consumes('multipart/form-data')
+      : Args(0)
+    {
+      pop->res->body('formdata1');
+    }
+
+    sub is_more_than_one_2
+      : Chained('multi') PathPart('')
+      : Consumes('HTMLForm')
+      : Args(0)
+    {
+      pop->res->body('formdata2');
+    }
+
+    sub is_more_than_one_3
+      : Chained('multi') PathPart('')
+      : Consumes('application/x-www-form-urlencoded,multipart/form-data')
+      : Args(0)
+    {
+      pop->res->body('formdata3');
+    }
+
+
+__PACKAGE__->meta->make_immutable;