test/helper.rb in ds9-1.0.0 vs test/helper.rb in ds9-1.1.0
- old
+ new
@@ -76,11 +76,11 @@
class Client < DS9::Client
include IOEvents
class Response
- attr_reader :stream_id, :body
+ attr_reader :stream_id, :body, :headers
def initialize stream_id
@stream_id = stream_id
@headers = {}
@body = StringIO.new
@@ -129,10 +129,11 @@
include IOEvents
def initialize read, write, app
@app = app
@read_streams = {}
+ @read_post_streams = {}
@write_streams = {}
super(read, write)
end
def before_frame_send frame
@@ -157,14 +158,17 @@
@app.call request, response
@write_streams[response.stream_id] = response
end
def on_header name, value, frame, flags
+ if name == ":method" && value == "POST"
+ @read_post_streams[frame.stream_id] = []
+ end
@read_streams[frame.stream_id] << [name, value]
end
- class Request < Struct.new :stream, :stream_id, :headers
+ class Request < Struct.new :stream, :stream_id, :headers, :body
def path
headers[':path']
end
end
@@ -181,16 +185,23 @@
body << str
body << nil
end
end
+ def on_data_chunk_recv id, data, flags
+ @read_post_streams[id] << data
+ end
+
def on_frame_recv frame
- return unless frame.headers?
+ return unless (frame.data? || frame.headers?) && frame.end_stream?
req_headers = @read_streams[frame.stream_id]
response = Response.new(self, frame.stream_id, [])
request = Request.new(self, frame.stream_id, Hash[req_headers])
+ if @read_post_streams[frame.stream_id]
+ request.body = @read_post_streams[frame.stream_id].join
+ end
@app.call request, response
@write_streams[frame.stream_id] = response
end