Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 437 lines (372 sloc) 9.796 kb
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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
require 'backports'
require_relative 'spec_helper'

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 }
      ran.should be_true
    end

    it 'returns the stream object' do
      out = stream { }
      out.should be_a(Sinatra::Helpers::Stream)
    end

    it 'fires a request against that stream' do
      stream { |out| out << "Hello World!" }
      last_response.should be_ok
      body.should be == "Hello World!"
    end

    it 'passes the stream object to the block' do
      passed = nil
      returned = stream { |out| passed = out }
      passed.should be == returned
    end
  end

  context Sinatra::Streaming::Stream do
    it 'should extend the stream object' do
      out = stream { }
      out.should be_a(Sinatra::Streaming::Stream)
    end

    it 'should not extend stream objects of other apps' do
      out = nil
      mock_app { get('/') { out = stream { }}}
      get('/')
      out.should be_a(Sinatra::Helpers::Stream)
      out.should_not be_a(Sinatra::Streaming::Stream)
    end
  end

  context EventMachine::Deferrable do
    it 'allows attaching more than one callback' do
      a = b = false
      stream do |out|
        out.callback { a = true }
        out.callback { b = true }
      end
      a.should be_true
      b.should be_true
    end

    it 'triggers callbacks after streaming' do
      triggered = false
      stream do |out|
        out.callback { triggered = true }
        triggered.should be_false
      end
      triggered.should be_true
    end
  end

  context 'app' do
    it 'is the app instance the stream was created from' do
      out = stream { }
      out.app.should be_a(Sinatra::Base)
    end
  end

  context 'lineno' do
    it 'defaults to 0' do
      stream { }.lineno.should be == 0
    end

    it 'does not increase on write' do
      stream do |out|
        out << "many\nlines\n"
        out.lineno.should be == 0
      end
    end

    it 'is writable' do
      out = stream { }
      out.lineno = 10
      out.lineno.should be == 10
    end
  end

  context 'pos' do
    it 'defaults to 0' do
      stream { }.pos.should be == 0
    end

    it 'increases when writing data' do
      stream do |out|
        out.pos.should be == 0
        out << 'hi'
        out.pos.should be == 2
      end
    end

    it 'is writable' do
      out = stream { }
      out.pos = 10
      out.pos.should be == 10
    end

    it 'aliased to #tell' do
      out = stream { }
      out.tell.should be == 0
      out.pos = 10
      out.tell.should be == 10
    end
  end

  context 'closed' do
    it 'returns false while streaming' do
      stream { |out| out.should_not be_closed }
    end

    it 'returns true after streaming' do
      stream {}.should be_closed
    end
  end

  context 'map!' do
    it 'applies transformations later' do
      stream do |out|
        out.map! { |s| s.upcase }
        out << 'ok'
      end
      body.should be == "OK"
    end

    it 'is chainable' do
      stream do |out|
        out.map! { |s| s.upcase }
        out.map! { |s| s.reverse }
        out << 'ok'
      end
      body.should be == "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" }
      body.should be == "OK"
    end

    it 'modifies each value separately' do
      stream do |out|
        out.map! { |s| s.reverse }
        out << "ab" << "cd"
      end
      body.should be == "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" }
      body.should be == "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" }
      body.should be == "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" }
      body.should be == "OK"
    end

    it 'does not modify the original body' do
      stream do |out|
        out.map { |s| s.reverse }
        out << 'ok'
      end
      body.should be == 'ok'
    end
  end

  context 'write' do
    it 'writes to the stream' do
      stream { |out| out.write 'hi' }
      body.should be == 'hi'
    end

    it 'returns the number of bytes' do
      stream do |out|
        out.write('hi').should be == 2
        out.write('hello').should be == 5
      end
    end

    it 'accepts non-string objects' do
      stream do |out|
        out.write(12).should be == 2
      end
    end

    it 'should be aliased to syswrite' do
      stream { |out| out.syswrite('hi').should be == 2 }
      body.should be == 'hi'
    end

    it 'should be aliased to write_nonblock' do
      stream { |out| out.write_nonblock('hi').should be == 2 }
      body.should be == 'hi'
    end
  end

  context 'print' do
    it 'writes to the stream' do
      stream { |out| out.print('hi') }
      body.should be == 'hi'
    end

    it 'accepts multiple arguments' do
      stream { |out| out.print(1, 2, 3, 4) }
      body.should be == '1234'
    end

    it 'returns nil' do
      stream { |out| out.print('hi').should be_nil }
    end
  end

  context 'printf' do
    it 'writes to the stream' do
      stream { |out| out.printf('hi') }
      body.should be == 'hi'
    end

    it 'interpolates the format string' do
      stream { |out| out.printf("%s: %d", "answer", 42) }
      body.should be == 'answer: 42'
    end

    it 'returns nil' do
      stream { |out| out.printf('hi').should be_nil }
    end
  end

  context 'putc' do
    it 'writes the first character of a string' do
      stream { |out| out.putc('hi') }
      body.should be == 'h'
    end

    it 'writes the character corresponding to an integer' do
      stream { |out| out.putc(42) }
      body.should be == '*'
    end

    it 'returns nil' do
      stream { |out| out.putc('hi').should be_nil }
    end
  end

  context 'puts' do
    it 'writes to the stream' do
      stream { |out| out.puts('hi') }
      body.should be == "hi\n"
    end

    it 'accepts multiple arguments' do
      stream { |out| out.puts(1, 2, 3, 4) }
      body.should be == "1\n2\n3\n4\n"
    end

    it 'returns nil' do
      stream { |out| out.puts('hi').should be_nil }
    end
  end

  context 'close' do
    it 'sets #closed? to true' do
      stream do |out|
        out.close
        out.should be_closed
      end
    end

    it 'sets #closed_write? to true' do
      stream do |out|
        out.should_not be_closed_write
        out.close
        out.should be_closed_write
      end
    end

    it 'fires callbacks' do
      stream do |out|
        fired = false
        out.callback { fired = true }
        out.close
        fired.should 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| out.should be_closed_read }}
  end

  context 'rewind' do
    it 'resets pos' do
      stream do |out|
        out << 'hi'
        out.rewind
        out.pos.should be == 0
      end
    end

    it 'resets lineno' do
      stream do |out|
        out.lineno = 10
        out.rewind
        out.lineno.should be == 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| out.public_send(method).should 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| out.public_send(method).should be_nil }
      end
    end
  end
end
Something went wrong with that request. Please try again.