Skip to content

Commit

Permalink
Add Rack::HostMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrosby committed Apr 28, 2009
1 parent c0cd4ce commit 988cd66
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ interface:
* Rack::Backstage - Returns content of specified file if it exists, which makes
it convenient for putting up maintenance pages.
* Rack::Format - Adds a format extension at the end of the URI when there is none, corresponding to the mime-type given in the Accept HTTP header.
* Rack::HostMeta - Configures /host-meta using a block

=== Use

Expand Down
41 changes: 41 additions & 0 deletions lib/rack/contrib/host_meta.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Rack

# Rack middleware implementing the IETF draft: "Host Metadata for the Web"
#
# Usage:
# use Rack::HostMeta do
# register :uri => '/robots.txt', :rel => 'robots'
# register :uri => '/w3c/p3p.xml', :rel => 'privacy', :type => 'application/p3p.xml'
# end
#
# See also: http://tools.ietf.org/html/draft-nottingham-site-meta
#
# TODO:
# Accept POST operations allowing downstream services to register themselves
#
class HostMeta
def initialize(app, &block)
@app = app
@links = []
instance_eval(&block)
@response = @links.join("\n")
end

def call(env)
if env['PATH_INFO'] == '/host-meta'
[200, {'Content-Type' => 'application/host-meta'}, [@response]]
else
@app.call(env)
end
end

protected

def register(config)
link = "Link: <#{config[:uri]}>;"
link += " rel=\"#{config[:rel]}\"" if config[:rel]
link += " type=\"#{config[:type]}\"" if config[:type]
@links << link
end
end
end
43 changes: 43 additions & 0 deletions test/spec_rack_host_meta.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'test/spec'
require 'rack/mock'
require 'rack/contrib/host_meta'
require 'rack/contrib/not_found'

context "Rack::HostMeta" do

setup do
app = Rack::Builder.new do
use Rack::Lint
use Rack::ContentLength
use Rack::HostMeta do
register :uri => '/robots.txt', :rel => 'robots'
register :uri => '/w3c/p3p.xml', :rel => 'privacy', :type => 'application/p3p.xml'
end
run Rack::NotFound.new('test/404.html')
end
@response = Rack::MockRequest.new(app).get('/host-meta')
end

specify "should respond to /host-meta" do
@response.status.should.equal 200
end

specify "should respond with the correct media type" do
@response['Content-Type'].should.equal 'application/host-meta'
end

specify "should include a Link entry for each item in the config block" do
@response.body.should.match(/Link:\s*<\/robots.txt>;.*\n/)
@response.body.should.match(/Link:\s*<\/w3c\/p3p.xml>;.*/)
end

specify "should include a rel attribute for each Link entry where specified" do
@response.body.should.match(/rel="robots"/)
@response.body.should.match(/rel="privacy"/)
end

specify "should include a type attribute for each Link entry where specified" do
@response.body.should.match(/Link:\s*<\/w3c\/p3p.xml>;.*type.*application\/p3p.xml/)
end

end

0 comments on commit 988cd66

Please sign in to comment.