http-2 0.10.1 → 0.10.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,119 +8,135 @@ require 'coveralls'
8
8
 
9
9
  Coveralls.wear! if ENV['CI']
10
10
 
11
- require 'http/2'
12
-
13
11
  # rubocop: disable Style/MixinUsage
12
+ require 'http/2'
14
13
  include HTTP2
15
14
  include HTTP2::Header
16
15
  include HTTP2::Error
17
16
  # rubocop: enable Style/MixinUsage
18
17
 
19
- DATA = {
20
- type: :data,
21
- flags: [:end_stream].freeze,
22
- stream: 1,
23
- payload: 'text'.freeze,
24
- }.freeze
25
-
26
- HEADERS = {
27
- type: :headers,
28
- flags: [:end_headers].freeze,
29
- stream: 1,
30
- payload: Compressor.new.encode([%w(a b)]).freeze,
31
- }.freeze
32
-
33
- HEADERS_END_STREAM = {
34
- type: :headers,
35
- flags: [:end_headers, :end_stream].freeze,
36
- stream: 1,
37
- payload: Compressor.new.encode([%w(a b)]).freeze,
38
- }.freeze
39
-
40
- PRIORITY = {
41
- type: :priority,
42
- stream: 1,
43
- exclusive: false,
44
- stream_dependency: 0,
45
- weight: 20,
46
- }.freeze
47
-
48
- RST_STREAM = {
49
- type: :rst_stream,
50
- stream: 1,
51
- error: :stream_closed,
52
- }.freeze
53
-
54
- SETTINGS = {
55
- type: :settings,
56
- stream: 0,
57
- payload: [
58
- [:settings_max_concurrent_streams, 10].freeze,
59
- [:settings_initial_window_size, 0x7fffffff].freeze,
60
- ].freeze,
61
- }.freeze
62
-
63
- PUSH_PROMISE = {
64
- type: :push_promise,
65
- flags: [:end_headers].freeze,
66
- stream: 1,
67
- promise_stream: 2,
68
- payload: Compressor.new.encode([%w(a b)]).freeze,
69
- }.freeze
70
-
71
- PING = {
72
- stream: 0,
73
- type: :ping,
74
- payload: '12345678'.freeze,
75
- }.freeze
76
-
77
- PONG = {
78
- stream: 0,
79
- type: :ping,
80
- flags: [:ack].freeze,
81
- payload: '12345678'.freeze,
82
- }.freeze
83
-
84
- GOAWAY = {
85
- type: :goaway,
86
- last_stream: 2,
87
- error: :no_error,
88
- payload: 'debug'.freeze,
89
- }.freeze
90
-
91
- WINDOW_UPDATE = {
92
- type: :window_update,
93
- increment: 10,
94
- }.freeze
95
-
96
- CONTINUATION = {
97
- type: :continuation,
98
- flags: [:end_headers].freeze,
99
- payload: '-second-block'.freeze,
100
- }.freeze
101
-
102
- ALTSVC = {
103
- type: :altsvc,
104
- max_age: 1_402_290_402, # 4
105
- port: 8080, # 2 reserved 1
106
- proto: 'h2-12'.freeze, # 1 + 5
107
- host: 'www.example.com'.freeze, # 1 + 15
108
- origin: 'www.example.com'.freeze, # 15
109
- }.freeze
110
-
111
- FRAME_TYPES = [
112
- DATA,
113
- HEADERS,
114
- PRIORITY,
115
- RST_STREAM,
116
- SETTINGS,
117
- PUSH_PROMISE,
118
- PING,
119
- GOAWAY,
120
- WINDOW_UPDATE,
121
- CONTINUATION,
122
- ALTSVC,
123
- ].freeze
18
+ REQUEST_HEADERS = [%w(:scheme https),
19
+ %w(:path /),
20
+ %w(:authority example.com),
21
+ %w(:method GET),
22
+ %w(a b)].freeze
23
+ RESPONSE_HEADERS = [%w(:status 200)].freeze
24
+
25
+ module FrameHelpers
26
+ def data_frame
27
+ {
28
+ type: :data,
29
+ flags: [:end_stream],
30
+ stream: 1,
31
+ payload: 'text',
32
+ }
33
+ end
34
+
35
+ def headers_frame
36
+ {
37
+ type: :headers,
38
+ flags: [:end_headers].freeze,
39
+ stream: 1,
40
+ payload: Compressor.new.encode(REQUEST_HEADERS),
41
+ }
42
+ end
43
+
44
+ def priority_frame
45
+ {
46
+ type: :priority,
47
+ stream: 1,
48
+ exclusive: false,
49
+ stream_dependency: 0,
50
+ weight: 20,
51
+ }
52
+ end
53
+
54
+ def rst_stream_frame
55
+ {
56
+ type: :rst_stream,
57
+ stream: 1,
58
+ error: :stream_closed,
59
+ }
60
+ end
61
+
62
+ def settings_frame
63
+ {
64
+ type: :settings,
65
+ stream: 0,
66
+ payload: [
67
+ [:settings_max_concurrent_streams, 10],
68
+ [:settings_initial_window_size, 0x7fffffff],
69
+ ],
70
+ }
71
+ end
72
+
73
+ def push_promise_frame
74
+ {
75
+ type: :push_promise,
76
+ flags: [:end_headers],
77
+ stream: 1,
78
+ promise_stream: 2,
79
+ payload: Compressor.new.encode([%w(a b)]),
80
+ }
81
+ end
82
+
83
+ def ping_frame
84
+ {
85
+ stream: 0,
86
+ type: :ping,
87
+ payload: '12345678',
88
+ }
89
+ end
90
+
91
+ def pong_frame
92
+ {
93
+ stream: 0,
94
+ type: :ping,
95
+ flags: [:ack],
96
+ payload: '12345678',
97
+ }
98
+ end
99
+
100
+ def goaway_frame
101
+ {
102
+ type: :goaway,
103
+ last_stream: 2,
104
+ error: :no_error,
105
+ payload: 'debug',
106
+ }
107
+ end
108
+
109
+ def window_update_frame
110
+ {
111
+ type: :window_update,
112
+ increment: 10,
113
+ }
114
+ end
115
+
116
+ def continuation_frame
117
+ {
118
+ type: :continuation,
119
+ flags: [:end_headers],
120
+ payload: '-second-block',
121
+ }
122
+ end
123
+
124
+ def altsvc_frame
125
+ {
126
+ type: :altsvc,
127
+ max_age: 1_402_290_402, # 4
128
+ port: 8080, # 2 reserved 1
129
+ proto: 'h2-12', # 1 + 5
130
+ host: 'www.example.com', # 1 + 15
131
+ origin: 'www.example.com' # 15
132
+ }
133
+ end
134
+
135
+ def frame_types
136
+ methods.select { |meth| meth.to_s.end_with?('_frame') }
137
+ .map { |meth| __send__(meth) }
138
+ end
139
+ end
124
140
 
