File: rfc7541_spec.rb

package info (click to toggle)
ruby-protocol-hpack 1.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 312 kB
  • sloc: ruby: 1,841; makefile: 4
file content (102 lines) | stat: -rw-r--r-- 2,721 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
# frozen_string_literal: true

require 'protocol/hpack/compressor'
require 'protocol/hpack/decompressor'

require 'yaml'

RSpec.describe "RFC7541" do
	def self.fixtures(mode)
		Dir.glob(File.expand_path("rfc7541/*.yaml", __dir__)) do |path|
			fixture = YAML::load_file(path)
			
			if only = fixture[:only]
				next unless only == mode
			end
			
			yield fixture
		end
	end
	
	context Protocol::HPACK::Decompressor do
		fixtures(:decompressor) do |example|
			context example[:title] do
				example[:streams].size.times do |nth|
					context "request #{nth + 1}" do
						let(:context) {Protocol::HPACK::Context.new(huffman: example[:huffman], table_size: example[:table_size])}
						let(:buffer) {String.new.b}
						
						let(:decompressor) {Protocol::HPACK::Decompressor.new(buffer, context)}
						
						before do
							(0...nth).each do |i|
								buffer << [example[:streams][i][:wire].gsub(/\s/, '')].pack('H*')
								decompressor.decode
							end
						end
						
						let(:bytes) {[example[:streams][nth][:wire].gsub(/\s/, '')].pack('H*')}
						
						subject! do
							buffer << bytes
							decompressor.decode
						end
						
						it 'should emit expected headers' do
							expect(subject).to eq example[:streams][nth][:emitted]
						end
						
						it 'should update header table' do
							expect(context.table).to eq example[:streams][nth][:table]
						end
						
						it 'should compute header table size' do
							expect(context.current_table_size).to eq example[:streams][nth][:table_size]
						end
					end
				end
			end
		end
	end
	
	context Protocol::HPACK::Compressor do
		fixtures(:compressor) do |example|
			context example[:title] do
				example[:streams].size.times do |nth|
					context "request #{nth + 1}" do
						let(:context) {Protocol::HPACK::Context.new(huffman: example[:huffman], table_size: example[:table_size])}
						let(:buffer) {String.new.b}
						
						let(:compressor) {Protocol::HPACK::Compressor.new(buffer, context)}
						
						before do
							(0...nth).each do |i|
								compressor.encode(example[:streams][i][:emitted])
							end
						end
						
						subject! do
							buffer.clear
							
							compressor.encode(example[:streams][nth][:emitted])
							
							buffer
						end
						
						it 'should emit expected bytes' do
							expect(subject.unpack1('H*')).to eq example[:streams][nth][:wire].gsub(/\s/, '')
						end
						
						it 'should update header table' do
							expect(context.table).to eq example[:streams][nth][:table]
						end
						
						it 'should compute header table size' do
							expect(context.current_table_size).to eq example[:streams][nth][:table_size]
						end
					end
				end
			end
		end
	end
end