Permalink
Browse files

added basic auth support.

  EventMachine::PubSubHubbub.new 'http://example.com/hub',:basic=>['user','pass']
  • Loading branch information...
1 parent 6e0d2de commit 61342ef22562e0ad0674402fc7c6b7f44dbc0c1f Nick Howard committed Jan 10, 2010
Showing with 30 additions and 3 deletions.
  1. +11 −3 lib/pubsubhubbub/client.rb
  2. +19 −0 test/test_client.rb
View
@@ -10,7 +10,15 @@ class PubSubHubbub
HEADERS = {"User-Agent" => "PubSubHubbub Ruby", "Content-Type" => "application/x-www-form-urlencoded"}
- def initialize(hub)
+ # Options
+ # :basic -- to use basic auth with the hub. expects ['user','pass']
+ def initialize(hub,options={})
+ @basic_auth= options.delete(:basic)
+ @headers = if @basic_auth
+ HEADERS.merge 'authorization' => @basic_auth
+ else
+ HEADERS
+ end
@hub = hub.kind_of?(URI) ? hub : URI::parse(hub)
end
@@ -19,7 +27,7 @@ def publish(*feeds)
{'hub.url' => feed, 'hub.mode' => 'publish'}.to_params
end.join("&")
- r = EventMachine::HttpRequest.new(@hub).post :body => data, :head => HEADERS
+ r = EventMachine::HttpRequest.new(@hub).post :body => data, :head => @headers
r.callback {
if r.response_header.status == 204
succeed r
@@ -45,7 +53,7 @@ def command(cmd, feed, callback, options)
options['hub.verify'] ||= "sync"
params = {'hub.topic' => feed, 'hub.mode' => cmd, 'hub.callback' => callback}.merge(options).to_params
- r = EventMachine::HttpRequest.new(@hub).post :body => params, :head => HEADERS
+ r = EventMachine::HttpRequest.new(@hub).post :body => params, :head => @headers
r.callback {
if r.response_header.status == 204
View
@@ -19,6 +19,25 @@ def failed
fail
end
+ describe 'using basic auth' do
+ it "should accept basic auth options" do
+
+ EventMachine::HttpRequest.should_receive(:new).and_return(req = mock('request',:null_object=>true))
+ req.should_receive(:post).with(
+ :body=>anything,
+ :head=>hash_including(
+ 'authorization'=> ['username','password']
+ )
+ ).and_return(req)
+ req.stub!(:callback) {EventMachine.stop}
+
+ timed_event_machine_run {
+ pub = EventMachine::PubSubHubbub.new 'http://example.com/hub',:basic=>['username','password']
+ pub.subscribe 'http://example.com/feed','http://example.com/callback'
+ }
+ end
+ end
+
it "should publish single feed to hub" do
timed_event_machine_run {
pub = EventMachine::PubSubHubbub.new('http://pubsubhubbub.appspot.com/publish').publish "http://www.test.com/"

0 comments on commit 61342ef

Please sign in to comment.