Commits

Anonymous committed da1db33

add authentication

Comments (0)

Files changed (2)

lib/mrt/ingest/client.rb

   module Ingest
 
     class Client
-      def initialize(base_uri)
+      def initialize(base_uri, username=nil, password=nil)
         @base_uri = base_uri
+        @username = username
+        @password = password
       end
       
-      def ingest(request)
-        return Response.new(RestClient.post(@base_uri, request.mk_args(), { :multipart => true }))
+      def mk_rest_request(ingest_req)
+        args = { 
+          :method   => :post,
+          :url      => @base_uri,
+          :user     => @username,
+          :password => @password,
+          :payload  => ingest_req.mk_args(),
+          :headers  => { :multipart => true } }.delete_if { |k,v| (v.nil? || v == "") }
+        return RestClient::Request.new(args)
+      end
+
+      def ingest(ingest_req)
+        return Response.new(mk_rest_request(ingest_req).execute())
       end
     end
   end

test/test_client.rb

+require 'rubygems'
+
+require 'checkm'
+require 'fakeweb'
+require 'mocha'
+require 'mrt/ingest'
+require 'shoulda'
+require 'open-uri'
+
+class TestClient < Test::Unit::TestCase
+  context "creating a client" do
+    should "be able to create an ingest client" do
+      client = Mrt::Ingest::Client.new("http://example.org/ingest")
+      assert_instance_of(Mrt::Ingest::Client, client)
+    end
+
+    should "be able to create an ingest client with login credentials" do
+      client = Mrt::Ingest::Client.new("http://example.org/ingest", "me", "secret")
+      assert_instance_of(Mrt::Ingest::Client, client)
+    end
+  end
+  
+  context "ingest clients" do
+    setup do
+      @client = Mrt::Ingest::Client.new("http://example.org/ingest", "me", "secret")
+      @iobject = Mrt::Ingest::IObject.new
+      @ingest_req = @iobject.mk_request("profile", "submitter")
+    end
+
+    should "should create a good rest client request" do
+      rest_req = @client.mk_rest_request(@ingest_req)
+      assert_equal("me", rest_req.user)
+      assert_equal("secret", rest_req.password)
+    end
+  end
+end