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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
require 'spec_helper'
RSpec.describe Sinatra::Streaming do
def stream(&block)
rack_middleware = @use
out = nil
mock_app do
rack_middleware.each { |args| use(*args) }
helpers Sinatra::Streaming
get('/') { out = stream(&block) }
end
get('/')
out
end
def use(*args)
@use << args
end
before do
@use = []
end
context 'stream test helper' do
it 'runs the given block' do
ran = false
stream { ran = true }
expect(ran).to be true
end
it 'returns the stream object' do
out = stream { }
expect(out).to be_a(Sinatra::Helpers::Stream)
end
it 'fires a request against that stream' do
stream { |out| out << "Hello World!" }
expect(last_response).to be_ok
expect(body).to eq("Hello World!")
end
it 'passes the stream object to the block' do
passed = nil
returned = stream { |out| passed = out }
expect(passed).to eq(returned)
end
end
context Sinatra::Streaming::Stream do
it 'should extend the stream object' do
out = stream { }
expect(out).to be_a(Sinatra::Streaming::Stream)
end
it 'should not extend stream objects of other apps' do
out = nil
mock_app { get('/') { out = stream { }}}
get('/')
expect(out).to be_a(Sinatra::Helpers::Stream)
expect(out).not_to be_a(Sinatra::Streaming::Stream)
end
end
context 'app' do
it 'is the app instance the stream was created from' do
out = stream { }
expect(out.app).to be_a(Sinatra::Base)
end
end
context 'lineno' do
it 'defaults to 0' do
expect(stream { }.lineno).to eq(0)
end
it 'does not increase on write' do
stream do |out|
out << "many\nlines\n"
expect(out.lineno).to eq(0)
end
end
it 'is writable' do
out = stream { }
out.lineno = 10
expect(out.lineno).to eq(10)
end
end
context 'pos' do
it 'defaults to 0' do
expect(stream { }.pos).to eq(0)
end
it 'increases when writing data' do
stream do |out|
expect(out.pos).to eq(0)
out << 'hi'
expect(out.pos).to eq(2)
end
end
it 'is writable' do
out = stream { }
out.pos = 10
expect(out.pos).to eq(10)
end
it 'aliased to #tell' do
out = stream { }
expect(out.tell).to eq(0)
out.pos = 10
expect(out.tell).to eq(10)
end
end
context 'closed' do
it 'returns false while streaming' do
stream { |out| expect(out).not_to be_closed }
end
it 'returns true after streaming' do
expect(stream {}).to be_closed
end
end
context 'map!' do
it 'applies transformations later' do
stream do |out|
out.map! { |s| s.upcase }
out << 'ok'
end
expect(body).to eq("OK")
end
it 'is chainable' do
stream do |out|
out.map! { |s| s.upcase }
out.map! { |s| s.reverse }
out << 'ok'
end
expect(body).to eq("KO")
end
it 'works with middleware' do
middleware = Class.new do
def initialize(app) @app = app end
def call(env)
status, headers, body = @app.call(env)
body.map! { |s| s.upcase }
[status, headers, body]
end
end
use middleware
stream { |out| out << "ok" }
expect(body).to eq("OK")
end
it 'modifies each value separately' do
stream do |out|
out.map! { |s| s.reverse }
out << "ab" << "cd"
end
expect(body).to eq("badc")
end
end
context 'map' do
it 'works with middleware' do
middleware = Class.new do
def initialize(app) @app = app end
def call(env)
status, headers, body = @app.call(env)
[status, headers, body.map(&:upcase)]
end
end
use middleware
stream { |out| out << "ok" }
expect(body).to eq("OK")
end
it 'is chainable' do
middleware = Class.new do
def initialize(app) @app = app end
def call(env)
status, headers, body = @app.call(env)
[status, headers, body.map(&:upcase).map(&:reverse)]
end
end
use middleware
stream { |out| out << "ok" }
expect(body).to eq("KO")
end
it 'can be written as each.map' do
middleware = Class.new do
def initialize(app) @app = app end
def call(env)
status, headers, body = @app.call(env)
[status, headers, body.each.map(&:upcase)]
end
end
use middleware
stream { |out| out << "ok" }
expect(body).to eq("OK")
end
it 'does not modify the original body' do
stream do |out|
out.map { |s| s.reverse }
out << 'ok'
end
expect(body).to eq('ok')
end
end
context 'write' do
it 'writes to the stream' do
stream { |out| out.write 'hi' }
expect(body).to eq('hi')
end
it 'returns the number of bytes' do
stream do |out|
expect(out.write('hi')).to eq(2)
expect(out.write('hello')).to eq(5)
end
end
it 'accepts non-string objects' do
stream do |out|
expect(out.write(12)).to eq(2)
end
end
it 'should be aliased to syswrite' do
stream { |out| expect(out.syswrite('hi')).to eq(2) }
expect(body).to eq('hi')
end
it 'should be aliased to write_nonblock' do
stream { |out| expect(out.write_nonblock('hi')).to eq(2) }
expect(body).to eq('hi')
end
end
context 'print' do
it 'writes to the stream' do
stream { |out| out.print('hi') }
expect(body).to eq('hi')
end
it 'accepts multiple arguments' do
stream { |out| out.print(1, 2, 3, 4) }
expect(body).to eq('1234')
end
it 'returns nil' do
stream { |out| expect(out.print('hi')).to be_nil }
end
end
context 'printf' do
it 'writes to the stream' do
stream { |out| out.printf('hi') }
expect(body).to eq('hi')
end
it 'interpolates the format string' do
stream { |out| out.printf("%s: %d", "answer", 42) }
expect(body).to eq('answer: 42')
end
it 'returns nil' do
stream { |out| expect(out.printf('hi')).to be_nil }
end
end
context 'putc' do
it 'writes the first character of a string' do
stream { |out| out.putc('hi') }
expect(body).to eq('h')
end
it 'writes the character corresponding to an integer' do
stream { |out| out.putc(42) }
expect(body).to eq('*')
end
it 'returns nil' do
stream { |out| expect(out.putc('hi')).to be_nil }
end
end
context 'puts' do
it 'writes to the stream' do
stream { |out| out.puts('hi') }
expect(body).to eq("hi\n")
end
it 'accepts multiple arguments' do
stream { |out| out.puts(1, 2, 3, 4) }
expect(body).to eq("1\n2\n3\n4\n")
end
it 'returns nil' do
stream { |out| expect(out.puts('hi')).to be_nil }
end
end
context 'close' do
it 'sets #closed? to true' do
stream do |out|
out.close
expect(out).to be_closed
end
end
it 'sets #closed_write? to true' do
stream do |out|
expect(out).not_to be_closed_write
out.close
expect(out).to be_closed_write
end
end
it 'fires callbacks' do
stream do |out|
fired = false
out.callback { fired = true }
out.close
expect(fired).to be true
end
end
it 'prevents from further writing' do
stream do |out|
out.close
expect { out << 'hi' }.to raise_error(IOError, 'not opened for writing')
end
end
end
context 'close_read' do
it 'raises the appropriate exception' do
expect { stream { |out| out.close_read }}.
to raise_error(IOError, "closing non-duplex IO for reading")
end
end
context 'closed_read?' do
it('returns true') { stream { |out| expect(out).to be_closed_read }}
end
context 'rewind' do
it 'resets pos' do
stream do |out|
out << 'hi'
out.rewind
expect(out.pos).to eq(0)
end
end
it 'resets lineno' do
stream do |out|
out.lineno = 10
out.rewind
expect(out.lineno).to eq(0)
end
end
end
raises = %w[
bytes eof? eof getbyte getc gets read read_nonblock readbyte readchar
readline readlines readpartial sysread ungetbyte ungetc
]
enum = %w[chars each_line each_byte each_char lines]
dummies = %w[flush fsync internal_encoding pid]
raises.each do |method|
context method do
it 'raises the appropriate exception' do
expect { stream { |out| out.public_send(method) }}.
to raise_error(IOError, "not opened for reading")
end
end
end
enum.each do |method|
context method do
it 'creates an Enumerator' do
stream { |out| expect(out.public_send(method)).to be_a(Enumerator) }
end
it 'calling each raises the appropriate exception' do
expect { stream { |out| out.public_send(method).each { }}}.
to raise_error(IOError, "not opened for reading")
end
end
end
dummies.each do |method|
context method do
it 'returns nil' do
stream { |out| expect(out.public_send(method)).to be_nil }
end
end
end
end
|