Sha256: acd958b0e01847b647891c4c62b6dd425c1e18e6caeaa139f56f3bb6c2775876

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

require 'test_helper'
class Nyoibo::DaemonTest < Test::Unit::TestCase
  class TestApp < Test::Unit::TestCase
    include Nyoibo::Callback
    after_upload "/" do |json, binary|
      File.open("/tmp/test.jpg", "w:binary"){|f|
        f.write(binary)
      }
      raise "finame is not 'test.jpg'" if json["filename"] != "test.jpg"
      raise "Binary size is not '3137' byte" if binary.length != 3137
    end
  end

  context "run" do
    setup do
      @file = File.new(File.expand_path("../../fixtures/test.jpg", __FILE__), "rb")
      Nyoibo.configure do
        host "localhost"
        port 3030
      end
    end

    should "listen port" do
      EM.run do
        EventMachine.add_timer(0.1) do
          http = EventMachine::HttpRequest.new('ws://localhost:3030/').get(:timeout => 0)
          http.errback { failed }
          @start = 0
          @sendsize = 1024
          http.stream{|msg|
            case  msg
            when "OK Ready"
              http.send("JSON: " + {:filename => File.basename(@file.path), :size => @file.size}.to_json)
            when "OK Bye"
              http.close_connection
            when "NEXT"
              @end = @start + @sendsize
              @encoded ||= Base64.encode64(@file.read)

              @end = @encoded.size - 1 if @end >= @encoded.size
              if @start == @encoded.size
                http.send("QUIT")
              else
                chunk = @encoded.slice(@start..@end)
                if @start == 0
                  chunk = "data:application/octet-stream;base64," + chunk
                end

                http.send(chunk)
              end
              @start = @end + 1
            end
          }
          http.disconnect{
            EventMachine.stop
          }
        end
        Nyoibo.run
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nyoibo-0.0.3.3 test/unit/daemon_test.rb
nyoibo-0.0.3.2 test/unit/daemon_test.rb
nyoibo-0.0.3.1 test/unit/daemon_test.rb
nyoibo-0.0.3 test/unit/daemon_test.rb