Skip to content

Commit

Permalink
Support setting custom options on the socket and web server.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristandunn committed Oct 22, 2013
1 parent 649a65c commit fee2c83
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Expand Up @@ -2,6 +2,7 @@

## 0.11.0 — Unreleased

* Support setting custom options on the socket and web server. (Tristan Dunn)
* Update the Pusher JS client to version 2.1.3. (Tristan Dunn)
* Update dependencies. (Tristan Dunn)

Expand Down
65 changes: 45 additions & 20 deletions lib/pusher-fake/configuration.rb
Expand Up @@ -9,40 +9,65 @@ class Configuration
# @return [String] The Pusher API token. (Defaults to +PUSHER_API_SECRET+.)
attr_accessor :secret

# @return [String] The host on which the socket server listens. (Defaults to +127.0.0.1+.)
attr_accessor :socket_host
# @return [Hash] Options for the socket server.
attr_accessor :socket_options

# @return [Fixnum] The port on which the socket server listens. (Defaults to +8080+.)
attr_accessor :socket_port

# @return [String] The host on which the web server listens. (Defaults to +127.0.0.1+.)
attr_accessor :web_host

# @return [Fixnum] The port on which the web server listens. (Defaults to +8081+.)
attr_accessor :web_port
# @return [Hash] Options for the web server.
attr_accessor :web_options

# @return [Array] An array of webhook URLs. (Defaults to +[]+.)
attr_accessor :webhooks

# Instantiated from {PusherFake.configuration}. Sets the defaults.
def initialize
self.app_id = "PUSHER_APP_ID"
self.key = "PUSHER_API_KEY"
self.secret = "PUSHER_API_SECRET"
self.socket_host = "127.0.0.1"
self.socket_port = 8080
self.web_host = "127.0.0.1"
self.web_port = 8081
self.webhooks = []
self.app_id = "PUSHER_APP_ID"
self.key = "PUSHER_API_KEY"
self.secret = "PUSHER_API_SECRET"
self.webhooks = []

self.socket_options = { host: "127.0.0.1", port: 8080 }
self.web_options = { host: "127.0.0.1", port: 8081 }
end

# Set the host on which the socket server listens.
#
# @deprecated
# @param host String
def socket_host=(host)
socket_options[:host] = host
end

# Set the port on which the socket server listens.
#
# @deprecated
# @param port Integer
def socket_port=(port)
socket_options[:port] = port
end

# Set the host on which the web server listens.
#
# @deprecated
# @param host String
def web_host=(host)
web_options[:host] = host
end

# Set the port on which the web server listens.
#
# @deprecated
# @param port Integer
def web_port=(port)
web_options[:port] = port
end

# Convert the configuration to a hash sutiable for Pusher JS options.
#
# @param [Hash] options Custom options for Pusher client.
def to_options(options = {})
options.merge({
wsHost: socket_host,
wsPort: socket_port
wsHost: socket_options[:host],
wsPort: socket_options[:port]
})
end
end
Expand Down
12 changes: 10 additions & 2 deletions lib/pusher-fake/server.rb
Expand Up @@ -13,7 +13,7 @@ def self.start

# Start the WebSocket server.
def self.start_socket_server
EventMachine::WebSocket.start(socket_server_options) do |socket|
EventMachine::WebSocket.start(configuration.socket_options) do |socket|
socket.onopen do
connection = Connection.new(socket)
connection.establish
Expand All @@ -30,8 +30,16 @@ def self.start_socket_server

# Start the web server.
def self.start_web_server
options = configuration.web_options.dup

Thin::Logging.silent = true
Thin::Server.start(configuration.web_host, configuration.web_port, Application)
Thin::Server.new(options.delete(:host), options.delete(:port), Application).tap do |server|
options.each do |key, value|
server.__send__("#{key}=", value)
end

server.start!
end
end

