Permalink
Browse files

Adding basic auth support

  • Loading branch information...
1 parent 6b5e44f commit 7e231609d2f6a42cc155743609685cb4249be871 Matte Noble committed Sep 7, 2011
Showing with 36 additions and 0 deletions.
  1. +11 −0 lib/remotely.rb
  2. +3 −0 lib/remotely/http_methods.rb
  3. +17 −0 spec/remotely/model_spec.rb
  4. +5 −0 spec/remotely_spec.rb
View
@@ -57,10 +57,21 @@ def app(name, url)
apps[name] = url
end
+ # Set the Basic Auth user and password to use when making
+ # requests.
+ #
+ # @param [String] user BasicAuth user
+ # @param [String] password BasicAuth password
+ #
+ def basic_auth(user=nil, password=nil)
+ user and password and @basic_auth = [user, password] or @basic_auth
+ end
+
# Clear all registered apps
#
def reset!
@apps = {}
+ @basic_auth = nil
end
end
end
@@ -57,6 +57,9 @@ def remotely_connection
b.request :url_encoded
b.adapter :net_http
end
+
+ @connection.basic_auth(*Remotely.basic_auth) if Remotely.basic_auth
+ @connection
end
# GET request.
@@ -262,6 +262,23 @@
end
end
+ context "basic auth" do
+ after do
+ Remotely.reset!
+ end
+
+ it "sends Authorization headers when basic auth is configured" do
+ Remotely.configure { basic_auth "user", "password" }
+ Adventure.find(1)
+ a_request(:get, "#{app}/adventures/1").with(headers: {'Authorization' => "Basic dXNlcjpwYXNzd29yZA=="})
+ end
+
+ it "doesn't send Authorization headers when basic auth is not configured" do
+ Adventure.find(1)
+ a_request(:get, "#{app}/adventures/1").with(headers: {})
+ end
+ end
+
it "sets the app it belongs to" do
Adventure.app.should == :adventure_app
end
@@ -10,6 +10,11 @@
Remotely.apps.should include(:configapp)
end
+ it "configures basic auth parameters" do
+ Remotely.configure { basic_auth "user", "password" }
+ Remotely.basic_auth.should == ["user", "password"]
+ end
+
it "is resetable" do
Remotely.configure { app :configapp, "localhost:2222" }
Remotely.reset!

0 comments on commit 7e23160

Please sign in to comment.