125
141
  def set_stream_id(bytes, id)
126
142
  scheme = 'CnCCN'.freeze
@@ -1,6 +1,7 @@
1
1
  require 'helper'
2
2
 
3
3
  RSpec.describe HTTP2::Server do
4
+ include FrameHelpers
4
5
  before(:each) do
5
6
  @srv = Server.new
6
7
  end
@@ -46,6 +47,6 @@ RSpec.describe HTTP2::Server do
46
47
  end
47
48
 
48
49
  client.new_stream
49
- client.send HEADERS.deep_dup
50
+ client.send headers_frame
50
51
  end
51
52
  end
@@ -1,6 +1,7 @@
1
1
  require 'helper'
2
2
 
3
3
  RSpec.describe HTTP2::Stream do
4
+ include FrameHelpers
4
5
  before(:each) do
5
6
  @client = Client.new
6
7
  @stream = @client.new_stream
@@ -18,57 +19,57 @@ RSpec.describe HTTP2::Stream do
18
19
 
19
20
  context 'idle' do
20
21
  it 'should transition to open on sent HEADERS' do
21
- @stream.send HEADERS.deep_dup
22
+ @stream.send headers_frame
22
23
  expect(@stream.state).to eq :open
23
24
  end
24
25
  it 'should transition to open on received HEADERS' do
25
- @stream.receive HEADERS
26
+ @stream.receive headers_frame
26
27
  expect(@stream.state).to eq :open
27
28
  end
28
29
  it 'should transition to reserved (local) on sent PUSH_PROMISE' do
29
- @stream.send PUSH_PROMISE.deep_dup
30
+ @stream.send push_promise_frame
30
31
  expect(@stream.state).to eq :reserved_local
31
32
  end
32
33
  it 'should transition to reserved (remote) on received PUSH_PROMISE' do
33
- @stream.receive PUSH_PROMISE
34
+ @stream.receive push_promise_frame
34
35
  expect(@stream.state).to eq :reserved_remote
35
36
  end
36
37
  it 'should reprioritize stream on sent PRIORITY' do
37
- expect { @stream.send PRIORITY.dup }.to_not raise_error
38
+ expect { @stream.send priority_frame }.to_not raise_error
38
39
  expect(@stream.weight).to eq 20
39
40
  end
40
41
  it 'should reprioritize stream on received PRIORITY' do
41
- expect { @stream.send PRIORITY.dup }.to_not raise_error
42
+ expect { @stream.send priority_frame }.to_not raise_error
42
43
  expect(@stream.weight).to eq 20
43
44
  end
44
45
  end
45
46
 
46
47
  context 'reserved (local)' do
47
- before(:each) { @stream.send PUSH_PROMISE.deep_dup }
48
+ before(:each) { @stream.send push_promise_frame }
48
49
 
49
50
  it 'should transition on sent PUSH_PROMISE' do
50
51
  expect(@stream.state).to eq :reserved_local
51
52
  end
52
53
 
53
54
  it 'should allow HEADERS to be sent' do
54
- expect { @stream.send HEADERS.deep_dup }.to_not raise_error
55
+ expect { @stream.send headers_frame }.to_not raise_error
55
56
  end
56
57
 
57
58
  it 'should raise error if sending invalid frames' do
58
- (FRAME_TYPES - [HEADERS, RST_STREAM]).each do |type|
59
+ frame_types.reject { |frame| %i[headers rst_stream].include?(frame[:type]) }.each do |type|
59
60
  expect { @stream.dup.send type }.to raise_error InternalError
