sanford 0.10.1 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/README.md +41 -56
- data/Rakefile +0 -1
- data/bench/client.rb +8 -3
- data/bench/{services.rb → config.sanford} +11 -6
- data/bench/{runner.rb → report.rb} +2 -2
- data/bench/report.txt +32 -32
- data/lib/sanford/cli.rb +42 -28
- data/lib/sanford/config_file.rb +79 -0
- data/lib/sanford/{worker.rb → connection_handler.rb} +28 -20
- data/lib/sanford/error_handler.rb +7 -7
- data/lib/sanford/pid_file.rb +42 -0
- data/lib/sanford/process.rb +136 -0
- data/lib/sanford/process_signal.rb +20 -0
- data/lib/sanford/route.rb +48 -0
- data/lib/sanford/router.rb +36 -0
- data/lib/sanford/runner.rb +30 -58
- data/lib/sanford/sanford_runner.rb +19 -9
- data/lib/sanford/server.rb +211 -42
- data/lib/sanford/server_data.rb +47 -0
- data/lib/sanford/service_handler.rb +8 -46
- data/lib/sanford/template_source.rb +19 -2
- data/lib/sanford/test_runner.rb +27 -28
- data/lib/sanford/version.rb +1 -1
- data/lib/sanford.rb +1 -23
- data/sanford.gemspec +4 -5
- data/test/helper.rb +3 -20
- data/test/support/app_server.rb +142 -0
- data/test/support/config.sanford +7 -0
- data/test/support/config_invalid_run.sanford +3 -0
- data/test/support/config_no_run.sanford +0 -0
- data/test/support/fake_server_connection.rb +58 -0
- data/test/support/pid_file_spy.rb +19 -0
- data/test/support/template.erb +1 -0
- data/test/system/server_tests.rb +378 -0
- data/test/system/service_handler_tests.rb +224 -0
- data/test/unit/cli_tests.rb +187 -0
- data/test/unit/config_file_tests.rb +59 -0
- data/test/unit/connection_handler_tests.rb +254 -0
- data/test/unit/error_handler_tests.rb +30 -35
- data/test/unit/pid_file_tests.rb +70 -0
- data/test/unit/process_signal_tests.rb +61 -0
- data/test/unit/process_tests.rb +428 -0
- data/test/unit/route_tests.rb +92 -0
- data/test/unit/router_tests.rb +65 -0
- data/test/unit/runner_tests.rb +61 -15
- data/test/unit/sanford_runner_tests.rb +162 -28
- data/test/unit/sanford_tests.rb +0 -8
- data/test/unit/server_data_tests.rb +87 -0
- data/test/unit/server_tests.rb +502 -21
- data/test/unit/service_handler_tests.rb +114 -219
- data/test/unit/template_engine_tests.rb +1 -1
- data/test/unit/template_source_tests.rb +56 -16
- data/test/unit/test_runner_tests.rb +206 -0
- metadata +67 -67
- data/bench/tasks.rb +0 -41
- data/lib/sanford/config.rb +0 -28
- data/lib/sanford/host.rb +0 -129
- data/lib/sanford/host_data.rb +0 -65
- data/lib/sanford/hosts.rb +0 -38
- data/lib/sanford/manager.rb +0 -275
- data/test/support/fake_connection.rb +0 -36
- data/test/support/helpers.rb +0 -17
- data/test/support/service_handlers.rb +0 -154
- data/test/support/services.rb +0 -123
- data/test/support/simple_client.rb +0 -62
- data/test/system/request_handling_tests.rb +0 -306
- data/test/unit/config_tests.rb +0 -56
- data/test/unit/host_data_tests.rb +0 -71
- data/test/unit/host_tests.rb +0 -141
- data/test/unit/hosts_tests.rb +0 -50
- data/test/unit/manager_tests.rb +0 -195
- data/test/unit/worker_tests.rb +0 -24
@@ -0,0 +1,187 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'sanford/cli'
|
3
|
+
|
4
|
+
class Sanford::CLI
|
5
|
+
|
6
|
+
class UnitTests < Assert::Context
|
7
|
+
desc "Sanford::CLI"
|
8
|
+
setup do
|
9
|
+
@kernel_spy = KernelSpy.new
|
10
|
+
@file_path = Factory.file_path
|
11
|
+
|
12
|
+
@server = TestServer.new
|
13
|
+
|
14
|
+
@config_file = FakeConfigFile.new(@server)
|
15
|
+
Assert.stub(Sanford::ConfigFile, :new).with(@file_path){ @config_file }
|
16
|
+
|
17
|
+
@cli = Sanford::CLI.new(@kernel_spy)
|
18
|
+
end
|
19
|
+
subject{ @cli }
|
20
|
+
|
21
|
+
should have_cmeths :run
|
22
|
+
should have_imeths :run
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
class CommandTests < UnitTests
|
27
|
+
setup do
|
28
|
+
@process_spy = ProcessSpy.new
|
29
|
+
@process_signal_spy = ProcessSignalSpy.new
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
class DefaultsTests < CommandTests
|
35
|
+
desc "with no command or file path"
|
36
|
+
setup do
|
37
|
+
file_path = 'config.sanford'
|
38
|
+
Assert.stub(Sanford::ConfigFile, :new).with(file_path){ @config_file }
|
39
|
+
Assert.stub(Sanford::Process, :new).with(@server, :daemonize => false) do
|
40
|
+
@process_spy
|
41
|
+
end
|
42
|
+
|
43
|
+
@cli.run
|
44
|
+
end
|
45
|
+
|
46
|
+
should "have defaulted the command and file path" do
|
47
|
+
assert_true @process_spy.run_called
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
class RunTests < CommandTests
|
53
|
+
desc "with the run command"
|
54
|
+
setup do
|
55
|
+
Assert.stub(Sanford::Process, :new).with(@server, :daemonize => false) do
|
56
|
+
@process_spy
|
57
|
+
end
|
58
|
+
|
59
|
+
@cli.run(@file_path, 'run')
|
60
|
+
end
|
61
|
+
|
62
|
+
should "have built and run a non-daemonized process" do
|
63
|
+
assert_true @process_spy.run_called
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
class StartTests < CommandTests
|
69
|
+
desc "with the start command"
|
70
|
+
setup do
|
71
|
+
Assert.stub(Sanford::Process, :new).with(@server, :daemonize => true) do
|
72
|
+
@process_spy
|
73
|
+
end
|
74
|
+
|
75
|
+
@cli.run(@file_path, 'start')
|
76
|
+
end
|
77
|
+
|
78
|
+
should "have built and run a daemonized process" do
|
79
|
+
assert_true @process_spy.run_called
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
class StopTests < CommandTests
|
85
|
+
desc "with the stop command"
|
86
|
+
setup do
|
87
|
+
Assert.stub(Sanford::ProcessSignal, :new).with(@server, 'TERM') do
|
88
|
+
@process_signal_spy
|
89
|
+
end
|
90
|
+
|
91
|
+
@cli.run(@file_path, 'stop')
|
92
|
+
end
|
93
|
+
|
94
|
+
should "have built and sent a TERM signal" do
|
95
|
+
assert_true @process_signal_spy.send_called
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
class RestartTests < CommandTests
|
101
|
+
desc "with the restart command"
|
102
|
+
setup do
|
103
|
+
Assert.stub(Sanford::ProcessSignal, :new).with(@server, 'USR2') do
|
104
|
+
@process_signal_spy
|
105
|
+
end
|
106
|
+
|
107
|
+
@cli.run(@file_path, 'restart')
|
108
|
+
end
|
109
|
+
|
110
|
+
should "have built and sent a USR2 signal" do
|
111
|
+
assert_true @process_signal_spy.send_called
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
class InvalidCommandTests < UnitTests
|
117
|
+
desc "with an invalid command"
|
118
|
+
setup do
|
119
|
+
@command = Factory.string
|
120
|
+
@cli.run(@file_path, @command)
|
121
|
+
end
|
122
|
+
|
123
|
+
should "output the error with the help" do
|
124
|
+
expected = "#{@command.inspect} is not a valid command"
|
125
|
+
assert_includes expected, @kernel_spy.output
|
126
|
+
assert_includes "Usage: sanford", @kernel_spy.output
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
class KernelSpy
|
132
|
+
attr_reader :exit_status
|
133
|
+
|
134
|
+
def initialize
|
135
|
+
@output = StringIO.new
|
136
|
+
@exit_status = nil
|
137
|
+
end
|
138
|
+
|
139
|
+
def output
|
140
|
+
@output.rewind
|
141
|
+
@output.read
|
142
|
+
end
|
143
|
+
|
144
|
+
def puts(message)
|
145
|
+
@output.puts(message)
|
146
|
+
end
|
147
|
+
|
148
|
+
def exit(code)
|
149
|
+
@exit_status = code
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
class TestServer
|
154
|
+
include Sanford::Server
|
155
|
+
|
156
|
+
name Factory.string
|
157
|
+
ip Factory.string
|
158
|
+
port Factory.integer
|
159
|
+
end
|
160
|
+
|
161
|
+
FakeConfigFile = Struct.new(:server)
|
162
|
+
|
163
|
+
class ProcessSpy
|
164
|
+
attr_reader :run_called
|
165
|
+
|
166
|
+
def initialize
|
167
|
+
@run_called = false
|
168
|
+
end
|
169
|
+
|
170
|
+
def run
|
171
|
+
@run_called = true
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
class ProcessSignalSpy
|
176
|
+
attr_reader :send_called
|
177
|
+
|
178
|
+
def initialize
|
179
|
+
@send_called = false
|
180
|
+
end
|
181
|
+
|
182
|
+
def send
|
183
|
+
@send_called = true
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'sanford/config_file'
|
3
|
+
|
4
|
+
class Sanford::ConfigFile
|
5
|
+
|
6
|
+
class UnitTests < Assert::Context
|
7
|
+
desc "Sanford::ConfigFile"
|
8
|
+
setup do
|
9
|
+
@file_path = ROOT_PATH.join('test/support/config.sanford')
|
10
|
+
@config_file = Sanford::ConfigFile.new(@file_path)
|
11
|
+
end
|
12
|
+
subject{ @config_file }
|
13
|
+
|
14
|
+
should have_readers :server
|
15
|
+
should have_imeths :run
|
16
|
+
|
17
|
+
should "know its server" do
|
18
|
+
assert_instance_of AppServer, subject.server
|
19
|
+
end
|
20
|
+
|
21
|
+
should "define constants in the file at the top-level binding" do
|
22
|
+
assert_not_nil defined?(::TestConstant)
|
23
|
+
end
|
24
|
+
|
25
|
+
should "set its server using run" do
|
26
|
+
fake_server = Factory.string
|
27
|
+
subject.run fake_server
|
28
|
+
assert_equal fake_server, subject.server
|
29
|
+
end
|
30
|
+
|
31
|
+
should "allow passing a path without the extension" do
|
32
|
+
file_path = ROOT_PATH.join('test/support/config')
|
33
|
+
config_file = nil
|
34
|
+
|
35
|
+
assert_nothing_raised do
|
36
|
+
config_file = Sanford::ConfigFile.new(file_path)
|
37
|
+
end
|
38
|
+
assert_instance_of AppServer, config_file.server
|
39
|
+
end
|
40
|
+
|
41
|
+
should "raise no config file error when the file doesn't exist" do
|
42
|
+
assert_raises(NoConfigFileError) do
|
43
|
+
Sanford::ConfigFile.new(Factory.file_path)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
should "raise a no server error when the file doesn't call run" do
|
48
|
+
file_path = ROOT_PATH.join('test/support/config_no_run.sanford')
|
49
|
+
assert_raises(NoServerError){ Sanford::ConfigFile.new(file_path) }
|
50
|
+
end
|
51
|
+
|
52
|
+
should "raise a no server error when the file provides an invalid server" do
|
53
|
+
file_path = ROOT_PATH.join('test/support/config_invalid_run.sanford')
|
54
|
+
assert_raises(NoServerError){ Sanford::ConfigFile.new(file_path) }
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,254 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'sanford/connection_handler'
|
3
|
+
|
4
|
+
require 'sanford/route'
|
5
|
+
require 'sanford/server_data'
|
6
|
+
require 'test/support/fake_server_connection'
|
7
|
+
|
8
|
+
class Sanford::ConnectionHandler
|
9
|
+
|
10
|
+
class UnitTests < Assert::Context
|
11
|
+
desc "Sanford::ConnectionHandler"
|
12
|
+
setup do
|
13
|
+
@route = Sanford::Route.new(Factory.string, TestHandler.to_s).tap(&:validate!)
|
14
|
+
@server_data = Sanford::ServerData.new({
|
15
|
+
:logger => Sanford::NullLogger.new,
|
16
|
+
:verbose_logging => Factory.boolean,
|
17
|
+
:routes => [ @route ]
|
18
|
+
})
|
19
|
+
@connection = FakeServerConnection.with_request(@route.name)
|
20
|
+
@request = @connection.request
|
21
|
+
@response = Sanford::Protocol::Response.new(Factory.integer, Factory.string)
|
22
|
+
@exception = RuntimeError.new(Factory.string)
|
23
|
+
|
24
|
+
@handler_class = Sanford::ConnectionHandler
|
25
|
+
end
|
26
|
+
subject{ @handler_class }
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
class InitTests < UnitTests
|
31
|
+
desc "when init"
|
32
|
+
setup do
|
33
|
+
@connection_handler = @handler_class.new(@server_data, @connection)
|
34
|
+
end
|
35
|
+
subject{ @connection_handler }
|
36
|
+
|
37
|
+
should have_readers :server_data, :connection
|
38
|
+
should have_readers :logger
|
39
|
+
should have_imeths :run
|
40
|
+
|
41
|
+
should "know its server data and connection" do
|
42
|
+
assert_equal @server_data, subject.server_data
|
43
|
+
assert_equal @connection, subject.connection
|
44
|
+
end
|
45
|
+
|
46
|
+
should "know its logger" do
|
47
|
+
assert_instance_of Sanford::Logger, subject.logger
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
class RunTests < InitTests
|
53
|
+
desc "and run"
|
54
|
+
setup do
|
55
|
+
@route_called_with = nil
|
56
|
+
Assert.stub(@route, :run) do |*args|
|
57
|
+
@route_called_with = args
|
58
|
+
@response
|
59
|
+
end
|
60
|
+
|
61
|
+
@processed_service = @connection_handler.run
|
62
|
+
end
|
63
|
+
subject{ @processed_service }
|
64
|
+
|
65
|
+
should "return a processed service" do
|
66
|
+
assert_instance_of ProcessedService, subject
|
67
|
+
assert_equal @request, subject.request
|
68
|
+
assert_equal @route.handler_class, subject.handler_class
|
69
|
+
assert_equal @response, subject.response
|
70
|
+
assert_nil subject.exception
|
71
|
+
assert_not_nil subject.time_taken
|
72
|
+
end
|
73
|
+
|
74
|
+
should "run the route" do
|
75
|
+
assert_not_nil @route_called_with
|
76
|
+
assert_includes @request, @route_called_with
|
77
|
+
assert_includes @server_data, @route_called_with
|
78
|
+
end
|
79
|
+
|
80
|
+
should "have written the response to the connection" do
|
81
|
+
assert_equal @response, @connection.response
|
82
|
+
assert_true @connection.write_closed
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
class RunWithExceptionTests < InitTests
|
88
|
+
desc "and run with a route that throws an exception"
|
89
|
+
setup do
|
90
|
+
Assert.stub(@route, :run){ raise @exception }
|
91
|
+
|
92
|
+
error_handler = Sanford::ErrorHandler.new(
|
93
|
+
@exception,
|
94
|
+
@server_data,
|
95
|
+
@request
|
96
|
+
)
|
97
|
+
@expected_response = error_handler.run
|
98
|
+
@expected_exception = error_handler.exception
|
99
|
+
|
100
|
+
@processed_service = @connection_handler.run
|
101
|
+
end
|
102
|
+
subject{ @processed_service }
|
103
|
+
|
104
|
+
should "return a processed service with an exception" do
|
105
|
+
assert_instance_of ProcessedService, subject
|
106
|
+
assert_equal @expected_response, subject.response
|
107
|
+
assert_equal @expected_exception, subject.exception
|
108
|
+
end
|
109
|
+
|
110
|
+
should "have written the error response to the connection" do
|
111
|
+
assert_equal @expected_response, @connection.response
|
112
|
+
assert_true @connection.write_closed
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
class RunWithExceptionWhileWritingTests < InitTests
|
118
|
+
desc "and run with an exception thrown while writing the response"
|
119
|
+
setup do
|
120
|
+
Assert.stub(@route, :run){ @response }
|
121
|
+
|
122
|
+
@connection.raise_on_write = true
|
123
|
+
|
124
|
+
error_handler = Sanford::ErrorHandler.new(
|
125
|
+
@connection.write_exception,
|
126
|
+
@server_data,
|
127
|
+
@request
|
128
|
+
)
|
129
|
+
@expected_response = error_handler.run
|
130
|
+
@expected_exception = error_handler.exception
|
131
|
+
|
132
|
+
@processed_service = @connection_handler.run
|
133
|
+
end
|
134
|
+
subject{ @processed_service }
|
135
|
+
|
136
|
+
should "return a processed service with an exception" do
|
137
|
+
assert_instance_of ProcessedService, subject
|
138
|
+
assert_equal @expected_response, subject.response
|
139
|
+
assert_equal @expected_exception, subject.exception
|
140
|
+
end
|
141
|
+
|
142
|
+
should "have written the error response to the connection" do
|
143
|
+
assert_equal @expected_response, @connection.response
|
144
|
+
assert_true @connection.write_closed
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
class RunWithExceptionWhileDebuggingTests < InitTests
|
150
|
+
desc "and run with a route that throws an exception in debug mode"
|
151
|
+
setup do
|
152
|
+
ENV['SANFORD_DEBUG'] = '1'
|
153
|
+
Assert.stub(@route, :run){ raise @exception }
|
154
|
+
end
|
155
|
+
teardown do
|
156
|
+
ENV.delete('SANFORD_DEBUG')
|
157
|
+
end
|
158
|
+
|
159
|
+
should "raise the exception" do
|
160
|
+
assert_raises(@exception.class){ @connection_handler.run }
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
class RunWithVerboseLoggingTests < UnitTests
|
166
|
+
desc "run with verbose logging"
|
167
|
+
setup do
|
168
|
+
@spy_logger = SpyLogger.new
|
169
|
+
@server_data = Sanford::ServerData.new({
|
170
|
+
:logger => @spy_logger,
|
171
|
+
:verbose_logging => true,
|
172
|
+
:routes => [ @route ]
|
173
|
+
})
|
174
|
+
Assert.stub(@route, :run){ raise @exception }
|
175
|
+
|
176
|
+
@connection_handler = @handler_class.new(@server_data, @connection)
|
177
|
+
@processed_service = @connection_handler.run
|
178
|
+
end
|
179
|
+
subject{ @spy_logger }
|
180
|
+
|
181
|
+
should "have logged the service" do
|
182
|
+
time_taken = @processed_service.time_taken
|
183
|
+
status = @processed_service.response.status.to_s
|
184
|
+
expected = "[Sanford] ===== Received request =====" \
|
185
|
+
"[Sanford] Service: #{@request.name.inspect}" \
|
186
|
+
"[Sanford] Params: #{@request.params.inspect}" \
|
187
|
+
"[Sanford] Handler: #{@route.handler_class}" \
|
188
|
+
"[Sanford] ===== Completed in #{time_taken}ms #{status} ====="
|
189
|
+
assert_equal expected, subject.info_logged.join
|
190
|
+
end
|
191
|
+
|
192
|
+
should "log an exception when one is thrown" do
|
193
|
+
err = @processed_service.exception
|
194
|
+
backtrace = err.backtrace.join("\n")
|
195
|
+
expected = "[Sanford] #{err.class}: #{err.message}\n#{backtrace}"
|
196
|
+
assert_equal expected, subject.error_logged.join
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
class RunWithSummaryLoggingTests < UnitTests
|
202
|
+
desc "run with summary logging"
|
203
|
+
setup do
|
204
|
+
@spy_logger = SpyLogger.new
|
205
|
+
@server_data = Sanford::ServerData.new({
|
206
|
+
:logger => @spy_logger,
|
207
|
+
:verbose_logging => false,
|
208
|
+
:routes => [ @route ]
|
209
|
+
})
|
210
|
+
Assert.stub(@route, :run){ raise @exception }
|
211
|
+
|
212
|
+
@connection_handler = @handler_class.new(@server_data, @connection)
|
213
|
+
@processed_service = @connection_handler.run
|
214
|
+
end
|
215
|
+
subject{ @spy_logger }
|
216
|
+
|
217
|
+
should "have logged the service" do
|
218
|
+
time_taken = @processed_service.time_taken
|
219
|
+
status = @processed_service.response.status.to_i
|
220
|
+
expected = "[Sanford] " \
|
221
|
+
"time=#{time_taken} " \
|
222
|
+
"status=#{status} " \
|
223
|
+
"handler=#{@route.handler_class} " \
|
224
|
+
"service=#{@request.name.inspect} " \
|
225
|
+
"params=#{@request.params.inspect}"
|
226
|
+
assert_equal expected, subject.info_logged.join
|
227
|
+
end
|
228
|
+
|
229
|
+
should "not have logged the exception" do
|
230
|
+
assert_empty @spy_logger.error_logged
|
231
|
+
end
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
TestHandler = Class.new
|
236
|
+
|
237
|
+
class SpyLogger
|
238
|
+
attr_reader :info_logged, :error_logged
|
239
|
+
|
240
|
+
def initialize
|
241
|
+
@info_logged = []
|
242
|
+
@error_logged = []
|
243
|
+
end
|
244
|
+
|
245
|
+
def info(message)
|
246
|
+
@info_logged << message
|
247
|
+
end
|
248
|
+
|
249
|
+
def error(message)
|
250
|
+
@error_logged << message
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
end
|
@@ -1,18 +1,20 @@
|
|
1
1
|
require 'assert'
|
2
2
|
require 'sanford/error_handler'
|
3
3
|
|
4
|
+
require 'sanford/server_data'
|
5
|
+
|
4
6
|
class Sanford::ErrorHandler
|
5
7
|
|
6
8
|
class UnitTests < Assert::Context
|
7
9
|
desc "Sanford::ErrorHandler"
|
8
10
|
setup do
|
9
11
|
@exception = RuntimeError.new('test')
|
10
|
-
@
|
11
|
-
@error_handler = Sanford::ErrorHandler.new(@exception, @
|
12
|
+
@server_data = Sanford::ServerData.new
|
13
|
+
@error_handler = Sanford::ErrorHandler.new(@exception, @server_data)
|
12
14
|
end
|
13
15
|
subject{ @error_handler }
|
14
16
|
|
15
|
-
should have_imeths :exception, :
|
17
|
+
should have_imeths :exception, :server_data, :request, :run
|
16
18
|
|
17
19
|
should "return a Sanford::Protocol::Response with `run`" do
|
18
20
|
assert_instance_of Sanford::Protocol::Response, subject.run
|
@@ -31,18 +33,13 @@ class Sanford::ErrorHandler
|
|
31
33
|
|
32
34
|
class ResponseFromProcTests < UnitTests
|
33
35
|
desc "generating a respone from an error proc"
|
34
|
-
setup do
|
35
|
-
@host_defaults = { :ip => "localhost", :port => 8000 }
|
36
|
-
end
|
37
36
|
|
38
37
|
should "use the return-value of the error proc if it is a protocol response" do
|
39
38
|
error_proc = proc do |exception, host_data, request|
|
40
39
|
Sanford::Protocol::Response.new([ 567, 'custom message'], 'custom data')
|
41
40
|
end
|
42
|
-
|
43
|
-
|
44
|
-
}))
|
45
|
-
response = Sanford::ErrorHandler.new(@exception, host_data).run
|
41
|
+
server_data = Sanford::ServerData.new(:error_procs => [ error_proc ])
|
42
|
+
response = Sanford::ErrorHandler.new(@exception, server_data).run
|
46
43
|
|
47
44
|
assert_equal 567, response.code
|
48
45
|
assert_equal 'custom message', response.status.message
|
@@ -50,10 +47,8 @@ class Sanford::ErrorHandler
|
|
50
47
|
end
|
51
48
|
|
52
49
|
should "use an integer returned by the error proc to generate a protocol response" do
|
53
|
-
|
54
|
-
|
55
|
-
}))
|
56
|
-
response = Sanford::ErrorHandler.new(@exception, host_data).run
|
50
|
+
server_data = Sanford::ServerData.new(:error_procs => [ proc{ 345 } ])
|
51
|
+
response = Sanford::ErrorHandler.new(@exception, server_data).run
|
57
52
|
|
58
53
|
assert_equal 345, response.code
|
59
54
|
assert_nil response.status.message
|
@@ -61,10 +56,10 @@ class Sanford::ErrorHandler
|
|
61
56
|
end
|
62
57
|
|
63
58
|
should "use a symbol returned by the error proc to generate a protocol response" do
|
64
|
-
|
59
|
+
server_data = Sanford::ServerData.new({
|
65
60
|
:error_procs => [ proc{ :not_found } ]
|
66
|
-
})
|
67
|
-
response = Sanford::ErrorHandler.new(@exception,
|
61
|
+
})
|
62
|
+
response = Sanford::ErrorHandler.new(@exception, server_data).run
|
68
63
|
|
69
64
|
assert_equal 404, response.code
|
70
65
|
assert_nil response.status.message
|
@@ -72,10 +67,8 @@ class Sanford::ErrorHandler
|
|
72
67
|
end
|
73
68
|
|
74
69
|
should "use the default behavior if the error proc doesn't return a valid response result" do
|
75
|
-
|
76
|
-
|
77
|
-
}))
|
78
|
-
response = Sanford::ErrorHandler.new(@exception, host_data).run
|
70
|
+
server_data = Sanford::ServerData.new(:error_procs => [ proc{ true } ])
|
71
|
+
response = Sanford::ErrorHandler.new(@exception, server_data).run
|
79
72
|
|
80
73
|
assert_equal 500, response.code
|
81
74
|
assert_equal 'An unexpected error occurred.', response.status.message
|
@@ -83,10 +76,10 @@ class Sanford::ErrorHandler
|
|
83
76
|
|
84
77
|
should "use the default behavior for an exception raised by the error proc " \
|
85
78
|
"and ignore the original exception" do
|
86
|
-
|
79
|
+
server_data = Sanford::ServerData.new({
|
87
80
|
:error_procs => [ proc{ raise Sanford::NotFoundError } ]
|
88
|
-
})
|
89
|
-
response = Sanford::ErrorHandler.new(@exception,
|
81
|
+
})
|
82
|
+
response = Sanford::ErrorHandler.new(@exception, server_data).run
|
90
83
|
|
91
84
|
assert_equal 404, response.code
|
92
85
|
assert_nil response.status.message
|
@@ -100,7 +93,7 @@ class Sanford::ErrorHandler
|
|
100
93
|
|
101
94
|
should "build a 400 response with a protocol BadMessageError" do
|
102
95
|
exception = generate_exception(Sanford::Protocol::BadMessageError, 'bad message')
|
103
|
-
response = Sanford::ErrorHandler.new(exception, @
|
96
|
+
response = Sanford::ErrorHandler.new(exception, @server_data).run
|
104
97
|
|
105
98
|
assert_equal 400, response.code
|
106
99
|
assert_equal 'bad message', response.status.message
|
@@ -108,7 +101,7 @@ class Sanford::ErrorHandler
|
|
108
101
|
|
109
102
|
should "build a 400 response with a protocol BadRequestError" do
|
110
103
|
exception = generate_exception(Sanford::Protocol::BadRequestError, 'bad request')
|
111
|
-
response = Sanford::ErrorHandler.new(exception, @
|
104
|
+
response = Sanford::ErrorHandler.new(exception, @server_data).run
|
112
105
|
|
113
106
|
assert_equal 400, response.code
|
114
107
|
assert_equal 'bad request', response.status.message
|
@@ -116,14 +109,14 @@ class Sanford::ErrorHandler
|
|
116
109
|
|
117
110
|
should "build a 404 response with a NotFoundError" do
|
118
111
|
exception = generate_exception(Sanford::NotFoundError, 'not found')
|
119
|
-
response = Sanford::ErrorHandler.new(exception, @
|
112
|
+
response = Sanford::ErrorHandler.new(exception, @server_data).run
|
120
113
|
|
121
114
|
assert_equal 404, response.code
|
122
115
|
assert_nil response.status.message
|
123
116
|
end
|
124
117
|
|
125
118
|
should "build a 500 response with all other exceptions" do
|
126
|
-
response = Sanford::ErrorHandler.new(RuntimeError.new('test'), @
|
119
|
+
response = Sanford::ErrorHandler.new(RuntimeError.new('test'), @server_data).run
|
127
120
|
|
128
121
|
assert_equal 500, response.code
|
129
122
|
assert_equal 'An unexpected error occurred.', response.status.message
|
@@ -135,13 +128,14 @@ class Sanford::ErrorHandler
|
|
135
128
|
desc "with multiple error procs"
|
136
129
|
setup do
|
137
130
|
@first_called, @second_called, @third_called = nil, nil, nil
|
138
|
-
@
|
131
|
+
@server_data = Sanford::ServerData.new({
|
139
132
|
:error_procs => [ first_proc, second_proc, third_proc ]
|
140
|
-
})
|
133
|
+
})
|
141
134
|
end
|
142
135
|
|
143
136
|
should "call every error proc" do
|
144
|
-
|
137
|
+
exception = RuntimeError.new('test')
|
138
|
+
@error_handler = Sanford::ErrorHandler.new(exception, @server_data)
|
145
139
|
@error_handler.run
|
146
140
|
|
147
141
|
assert_equal true, @first_called
|
@@ -151,14 +145,15 @@ class Sanford::ErrorHandler
|
|
151
145
|
|
152
146
|
should "should return the response of the last configured error proc " \
|
153
147
|
"that returned a valid response" do
|
154
|
-
|
148
|
+
exception = RuntimeError.new('test')
|
149
|
+
@error_handler = Sanford::ErrorHandler.new(exception, @server_data)
|
155
150
|
response = @error_handler.run
|
156
151
|
|
157
152
|
# use the second proc's generated response
|
158
153
|
assert_equal 987, response.code
|
159
154
|
|
160
155
|
exception = generate_exception(Sanford::NotFoundError, 'not found')
|
161
|
-
@error_handler = Sanford::ErrorHandler.new(exception, @
|
156
|
+
@error_handler = Sanford::ErrorHandler.new(exception, @server_data)
|
162
157
|
response = @error_handler.run
|
163
158
|
|
164
159
|
# use the third proc's generated response
|
@@ -170,14 +165,14 @@ class Sanford::ErrorHandler
|
|
170
165
|
end
|
171
166
|
|
172
167
|
def second_proc
|
173
|
-
proc do |exception,
|
168
|
+
proc do |exception, server_data, request|
|
174
169
|
@second_called = true
|
175
170
|
987
|
176
171
|
end
|
177
172
|
end
|
178
173
|
|
179
174
|
def third_proc
|
180
|
-
proc do |exception,
|
175
|
+
proc do |exception, server_data, request|
|
181
176
|
@third_called = true
|
182
177
|
876 if exception.kind_of?(Sanford::NotFoundError)
|
183
178
|
end
|