Skip to content

Commit

Permalink
MockServer.new takes generic Rack app
Browse files Browse the repository at this point in the history
  • Loading branch information
vangberg committed Dec 9, 2010
1 parent a9e5084 commit ba97239
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
10 changes: 5 additions & 5 deletions lib/mock_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ class App < Sinatra::Base
use Rack::ShowExceptions
end

def initialize(port = 4000, &block)
def initialize(app, port = 4000, &block)
@app = app
@port = port

@app = Class.new(Sinatra::Base)
@app.class_eval(&block)
end

def start
Expand All @@ -27,7 +25,9 @@ def start

module Methods
def mock_server(*args, &block)
@server = MockServer.new(*args, &block).start
app = Class.new(Sinatra::Base)
app.class_eval(&block)
@server = MockServer.new(app, *args, &block).start
end
end

Expand Down
33 changes: 25 additions & 8 deletions test/mock_server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,49 @@
require "ruby-debug"
require "open-uri"

class MockServerTest < Test::Unit::TestCase
@@server = MockServer.new do
get "/" do
"Hello"
end
class HelloWorldSinatra < Sinatra::Base
get "/" do
"Hello"
end
end

class MockServerTest < Test::Unit::TestCase
def setup
@@server.start
@server = MockServer.new(HelloWorldSinatra)
@server.start
end

def test_server
assert_equal "Hello", open("http://localhost:4000").read
end
end

HelloWorldRackBuilder = Rack::Builder.new do
run lambda {|env|
[200, {"Content-Type" => "text/plain", "Content-Length" => "7"}, ["Rackup!"]]
}
end

class MockServerRackBuilderTest < Test::Unit::TestCase
def setup
@server = MockServer.new(HelloWorldRackBuilder, 4001)
@server.start
end

def test_server
assert_equal "Rackup!", open("http://localhost:4001").read
end end

class MockServerMethodsTest < Test::Unit::TestCase
extend MockServer::Methods

mock_server(4001) {
mock_server(4002) {
get "/" do
"Goodbye"
end
}

def test_server
assert_equal "Goodbye", open("http://localhost:4001").read
assert_equal "Goodbye", open("http://localhost:4002").read
end
end

0 comments on commit ba97239

Please sign in to comment.