Skip to content

Commit 0fd9e89

Browse files
committed
Add tests for error handlers
1 parent 945815d commit 0fd9e89

9 files changed

Lines changed: 62 additions & 8 deletions

File tree

TODO.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
## Immediate
22

3-
- [ ] Testing
4-
- [v] A module that exports a proc that takes 2 args, or maybe kwargs, how
5-
does the system deal with that?
6-
- [ ] middleware hook composition
7-
- [ ] Scenario with multiple hooks on different levels
8-
- [ ] error handler
9-
- [ ] Scenario with multiple error handlers on different levels
103
- [ ] Controllers
114
- [ ] Streamline names of ready-made control methods:
125
- [ ] dispatch_by_host('.', mapping:) - currently `route_by_hosst`

test/fixtures/app_errors/_error.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export ->(req, error) {
2+
req.respond("root: #{error.message}", ':status' => Error.http_status(error))
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export ->(req, error) {
2+
req.respond("foo: #{error.message}", ':status' => Error.http_status(error))
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export ->(req, error) {
2+
req.respond("bar: #{error.message}", ':status' => Error.http_status(error))
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export ->(req) {
2+
raise Error.teapot('baz')
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export ->(req) {
2+
raise Error.teapot('bar')
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export ->(req) {
2+
raise Error.teapot('foo')
3+
}

test/fixtures/app_errors/index.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export ->(req) {
2+
raise Error.teapot('root')
3+
}

test/test_app.rb

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def test_middleware_invocation_on_404
188188
end
189189
end
190190

191-
class MiddlewareTest < Minitest::Test
191+
class MiddlewareHooksTest < Minitest::Test
192192
HTTP = Syntropy::HTTP
193193

194194
APP_ROOT = File.join(__dir__, 'fixtures/app_hooks')
@@ -228,6 +228,46 @@ def test_middleware_composition
228228
end
229229
end
230230

231+
class ErrorHandlerTest < Minitest::Test
232+
HTTP = Syntropy::HTTP
233+
234+
APP_ROOT = File.join(__dir__, 'fixtures/app_errors')
235+
236+
def setup
237+
@machine = UM.new
238+
239+
@tmp_path = '/test/tmp'
240+
@tmp_fn = File.join(APP_ROOT, 'tmp.rb')
241+
242+
@app = Syntropy::App.new(
243+
app_root: APP_ROOT,
244+
mount_path: '/',
245+
watch_files: 0.05,
246+
machine: @machine
247+
)
248+
249+
@test_harness = Syntropy::TestHarness.new(@app)
250+
end
251+
252+
def test_error_handlers
253+
req = @test_harness.request(':method' => 'GET', ':path' => '/')
254+
assert_equal HTTP::TEAPOT, req.response_status
255+
assert_equal 'root: root', req.response_body
256+
257+
req = @test_harness.request(':method' => 'GET', ':path' => '/foo')
258+
assert_equal HTTP::TEAPOT, req.response_status
259+
assert_equal 'foo: foo', req.response_body
260+
261+
req = @test_harness.request(':method' => 'GET', ':path' => '/foo/bar')
262+
assert_equal HTTP::TEAPOT, req.response_status
263+
assert_equal 'bar: bar', req.response_body
264+
265+
req = @test_harness.request(':method' => 'GET', ':path' => '/foo/bar/baz')
266+
assert_equal HTTP::TEAPOT, req.response_status
267+
assert_equal 'bar: baz', req.response_body
268+
end
269+
end
270+
231271
class CustomAppTest < Minitest::Test
232272
HTTP = Syntropy::HTTP
233273

0 commit comments

Comments
 (0)