test/unit/runner_tests.rb in deas-0.43.4 vs test/unit/runner_tests.rb in deas-0.43.5

- old
+ new

@@ -10,36 +10,45 @@ class Deas::Runner class UnitTests < Assert::Context desc "Deas::Runner" setup do - @handler_class = EmptyViewHandler + @handler_class = Class.new{ include Deas::ViewHandler } @runner_class = Deas::Runner end subject{ @runner_class } + should have_imeth :body_value + should "know its default mime type" do assert_equal 'application/octet-stream', subject::DEFAULT_MIME_TYPE end should "know its default charset" do assert_equal 'utf-8', subject::DEFAULT_CHARSET end - should "know its default status" do - assert_equal 200, subject::DEFAULT_STATUS - end + should "know how to build appropriate body values" do + assert_nil subject.body_value([nil, ''].sample) - should "know its default body" do - assert_equal [], subject::DEFAULT_BODY + exp = [Factory.string] + assert_equal exp, subject.body_value(exp) + assert_equal exp, subject.body_value(exp.first) + + exp = [Factory.integer.to_s] + assert_equal exp, subject.body_value(exp.first.to_i) end end class InitTests < UnitTests desc "when init" setup do + @handler_class.default_status(Factory.integer) + @handler_class.default_headers(Factory.string => Factory.string) + @handler_class.default_body(Factory.string) + @request = Factory.request @runner = @runner_class.new(@handler_class, :request => @request) end subject{ @runner } @@ -149,13 +158,13 @@ assert_raises(NotImplementedError){ subject.run } end should "know its `to_rack` representation" do exp = [ - subject.class::DEFAULT_STATUS, + @handler_class.default_status, subject.headers.to_hash, - subject.class::DEFAULT_BODY + @handler_class.default_body ] assert_equal exp, subject.to_rack status = Factory.integer Assert.stub(subject, :status){ status } @@ -178,36 +187,35 @@ assert_equal exp, subject.status end should "know and merge values on its response headers" do assert_kind_of Rack::Utils::HeaderHash, subject.headers - assert_equal({}, subject.headers) + assert_equal @handler_class.default_headers, subject.headers new_header_values = { Factory.string => Factory.string } subject.headers(new_header_values) assert_kind_of Rack::Utils::HeaderHash, subject.headers - assert_equal new_header_values, subject.headers + exp = @handler_class.default_headers.merge(new_header_values) + assert_equal exp, subject.headers location = Factory.string subject.headers['Location'] = location - exp = new_header_values.merge('Location' => location) + exp = @handler_class.default_headers.merge( + new_header_values.merge('Location' => location) + ) assert_equal exp, subject.headers end should "know and set its response body" do assert_nil subject.body - exp = [Factory.string] - subject.body exp - assert_equal exp, subject.body + subject.body(nil) + assert_nil subject.body - subject.body exp.first - assert_equal exp, subject.body - - exp = [Factory.integer.to_s] - subject.body exp.first.to_i - assert_equal exp, subject.body + value = ['', [Factory.string], Factory.string, Factory.integer].sample + exp = Deas::Runner.body_value(value) + assert_equal exp, subject.body(value) end should "know and set its response content type header" do extname = ".#{Factory.string}" @@ -261,11 +269,11 @@ assert_equal exp, rack_utils_set_cookie_header_called_with end end - class HaltTests < InitTests + class HaltTests < UnitTests desc "the `halt` method" setup do @status = Factory.integer @headers = { Factory.string => Factory.string } @body = [Factory.string] @@ -295,10 +303,15 @@ runner = runner_halted_with(@status, @headers) assert_equal @status, runner.status assert_equal @headers, runner.headers assert_nil runner.body + runner = runner_halted_with(@status, @headers, '') + assert_equal @status, runner.status + assert_equal @headers, runner.headers + assert_nil runner.body + runner = runner_halted_with(@status, @body) assert_equal @status, runner.status assert_equal({}, runner.headers) assert_equal @body, runner.body @@ -321,14 +334,18 @@ end end end - class HaltCalledWithTests < InitTests + class HaltCalledWithTests < UnitTests setup do + @request = Factory.request + @runner = @runner_class.new(@handler_class, :request => @request) + @halt_called_with = nil Assert.stub(@runner, :halt){ |*args| @halt_called_with = args; throw :halt } end + subject{ @runner } end class RedirectTests < HaltCalledWithTests desc "the `redirect` method"