|
| 1 | +require "spec_helper" |
| 2 | + |
| 3 | +describe PrivatePub do |
| 4 | + before(:each) do |
| 5 | + PrivatePub.reset_config |
| 6 | + end |
| 7 | + |
| 8 | + it "has secret token, server, and key expiration settings" do |
| 9 | + PrivatePub.secret_token = "secret token" |
| 10 | + PrivatePub.secret_token.should == "secret token" |
| 11 | + PrivatePub.server = "http://localhost/" |
| 12 | + PrivatePub.server.should == "http://localhost/" |
| 13 | + PrivatePub.key_expiration = 1000 |
| 14 | + PrivatePub.key_expiration.should == 1000 |
| 15 | + end |
| 16 | + |
| 17 | + it "defaults server to localhost:9292/faye" do |
| 18 | + PrivatePub.server.should == "http://localhost:9292/faye" |
| 19 | + end |
| 20 | + |
| 21 | + it "defaults key_expiration to 1 hour" do |
| 22 | + PrivatePub.key_expiration.should == 60 * 60 |
| 23 | + end |
| 24 | + |
| 25 | + it "defaults subscription timestamp to current time in milliseconds" do |
| 26 | + time = Time.now |
| 27 | + Time.stub!(:now).and_return(time) |
| 28 | + PrivatePub.subscription[:timestamp].should == (time.to_f * 1000).round |
| 29 | + end |
| 30 | + |
| 31 | + it "includes channel and custom time in subscription" do |
| 32 | + subscription = PrivatePub.subscription(:timestamp => 123, :channel => "hello") |
| 33 | + subscription[:timestamp].should == 123 |
| 34 | + subscription[:channel].should == "hello" |
| 35 | + end |
| 36 | + |
| 37 | + it "does a sha1 digest of channel, timestamp, and secret token" do |
| 38 | + PrivatePub.secret_token = "token" |
| 39 | + subscription = PrivatePub.subscription(:timestamp => 123, :channel => "channel") |
| 40 | + subscription[:key].should == Digest::SHA1.hexdigest("tokenchannel123") |
| 41 | + end |
| 42 | + |
| 43 | + it "publishes to server using Net::HTTP" do |
| 44 | + Net::HTTP.should_receive(:post_form).with(URI.parse(PrivatePub.server), "hello world").and_return(:result) |
| 45 | + PrivatePub.publish("hello world").should == :result |
| 46 | + end |
| 47 | +end |
0 commit comments