forked from rack/rack-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |