test/test_resource.rb in impression-0.2 vs test/test_resource.rb in impression-0.3
- old
+ new
@@ -76,20 +76,20 @@
req = mock_req(':method' => 'GET', ':path' => '/')
assert_nil r1.route(req)
req = mock_req(':method' => 'GET', ':path' => '/foo')
- r1.route(req).respond(req)
+ r1.route_and_call(req)
# default reply
assert_equal Qeweney::Status::NOT_FOUND, req.response_status
req = mock_req(':method' => 'GET', ':path' => '/foo/bar')
- r1.route(req).respond(req)
+ r1.route_and_call(req)
assert_equal '/foo/bar', req.response_body
req = mock_req(':method' => 'GET', ':path' => '/foo/baz')
- r1.route(req).respond(req)
+ r1.route_and_call(req)
assert_equal '/foo/baz', req.response_body
req = mock_req(':method' => 'GET', ':path' => '/foo/bbb')
assert_equal r1, r1.route(req)
end
@@ -101,29 +101,101 @@
req = mock_req(':method' => 'GET', ':path' => '/')
assert_nil r1.route(req)
req = mock_req(':method' => 'GET', ':path' => '/foo')
- r1.route(req).respond(req)
+ r1.route_and_call(req)
assert_equal '/foo /', req.response_body
req = mock_req(':method' => 'GET', ':path' => '/foo/zzz')
- r1.route(req).respond(req)
+ r1.route_and_call(req)
assert_equal '/foo /zzz', req.response_body
req = mock_req(':method' => 'GET', ':path' => '/foo/bar')
- r1.route(req).respond(req)
+ r1.route_and_call(req)
assert_equal '/foo/bar /', req.response_body
req = mock_req(':method' => 'GET', ':path' => '/foo/bar/zzz')
- r1.route(req).respond(req)
+ r1.route_and_call(req)
assert_equal '/foo/bar /zzz', req.response_body
req = mock_req(':method' => 'GET', ':path' => '/foo/baz')
- r1.route(req).respond(req)
+ r1.route_and_call(req)
assert_equal '/foo/baz /', req.response_body
req = mock_req(':method' => 'GET', ':path' => '/foo/baz/xxx/yyy')
- r1.route(req).respond(req)
+ r1.route_and_call(req)
assert_equal '/foo/baz /xxx/yyy', req.response_body
+ end
+
+ class CallableResource < Impression::Resource
+ def initialize(**props, &block)
+ super(**props)
+ @block = block
+ end
+
+ def call(req)
+ @block.call(req)
+ end
+ end
+
+ def test_callable_resource
+ r1 = CompletePathInfoRenderingResource.new(path: 'foo')
+ r2 = CallableResource.new(parent: r1, path: 'bar') { |req| req.respond('hi') }
+
+ req = mock_req(':method' => 'GET', ':path' => '/foo/bar')
+ r1.route_and_call(req)
+ assert_equal 'hi', req.response_body
+ end
+
+ class CallableRouteResource < Impression::Resource
+ def initialize(**props, &block)
+ super(**props)
+ @block = block
+ end
+
+ def route(req)
+ @block
+ end
+ end
+
+ def test_callable_from_route_method
+ r1 = CompletePathInfoRenderingResource.new(path: 'foo')
+ r2 = CallableRouteResource.new(parent: r1, path: 'bar') { |req| req.respond('bye') }
+
+ req = mock_req(':method' => 'GET', ':path' => '/foo/bar')
+ r1.route_and_call(req)
+ assert_equal 'bye', req.response_body
+ end
+
+ def test_text_response
+ c = Class.new(Impression::Resource) do
+ def route(req)
+ case req.path
+ when '/text'
+ text_response('foo')
+ when '/html'
+ html_response('bar')
+ when '/json'
+ json_response({ :baz => 123 })
+ end
+ end
+ end
+
+ r = c.new(path: '/')
+
+ req = mock_req(':method' => 'GET', ':path' => '/text')
+ r.route_and_call(req)
+ assert_equal 'foo', req.response_body
+ assert_equal 'text/plain', req.response_content_type
+
+ req = mock_req(':method' => 'GET', ':path' => '/html')
+ r.route_and_call(req)
+ assert_equal 'bar', req.response_body
+ assert_equal 'text/html', req.response_content_type
+
+ req = mock_req(':method' => 'GET', ':path' => '/json')
+ r.route_and_call(req)
+ assert_equal '{"baz":123}', req.response_body
+ assert_equal 'application/json', req.response_content_type
end
end