/plugins/thingfish-handler-inspect/spec/thingfish/handler/inspect_spec.rb

https://bitbucket.org/laika/thingfish · Ruby · 141 lines · 102 code · 32 blank · 7 comment · 6 complexity · 059e2e3c1b9cf9e6138248d859c6833d MD5 · raw file

  1. #!/usr/bin/env ruby
  2. BEGIN {
  3. require 'pathname'
  4. plugindir = Pathname.new( __FILE__ ).dirname.parent.parent.parent
  5. basedir = plugindir.parent.parent
  6. libdir = basedir + "lib"
  7. pluglibdir = plugindir + "lib"
  8. $LOAD_PATH.unshift( basedir ) unless $LOAD_PATH.include?( basedir )
  9. $LOAD_PATH.unshift( libdir ) unless $LOAD_PATH.include?( libdir )
  10. $LOAD_PATH.unshift( pluglibdir ) unless $LOAD_PATH.include?( pluglibdir )
  11. }
  12. require 'rspec'
  13. require 'spec/lib/helpers'
  14. require 'spec/lib/handler_behavior'
  15. require 'pathname'
  16. require 'stringio'
  17. require 'thingfish/constants'
  18. require 'thingfish/handler/inspect'
  19. require 'thingfish/exceptions'
  20. include ThingFish::Constants,
  21. ThingFish::TestConstants,
  22. ThingFish::SpecHelpers
  23. #####################################################################
  24. ### C O N T E X T S
  25. #####################################################################
  26. describe "The inspection handler" do
  27. before(:all) do
  28. ThingFish.reset_logger
  29. ThingFish.logger.level = Logger::FATAL
  30. end
  31. before(:each) do
  32. resdir = Pathname.new( __FILE__ ).expand_path.dirname.parent + 'resources'
  33. @handler = ThingFish::Handler.create( 'inspect', 'resource_dir' => resdir )
  34. @request = mock( "request" ).as_null_object
  35. @response = mock( "response" ).as_null_object
  36. @request_headers = mock( "request headers" ).as_null_object
  37. @request.stub!( :headers ).and_return( @request_headers )
  38. @response_headers = mock( "response headers" ).as_null_object
  39. @response.stub!( :headers ).and_return( @response_headers )
  40. @response_data = mock( "response data" ).as_null_object
  41. @response.stub!( :data ).and_return( @response_data )
  42. @daemon = mock( "daemon object" ).as_null_object
  43. @handler.on_startup( @daemon )
  44. end
  45. after( :all ) do
  46. reset_logging()
  47. end
  48. # Shared behaviors
  49. it_should_behave_like "a handler"
  50. # Examples
  51. it "responds with a Hash of supported objects for inspection if there's nothing in the path_info" do
  52. @response.should_receive( :status= ).with( HTTP::OK )
  53. @response.should_receive( :content_type= ).with( RUBY_MIMETYPE )
  54. @response.should_receive( :body= ).with( an_instance_of(Hash) )
  55. @response_data.should_receive( :[]= ).with( :tagline, an_instance_of(String) )
  56. @response_data.should_receive( :[]= ).with( :title, an_instance_of(String) )
  57. @handler.handle_get_request( '', @request, @response )
  58. end
  59. it "returns the current daemon object if the path_info contains 'daemon'" do
  60. @response.should_receive( :status= ).with( HTTP::OK )
  61. @response.should_receive( :body= ).with( @daemon )
  62. @response_data.should_receive( :[]= ).with( :tagline, an_instance_of(String) )
  63. @response_data.should_receive( :[]= ).with( :title, an_instance_of(String) )
  64. @handler.handle_get_request( '/daemon', @request, @response )
  65. end
  66. it "returns the current request object if the path_info contains 'request'" do
  67. @response.should_receive( :status= ).with( HTTP::OK )
  68. @response.should_receive( :body= ).with( @request )
  69. @response_data.should_receive( :[]= ).with( :tagline, an_instance_of(String) )
  70. @response_data.should_receive( :[]= ).with( :title, an_instance_of(String) )
  71. @handler.handle_get_request( '/request', @request, @response )
  72. end
  73. it "returns the current daemon object if the path_info contains 'response'" do
  74. @response.should_receive( :status= ).with( HTTP::OK )
  75. @response.should_receive( :body= ).with( @response )
  76. @response_data.should_receive( :[]= ).with( :tagline, an_instance_of(String) )
  77. @response_data.should_receive( :[]= ).with( :title, an_instance_of(String) )
  78. @handler.handle_get_request( '/response', @request, @response )
  79. end
  80. it "returns the current daemon's config object if the path_info contains '/config'" do
  81. @daemon.should_receive( :config ).and_return( :the_config )
  82. @response.should_receive( :status= ).with( HTTP::OK )
  83. @response.should_receive( :body= ).with( :the_config )
  84. @response_data.should_receive( :[]= ).with( :tagline, an_instance_of(String) )
  85. @response_data.should_receive( :[]= ).with( :title, an_instance_of(String) )
  86. @handler.handle_get_request( '/config', @request, @response )
  87. end
  88. it "leaves the response status at the default for unsupported paths" do
  89. @response.should_not_receive( :status= )
  90. @handler.handle_get_request( "/mudweasel/thermographics", @request, @response )
  91. end
  92. it "can make an HTML fragment out of the inspected object for the HTML filter" do
  93. body = mock( "body" ).as_null_object
  94. template = mock( "ERB template" ).as_null_object
  95. @handler.stub!( :get_erb_resource ).and_return( template )
  96. template.should_receive( :result ).with( an_instance_of(Binding) ).and_return( :something )
  97. @handler.make_html_content( body, @request, @response ).should == :something
  98. end
  99. end
  100. # vim: set nosta noet ts=4 sw=4: