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