60
61
  end
61
62
  end
62
63
 
63
64
  it 'should raise error on receipt of invalid frames' do
64
- what_types = (FRAME_TYPES - [PRIORITY, RST_STREAM, WINDOW_UPDATE])
65
+ what_types = frame_types.reject { |frame| %i[priority window_update rst_stream].include?(frame[:type]) }
65
66
  what_types.each do |type|
66
67
  expect { @stream.dup.receive type }.to raise_error InternalError
67
68
  end
68
69
  end
69
70
 
70
71
  it 'should transition to half closed (remote) on sent HEADERS' do
71
- @stream.send HEADERS.deep_dup
72
+ @stream.send headers_frame
72
73
  expect(@stream.state).to eq :half_closed_remote
73
74
  end
74
75
 
@@ -78,42 +79,42 @@ RSpec.describe HTTP2::Stream do
78
79
  end
79
80
 
80
81
  it 'should transition to closed on received RST_STREAM' do
81
- @stream.receive RST_STREAM
82
+ @stream.receive rst_stream_frame
82
83
  expect(@stream.state).to eq :closed
83
84
  end
84
85
 
85
86
  it 'should reprioritize stream on PRIORITY' do
86
- expect { @stream.receive PRIORITY }.to_not raise_error
87
+ expect { @stream.receive priority_frame }.to_not raise_error
87
88
  expect(@stream.weight).to eq 20
88
89
  end
89
90
 
90
91
  it 'should increment remote_window on received WINDOW_UPDATE' do
91
- expect { @stream.receive WINDOW_UPDATE }.to_not raise_error
92
- expect(@stream.remote_window).to eq DEFAULT_FLOW_WINDOW + WINDOW_UPDATE[:increment]
92
+ expect { @stream.receive window_update_frame }.to_not raise_error
93
+ expect(@stream.remote_window).to eq DEFAULT_FLOW_WINDOW + window_update_frame[:increment]
93
94
  end
94
95
  end
95
96
 
96
97
  context 'reserved (remote)' do
97
- before(:each) { @stream.receive PUSH_PROMISE }
98
+ before(:each) { @stream.receive push_promise_frame }
98
99
 
99
100
  it 'should transition on received PUSH_PROMISE' do
100
101
  expect(@stream.state).to eq :reserved_remote
101
102
  end
102
103
 
103
104
  it 'should raise error if sending invalid frames' do
104
- (FRAME_TYPES - [PRIORITY, RST_STREAM, WINDOW_UPDATE]).each do |type|
105
+ frame_types.reject { |frame| %i[priority rst_stream window_update].include?(frame[:type]) }.each do |type|
105
106
  expect { @stream.dup.send type }.to raise_error InternalError
106
107
  end
107
108
  end
108
109
 
109
110
  it 'should raise error on receipt of invalid frames' do
110
- (FRAME_TYPES - [HEADERS, RST_STREAM]).each do |type|
111
+ frame_types.reject { |frame| %i[headers rst_stream].include?(frame[:type]) }.each do |type|
111
112
  expect { @stream.dup.receive type }.to raise_error InternalError
112
113
  end
113
114
  end
114
115
 
115
116
  it 'should transition to half closed (local) on received HEADERS' do
116
- @stream.receive HEADERS
117
+ @stream.receive headers_frame
117
118
  expect(@stream.state).to eq :half_closed_local
118
119
  end
119
120
 
@@ -123,38 +124,38 @@ RSpec.describe HTTP2::Stream do
123
124
  end
124
125
 
125
126
  it 'should transition to closed on received RST_STREAM' do
126
- @stream.receive RST_STREAM
127
+ @stream.receive rst_stream_frame
127
128
  expect(@stream.state).to eq :closed
128
129
  end
129
130
 
130
131
  it 'should reprioritize stream on PRIORITY' do
131
- expect { @stream.send PRIORITY.dup }.to_not raise_error
132
+ expect { @stream.send priority_frame }.to_not raise_error
132
133
  expect(@stream.weight).to eq 20
133
134
  end
134
135
 
135
136
  it 'should increment local_window on sent WINDOW_UPDATE' do
136
- expect { @stream.send WINDOW_UPDATE.dup }.to_not raise_error
137
- expect(@stream.local_window).to eq DEFAULT_FLOW_WINDOW + WINDOW_UPDATE[:increment]
137
+ expect { @stream.send window_update_frame }.to_not raise_error
138
+ expect(@stream.local_window).to eq DEFAULT_FLOW_WINDOW + window_update_frame[:increment]
138
139
  end
139
140
  end
140
141
 
141
142
  context 'open' do
142
- before(:each) { @stream.receive HEADERS }
143
+ before(:each) { @stream.receive headers_frame }
143
144
 
144
145
  it 'should allow any valid frames types to be sent' do
145
- (FRAME_TYPES - [PING, GOAWAY, SETTINGS]).each do |type|
146
+ (frame_types - [ping_frame, goaway_frame, settings_frame]).each do |type|
146
147
  expect { @stream.dup.send type.deep_dup }.to_not raise_error
147
148
  end
148
149
  end
149
150
 
