Permalink
Browse files

Add Rack::NotFound

  • Loading branch information...
1 parent af70908 commit 31370b78d75faa6609ffb2e15dbc2aa7edd0422d @jcrosby jcrosby committed Feb 2, 2009
Showing with 38 additions and 0 deletions.
  1. +1 −0 README.rdoc
  2. +1 −0 lib/rack/contrib.rb
  3. +18 −0 lib/rack/contrib/not_found.rb
  4. +1 −0 test/404.html
  5. +17 −0 test/spec_rack_not_found.rb
View
@@ -28,6 +28,7 @@ interface:
* Rack::Evil - Lets the rack application return a response to the client from any place.
* Rack::Callbacks - Implements DLS for pure before/after filter like Middlewares.
* Rack::Config - Shared configuration for cooperative middleware.
+* Rack::NotFound - A default 404 application.
=== Use
View
@@ -23,4 +23,5 @@ def self.release
autoload :Callbacks, "rack/contrib/callbacks"
autoload :NestedParams, "rack/contrib/nested_params"
autoload :Config, "rack/contrib/config"
+ autoload :NotFound, "rack/contrib/not_found"
end
@@ -0,0 +1,18 @@
+module Rack
+ # Rack::NotFound is a default endpoint. Initialize with the path to
+ # your 404 page.
+
+ class NotFound
+ F = ::File
+
+ def initialize(path)
+ file = F.expand_path(path)
+ @content = F.read(file)
+ @length = @content.size.to_s
+ end
+
+ def call(env)
+ [404, {'Content-Type' => 'text/html', 'Content-Length' => @length}, [@content]]
+ end
+ end
+end
View
@@ -0,0 +1 @@
+Not Found
@@ -0,0 +1,17 @@
+require 'test/spec'
+require 'rack/mock'
+require 'rack/contrib/not_found'
+
+context "Rack::NotFound" do
+
+ specify "should render the file at the given path for all requests" do
+ app = Rack::Builder.new do
+ use Rack::Lint
+ run Rack::NotFound.new('test/404.html')
+ end
+ response = Rack::MockRequest.new(app).get('/')
+ response.body.should.equal('Not Found')
+ response.status.should.equal(404)
+ end
+
+end

0 comments on commit 31370b7

Please sign in to comment.