File tree Expand file tree Collapse file tree 3 files changed +24
-25
lines changed
Expand file tree Collapse file tree 3 files changed +24
-25
lines changed Original file line number Diff line number Diff line change 11#!/usr/bin/env ruby
22$:<< '../lib' << 'lib'
3-
43require 'goliath'
54
6- class Error < Goliath ::API
5+ class EarlyAbort < Goliath ::API
76include Goliath ::Validation
87MAX_SIZE = 10
98TEST_FILE = "/tmp/goliath-test-error.log"
10-
9+
1110def on_headers ( env , headers )
1211env . logger . info 'received headers: ' + headers . inspect
1312env [ 'async-headers' ] = headers
14- raise Goliath ::Validation ::NotImplementedError . new ( "Can't handle requests with X-Crash: true." ) if env [ 'HTTP_X_CRASH' ] && env [ 'HTTP_X_CRASH' ] == 'true'
13+
14+ if env [ 'HTTP_X_CRASH' ] && env [ 'HTTP_X_CRASH' ] == 'true'
15+ raise Goliath ::Validation ::NotImplementedError . new ( "Can't handle requests with X-Crash: true." )
16+ end
1517end
1618
1719def on_body ( env , data )
1820env . logger . info 'received data: ' + data
1921( env [ 'async-body' ] ||= '' ) << data
2022size = env [ 'async-body' ] . size
21- raise Goliath ::Validation ::BadRequestError . new ( "Payload size can't exceed #{ MAX_SIZE } bytes. Received #{ size . inspect } bytes." ) if size >= MAX_SIZE
23+
24+ if size >= MAX_SIZE
25+ raise Goliath ::Validation ::BadRequestError . new ( "Payload size can't exceed #{ MAX_SIZE } bytes. Received #{ size . inspect } bytes." )
26+ end
2227end
2328
2429def on_close ( env )
2530env . logger . info 'closing connection'
2631end
2732
28- def response ( env )
33+ def response ( env )
2934File . open ( TEST_FILE , "w+" ) { |f | f << "response that should not be here" }
3035[ 200 , { } , "OK" ]
3136end
32- end
37+ end
Original file line number Diff line number Diff line change @@ -202,16 +202,10 @@ def server_exception(e)
202202headers [ 'Content-Length' ] = body . bytesize . to_s
203203@env [ :terminate_connection ] = true
204204post_process ( [ status , headers , body ] )
205- # Pass the request status to succeeded, so that the callback declared in
206- # #post_process is executed as soon as possible, and not after the whole
207- # response has been generated. For example, if you wanted to abort the
208- # request in a #on_headers or #on_body hook (through exception raising),
209- # the previous behaviour still went through all the hooks and the
210- # #response method before returning the error. Now the error fires as
211- # soon as possible. Note that #on_body and #response hooks may still be
212- # executed if your machine is very fast or the request is very small,
213- # but at least for long requests you don't get stuck until the full
214- # request has been received.
205+
206+ # Mark the request as complete to force a flush on the response.
207+ # Note: #on_body and #response hooks may still fire if the data
208+ # is already in the parser buffer.
215209succeed
216210end
217211
Original file line number Diff line number Diff line change 11require 'spec_helper'
2- require File . join ( File . dirname ( __FILE__ ) , '../../' , 'examples/error ' )
2+ require File . join ( File . dirname ( __FILE__ ) , '../../' , 'examples/early_abort ' )
33
4- describe Error do
4+ describe EarlyAbort do
55let ( :err ) { Proc . new { fail "API request failed" } }
66
77after do
8- File . unlink ( Error ::TEST_FILE ) if File . exist? ( Error ::TEST_FILE )
8+ File . unlink ( EarlyAbort ::TEST_FILE ) if File . exist? ( EarlyAbort ::TEST_FILE )
99end
1010
1111it "should return OK" do
12- with_api ( Error ) do
12+ with_api ( EarlyAbort ) do
1313get_request ( { } , err ) do |c |
1414c . response . should == "OK"
1515end
1616end
1717end
18-
18+
1919# The following two tests are very brittle, since they depend on the speed
2020# of the machine executing the test and the size of the incoming data
2121# packets. I hope someone more knowledgeable will be able to refactor these
2222# ;-)
2323it 'fails without going in the response method if exception is raised in on_header hook' do
24- with_api ( Error ) do
24+ with_api ( EarlyAbort ) do
2525request_data = {
2626:body => ( [ "abcd" ] * 200_000 ) . join ,
2727:head => { 'X-Crash' => 'true' }
3333end
3434end
3535end
36-
36+
3737it 'fails without going in the response method if exception is raised in on_body hook' do
38- with_api ( Error ) do
38+ with_api ( EarlyAbort ) do
3939request_data = {
4040:body => ( [ "abcd" ] * 200_000 ) . join
4141}
You can’t perform that action at this time.
0 commit comments