Permalink
Browse files

Quickie: Passed: 16, not quite: 0, total tests: 16.

  • Loading branch information...
1 parent edf200e commit a8f852c1766230aaee87c976dc7127ff668970cb @michaeldv michaeldv committed Jan 28, 2012
Showing with 185 additions and 14 deletions.
  1. +8 −7 lib/dio/base.rb
  2. +6 −6 lib/dio/controller.rb
  3. +1 −1 lib/dio/router.rb
  4. +14 −0 test/dio_test.rb
  5. +73 −0 test/done_test.rb
  6. +1 −0 test/helpers_test.rb
  7. +67 −0 test/quickie_helpers.rb
  8. +1 −0 test/router_test.rb
  9. +14 −0 test/routing_test.rb
View
@@ -51,8 +51,9 @@ def call(env)
@params = universal_nested_hash(@request.params)
@params[:controller] = $1 if @request.path =~ /\/([\w.]+)/
@params[:controller] ||= :home
- ap @request.path
- ap @params
+
+ # ap @request.path
+ # ap @params
reply = catch(:done) { dispatch! }
reply = Array(reply) unless reply.is_a?(Array)
@@ -76,7 +77,7 @@ def call(env)
headers?(reply[1]) or body?(reply[1]) if reply.size > 1
body?(reply[2]) if reply.size > 2
- ap [ response.status, response.headers, response.body ]
+## ap [ response.status, response.headers, response.body ]
[ response.status, response.headers, response.body ]
end
@@ -97,8 +98,8 @@ def dispatch!
#--------------------------------------------------------------------------
def salvage!(e)
- ap e
- ap e.backtrace
+ # ap e
+ # ap e.backtrace
headers :content_type => "text/html"
if NotFound === e
status 404
@@ -113,11 +114,11 @@ def salvage!(e)
end
# Load controller file and create an instance of controller class.
+ # TODO: track mktime and use require if the file hasn't changed.
#--------------------------------------------------------------------------
def load_controller
controller_file_name = "#{settings.root}/#{@params[:controller]}.rb"
- # TODO: track mktime and use require if the file hasn't changed.
- puts "Loading #{controller_file_name}"
+ # puts "Loading #{controller_file_name}"
load controller_file_name
constantize(@params[:controller]).new(self) # TODO: handle missing class.
rescue LoadError
View
@@ -37,10 +37,10 @@ def set_router_rules
#--------------------------------------------------------------------------
def route!
- puts "route!(#{params.inspect})"
+ # puts "route!(#{params.inspect})"
action = request.router.match(request, params).to_sym
- puts "router match => #{action.inspect}"
- ap params
+ # puts "router match => #{action.inspect}"
+ # ap params
invoke(:before, action) if hooks(:before).any?
#
@@ -63,7 +63,7 @@ def hooks(before_or_after)
#--------------------------------------------------------------------------
def invoke(before_or_after, action)
- puts "invoke(#{before_or_after.inspect}, #{action.inspect})"
+ # puts "invoke(#{before_or_after.inspect}, #{action.inspect})"
self.class.hooks[before_or_after].each do |hook|
next if hook[:only] && !Array(hook[:only]).include?(action)
next if hook[:except] && Array(hook[:except]).include?(action)
@@ -83,7 +83,7 @@ class << self
#--------------------------------------------------------------------------
def method_added(method)
- puts "instance method #{method.inspect} added"
+ # puts "instance method #{method.inspect} added"
if public_instance_methods.include?(method) && !@adding_method
begin
@adding_method = true
@@ -106,7 +106,7 @@ def add_json_renderer_for(method)
vars = instance_variables - [:@request, :@response, :@params]
hash = Hash[ vars.map { |var| [ var.to_s[1..-1], instance_variable_get(var) ] } ]
response.headers["Content-Type"] = "application/json"
- response.body = hash.to_json
+ response.body = [ hash.to_json ]
end
end
#
View
@@ -39,7 +39,7 @@ def default(verb, rule)
# method, find matching pattern, update params keys, and return the action.
#--------------------------------------------------------------------------
def match(request, params)
-ap @rules
+ # ap @rules
verb = request.request_method.downcase.to_sym # "GET" => :get
rules = @rules[verb] # Get the rules for the verb starting with the last rule.
rules += @rules[:any] # Append the rules for any type of request.
View
@@ -0,0 +1,14 @@
+root = File.expand_path("../..", __FILE__)
+$:.unshift "#{root}/lib"
+
+ENV["RACK_ENV"] = "test"
+
+require "quickie"
+require "json"
+require "dio"
+require "awesome_print"
+require "#{root}/test/quickie_helpers"
+
+Dir["#{root}/test/*_test.rb"].each do |file|
+ require file unless file.end_with? __FILE__
+end
View
@@ -0,0 +1,73 @@
+class DoneTest < DioTest::Base
+ # Test various combinations of Dio::Controller#done
+ #
+ # #done with single parameter:
+ # done 200
+ # done "X-key" => "value
+ # done "body text that is a string"
+ # done [ "body", "that", "responds", "to", ":each" ]
+ #--------------------------------------------------------------------------
+ controller %{
+ class Home < Dio::Controller
+ def index
+ done 404
+ end
+ end
+ } do
+ response = app.get("/")
+ response.code.should == "404"
+ response.body.should == ""
+ response["Content-Type"].should == "text/html"
+ end
+
+ controller %{
+ class Home < Dio::Controller
+ def index
+ done "X-Key" => "value"
+ end
+ end
+ } do
+ response = app.get("/")
+ response.code.should == "200"
+ response.body.should == ""
+ response["X-Key"].should == "value"
+ response["Content-Type"].should == "text/html"
+ end
+
+ controller %{
+ class Home < Dio::Controller
+ def index
+ done "index"
+ end
+ end
+ } do
+ response = app.get("/")
+ response.code.should == "200"
+ response.body.should == "index"
+ response["Content-Type"].should == "text/html"
+ end
+
+ controller %{
+ class Home < Dio::Controller
+ def index
+ done %w(hello world)
+ end
+ end
+ } do
+ response = app.get("/")
+ response.code.should == "200"
+ response.body.should == "helloworld"
+ response["Content-Type"].should == "text/html"
+ end
+
+ # #done with two parameters:
+ # done 200, { "X-key" => "value }
+ # done "body text that is a string"
+ # done 200, [ "body", "that", "responds", "to", ":each" ]
+ #--------------------------------------------------------------------------
+
+ # #done with three parameters:
+ # done 200, { "X-key" => "value }, "body text that is a string"
+ # done 200, { "X-key" => "value }, [ "body", "that", "responds", "to", ":each" ]
+ #--------------------------------------------------------------------------
+end
View
@@ -0,0 +1 @@
+# puts __FILE__
View
@@ -0,0 +1,67 @@
+require "net/http"
+require "rack"
+require "rack/lint"
+require "thin"
+
+class DioTest
+ class TestApp < Dio::Base
+ def get(path, headers = {})
+ # uri = URI.parse("http://0.0.0.0:3131#{path}")
+ # Net::HTTP.get_response(uri)
+ Net::HTTP.start("0.0.0.0", 3131) do |http|
+ request = Net::HTTP::Get.new(path, headers)
+ http.request(request)
+ end
+ end
+
+ def post(path, form_data = {}, headers = {})
+ Net::HTTP.start("0.0.0.0", 3131) do |http|
+ request = Net::HTTP::Post.new(path, headers)
+ request.form_data = form_data
+ http.request(request)
+ end
+ end
+ end
+
+ class Runner
+ def self.start!(application, host = "0.0.0.0", port = 3131)
+ dio = application.new
+
+ Thread.new do
+ Thin::Logging.silent = true
+ Rack::Handler::Thin.run(Rack::Lint.new(dio), :Host => host, :Port => port) do |s|
+ @server = s
+ end
+ end
+ Thread.pass until @server && @server.running?
+
+ dio
+ end
+
+ def self.stop!
+ @server.stop if @server && @server.running?
+ end
+ end
+
+ class Base
+ @@app = Runner.start!(TestApp)
+
+ def self.app
+ @@app
+ end
+
+ def self.controller(contents, &block)
+ controller = contents.match(/class\s+(\w+)\s*<\s*Dio/)[1]
+ raise "Invalid controller class" unless controller
+ begin
+ file = File.expand_path(File.dirname(__FILE__)) << "/#{controller.downcase}.rb"
+ File.open(file, "w") { |f| f.write(contents) }
+ yield
+ ensure
+ File.unlink(file)
+ end
+ end
+ end
+end
+
+at_exit { DioTest::Runner.stop! }
View
@@ -0,0 +1 @@
+# puts __FILE__
View
@@ -0,0 +1,14 @@
+class RoutingTest < DioTest::Base
+ controller %{
+ class Ping < Dio::Controller
+ def index
+ @ping = 42
+ end
+ end
+ } do
+ response = app.get("/ping")
+ response.code.should == "200"
+ response.body.should == { :ping => 42 }.to_json
+ response["Content-Type"].should == "application/json"
+ end
+end

0 comments on commit a8f852c

Please sign in to comment.