File: headers_spec.rb

package info (click to toggle)
ruby-protocol-http 0.23.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 472 kB
  • sloc: ruby: 2,794; makefile: 4
file content (258 lines) | stat: -rw-r--r-- 6,276 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# frozen_string_literal: true

# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require 'protocol/http/headers'
require 'protocol/http/cookie'

RSpec.describe Protocol::HTTP::Headers do
	let(:fields) do
		[
			['Content-Type', 'text/html'],
			['Set-Cookie', 'hello=world'],
			['Accept', '*/*'],
			['Set-Cookie', 'foo=bar'],
			['Connection', 'Keep-Alive']
		]
	end
	
	before(:each) do
		fields&.each do |name, value|
			subject.add(name, value)
		end
	end
	
	describe '#freeze' do
		it "can't modify frozen headers" do
			subject.freeze
			
			expect(subject.fields).to be == fields
			expect(subject.fields).to be_frozen
			expect(subject.to_h).to be_frozen
		end
	end
	
	describe '#dup' do
		it "should not modify source object" do
			headers = subject.dup
			
			headers['field'] = 'value'
			
			expect(subject).to_not include('field')
		end
	end
	
	describe '#empty?' do
		it "shouldn't be empty" do
			expect(subject).to_not be_empty
		end
	end
	
	describe '#include?' do
		it "should include? named fields" do
			expect(subject).to be_include('set-cookie')
		end
	end
	
	describe '#key?' do
		it "should key? named fields" do
			expect(subject).to be_key('set-cookie')
		end
	end
	
	describe '#fields' do
		it 'should add fields in order' do
			expect(subject.fields).to be == fields
		end
		
		it 'can enumerate fields' do
			subject.each.with_index do |field, index|
				expect(field).to be == fields[index]
			end
		end
	end
	
	describe '#to_h' do
		it 'should generate array values for duplicate keys' do
			expect(subject.to_h['set-cookie']).to be == ['hello=world', 'foo=bar']
		end
	end
	
	describe '#[]' do
		it 'can lookup fields' do
			expect(subject['content-type']).to be == 'text/html'
		end
	end
	
	describe '#[]=' do
		it 'can add field' do
			subject['Content-Length'] = 1
			
			expect(subject.fields.last).to be == ['Content-Length', 1]
			expect(subject['content-length']).to be == 1
		end
		
		it 'can add field with indexed hash' do
			expect(subject.to_h).to_not be_empty
			
			subject['Content-Length'] = 1
			expect(subject['content-length']).to be == 1
		end
	end
	
	describe '#add' do
		it 'can add field' do
			subject.add('Content-Length', 1)
			
			expect(subject.fields.last).to be == ['Content-Length', 1]
			expect(subject['content-length']).to be == 1
		end
	end
	
	describe '#set' do
		it 'can replace an existing field' do
			subject.add('accept-encoding', 'gzip,deflate')
			
			subject.set('accept-encoding', 'gzip')
			
			expect(subject['accept-encoding']).to be == ['gzip']
		end
	end
	
	describe '#extract' do
		it "can extract key's that don't exist" do
			expect(subject.extract('foo')).to be_empty
		end
		
		it 'can extract single key' do
			expect(subject.extract('content-type')).to be == [['Content-Type', 'text/html']]
		end
	end
	
	describe '#==' do
		it "can compare with array" do
			expect(subject).to be == fields
		end
		
		it "can compare with itself" do
			expect(subject).to be == subject
		end
		
		it "can compare with hash" do
			expect(subject).to_not be == {}
		end
	end
	
	describe '#delete' do
		it 'can delete case insensitive fields' do
			expect(subject.delete('content-type')).to be == 'text/html'
			
			expect(subject.fields).to be == fields[1..-1]
		end
		
		it 'can delete non-existant fields' do
			expect(subject.delete('transfer-encoding')).to be_nil
		end
	end
	
	describe '#merge' do
		it "can merge content-length" do
			subject.merge!('content-length' => 2)
			
			expect(subject['content-length']).to be == 2
		end
	end
	
	describe '#trailer!' do
		it "can add trailer" do
			subject.add('trailer', 'etag')
			
			trailer = subject.trailer!
			
			subject.add('etag', 'abcd')
			
			expect(trailer.to_h).to be == {'etag' => 'abcd'}
		end
		
		it "can add trailer without explicit header" do
			trailer = subject.trailer!
			
			subject.add('etag', 'abcd')
			
			expect(trailer.to_h).to be == {'etag' => 'abcd'}
		end
	end
	
	describe '#trailer' do
		it "can enumerate trailer" do
			subject.add('trailer', 'etag')
			subject.trailer!
			subject.add('etag', 'abcd')
			
			expect(subject.trailer.to_h).to be == {'etag' => 'abcd'}
		end
	end
	
	describe '#flatten!' do
		it "can flatten trailer" do
			subject.add('trailer', 'etag')
			trailer = subject.trailer!
			subject.add('etag', 'abcd')
			
			subject.flatten!
			
			expect(subject).to_not include('trailer')
			expect(subject).to include('etag')
		end
	end
	
	describe '#flatten' do
		it "can flatten trailer" do
			subject.add('trailer', 'etag')
			trailer = subject.trailer!
			subject.add('etag', 'abcd')
			
			copy = subject.flatten
			
			expect(subject).to include('trailer')
			expect(subject).to include('etag')
			
			expect(copy).to_not include('trailer')
			expect(copy).to include('etag')
		end
	end
	
	describe 'set-cookie' do
		it "can extract parsed cookies" do
			expect(subject['set-cookie']).to be_kind_of(Protocol::HTTP::Header::Cookie)
		end
	end
	
	describe 'connection' do
		it "can extract connection options" do
			expect(subject['connection']).to be_kind_of(Protocol::HTTP::Header::Connection)
		end
		
		it "should normalize to lower case" do
			expect(subject['connection']).to be == ['keep-alive']
		end
	end
end