Skip to content

Commit

Permalink
Add SSL capabilities to PrivatePub Ruby library
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Tilley committed May 25, 2011
1 parent ef03ef3 commit fbd0209
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
12 changes: 11 additions & 1 deletion lib/private_pub.rb
Expand Up @@ -23,14 +23,24 @@ def load_config(filename, environment)
yaml.each { |k, v| config[k.to_sym] = v }
end

def ssl?
uri = URI.parse(@config[:server])
uri.scheme == "https"
end

def subscription(options = {})
sub = {:timestamp => (Time.now.to_f * 1000).round}.merge(options)
sub[:signature] = Digest::SHA1.hexdigest([config[:secret_token], sub[:channel], sub[:timestamp]].join)
sub
end

def publish(data)
Net::HTTP.post_form(URI.parse(config[:server]), data)
url = URI.parse(@config[:server])
req = Net::HTTP::Post.new(url.path)
req.set_form_data(data)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = ssl?
res = http.start { |http| http.request(req) }
end

def faye_extension
Expand Down
26 changes: 24 additions & 2 deletions spec/private_pub_spec.rb
Expand Up @@ -49,9 +49,31 @@
subscription[:signature].should == Digest::SHA1.hexdigest("tokenchannel123")
end

it "determines that the server does not use HTTPS if the URL scheme is not HTTPS" do
PrivatePub.ssl?.should == false
end

it "determines that the server uses HTTPS if the URL scheme is HTTPS" do
PrivatePub.config[:server] = "https://localhost:9292/faye"
PrivatePub.ssl?.should == true
end

it "publishes to server using Net::HTTP" do
Net::HTTP.should_receive(:post_form).with(URI.parse(PrivatePub.config[:server]), "hello world").and_return(:result)
PrivatePub.publish("hello world").should == :result
uri = URI.parse(PrivatePub.config[:server])
http = mock(:http).as_null_object

Net::HTTP.should_receive(:new).with(uri.host, uri.port).and_return(http)
http.should_receive(:start).and_return(:result)
PrivatePub.publish("data" => "hello world").should == :result
end

it "publishes to server using an HTTPS connection if the server uses an HTTPS scheme" do
http = mock(:http).as_null_object

PrivatePub.config[:server] = "https://localhost:9292/faye"
Net::HTTP.should_receive(:new).and_return(http)
http.should_receive(:use_ssl=).with(true)
PrivatePub.publish("data" => "hello world")
end

it "has a FayeExtension instance" do
Expand Down

1 comment on commit fbd0209

@mrrooijen
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks similar to what I tried earlier, but this is probably a bit different from what I wrote. Does this work when submitting to Faye?

Please sign in to comment.