aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2016-05-08 02:01:11 +0900
committerKazuki Yamaguchi <k@rhe.jp>2016-05-08 15:17:15 +0900
commit30a6b4df628937e4905b5932d4e43e1708332b62 (patch)
treea77adc1c538f78918376ff126182969bcddbe49f
parent4e67368773d4343ea953518f26af92110e4ccd2d (diff)
downloadplum-30a6b4df628937e4905b5932d4e43e1708332b62.tar.gz
client: make Response's internal methods private
And use Object#send to call them. Since they are internal, there should be no problem.
-rw-r--r--lib/plum/client/client_session.rb15
-rw-r--r--lib/plum/client/legacy_client_session.rb9
-rw-r--r--lib/plum/client/response.rb20
-rw-r--r--test/plum/client/test_response.rb50
4 files changed, 46 insertions, 48 deletions
diff --git a/lib/plum/client/client_session.rb b/lib/plum/client/client_session.rb
index cdc3e7b..bfc24a7 100644
--- a/lib/plum/client/client_session.rb
+++ b/lib/plum/client/client_session.rb
@@ -30,7 +30,7 @@ module Plum
def close
@closed = true
- @responses.each(&:_fail)
+ @responses.each { |res| res.send(:fail) }
@responses.clear
@plum.close
end
@@ -49,24 +49,25 @@ module Plum
stream.send_data(body, end_stream: true) if body
stream.on(:headers) { |resp_headers_raw|
- response._headers(resp_headers_raw)
+ response.send(:set_headers, resp_headers_raw.to_h)
headers_cb.call(response) if headers_cb
}
stream.on(:data) { |chunk|
- response._chunk(chunk)
+ response.send(:add_chunk, chunk)
check_window(stream)
}
stream.on(:end_stream) {
- response._finish
+ response.send(:finish)
@responses.delete(response)
}
stream.on(:stream_error) { |ex|
- response._fail
+ response.send(:fail, ex)
raise ex
}
stream.on(:local_stream_error) { |type|
- response.fail
- raise LocalStreamError.new(type)
+ ex = LocalStreamError.new(type)
+ response.send(:fail, ex)
+ raise ex
}
response
end
diff --git a/lib/plum/client/legacy_client_session.rb b/lib/plum/client/legacy_client_session.rb
index bc531ac..166c0ef 100644
--- a/lib/plum/client/legacy_client_session.rb
+++ b/lib/plum/client/legacy_client_session.rb
@@ -26,7 +26,7 @@ module Plum
def close
@closed = true
- @response._fail if @response
+ @response.send(:fail) if @response
end
def request(headers, body, options, &headers_cb)
@@ -95,17 +95,18 @@ module Plum
def setup_parser
parser = HTTP::Parser.new
parser.on_headers_complete = proc {
+ # FIXME: duplicate header name?
resp_headers = parser.headers.map { |key, value| [key.downcase, value] }.to_h
- @response._headers({ ":status" => parser.status_code.to_s }.merge(resp_headers))
+ @response.send(:set_headers, { ":status" => parser.status_code.to_s }.merge(resp_headers))
@headers_callback.call(@response) if @headers_callback
}
parser.on_body = proc { |chunk|
- @response._chunk(chunk)
+ @response.send(:add_chunk, chunk)
}
parser.on_message_complete = proc { |env|
- @response._finish
+ @response.send(:finish)
@response = nil
@headers_callback = nil
close unless parser.keep_alive?
diff --git a/lib/plum/client/response.rb b/lib/plum/client/response.rb
index 2cc01a8..0e83fd7 100644
--- a/lib/plum/client/response.rb
+++ b/lib/plum/client/response.rb
@@ -70,15 +70,14 @@ module Plum
@body.join
end
- # @api private
- def _headers(raw_headers)
- # response headers should not have duplicates
- @headers = raw_headers.to_h.freeze
+ private
+ # internal: set headers and setup decoder
+ def set_headers(headers)
+ @headers = headers.freeze
@decoder = setup_decoder
end
- # @api private
- def _chunk(encoded)
+ def add_chunk(encoded)
chunk = @decoder.decode(encoded)
if @on_chunk
@on_chunk.call(chunk)
@@ -87,19 +86,16 @@ module Plum
end
end
- # @api private
- def _finish
+ def finish
@finished = true
@decoder.finish
@on_finish.call if @on_finish
end
- # @api private
- def _fail
- @failed = true
+ def fail(ex = nil)
+ @failed = ex || true # FIXME
end
- private
def setup_decoder
if @auto_decode
klass = Decoders::DECODERS[@headers["content-encoding"]]
diff --git a/test/plum/client/test_response.rb b/test/plum/client/test_response.rb
index 511a073..d3d4e42 100644
--- a/test/plum/client/test_response.rb
+++ b/test/plum/client/test_response.rb
@@ -4,49 +4,49 @@ using Plum::BinaryString
class ResponseTest < Minitest::Test
def test_finished
resp = Response.new
- resp._headers({})
+ resp.send(:set_headers, {})
assert_equal(false, resp.finished?)
- resp._finish
+ resp.send(:finish)
assert_equal(true, resp.finished?)
end
def test_fail
resp = Response.new
- resp._fail
- assert(true, resp.failed?)
+ resp.send(:fail, true)
+ assert(resp.failed?, "response must be failed")
end
def test_status
resp = Response.new
- resp._headers([
- [":status", "200"]
- ])
+ resp.send(:set_headers,
+ ":status" => "200"
+ )
assert_equal("200", resp.status)
end
def test_headers
resp = Response.new
- resp._headers([
- [":status", "200"],
- ["header", "abc"]
- ])
+ resp.send(:set_headers,
+ ":status" => "200",
+ "header" => "abc"
+ )
assert_equal("abc", resp[:HEADER])
end
def test_body
resp = Response.new
- resp._headers({})
- resp._chunk("a")
- resp._chunk("b")
- resp._finish
+ resp.send(:set_headers, {})
+ resp.send(:add_chunk, "a")
+ resp.send(:add_chunk, "b")
+ resp.send(:finish)
assert_equal("ab", resp.body)
end
def test_body_not_finished
resp = Response.new
- resp._headers({})
- resp._chunk("a")
- resp._chunk("b")
+ resp.send(:set_headers, {})
+ resp.send(:add_chunk, "a")
+ resp.send(:add_chunk, "b")
assert_raises { # TODO
resp.body
}
@@ -54,23 +54,23 @@ class ResponseTest < Minitest::Test
def test_on_chunk
resp = Response.new
- resp._headers({})
+ resp.send(:set_headers, {})
res = []
- resp._chunk("a")
- resp._chunk("b")
- resp._finish
+ resp.send(:add_chunk, "a")
+ resp.send(:add_chunk, "b")
+ resp.send(:finish)
resp.on_chunk { |chunk| res << chunk }
assert_equal(["a", "b"], res)
- resp._chunk("c")
+ resp.send(:add_chunk, "c")
assert_equal(["a", "b", "c"], res)
end
def test_on_finish
resp = Response.new
- resp._headers({})
+ resp.send(:set_headers, {})
ran = false
resp.on_finish { ran = true }
- resp._finish
+ resp.send(:finish)
assert(ran)
ran = false
resp.on_finish { ran = true }