Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add SSL capabilities to PrivatePub Ruby library

  • Loading branch information...
commit fbd0209827c5069d5b9941ec483c74bfc09af276 1 parent ef03ef3
Brandon Tilley authored
Showing with 35 additions and 3 deletions.
  1. +11 −1 lib/private_pub.rb
  2. +24 −2 spec/private_pub_spec.rb
View
12 lib/private_pub.rb
@@ -23,6 +23,11 @@ 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)
@@ -30,7 +35,12 @@ def subscription(options = {})
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
View
26 spec/private_pub_spec.rb
@@ -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

1 comment on commit fbd0209

@meskyanichi

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.
Something went wrong with that request. Please try again.