150
151
  it 'should allow frames of any type to be received' do
151
- FRAME_TYPES.each do |type|
152
+ frame_types.each do |type|
152
153
  expect { @stream.dup.receive type }.to_not raise_error
153
154
  end
154
155
  end
155
156
 
156
157
  it 'should transition to half closed (local) if sending END_STREAM' do
157
- [DATA, HEADERS].each do |frame|
158
+ [data_frame, headers_frame].each do |frame|
158
159
  s, f = @stream.dup, frame.deep_dup
159
160
  f[:flags] = [:end_stream]
160
161
 
@@ -164,7 +165,7 @@ RSpec.describe HTTP2::Stream do
164
165
  end
165
166
 
166
167
  it 'should transition to half closed (remote) if receiving END_STREAM' do
167
- [DATA, HEADERS].each do |frame|
168
+ [data_frame, headers_frame].each do |frame|
168
169
  s, f = @stream.dup, frame.dup
169
170
  f[:flags] = [:end_stream]
170
171
 
@@ -175,7 +176,7 @@ RSpec.describe HTTP2::Stream do
175
176
 
176
177
  it 'should transition to half closed if remote opened with END_STREAM' do
177
178
  s = @client.new_stream
178
- hclose = HEADERS.dup
179
+ hclose = headers_frame
179
180
  hclose[:flags] = [:end_stream]
180
181
 
181
182
  s.receive hclose
@@ -184,7 +185,7 @@ RSpec.describe HTTP2::Stream do
184
185
 
185
186
  it 'should transition to half closed if local opened with END_STREAM' do
186
187
  s = @client.new_stream
187
- hclose = HEADERS.deep_dup
188
+ hclose = headers_frame
188
189
  hclose[:flags] = [:end_stream]
189
190
 
190
191
  s.send hclose
@@ -198,7 +199,7 @@ RSpec.describe HTTP2::Stream do
198
199
  end
199
200
 
200
201
  it 'should transition to closed if receiving RST_STREAM' do
201
- @stream.receive RST_STREAM
202
+ @stream.receive rst_stream_frame
202
203
  expect(@stream.state).to eq :closed
203
204
  end
204
205
 
@@ -209,8 +210,8 @@ RSpec.describe HTTP2::Stream do
209
210
  sp.on(:active) { openp = true }
210
211
  sr.on(:active) { openr = true }
211
212
 
212
- sp.receive HEADERS
213
- sr.send HEADERS.deep_dup
213
+ sp.receive headers_frame
214
+ sr.send headers_frame
214
215
 
215
216
  expect(openp).to be_truthy
216
217
  expect(openr).to be_truthy
@@ -223,11 +224,11 @@ RSpec.describe HTTP2::Stream do
223
224
  stream.on(:half_close) { order << :half_close }
224
225
  stream.on(:close) { order << :close }
225
226
 
226
- req = HEADERS.deep_dup
227
+ req = headers_frame
227
228
  req[:flags] = [:end_headers]
228
229
 
229
230
  stream.send req
230
- stream.send DATA.dup
231
+ stream.send data_frame
231
232
  expect(order).to eq [:active, :half_close]
232
233
  end
233
234
 
@@ -238,7 +239,7 @@ RSpec.describe HTTP2::Stream do
238
239
  sp.on(:close) { closep = true }
239
240
  sr.on(:close) { closer = true }
240
241
 
241
- sp.receive RST_STREAM
242
+ sp.receive rst_stream_frame
242
243
  sr.close
243
244
 
244
245
  expect(closep).to be_truthy
@@ -253,12 +254,12 @@ RSpec.describe HTTP2::Stream do
253
254
  stream.on(:half_close) { order << :half_close }
254
255
  stream.on(:close) { order << :close }
255
256
 
256
- req = HEADERS.deep_dup
257
+ req = headers_frame
257
258
  req[:flags] = [:end_stream, :end_headers]
258
259
 
259
260
  stream.send req
260
- stream.receive HEADERS
261
- stream.receive DATA
261
+ stream.receive headers_frame
262
+ stream.receive data_frame
262
263
 
263
264
  expect(order).to eq [:active, :half_close, :data, :close]
264
265
  end
@@ -266,31 +267,31 @@ RSpec.describe HTTP2::Stream do
266
267
  it 'should emit :close with reason' do
267
268
  reason = nil
268
269
  @stream.on(:close) { |r| reason = r }
269
- @stream.receive RST_STREAM
270
+ @stream.receive rst_stream_frame
270
271
  expect(reason).not_to be_nil
271
272
  end
272
273
 
273
274
  it 'should reprioritize stream on sent PRIORITY' do
274
- expect { @stream.send PRIORITY.dup }.to_not raise_error
275
+ expect { @stream.send priority_frame }.to_not raise_error
275
276
  expect(@stream.weight).to eq 20
276
277
  end
277
278
  it 'should reprioritize stream on received PRIORITY' do
278
- expect { @stream.receive PRIORITY }.to_not raise_error
279
+ expect { @stream.receive priority_frame }.to_not raise_error
279
280
  expect(@stream.weight).to eq 20
280
281
  end
281
282
  end
282
283
 
283
284
  context 'half closed (local)' do
