Permalink
Browse files

merging basic-auth support for PSHB client

Merge branch 'master' of git://github.com/baroquebobcat/PubSubHubbub

Conflicts:
	lib/pubsubhubbub/client.rb
  • Loading branch information...
2 parents d24f17a + 61342ef commit 1d7b8bf4548d16b7cec17ebfb1eafdc70ddcf2ec @igrigorik committed Jun 19, 2010
Showing with 53 additions and 23 deletions.
  1. +11 −0 Rakefile
  2. +22 −22 lib/pubsubhubbub/client.rb
  3. +20 −1 test/test_client.rb
View
@@ -18,3 +18,14 @@ begin
rescue LoadError
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
end
+
+
+require 'spec/rake/spectask'
+
+desc "Run all examples"
+Spec::Rake::SpecTask.new('spec') do |t|
+ t.spec_files = FileList['test/**/*.rb']
+end
+
+
+task :default => :spec
View
@@ -10,7 +10,8 @@ class PubSubHubbub
HEADERS = {"User-Agent" => "PubSubHubbub Ruby", "Content-Type" => "application/x-www-form-urlencoded"}
- def initialize(hub)
+ def initialize(hub, options={})
+ @headers = HEADERS.merge(options[:head]) if options[:head]
@hub = hub.kind_of?(URI) ? hub : URI::parse(hub)
end
@@ -19,7 +20,7 @@ def publish(*feeds)
{'hub.url' => feed, 'hub.mode' => 'publish'}.to_params
end.join("&")
- request(:body => data, :head => HEADERS)
+ request(:body => data, :head => @headers)
end
# These command will work only if the callback URL supports confirmation.
@@ -28,31 +29,30 @@ def unsubscribe(feed, callback, options = {}); command('unsubscribe', feed, call
private
- def command(cmd, feed, callback, options)
- options['hub.verify'] ||= "sync"
- params = {'hub.topic' => feed, 'hub.mode' => cmd, 'hub.callback' => callback}.merge(options).to_params
+ def command(cmd, feed, callback, options)
+ options['hub.verify'] ||= "sync"
+ params = {'hub.topic' => feed, 'hub.mode' => cmd, 'hub.callback' => callback}.merge(options).to_params
- request(:body => params, :head => HEADERS)
- end
+ request(:body => params, :head => @headers)
+ end
- def request(opts)
- r = http_request(opts)
+ def request(opts)
+ r = http_request(opts)
+ r.errback { fail }
- r.callback do
- if r.response_header.status == 204
- succeed r
- else
- fail r
- end
+ r.callback do
+ if r.response_header.status == 204
+ succeed r
+ else
+ fail r
end
-
- r.errback { fail }
- r
-
end
- def http_request(opts)
- EventMachine::HttpRequest.new(@hub).post opts
- end
+ r
+ end
+
+ def http_request(opts)
+ EventMachine::HttpRequest.new(@hub).post opts
+ end
end
end
View
@@ -10,6 +10,25 @@ def failed
fail
end
+ describe "protected hub" 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',:head => {'authorization' => ['username','password']}
+ pub.subscribe 'http://example.com/feed', 'http://example.com/callback'
+ }
+ end
+ end
+
it "should publish single feed to hub" do
EventMachine.run {
pub = EventMachine::PubSubHubbub.new('http://pubsubhubbub.appspot.com/publish').publish "http://www.test.com/"
@@ -56,7 +75,7 @@ def failed
sub.response_header.status.should == 204
EventMachine.stop
}
- }
+ }
end
end

0 comments on commit 1d7b8bf

Please sign in to comment.