Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Validate arguments to ShamRack.mount.

  • Loading branch information...
commit 830403d46ed67aa3d81e512f34c28f74304808b6 1 parent 8824008
@mdub authored
Showing with 33 additions and 10 deletions.
  1. +19 −10 lib/sham_rack/registry.rb
  2. +14 −0 spec/sham_rack_spec.rb
View
29 lib/sham_rack/registry.rb
@@ -1,12 +1,21 @@
module ShamRack
module Registry
-
+
+ ADDRESS_PATTERN = /^[a-z0-9-]+(\.[a-z0-9-]+)*$/i
+
def mount(rack_app, address, port = nil)
- port ||= Net::HTTP.default_port
+ unless address =~ ADDRESS_PATTERN
+ raise ArgumentError, "invalid address"
+ end
+ if port.nil?
+ port = Net::HTTP.default_port
+ else
+ port = Integer(port)
+ end
registry[[address, port]] = rack_app
end
-
+
def unmount_all
registry.clear
end
@@ -18,24 +27,24 @@ def at(address, port = nil, &block)
Registrar.new(address, port)
end
end
-
+
def application_for(address, port = nil)
port ||= Net::HTTP.default_port
registry[[address, port]]
end
private
-
+
def registry
@registry ||= {}
end
-
+
end
-
+
extend Registry
class Registrar
-
+
def initialize(address, port = nil)
@address = address
@port = port
@@ -46,7 +55,7 @@ def rackup(&block)
app = Rack::Builder.new(&block).to_app
ShamRack.mount(app, @address, @port)
end
-
+
def sinatra(&block)
require "sinatra/base"
sinatra_app = Class.new(Sinatra::Base)
@@ -58,7 +67,7 @@ def stub
require "sham_rack/stub_web_service"
ShamRack.mount(StubWebService.new, @address, @port)
end
-
+
end
end
View
14 spec/sham_rack_spec.rb
@@ -56,6 +56,20 @@
end
+ describe ".mount" do
+
+ context "with a URL" do
+
+ it "raises an ArgumentError" do
+ lambda do
+ ShamRack.mount(GreetingApp.new, "http://www.greetings.com")
+ end.should raise_error(ArgumentError, "invalid address")
+ end
+
+ end
+
+ end
+
describe ".at" do
describe "with a block" do
Please sign in to comment.
Something went wrong with that request. Please try again.