284
- before(:each) { @stream.send HEADERS_END_STREAM.deep_dup }
285
+ before(:each) { @stream.send headers_frame.merge(flags: [:end_headers, :end_stream]) }
285
286
 
286
287
  it 'should raise error on attempt to send invalid frames' do
287
- (FRAME_TYPES - [PRIORITY, RST_STREAM, WINDOW_UPDATE]).each do |frame|
288
+ frame_types.reject { |frame| %i[priority rst_stream window_update].include?(frame[:type]) }.each do |frame|
288
289
  expect { @stream.dup.send frame }.to raise_error InternalError
289
290
  end
290
291
  end
291
292
 
292
293
  it 'should transition to closed on receipt of END_STREAM flag' do
293
- [DATA, HEADERS, CONTINUATION].each do |frame|
294
+ [data_frame, headers_frame, continuation_frame].each do |frame|
294
295
  s, f = @stream.dup, frame.dup
295
296
  f[:flags] = [:end_stream]
296
297
 
@@ -300,38 +301,38 @@ RSpec.describe HTTP2::Stream do
300
301
  end
301
302
 
302
303
  it 'should transition to closed on receipt of RST_STREAM frame' do
303
- @stream.receive RST_STREAM
304
+ @stream.receive rst_stream_frame
304
305
  expect(@stream.state).to eq :closed
305
306
  end
306
307
 
307
308
  it 'should transition to closed if RST_STREAM frame is sent' do
308
- @stream.send RST_STREAM.deep_dup
309
+ @stream.send rst_stream_frame
309
310
  expect(@stream.state).to eq :closed
310
311
  end
311
312
 
312
313
  it 'should ignore received WINDOW_UPDATE frames' do
313
- expect { @stream.receive WINDOW_UPDATE }.to_not raise_error
314
+ expect { @stream.receive window_update_frame }.to_not raise_error
314
315
  expect(@stream.state).to eq :half_closed_local
315
316
  end
316
317
 
317
318
  it 'should ignore received PRIORITY frames' do
318
- expect { @stream.receive PRIORITY }.to_not raise_error
319
+ expect { @stream.receive priority_frame }.to_not raise_error
319
320
  expect(@stream.state).to eq :half_closed_local
320
321
  end
321
322
 
322
323
  it 'should reprioritize stream on sent PRIORITY' do
323
- expect { @stream.send PRIORITY.dup }.to_not raise_error
324
+ expect { @stream.send priority_frame }.to_not raise_error
324
325
  expect(@stream.weight).to eq 20
325
326
  end
326
327
 
327
328
  it 'should reprioritize stream (and decendants) on received PRIORITY' do
328
- expect { @stream.receive PRIORITY }.to_not raise_error
329
+ expect { @stream.receive priority_frame }.to_not raise_error
329
330
  expect(@stream.weight).to eq 20
330
331
  end
331
332
 
332
333
  it 'should increment local_window on sent WINDOW_UPDATE' do
333
- expect { @stream.send WINDOW_UPDATE.dup }.to_not raise_error
334
- expect(@stream.local_window).to eq DEFAULT_FLOW_WINDOW + WINDOW_UPDATE[:increment]
334
+ expect { @stream.send window_update_frame }.to_not raise_error
335
+ expect(@stream.local_window).to eq DEFAULT_FLOW_WINDOW + window_update_frame[:increment]
335
336
  end
336
337
 
337
338
  it 'should emit :half_close event on transition' do
@@ -340,7 +341,7 @@ RSpec.describe HTTP2::Stream do
340
341
  stream.on(:active) { order << :active }
341
342
  stream.on(:half_close) { order << :half_close }
342
343
 
343
- req = HEADERS.deep_dup
344
+ req = headers_frame
344
345
  req[:flags] = [:end_stream, :end_headers]
345
346
 
346
347
  stream.send req
@@ -350,7 +351,7 @@ RSpec.describe HTTP2::Stream do
350
351
  it 'should emit :close event on transition to closed' do
351
352
  closed = false
352
353
  @stream.on(:close) { closed = true }
353
- @stream.receive RST_STREAM
354
+ @stream.receive rst_stream_frame
354
355
 
355
356
  expect(@stream.state).to eq :closed
356
357
  expect(closed).to be_truthy
@@ -358,10 +359,10 @@ RSpec.describe HTTP2::Stream do
358
359
  end
359
360
 
360
361
  context 'half closed (remote)' do
361
- before(:each) { @stream.receive HEADERS_END_STREAM }
362
+ before(:each) { @stream.receive headers_frame.merge(flags: [:end_headers, :end_stream]) }
362
363
 
363
364
  it 'should raise STREAM_CLOSED error on reciept of frames' do
364
- (FRAME_TYPES - [PRIORITY, RST_STREAM, WINDOW_UPDATE]).each do |frame|
365
+ (frame_types - [priority_frame, rst_stream_frame, window_update_frame]).each do |frame|
365
366
  expect do
366
367
  @stream.dup.receive frame
367
368
  end.to raise_error(StreamClosed)
@@ -369,7 +370,7 @@ RSpec.describe HTTP2::Stream do
369
370
  end
370
371
 
371
372
  it 'should transition to closed if END_STREAM flag is sent' do
