File: frame.rb

package info (click to toggle)
ruby-protocol-http2 0.24.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 484 kB
  • sloc: ruby: 3,671; makefile: 4
file content (123 lines) | stat: -rw-r--r-- 2,995 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2023-2024, by Samuel Williams.

require "protocol/http2/frame"

describe Protocol::HTTP2::Frame do
	let(:frame) {subject.new(0, 0, 0)}
	
	with "#read_header" do
		it "can't read a frame header with an empty stream" do
			buffer = StringIO.new
			
			expect do
				frame.read_header(buffer)
			end.to raise_exception(EOFError)
		end
	end
	
	with "#read_payload" do
		it "can read a frame with payload and matching length" do
			buffer = StringIO.new("Hello World!")
			
			frame.length = 12
			frame.read_payload(buffer)
			
			expect(frame.payload).to be == "Hello World!"
		end
		
		it "can't read a frame with an empty stream" do
			buffer = StringIO.new
			
			frame.length = 1
			expect do
				frame.read_payload(buffer)
			end.to raise_exception(EOFError)
		end
	end
	
	with "#header" do
		it "can generate a frame header" do
			frame.length = 0
			expect(frame.header).to be == "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
		end
		
		it "can't generate a frame header with an invalid length" do
			frame.length = 2**24
			expect do
				frame.header
			end.to raise_exception(Protocol::HTTP2::ProtocolError, message: be =~ /Invalid frame length/)
		end
		
		it "can't generate a frame header with an invalid stream_id" do
			frame.length = 0
			frame.stream_id = 2**31
			expect do
				frame.header
			end.to raise_exception(Protocol::HTTP2::ProtocolError, message: be =~ /Invalid stream identifier/)
		end
	end
	
	with "#write_payload" do
		it "can write a frame with payload and matching length" do
			buffer = StringIO.new
			
			frame.payload = "Hello World!"
			frame.length = 12
			frame.write_payload(buffer)
			
			expect(buffer.string).to be == "Hello World!"
		end
	end
	
	it "isn't a valid frame type" do
		expect(frame).not.to be(:valid_type?)
	end
	
	it "cannot pack frame with payload larger than maximum frame size" do
		expect do
			frame.pack("Hello World!", maximum_size: 6)
		end.to raise_exception(
			Protocol::HTTP2::ProtocolError,
			message: be =~ /Frame length bigger than maximum allowed/
		)
	end
	
	it "cannot write a frame with missing payload and non-zero length" do
		expect do
			frame.length = 1
			frame.write(StringIO.new)
		end.to raise_exception(
			Protocol::HTTP2::ProtocolError,
			message: be =~ /Invalid frame length/
		)
	end
	
	it "cannot write frame with payload and mismatched length" do
		expect do
			frame.payload = "Hello World!"
			frame.length = 1
			frame.write(StringIO.new)
		end.to raise_exception(
			Protocol::HTTP2::ProtocolError,
			message: be =~ /Invalid payload size/
		)
	end
	
	with "a connection" do
		let(:connection) {Protocol::HTTP2::Connection.new(nil, 0)}
		
		it "can apply a frame to the connection" do
			expect(connection).to receive(:receive_frame)
			frame.apply(connection)
		end
	end
	
	with "#inspect" do
		it "can generate a string representation" do
			expect(frame.inspect).to be =~ /Protocol::HTTP2::Frame stream_id=0/
		end
	end
end