Skip to content
This repository has been archived by the owner on Aug 31, 2022. It is now read-only.

Commit

Permalink
tests: add test cases for Client
Browse files Browse the repository at this point in the history
  • Loading branch information
rhenium committed Nov 9, 2015
1 parent e44ecc1 commit 223addb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/plum/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
module Plum
class Client
DEFAULT_CONFIG = {
http2: true,
scheme: "https",
hostname: nil,
verify_mode: OpenSSL::SSL::VERIFY_PEER,
ssl_context: nil,
hostname: nil,
http2_settings: {},
user_agent: "plum/#{Plum::VERSION}",
http2: true,
}.freeze

attr_reader :host, :port, :config
attr_reader :socket
attr_reader :socket, :session

# Creates a new HTTP client and starts communication.
# A shorthand for `Plum::Client.new(args).start(&block)`
Expand Down
18 changes: 18 additions & 0 deletions test/plum/client/test_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ def test_raise_error_async_block
server_thread.join if server_thread
end

def test_session_socket_http2_https
sock = StringSocket.new
client = Client.start(sock, nil, http2: true, scheme: "https")
assert(client.session.class == ClientSession)
end

def test_session_socket_http2_http
sock = StringSocket.new("HTTP/1.1 100\r\n\r\n")
client = Client.start(sock, nil, http2: true, scheme: "http")
assert(client.session.class == UpgradeClientSession)
end

def test_session_socket_http1
sock = StringSocket.new
client = Client.start(sock, nil, http2: false)
assert(client.session.class == LegacyClientSession)
end

private
def start_tls_server(&block)
ctx = OpenSSL::SSL::SSLContext.new
Expand Down
9 changes: 6 additions & 3 deletions test/utils/string_socket.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
class StringSocket
class StringSocket < IO
# remove all methods
(IO.instance_methods - Object.instance_methods).each { |symbol| undef_method symbol }

extend Forwardable
def_delegators :@rio, :readpartial
def_delegators :@wio, :<<, :write

attr_reader :rio, :wio

def initialize(str)
@rio = StringIO.new(str)
def initialize(str = nil)
@rio = StringIO.new(str.to_s)
@wio = StringIO.new
end
end

0 comments on commit 223addb

Please sign in to comment.