372
- [DATA, HEADERS].each do |frame|
373
+ [data_frame, headers_frame].each do |frame|
373
374
  s, f = @stream.dup, frame.deep_dup
374
375
  f[:flags] = [:end_stream]
375
376
 
@@ -409,26 +410,26 @@ RSpec.describe HTTP2::Stream do
409
410
  end
410
411
 
411
412
  it 'should transition to closed on reciept of RST_STREAM frame' do
412
- @stream.receive RST_STREAM
413
+ @stream.receive rst_stream_frame
413
414
  expect(@stream.state).to eq :closed
414
415
  end
415
416
 
416
417
  it 'should ignore sent WINDOW_UPDATE frames' do
417
- expect { @stream.send WINDOW_UPDATE.dup }.to_not raise_error
418
+ expect { @stream.send window_update_frame }.to_not raise_error
418
419
  expect(@stream.state).to eq :half_closed_remote
419
420
  end
420
421
 
421
422
  it 'should increment remote_window on received WINDOW_UPDATE' do
422
- expect { @stream.receive WINDOW_UPDATE }.to_not raise_error
423
- expect(@stream.remote_window).to eq DEFAULT_FLOW_WINDOW + WINDOW_UPDATE[:increment]
423
+ expect { @stream.receive window_update_frame }.to_not raise_error
424
+ expect(@stream.remote_window).to eq DEFAULT_FLOW_WINDOW + window_update_frame[:increment]
424
425
  end
425
426
 
426
427
  it 'should reprioritize stream on sent PRIORITY' do
427
- expect { @stream.send PRIORITY.dup }.to_not raise_error
428
+ expect { @stream.send priority_frame }.to_not raise_error
428
429
  expect(@stream.weight).to eq 20
429
430
  end
430
431
  it 'should reprioritize stream on received PRIORITY' do
431
- expect { @stream.receive PRIORITY }.to_not raise_error
432
+ expect { @stream.receive priority_frame }.to_not raise_error
432
433
  expect(@stream.weight).to eq 20
433
434
  end
434
435
 
@@ -438,7 +439,7 @@ RSpec.describe HTTP2::Stream do
438
439
  stream.on(:active) { order << :active }
439
440
  stream.on(:half_close) { order << :half_close }
440
441
 
441
- req = HEADERS.dup
442
+ req = headers_frame
442
443
  req[:flags] = [:end_stream, :end_headers]
443
444
 
444
445
  stream.receive req
@@ -458,12 +459,12 @@ RSpec.describe HTTP2::Stream do
458
459
  context 'closed' do
459
460
  context 'remote closed stream' do
460
461
  before(:each) do
461
- @stream.send HEADERS_END_STREAM.deep_dup # half closed local
462
- @stream.receive HEADERS_END_STREAM # closed by remote
462
+ @stream.send headers_frame.merge(flags: [:end_headers, :end_stream]) # half closed local
463
+ @stream.receive headers_frame.merge(flags: [:end_headers, :end_stream]) # closed by remote
463
464
  end
464
465
 
465
466
  it 'should raise STREAM_CLOSED on attempt to send frames' do
466
- (FRAME_TYPES - [PRIORITY, RST_STREAM]).each do |frame|
467
+ (frame_types - [priority_frame, rst_stream_frame]).each do |frame|
467
468
  expect do
468
469
  @stream.dup.send frame
469
470
  end.to raise_error(StreamClosed)
@@ -471,7 +472,7 @@ RSpec.describe HTTP2::Stream do
471
472
  end
472
473
 
473
474
  it 'should raise STREAM_CLOSED on receipt of frame' do
474
- (FRAME_TYPES - [PRIORITY, RST_STREAM, WINDOW_UPDATE]).each do |frame|
475
+ (frame_types - [priority_frame, rst_stream_frame, window_update_frame]).each do |frame|
475
476
  expect do
476
477
  @stream.dup.receive frame
477
478
  end.to raise_error(StreamClosed)
@@ -479,38 +480,38 @@ RSpec.describe HTTP2::Stream do
479
480
  end
480
481
 
481
482
  it 'should allow PRIORITY, RST_STREAM to be sent' do
482
- expect { @stream.send PRIORITY.dup }.to_not raise_error
483
- expect { @stream.send RST_STREAM.dup }.to_not raise_error
483
+ expect { @stream.send priority_frame }.to_not raise_error
484
+ expect { @stream.send rst_stream_frame }.to_not raise_error
484
485
  end
485
486
 
486
487
  it 'should allow PRIORITY, RST_STREAM to be received' do
487
- expect { @stream.receive PRIORITY }.to_not raise_error
488
- expect { @stream.receive RST_STREAM }.to_not raise_error
488
+ expect { @stream.receive priority_frame }.to_not raise_error
489
+ expect { @stream.receive rst_stream_frame }.to_not raise_error
489
490
  end
490
491
 
491
492
  it 'should reprioritize stream on sent PRIORITY' do
492
- expect { @stream.send PRIORITY.dup }.to_not raise_error
493
+ expect { @stream.send priority_frame }.to_not raise_error
493
494
  expect(@stream.weight).to eq 20
494
495
  end
495
496
  it 'should reprioritize stream on received PRIORITY' do
