Skip to content

Commit 6aa2b65

Browse files
committed
State machine tests.
1 parent e88da02 commit 6aa2b65

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/protocol/http1/connection.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ def initialize(stream, persistent: true, state: :idle)
109109
# State transition methods use a trailing "!".
110110
attr_accessor :state
111111

112+
def idle?
113+
@state == :idle
114+
end
115+
116+
def open?
117+
@state == :open
118+
end
119+
120+
def half_closed_local?
121+
@state == :half_closed_local
122+
end
123+
124+
def half_closed_remote?
125+
@state == :half_closed_remote
126+
end
127+
128+
def closed?
129+
@state == :closed
130+
end
131+
112132
# The number of requests processed.
113133
attr :count
114134

test/protocol/http1/connection.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,4 +547,25 @@
547547
end.to raise_exception(Protocol::HTTP1::BadHeader)
548548
end
549549
end
550+
551+
with "persistent connection" do
552+
it "returns back to idle state" do
553+
expect(client).to be(:idle?)
554+
555+
client.write_request("localhost", "GET", "/", "HTTP/1.1", {})
556+
expect(client).to be(:open?)
557+
client.write_body("HTTP/1.1", nil)
558+
559+
expect(client).to be(:half_closed_local?)
560+
561+
expect(server).to be(:idle?)
562+
563+
request = server.read_request
564+
expect(request).to be == ["localhost", "GET", "/", "HTTP/1.1", {}, nil]
565+
expect(server).to be(:open?)
566+
567+
server.write_response("HTTP/1.1", 200, {}, [])
568+
expect(server).to be(:idle?)
569+
570+
end
550571
end

0 commit comments

Comments
 (0)