Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add tests for response status

  • Loading branch information...
commit 4a026ec7fe375eaf3fc6a0455b3b87d6a33c4ee7 1 parent 55fe612
@tadzik authored
Showing with 44 additions and 10 deletions.
  1. +15 −5 lib/Bailador.pm
  2. +15 −5 lib/Bailador/Test.pm
  3. +14 −0 t/02-response-status.t
View
20 lib/Bailador.pm
@@ -1,10 +1,11 @@
-module Bailador;
use Bailador::App;
use Bailador::Request;
use Bailador::Response;
use Ratel;
use HTTP::Easy::PSGI;
+module Bailador;
+
my $current-app = Bailador::App.current;
my $current-request = Bailador::Request.new;
my $current-response = Bailador::Response.new;
@@ -57,15 +58,19 @@ sub template(Str $tmpl, %params = {}) is export {
return $template-engine.render(|%params);
}
+our sub dispatch_request(Bailador::Request $r) {
+ return dispatch($r.env);
+}
+
sub dispatch($env) {
- my $res = '';
+ $current-request.env = $env;
- $current-request.env = $env;
$current-response.code = 404;
$current-response.content = 'Not found';
$current-response.headers<Content-Type> = 'text/html';
my $r = $current-app.find_route($env);
+
if $r {
$current-response.code = 200;
if $/ {
@@ -75,12 +80,17 @@ sub dispatch($env) {
$current-response.content = $r.value.();
}
}
- return $current-response.psgi;
+
+ return $current-response;
+}
+
+sub dispatch-psgi($env) {
+ return dispatch($env).psgi;
}
sub baile is export {
given HTTP::Easy::PSGI.new(port => 3000) {
- .app(&dispatch);
+ .app(&dispatch-psgi);
.run;
}
}
View
20 lib/Bailador/Test.pm
@@ -1,7 +1,8 @@
-module Bailador::Test;
-
use Test;
-use Bailador::App;
+use Bailador;
+use Bailador::Request;
+
+module Bailador::Test;
sub route-exists($meth, $path, $desc = '') is export {
my $req = Bailador::Request.new_for_request($meth, $path);
@@ -13,8 +14,17 @@ sub route-doesnt-exist($meth, $path, $desc = '') is export {
ok !Bailador::App.current.find_route($req), $desc;
}
-sub response-status-is($meth, $path, $status, $desc) is export { ... }
-sub response-status-isnt($meth, $path, $status, $desc) is export { ... }
+sub response-status-is($meth, $path, $status, $desc = '') is export {
+ my $req = Bailador::Request.new_for_request($meth, $path);
+ my $resp = Bailador::dispatch_request($req);
+ is $resp.code, $status, $desc;
+}
+
+sub response-status-isnt($meth, $path, $status, $desc = '') is export {
+ my $req = Bailador::Request.new_for_request($meth, $path);
+ my $resp = Bailador::dispatch_request($req);
+ isnt $resp.code, $status, $desc;
+}
sub response-content-is($meth, $path, $cont, $desc) is export { ... }
sub response-content-isnt($meth, $path, $cont, $desc) is export { ... }
View
14 t/02-response-status.t
@@ -0,0 +1,14 @@
+use Test;
+use Bailador;
+use Bailador::Test;
+
+plan 4;
+
+get '/foo' => sub { }
+post '/bar' => sub { }
+
+response-status-is 'GET', '/foo', 200;
+response-status-isnt 'GET', '/bar', 200;
+
+response-status-is 'POST', '/bar', 200;
+response-status-isnt 'POST', '/foo', 200;
Please sign in to comment.
Something went wrong with that request. Please try again.