496
- expect { @stream.receive PRIORITY }.to_not raise_error
497
+ expect { @stream.receive priority_frame }.to_not raise_error
497
498
  expect(@stream.weight).to eq 20
498
499
  end
499
500
 
500
501
  it 'should ignore received WINDOW_UPDATE frames' do
501
- expect { @stream.receive WINDOW_UPDATE }.to_not raise_error
502
+ expect { @stream.receive window_update_frame }.to_not raise_error
502
503
  expect(@stream.state).to eq :closed
503
504
  end
504
505
  end
505
506
 
506
507
  context 'local closed via RST_STREAM frame' do
507
508
  before(:each) do
508
- @stream.send HEADERS.deep_dup # open
509
- @stream.send RST_STREAM.deep_dup # closed by local
509
+ @stream.send headers_frame # open
510
+ @stream.send rst_stream_frame # closed by local
510
511
  end
511
512
 
512
513
  it 'should ignore received frames' do
513
- (FRAME_TYPES - [PUSH_PROMISE]).each do |frame|
514
+ (frame_types - [push_promise_frame]).each do |frame|
514
515
  expect do
515
516
  cb = []
516
517
  @stream.on(:data) { cb << :data }
@@ -533,12 +534,12 @@ RSpec.describe HTTP2::Stream do
533
534
  # FIXME: Isn't this test same as "half closed (local)"?
534
535
  # context "local closed via END_STREAM flag" do
535
536
  # before(:each) do
536
- # @stream.send HEADERS # open
537
- # @stream.send DATA # contains end_stream flag
537
+ # @stream.send headers_frame # open
538
+ # @stream.send data_frame # contains end_stream flag
538
539
  # end
539
540
 
540
541
  # it "should ignore received frames" do
541
- # FRAME_TYPES.each do |frame|
542
+ # frame_types.each do |frame|
542
543
  # expect { @stream.dup.receive frame }.to_not raise_error
543
544
  # end
544
545
  # end
@@ -554,31 +555,31 @@ RSpec.describe HTTP2::Stream do
554
555
  end
555
556
 
556
557
  it 'should update window size on DATA frames only' do
557
- @stream.send HEADERS.deep_dup # go to open
558
+ @stream.send headers_frame # go to open
558
559
  expect(@stream.remote_window).to eq DEFAULT_FLOW_WINDOW
559
560
 
560
- (FRAME_TYPES - [DATA, PING, GOAWAY, SETTINGS]).each do |frame|
561
+ (frame_types - [data_frame, ping_frame, goaway_frame, settings_frame]).each do |frame|
561
562
  s = @stream.dup
562
563
  s.send frame.deep_dup
563
564
  expect(s.remote_window).to eq DEFAULT_FLOW_WINDOW
564
565
  end
565
566
 
566
- @stream.send DATA.dup
567
- expect(@stream.remote_window).to eq DEFAULT_FLOW_WINDOW - DATA[:payload].bytesize
567
+ @stream.send data_frame
568
+ expect(@stream.remote_window).to eq DEFAULT_FLOW_WINDOW - data_frame[:payload].bytesize
568
569
  end
569
570
 
570
571
  it 'should update window size on receipt of WINDOW_UPDATE' do
571
- @stream.send HEADERS.deep_dup
572
- @stream.send DATA.dup
573
- @stream.receive WINDOW_UPDATE
572
+ @stream.send headers_frame
573
+ @stream.send data_frame
574
+ @stream.receive window_update_frame
574
575
 
575
576
  expect(@stream.remote_window).to eq(
576
- DEFAULT_FLOW_WINDOW - DATA[:payload].bytesize + WINDOW_UPDATE[:increment],
577
+ DEFAULT_FLOW_WINDOW - data_frame[:payload].bytesize + window_update_frame[:increment],
577
578
  )
578
579
  end
579
580
 
580
581
  it 'should observe session flow control' do
581
- settings, data = SETTINGS.dup, DATA.dup
582
+ settings, data = settings_frame, data_frame
582
583
  settings[:payload] = [[:settings_initial_window_size, 1000]]
583
584
  settings[:stream] = 0
584
585
 
@@ -586,7 +587,7 @@ RSpec.describe HTTP2::Stream do
586
587
  @client << framer.generate(settings)
587
588
 
588
589
  s1 = @client.new_stream
589
- s1.send HEADERS.deep_dup
590
+ s1.send headers_frame
590
591
  s1.send data.merge(payload: 'x' * 900, flags: [])
591
592
  expect(s1.remote_window).to eq 100
592
593
 
@@ -594,31 +595,31 @@ RSpec.describe HTTP2::Stream do
594
595
  expect(s1.remote_window).to eq 0
595
596
  expect(s1.buffered_amount).to eq 100
596
597
 
597
- @client << framer.generate(WINDOW_UPDATE.merge(stream: s1.id, increment: 1000))
598
+ @client << framer.generate(window_update_frame.merge(stream: s1.id, increment: 1000))
598
599
  expect(s1.buffered_amount).to eq 0
599
600
  expect(s1.remote_window).to eq 900
600
601
  end
601
602
 
602
603
  it 'should not update window when data received is less than half of maximum local window size' do
