socky 0.2.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,60 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Socky::Options do
4
-
5
- context "class" do
6
- before(:all) do
7
- @default_options = Socky.options
8
- end
9
- after(:each) do
10
- Socky.options = @default_options
11
- end
12
-
13
- context "#prepare" do
14
- before(:each) do
15
- Socky::Options::Parser.stub!(:parse).and_return({})
16
- Socky::Options::Config.stub!(:read).and_return({})
17
- end
18
- it "should call parser with self option" do
19
- Socky::Options::Parser.should_receive(:parse).with([:a,:b,:c])
20
- Socky::Options.prepare([:a,:b,:c])
21
- end
22
- it "should call read_config with patch" do
23
- Socky::Options::Config.should_receive(:read).with("/var/run/socky.yml", :kill => nil)
24
- Socky::Options.prepare([])
25
- end
26
- it "should set Socky options to default hash when parse_options and read_config don't do anything" do
27
- Socky::Options.prepare([])
28
- Socky.options.should eql(default_options)
29
- end
30
- it "should value parse_options over default values" do
31
- Socky::Options::Parser.stub!(:parse).and_return(:log_path => "parsed")
32
- Socky::Options.prepare([])
33
- Socky.options.should eql(default_options.merge(:log_path=>"parsed"))
34
- end
35
- it "should value read_config over default values" do
36
- Socky::Options::Config.stub!(:read).and_return(:log_path => "from config")
37
- Socky::Options.prepare([])
38
- Socky.options.should eql(default_options.merge(:log_path=>"from config"))
39
- end
40
- it "should value parse_options over read_config" do
41
- Socky::Options::Config.stub!(:read).and_return(:log_path => "from config")
42
- Socky::Options::Parser.stub!(:parse).and_return(:log_path => "parsed")
43
- Socky::Options.prepare([])
44
- Socky.options.should eql(default_options.merge(:log_path=>"parsed"))
45
- end
46
- end
47
- end
48
-
49
- def default_options
50
- {
51
- :port => 8080,
52
- :log_path => nil,
53
- :debug => false,
54
- :deep_debug => false,
55
- :secure => false,
56
- :config_path => "/var/run/socky.yml"
57
- }
58
- end
59
-
60
- end
@@ -1,88 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Socky::Runner do
4
-
5
- context "#class" do
6
- context "#run" do
7
- before(:each) do
8
- @server = mock(:server, :start => nil)
9
- described_class.stub!(:new).and_return(@server)
10
- end
11
- it "should create new instance of self" do
12
- described_class.should_receive(:new).with("some args")
13
- described_class.run("some args")
14
- end
15
- it "should call #start on new instance of self if daemonize option is false" do
16
- Socky.stub(:options).and_return({:daemonize => false})
17
- @server.should_receive(:start)
18
- described_class.run
19
- end
20
- it "should call #daemonize on new instance of self if daemonize option is true" do
21
- Socky.stub(:options).and_return({:daemonize => true})
22
- @server.should_receive(:daemonize)
23
- described_class.run
24
- end
25
- it "should call #kill_pid on new instance of self if kill option is true" do
26
- Socky.stub(:options).and_return({:kill => true})
27
- @server.should_receive(:kill_pid)
28
- described_class.run
29
- end
30
- end
31
- context "#new" do
32
- it "should prepare options from args" do
33
- begin
34
- described_class.new(["-c", File.dirname(__FILE__) + "/../files/default.yml"])
35
- Socky.options.class.should eql(Hash)
36
- Socky.options.should_not be_empty
37
- ensure
38
- Socky.options = nil
39
- end
40
- end
41
- end
42
- end
43
-
44
- context "#instance" do
45
- before(:each) do
46
- Socky::Options.stub!(:prepare)
47
- @runner = described_class.new
48
- end
49
-
50
- context "#start" do
51
- it "should create valid websocket server" do
52
- begin
53
- EM.run do
54
- MSG = "Hello World!"
55
- EventMachine.add_timer(0.1) do
56
- http = EventMachine::HttpRequest.new('ws://127.0.0.1:12345/').get :timeout => 0
57
- http.errback {
58
- EM.stop
59
- fail
60
- }
61
- http.callback {
62
- http.response_header.status.should == 101
63
- EM.stop
64
- }
65
- end
66
-
67
- Socky.stub!(:options).and_return({:port => 12345})
68
- Socky.logger = mock(:logger, :info => nil, :debug => nil)
69
- @runner.start
70
- end
71
- ensure
72
- Socky.logger = nil
73
- end
74
- end
75
- end
76
-
77
- it "#stop should call EM.stop" do
78
- begin
79
- Socky.logger = mock(:logger, :info => nil, :debug => nil)
80
- EM.should_receive(:stop)
81
- @runner.stop
82
- ensure
83
- Socky.logger = nil
84
- end
85
- end
86
-
87
- end
88
- end
@@ -1,89 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Socky do
4
-
5
- context "class" do
6
-
7
- it "should have non-blank version" do
8
- Socky::VERSION.should_not be_nil
9
- end
10
- it "should have options in hash form" do
11
- Socky.options.should_not be_nil
12
- Socky.options.class.should eql(Hash)
13
- end
14
- it "should allow to set options" do
15
- Socky.options.should eql(Hash.new)
16
- begin
17
- Socky.options = {:key => :value}
18
- Socky.options.should eql({:key => :value})
19
- ensure
20
- Socky.options = Hash.new
21
- end
22
- end
23
- it "should have logger" do
24
- Socky.logger.should_not be_nil
25
- Socky.logger.class.should eql(Logger)
26
- end
27
- it "should have logger with STDOUT at default" do
28
- Socky.logger.instance_variable_get('@logdev').dev.class.should eql(IO)
29
- end
30
- it "should be able to set logger" do
31
- begin
32
- logger = Logger.new(STDOUT)
33
- Socky.logger.should_not equal(logger)
34
- Socky.logger = logger
35
- Socky.logger.should equal(logger)
36
- ensure
37
- Socky.logger = nil
38
- end
39
- end
40
- it "should be able to change verbosity of logger by setting debug option" do
41
- begin
42
- Socky.logger.level.should eql(Logger::INFO)
43
- Socky.logger = nil
44
- Socky.stub!(:options).and_return({:debug => true})
45
- Socky.logger.level.should eql(Logger::DEBUG)
46
- Socky.logger = nil
47
- Socky.stub!(:options).and_return({:debug => false})
48
- Socky.logger.level.should eql(Logger::INFO)
49
- ensure
50
- Socky.logger = nil
51
- end
52
- end
53
- it "should not have default log path" do
54
- Socky.log_path.should be_nil
55
- end
56
- it "should be able to change log path by settion log_path option" do
57
- Socky.stub!(:options).and_return({:log_path => "abstract"})
58
- Socky.log_path.should eql("abstract")
59
- end
60
- it "should be able to change logger write place" do
61
- begin
62
- Socky.options = {:log_path => File.join(File.dirname(__FILE__), 'files', 'socky.log')}
63
- Socky.logger.should_not be_nil
64
- Socky.logger.instance_variable_get('@logdev').dev.class.should eql(File)
65
- ensure
66
- Socky.logger = nil
67
- Socky.options = {}
68
- end
69
- end
70
- it "should have default pid path" do
71
- Socky.pid_path.should_not be_nil
72
- Socky.pid_path.should eql("/var/run/socky.pid")
73
- end
74
- it "should be able to change pid path by settion pid_path option" do
75
- Socky.stub!(:options).and_return({:pid_path => "abstract"})
76
- Socky.pid_path.should eql("abstract")
77
- end
78
- it "should have default config path" do
79
- Socky.config_path.should_not be_nil
80
- Socky.config_path.should eql("/var/run/socky.yml")
81
- end
82
- it "should be able to change config path by settion config_path option" do
83
- Socky.stub!(:options).and_return({:config_path => "abstract"})
84
- Socky.config_path.should eql("abstract")
85
- end
86
-
87
- end
88
-
89
- end
@@ -1,6 +0,0 @@
1
- require 'rubygems'
2
- require 'spec'
3
- require "spec/autorun"
4
-
5
- require 'socky'
6
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
@@ -1,96 +0,0 @@
1
- # #--
2
- # Includes portion originally Copyright (C)2008 Michael Fellinger
3
- # license See file LICENSE for details
4
- # #--
5
-
6
- require 'rack'
7
-
8
- module Stallion
9
- class Mount
10
- def initialize(name, *methods, &block)
11
- @name, @methods, @block = name, methods, block
12
- end
13
-
14
- def ride
15
- @block.call
16
- end
17
-
18
- def match?(request)
19
- method = request['REQUEST_METHOD']
20
- right_method = @methods.empty? or @methods.include?(method)
21
- end
22
- end
23
-
24
- class Stable
25
- attr_reader :request, :response
26
-
27
- def initialize
28
- @boxes = {}
29
- end
30
-
31
- def in(path, *methods, &block)
32
- mount = Mount.new(path, *methods, &block)
33
- @boxes[[path, methods]] = mount
34
- mount
35
- end
36
-
37
- def call(request, response)
38
- @request, @response = request, response
39
- @boxes.each do |(path, methods), mount|
40
- if mount.match?(request)
41
- mount.ride
42
- end
43
- end
44
- end
45
- end
46
-
47
- STABLES = {}
48
-
49
- def self.saddle(name = nil)
50
- STABLES[name] = stable = Stable.new
51
- yield stable
52
- end
53
-
54
- def self.run(options = {})
55
- options = {:Host => "127.0.0.1", :Port => 8080}.merge(options)
56
- Rack::Handler::Mongrel.run(Rack::Lint.new(self), options)
57
- end
58
-
59
- def self.call(env)
60
- request = Rack::Request.new(env)
61
- response = Rack::Response.new
62
-
63
- STABLES.each do |name, stable|
64
- stable.call(request, response)
65
- end
66
-
67
- response.finish
68
- end
69
- end
70
-
71
- Stallion.saddle :spec do |stable|
72
- stable.in '/' do
73
-
74
- if stable.request.path_info == '/fail'
75
- stable.response.status = 404
76
-
77
- elsif stable.request.path_info == '/timeout'
78
- sleep(10)
79
- stable.response.write 'timeout'
80
-
81
- elsif
82
- stable.response.write 'Hello, World!'
83
- end
84
-
85
- end
86
- end
87
-
88
- Thread.new do
89
- begin
90
- Stallion.run :Host => '127.0.0.1', :Port => 8765
91
- rescue Exception => e
92
- print e
93
- end
94
- end
95
-
96
- sleep(1)