private
Expand Down
38 changes: 32 additions & 6 deletions spec/lib/pusher-fake/configuration_spec.rb
Expand Up @@ -4,20 +4,46 @@
it { should have_configuration_option(:app_id).with_default("PUSHER_APP_ID") }
it { should have_configuration_option(:key).with_default("PUSHER_API_KEY") }
it { should have_configuration_option(:secret).with_default("PUSHER_API_SECRET") }
it { should have_configuration_option(:socket_host).with_default("127.0.0.1") }
it { should have_configuration_option(:socket_port).with_default(8080) }
it { should have_configuration_option(:web_host).with_default("127.0.0.1") }
it { should have_configuration_option(:web_port).with_default(8081) }
it { should have_configuration_option(:socket_options).with_default({ host: "127.0.0.1", port: 8080 }) }
it { should have_configuration_option(:web_options).with_default({ host: "127.0.0.1", port: 8081 }) }
it { should have_configuration_option(:webhooks).with_default([]) }
end

describe PusherFake::Configuration, "#socket_host=" do
it "sets socket options host value" do
subject.socket_host = "192.168.0.1"
subject.socket_options[:host].should == "192.168.0.1"
end
end

describe PusherFake::Configuration, "#socket_post=" do
it "sets socket options host value" do
subject.socket_port = 443
subject.socket_options[:port].should == 443
end
end

describe PusherFake::Configuration, "#web_host=" do
it "sets web options host value" do
subject.web_host = "192.168.0.1"
subject.web_options[:host].should == "192.168.0.1"
end
end

describe PusherFake::Configuration, "#web_post=" do
it "sets web options host value" do
subject.web_port = 443
subject.web_options[:port].should == 443
end
end

describe PusherFake::Configuration, "#to_options" do
it "includes the socket host as wsHost" do
subject.to_options.should include(wsHost: subject.socket_host)
subject.to_options.should include(wsHost: subject.socket_options[:host])
end

it "includes the socket port as wsPort" do
subject.to_options.should include(wsPort: subject.socket_port)
subject.to_options.should include(wsPort: subject.socket_options[:port])
end

it "supports passing custom options" do
Expand Down
21 changes: 16 additions & 5 deletions spec/lib/pusher-fake/server_spec.rb
Expand Up @@ -37,9 +37,9 @@
describe PusherFake::Server, ".start_socket_server" do
let(:data) { stub }
let(:socket) { stub(onopen: nil, onmessage: nil, onclose: nil) }
let(:options) { { host: configuration.socket_host, port: configuration.socket_port } }
let(:options) { configuration.socket_options }
let(:connection) { stub(establish: nil, process: nil) }
let(:configuration) { stub(socket_host: "192.168.0.1", socket_port: 8080) }
let(:configuration) { stub(socket_options: { host: "192.168.0.1", port: 8080 }) }

subject { PusherFake::Server }

Expand Down Expand Up @@ -128,12 +128,13 @@
describe PusherFake::Server, ".start_web_server" do
let(:host) { "192.168.0.1" }
let(:port) { 8081 }
let(:configuration) { stub(web_host: host, web_port: port) }
let(:server) { stub(:start! => true, :ssl= => true) }
let(:configuration) { stub(web_options: { host: host, port: port, ssl: true }) }

subject { PusherFake::Server }

before do
Thin::Server.stubs(:start)
Thin::Server.stubs(new: server)
Thin::Logging.stubs(:silent=)
PusherFake.stubs(configuration: configuration)
end
Expand All @@ -143,8 +144,18 @@
Thin::Logging.should have_received(:silent=).with(true)
end

it "creates the web server" do
subject.start_web_server
Thin::Server.should have_received(:new).with(host, port, PusherFake::Server::Application)
end

it "assigns custom options to the server" do
subject.start_web_server
server.should have_received(:ssl=).with(true)
end

it "starts the web server" do
subject.start_web_server
Thin::Server.should have_received(:start).with(host, port, PusherFake::Server::Application)
server.should have_received(:start!).with()
end
end
14 changes: 13 additions & 1 deletion spec/support/have_configuration_option_matcher.rb
Expand Up @@ -7,7 +7,19 @@ def initialize(option)
def matches?(configuration)
@configuration = configuration
@configuration.respond_to?(@option).should == true
@configuration.__send__(@option).should == @default if instance_variables.include?("@default")

if instance_variables.include?(:@default)
actual = @configuration.__send__(@option)

if @default.is_a?(Hash)
@default.each do |key, value|
actual[key].should == value
end
else
actual.should == @default
end
end

@configuration.__send__(:"#{@option}=", "value")
@configuration.__send__(@option).should == "value"
end
Expand Down

0 comments on commit fee2c83

Please sign in to comment.