603
- data = DATA.deep_dup
604
+ data = data_frame
604
605
  datalen = data[:payload].bytesize
605
606
  expect(@stream).not_to receive(:send) do |frame|
606
607
  expect(frame[:type]).to eq :window_update
607
608
  expect(frame[:increment]).to eq datalen
608
609
  end
609
- @stream.receive HEADERS.deep_dup
610
+ @stream.receive headers_frame
610
611
  @stream.receive data
611
612
  end
612
613
 
613
614
  it 'should update window when data received is over half of the maximum local window size' do
614
- data1 = DATA.merge(payload: 'a'*16_384, flags: [])
615
- data2 = DATA.merge(payload: 'a'*16_384)
615
+ data1 = data_frame.merge(payload: 'a'*16_384, flags: [])
616
+ data2 = data_frame.merge(payload: 'a'*16_384)
616
617
  datalen = 16_384 * 2
617
618
  expect(@stream).to receive(:send) do |frame|
618
619
  expect(frame[:type]).to eq :window_update
619
620
  expect(frame[:increment]).to eq datalen
620
621
  end
621
- @stream.receive HEADERS.deep_dup
622
+ @stream.receive headers_frame
622
623
  @stream.receive data1
623
624
  @stream.receive data2
624
625
  end
@@ -750,7 +751,7 @@ RSpec.describe HTTP2::Stream do
750
751
  end
751
752
 
752
753
  it 'should emit received headers via on(:headers)' do
753
- headers, recv = [%w(header value)], nil
754
+ headers, recv = REQUEST_HEADERS, nil
754
755
  @srv.on(:stream) do |stream|
755
756
  stream.on(:headers) { |h| recv = h }
756
757
  end
@@ -767,7 +768,7 @@ RSpec.describe HTTP2::Stream do
767
768
  end
768
769
  end
769
770
 
770
- @client_stream.headers('key' => 'value')
771
+ @client_stream.headers(REQUEST_HEADERS)
771
772
  @client_stream.data(payload)
772
773
  end
773
774
 
@@ -783,7 +784,7 @@ RSpec.describe HTTP2::Stream do
783
784
  end
784
785
  end
785
786
 
786
- @client_stream.headers('key' => 'value')
787
+ @client_stream.headers(REQUEST_HEADERS)
787
788
  @client_stream.reprioritize(weight: new_weight, dependency: new_dependency)
788
789
  expect(callback_called).to be
789
790
  end
@@ -795,18 +796,18 @@ RSpec.describe HTTP2::Stream do
795
796
  @server_stream = stream
796
797
  end
797
798
 
798
- @client_stream.headers('key' => 'value')
799
+ @client_stream.headers(REQUEST_HEADERS)
799
800
  end
800
801
 
801
802
  it '.promise should emit server initiated stream' do
802
803
  push = nil
803
- @server_stream.promise('key' => 'val') { |pstream| push = pstream }
804
+ @server_stream.promise(REQUEST_HEADERS) { |pstream| push = pstream }
804
805
  expect(push.id).to eq 2
805
806
  end
806
807
 
807
808
  it '.promise push stream should have parent stream' do
808
809
  push = nil
809
- @server_stream.promise('key' => 'val') { |pstream| push = pstream }
810
+ @server_stream.promise(REQUEST_HEADERS) { |pstream| push = pstream }
810
811
 
811
812
  expect(push.state).to eq :reserved_local
812
813
  expect(push.parent.id).to eq @server_stream.id
@@ -815,7 +816,7 @@ RSpec.describe HTTP2::Stream do
815
816
  context 'stream states' do
816
817
  it 'server: active > half close > close' do
817
818
  order = []
818
- @server_stream.promise('key' => 'val') do |push|
819
+ @server_stream.promise(REQUEST_HEADERS) do |push|
819
820
  stream = push
820
821
 
821
822
  expect(push.state).to eq :reserved_local
@@ -825,8 +826,8 @@ RSpec.describe HTTP2::Stream do
825
826
  push.on(:half_close) { order << :half_close }
826
827
  push.on(:close) { order << :close }
827
828
 
828
- push.headers('key2' => 'val2')
829
- push.send DATA.merge(stream: stream.id)
829
+ push.headers(RESPONSE_HEADERS)
830
+ push.send data_frame.merge(stream: stream.id)
830
831
  end
831
832
 
832
833
  expect(order).to eq [:reserved, :active, :half_close, :close]
@@ -854,13 +855,13 @@ RSpec.describe HTTP2::Stream do
854
855
  expect(push.id).to be_even
855
856
  end
856
857
 
857
- @server_stream.promise('key' => 'val') do |push|
858
- push.headers('key2' => 'val2')
858
+ @server_stream.promise(REQUEST_HEADERS) do |push|
859
+ push.headers(RESPONSE_HEADERS)
859
860
  push.data('somedata')
860
861
  end
861
862
 
862
- expect(promise_headers).to eq([%w(key val)])
863
- expect(headers).to eq([%w(key2 val2)])
863
+ expect(promise_headers).to eq(REQUEST_HEADERS)
864
+ expect(headers).to eq(RESPONSE_HEADERS)
864
865
  expect(order).to eq [
865
866
  :reserved,
866
867
  :promise_headers,