sevenwire-rest-client 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +151 -0
 - data/Rakefile +48 -0
 - data/VERSION.yml +4 -0
 - data/bin/restclient +82 -0
 - data/lib/rest_client.rb +2 -0
 - data/lib/restclient/exceptions.rb +34 -0
 - data/lib/restclient/mixin/response.rb +43 -0
 - data/lib/restclient/raw_response.rb +30 -0
 - data/lib/restclient/request.rb +232 -0
 - data/lib/restclient/resource.rb +146 -0
 - data/lib/restclient/response.rb +20 -0
 - data/lib/restclient.rb +93 -0
 - data/spec/base.rb +4 -0
 - data/spec/exceptions_spec.rb +12 -0
 - data/spec/mixin/response_spec.rb +46 -0
 - data/spec/raw_response_spec.rb +17 -0
 - data/spec/request_spec.rb +442 -0
 - data/spec/resource_spec.rb +75 -0
 - data/spec/response_spec.rb +16 -0
 - data/spec/restclient_spec.rb +53 -0
 - metadata +79 -0
 
| 
         @@ -0,0 +1,442 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require File.dirname(__FILE__) + '/base'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe RestClient::Request do
         
     | 
| 
      
 4 
     | 
    
         
            +
              before do
         
     | 
| 
      
 5 
     | 
    
         
            +
                @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
                @uri = mock("uri")
         
     | 
| 
      
 8 
     | 
    
         
            +
                @uri.stub!(:request_uri).and_return('/resource')
         
     | 
| 
      
 9 
     | 
    
         
            +
                @uri.stub!(:host).and_return('some')
         
     | 
| 
      
 10 
     | 
    
         
            +
                @uri.stub!(:port).and_return(80)
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                @net = mock("net::http base")
         
     | 
| 
      
 13 
     | 
    
         
            +
                @http = mock("net::http connection")
         
     | 
| 
      
 14 
     | 
    
         
            +
                Net::HTTP.stub!(:new).and_return(@net)
         
     | 
| 
      
 15 
     | 
    
         
            +
                @net.stub!(:start).and_yield(@http)
         
     | 
| 
      
 16 
     | 
    
         
            +
                @net.stub!(:use_ssl=)
         
     | 
| 
      
 17 
     | 
    
         
            +
                @net.stub!(:verify_mode=)
         
     | 
| 
      
 18 
     | 
    
         
            +
              end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
              it "requests xml mimetype" do
         
     | 
| 
      
 21 
     | 
    
         
            +
                @request.default_headers[:accept].should == 'application/xml'
         
     | 
| 
      
 22 
     | 
    
         
            +
              end
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
              it "decodes an uncompressed result body by passing it straight through" do
         
     | 
| 
      
 25 
     | 
    
         
            +
                @request.decode(nil, 'xyz').should == 'xyz'
         
     | 
| 
      
 26 
     | 
    
         
            +
              end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
              it "decodes a gzip body" do
         
     | 
| 
      
 29 
     | 
    
         
            +
                @request.decode('gzip', "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000").should == "i'm gziped\n"
         
     | 
| 
      
 30 
     | 
    
         
            +
              end
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
              it "ingores gzip for empty bodies" do
         
     | 
| 
      
 33 
     | 
    
         
            +
                @request.decode('gzip', '').should be_empty
         
     | 
| 
      
 34 
     | 
    
         
            +
              end
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
              it "decodes a deflated body" do
         
     | 
| 
      
 37 
     | 
    
         
            +
                @request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363").should == "some deflated text"
         
     | 
| 
      
 38 
     | 
    
         
            +
              end
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
              it "processes a successful result" do
         
     | 
| 
      
 41 
     | 
    
         
            +
                res = mock("result")
         
     | 
| 
      
 42 
     | 
    
         
            +
                res.stub!(:code).and_return("200")
         
     | 
| 
      
 43 
     | 
    
         
            +
                res.stub!(:body).and_return('body')
         
     | 
| 
      
 44 
     | 
    
         
            +
                res.stub!(:[]).with('content-encoding').and_return(nil)
         
     | 
| 
      
 45 
     | 
    
         
            +
                @request.process_result(res).should == 'body'
         
     | 
| 
      
 46 
     | 
    
         
            +
              end
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
              it "doesn't classify successful requests as failed" do
         
     | 
| 
      
 49 
     | 
    
         
            +
                203.upto(206) do |code|
         
     | 
| 
      
 50 
     | 
    
         
            +
                  res = mock("result")
         
     | 
| 
      
 51 
     | 
    
         
            +
                  res.stub!(:code).and_return(code.to_s)
         
     | 
| 
      
 52 
     | 
    
         
            +
                  res.stub!(:body).and_return("")
         
     | 
| 
      
 53 
     | 
    
         
            +
                  res.stub!(:[]).with('content-encoding').and_return(nil)
         
     | 
| 
      
 54 
     | 
    
         
            +
                  @request.process_result(res).should be_empty
         
     | 
| 
      
 55 
     | 
    
         
            +
                end
         
     | 
| 
      
 56 
     | 
    
         
            +
              end
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
      
 58 
     | 
    
         
            +
              it "parses a url into a URI object" do
         
     | 
| 
      
 59 
     | 
    
         
            +
                URI.should_receive(:parse).with('http://example.com/resource')
         
     | 
| 
      
 60 
     | 
    
         
            +
                @request.parse_url('http://example.com/resource')
         
     | 
| 
      
 61 
     | 
    
         
            +
              end
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
              it "adds http:// to the front of resources specified in the syntax example.com/resource" do
         
     | 
| 
      
 64 
     | 
    
         
            +
                URI.should_receive(:parse).with('http://example.com/resource')
         
     | 
| 
      
 65 
     | 
    
         
            +
                @request.parse_url('example.com/resource')
         
     | 
| 
      
 66 
     | 
    
         
            +
              end
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
              it "extracts the username and password when parsing http://user:password@example.com/" do
         
     | 
| 
      
 69 
     | 
    
         
            +
                URI.stub!(:parse).and_return(mock('uri', :user => 'joe', :password => 'pass1'))
         
     | 
| 
      
 70 
     | 
    
         
            +
                @request.parse_url_with_auth('http://joe:pass1@example.com/resource')
         
     | 
| 
      
 71 
     | 
    
         
            +
                @request.user.should == 'joe'
         
     | 
| 
      
 72 
     | 
    
         
            +
                @request.password.should == 'pass1'
         
     | 
| 
      
 73 
     | 
    
         
            +
              end
         
     | 
| 
      
 74 
     | 
    
         
            +
             
     | 
| 
      
 75 
     | 
    
         
            +
              it "doesn't overwrite user and password (which may have already been set by the Resource constructor) if there is no user/password in the url" do
         
     | 
| 
      
 76 
     | 
    
         
            +
                URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
         
     | 
| 
      
 77 
     | 
    
         
            +
                @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :user => 'beth', :password => 'pass2')
         
     | 
| 
      
 78 
     | 
    
         
            +
                @request.parse_url_with_auth('http://example.com/resource')
         
     | 
| 
      
 79 
     | 
    
         
            +
                @request.user.should == 'beth'
         
     | 
| 
      
 80 
     | 
    
         
            +
                @request.password.should == 'pass2'
         
     | 
| 
      
 81 
     | 
    
         
            +
              end
         
     | 
| 
      
 82 
     | 
    
         
            +
             
     | 
| 
      
 83 
     | 
    
         
            +
              it "correctly formats cookies provided to the constructor" do
         
     | 
| 
      
 84 
     | 
    
         
            +
                URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
         
     | 
| 
      
 85 
     | 
    
         
            +
                @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:session_id => '1' })
         
     | 
| 
      
 86 
     | 
    
         
            +
                @request.should_receive(:default_headers).and_return({'foo' => 'bar'})
         
     | 
| 
      
 87 
     | 
    
         
            +
                headers = @request.make_headers({}).should == { 'Foo' => 'bar', 'Cookie' => 'session_id=1'}
         
     | 
| 
      
 88 
     | 
    
         
            +
              end
         
     | 
| 
      
 89 
     | 
    
         
            +
             
     | 
| 
      
 90 
     | 
    
         
            +
              it "determines the Net::HTTP class to instantiate by the method name" do
         
     | 
| 
      
 91 
     | 
    
         
            +
                @request.net_http_request_class(:put).should == Net::HTTP::Put
         
     | 
| 
      
 92 
     | 
    
         
            +
              end
         
     | 
| 
      
 93 
     | 
    
         
            +
             
     | 
| 
      
 94 
     | 
    
         
            +
              it "merges user headers with the default headers" do
         
     | 
| 
      
 95 
     | 
    
         
            +
                @request.should_receive(:default_headers).and_return({ '1' => '2' })
         
     | 
| 
      
 96 
     | 
    
         
            +
                @request.make_headers('3' => '4').should == { '1' => '2', '3' => '4' }
         
     | 
| 
      
 97 
     | 
    
         
            +
              end
         
     | 
| 
      
 98 
     | 
    
         
            +
             
     | 
| 
      
 99 
     | 
    
         
            +
              it "prefers the user header when the same header exists in the defaults" do
         
     | 
| 
      
 100 
     | 
    
         
            +
                @request.should_receive(:default_headers).and_return({ '1' => '2' })
         
     | 
| 
      
 101 
     | 
    
         
            +
                @request.make_headers('1' => '3').should == { '1' => '3' }
         
     | 
| 
      
 102 
     | 
    
         
            +
              end
         
     | 
| 
      
 103 
     | 
    
         
            +
             
     | 
| 
      
 104 
     | 
    
         
            +
              it "converts header symbols from :content_type to 'Content-type'" do
         
     | 
| 
      
 105 
     | 
    
         
            +
                @request.should_receive(:default_headers).and_return({})
         
     | 
| 
      
 106 
     | 
    
         
            +
                @request.make_headers(:content_type => 'abc').should == { 'Content-type' => 'abc' }
         
     | 
| 
      
 107 
     | 
    
         
            +
              end
         
     | 
| 
      
 108 
     | 
    
         
            +
             
     | 
| 
      
 109 
     | 
    
         
            +
              it "converts header values to strings" do
         
     | 
| 
      
 110 
     | 
    
         
            +
                @request.make_headers('A' => 1)['A'].should == '1'
         
     | 
| 
      
 111 
     | 
    
         
            +
              end
         
     | 
| 
      
 112 
     | 
    
         
            +
             
     | 
| 
      
 113 
     | 
    
         
            +
              it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
         
     | 
| 
      
 114 
     | 
    
         
            +
                @request.should_receive(:parse_url_with_auth).with('http://some/resource').and_return(@uri)
         
     | 
| 
      
 115 
     | 
    
         
            +
                klass = mock("net:http class")
         
     | 
| 
      
 116 
     | 
    
         
            +
                @request.should_receive(:net_http_request_class).with(:put).and_return(klass)
         
     | 
| 
      
 117 
     | 
    
         
            +
                klass.should_receive(:new).and_return('result')
         
     | 
| 
      
 118 
     | 
    
         
            +
                @request.should_receive(:transmit).with(@uri, 'result', 'payload')
         
     | 
| 
      
 119 
     | 
    
         
            +
                @request.execute_inner
         
     | 
| 
      
 120 
     | 
    
         
            +
              end
         
     | 
| 
      
 121 
     | 
    
         
            +
             
     | 
| 
      
 122 
     | 
    
         
            +
              it "transmits the request with Net::HTTP" do
         
     | 
| 
      
 123 
     | 
    
         
            +
                @http.should_receive(:request).with('req', 'payload')
         
     | 
| 
      
 124 
     | 
    
         
            +
                @request.should_receive(:process_result)
         
     | 
| 
      
 125 
     | 
    
         
            +
                @request.should_receive(:response_log)
         
     | 
| 
      
 126 
     | 
    
         
            +
                @request.transmit(@uri, 'req', 'payload')
         
     | 
| 
      
 127 
     | 
    
         
            +
              end
         
     | 
| 
      
 128 
     | 
    
         
            +
             
     | 
| 
      
 129 
     | 
    
         
            +
              it "uses SSL when the URI refers to a https address" do
         
     | 
| 
      
 130 
     | 
    
         
            +
                @uri.stub!(:is_a?).with(URI::HTTPS).and_return(true)
         
     | 
| 
      
 131 
     | 
    
         
            +
                @net.should_receive(:use_ssl=).with(true)
         
     | 
| 
      
 132 
     | 
    
         
            +
                @http.stub!(:request)
         
     | 
| 
      
 133 
     | 
    
         
            +
                @request.stub!(:process_result)
         
     | 
| 
      
 134 
     | 
    
         
            +
                @request.stub!(:response_log)
         
     | 
| 
      
 135 
     | 
    
         
            +
                @request.transmit(@uri, 'req', 'payload')
         
     | 
| 
      
 136 
     | 
    
         
            +
              end
         
     | 
| 
      
 137 
     | 
    
         
            +
             
     | 
| 
      
 138 
     | 
    
         
            +
              it "sends nil payloads" do
         
     | 
| 
      
 139 
     | 
    
         
            +
                @http.should_receive(:request).with('req', nil)
         
     | 
| 
      
 140 
     | 
    
         
            +
                @request.should_receive(:process_result)
         
     | 
| 
      
 141 
     | 
    
         
            +
                @request.stub!(:response_log)
         
     | 
| 
      
 142 
     | 
    
         
            +
                @request.transmit(@uri, 'req', nil)
         
     | 
| 
      
 143 
     | 
    
         
            +
              end
         
     | 
| 
      
 144 
     | 
    
         
            +
             
     | 
| 
      
 145 
     | 
    
         
            +
              it "passes non-hash payloads straight through" do
         
     | 
| 
      
 146 
     | 
    
         
            +
                @request.process_payload("x").should == "x"
         
     | 
| 
      
 147 
     | 
    
         
            +
              end
         
     | 
| 
      
 148 
     | 
    
         
            +
             
     | 
| 
      
 149 
     | 
    
         
            +
              it "converts a hash payload to urlencoded data" do
         
     | 
| 
      
 150 
     | 
    
         
            +
                @request.process_payload(:a => 'b c+d').should == "a=b%20c%2Bd"
         
     | 
| 
      
 151 
     | 
    
         
            +
              end
         
     | 
| 
      
 152 
     | 
    
         
            +
             
     | 
| 
      
 153 
     | 
    
         
            +
              it "accepts nested hashes in payload" do
         
     | 
| 
      
 154 
     | 
    
         
            +
                payload = @request.process_payload(:user => { :name => 'joe', :location => { :country => 'USA', :state => 'CA' }})
         
     | 
| 
      
 155 
     | 
    
         
            +
                payload.should include('user[name]=joe')
         
     | 
| 
      
 156 
     | 
    
         
            +
                payload.should include('user[location][country]=USA')
         
     | 
| 
      
 157 
     | 
    
         
            +
                payload.should include('user[location][state]=CA')
         
     | 
| 
      
 158 
     | 
    
         
            +
              end
         
     | 
| 
      
 159 
     | 
    
         
            +
             
     | 
| 
      
 160 
     | 
    
         
            +
              it "set urlencoded content_type header on hash payloads" do
         
     | 
| 
      
 161 
     | 
    
         
            +
                @request.process_payload(:a => 1)
         
     | 
| 
      
 162 
     | 
    
         
            +
                @request.headers[:content_type].should == 'application/x-www-form-urlencoded'
         
     | 
| 
      
 163 
     | 
    
         
            +
              end
         
     | 
| 
      
 164 
     | 
    
         
            +
             
     | 
| 
      
 165 
     | 
    
         
            +
              it "sets up the credentials prior to the request" do
         
     | 
| 
      
 166 
     | 
    
         
            +
                @http.stub!(:request)
         
     | 
| 
      
 167 
     | 
    
         
            +
                @request.stub!(:process_result)
         
     | 
| 
      
 168 
     | 
    
         
            +
                @request.stub!(:response_log)
         
     | 
| 
      
 169 
     | 
    
         
            +
             
     | 
| 
      
 170 
     | 
    
         
            +
                @request.stub!(:user).and_return('joe')
         
     | 
| 
      
 171 
     | 
    
         
            +
                @request.stub!(:password).and_return('mypass')
         
     | 
| 
      
 172 
     | 
    
         
            +
                @request.should_receive(:setup_credentials).with('req')
         
     | 
| 
      
 173 
     | 
    
         
            +
             
     | 
| 
      
 174 
     | 
    
         
            +
                @request.transmit(@uri, 'req', nil)
         
     | 
| 
      
 175 
     | 
    
         
            +
              end
         
     | 
| 
      
 176 
     | 
    
         
            +
             
     | 
| 
      
 177 
     | 
    
         
            +
              it "does not attempt to send any credentials if user is nil" do
         
     | 
| 
      
 178 
     | 
    
         
            +
                @request.stub!(:user).and_return(nil)
         
     | 
| 
      
 179 
     | 
    
         
            +
                req = mock("request")
         
     | 
| 
      
 180 
     | 
    
         
            +
                req.should_not_receive(:basic_auth)
         
     | 
| 
      
 181 
     | 
    
         
            +
                @request.setup_credentials(req)
         
     | 
| 
      
 182 
     | 
    
         
            +
              end
         
     | 
| 
      
 183 
     | 
    
         
            +
             
     | 
| 
      
 184 
     | 
    
         
            +
              it "setup credentials when there's a user" do
         
     | 
| 
      
 185 
     | 
    
         
            +
                @request.stub!(:user).and_return('joe')
         
     | 
| 
      
 186 
     | 
    
         
            +
                @request.stub!(:password).and_return('mypass')
         
     | 
| 
      
 187 
     | 
    
         
            +
                req = mock("request")
         
     | 
| 
      
 188 
     | 
    
         
            +
                req.should_receive(:basic_auth).with('joe', 'mypass')
         
     | 
| 
      
 189 
     | 
    
         
            +
                @request.setup_credentials(req)
         
     | 
| 
      
 190 
     | 
    
         
            +
              end
         
     | 
| 
      
 191 
     | 
    
         
            +
             
     | 
| 
      
 192 
     | 
    
         
            +
              it "catches EOFError and shows the more informative ServerBrokeConnection" do
         
     | 
| 
      
 193 
     | 
    
         
            +
                @http.stub!(:request).and_raise(EOFError)
         
     | 
| 
      
 194 
     | 
    
         
            +
                lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::ServerBrokeConnection)
         
     | 
| 
      
 195 
     | 
    
         
            +
              end
         
     | 
| 
      
 196 
     | 
    
         
            +
             
     | 
| 
      
 197 
     | 
    
         
            +
              it "execute calls execute_inner" do
         
     | 
| 
      
 198 
     | 
    
         
            +
                @request.should_receive(:execute_inner)
         
     | 
| 
      
 199 
     | 
    
         
            +
                @request.execute
         
     | 
| 
      
 200 
     | 
    
         
            +
              end
         
     | 
| 
      
 201 
     | 
    
         
            +
             
     | 
| 
      
 202 
     | 
    
         
            +
              it "class method execute wraps constructor" do
         
     | 
| 
      
 203 
     | 
    
         
            +
                req = mock("rest request")
         
     | 
| 
      
 204 
     | 
    
         
            +
                RestClient::Request.should_receive(:new).with(1 => 2).and_return(req)
         
     | 
| 
      
 205 
     | 
    
         
            +
                req.should_receive(:execute)
         
     | 
| 
      
 206 
     | 
    
         
            +
                RestClient::Request.execute(1 => 2)
         
     | 
| 
      
 207 
     | 
    
         
            +
              end
         
     | 
| 
      
 208 
     | 
    
         
            +
             
     | 
| 
      
 209 
     | 
    
         
            +
              it "raises a Redirect with the new location when the response is in the 30x range" do
         
     | 
| 
      
 210 
     | 
    
         
            +
                res = mock('response', :code => '301', :header => { 'Location' => 'http://new/resource' })
         
     | 
| 
      
 211 
     | 
    
         
            +
                @request.process_result(res).should == 'Redirected to: http://new/resource'
         
     | 
| 
      
 212 
     | 
    
         
            +
              end
         
     | 
| 
      
 213 
     | 
    
         
            +
             
     | 
| 
      
 214 
     | 
    
         
            +
              it "handles redirects with relative paths" do
         
     | 
| 
      
 215 
     | 
    
         
            +
                res = mock('response', :code => '301', :header => { 'Location' => 'index' })
         
     | 
| 
      
 216 
     | 
    
         
            +
                @request.process_result(res).should == 'Redirected to: http://some/index'
         
     | 
| 
      
 217 
     | 
    
         
            +
              end
         
     | 
| 
      
 218 
     | 
    
         
            +
             
     | 
| 
      
 219 
     | 
    
         
            +
              it "handles redirects with absolute paths" do
         
     | 
| 
      
 220 
     | 
    
         
            +
                @request.instance_variable_set('@url', 'http://some/place/else')
         
     | 
| 
      
 221 
     | 
    
         
            +
                res = mock('response', :code => '301', :header => { 'Location' => '/index' })
         
     | 
| 
      
 222 
     | 
    
         
            +
                @request.process_result(res).should == 'Redirected to: http://some/index'
         
     | 
| 
      
 223 
     | 
    
         
            +
              end
         
     | 
| 
      
 224 
     | 
    
         
            +
             
     | 
| 
      
 225 
     | 
    
         
            +
              it "raises Unauthorized when the response is 401" do
         
     | 
| 
      
 226 
     | 
    
         
            +
                res = mock('response', :code => '401')
         
     | 
| 
      
 227 
     | 
    
         
            +
                @request.process_result(res).should == "Unauthorized"
         
     | 
| 
      
 228 
     | 
    
         
            +
              end
         
     | 
| 
      
 229 
     | 
    
         
            +
             
     | 
| 
      
 230 
     | 
    
         
            +
              it "raises ResourceNotFound when the response is 404" do
         
     | 
| 
      
 231 
     | 
    
         
            +
                res = mock('response', :code => '404')
         
     | 
| 
      
 232 
     | 
    
         
            +
                @request.process_result(res).should == 'Not Found'
         
     | 
| 
      
 233 
     | 
    
         
            +
              end
         
     | 
| 
      
 234 
     | 
    
         
            +
             
     | 
| 
      
 235 
     | 
    
         
            +
              it "raises RequestFailed otherwise" do
         
     | 
| 
      
 236 
     | 
    
         
            +
                res = mock('response', :code => '500')
         
     | 
| 
      
 237 
     | 
    
         
            +
                @request.process_result(res).should == "Failed"
         
     | 
| 
      
 238 
     | 
    
         
            +
              end
         
     | 
| 
      
 239 
     | 
    
         
            +
             
     | 
| 
      
 240 
     | 
    
         
            +
              it "creates a proxy class if a proxy url is given" do
         
     | 
| 
      
 241 
     | 
    
         
            +
                RestClient.stub!(:proxy).and_return("http://example.com/")
         
     | 
| 
      
 242 
     | 
    
         
            +
                @request.net_http_class.should include(Net::HTTP::ProxyDelta)
         
     | 
| 
      
 243 
     | 
    
         
            +
              end
         
     | 
| 
      
 244 
     | 
    
         
            +
             
     | 
| 
      
 245 
     | 
    
         
            +
              it "creates a non-proxy class if a proxy url is not given" do
         
     | 
| 
      
 246 
     | 
    
         
            +
                @request.net_http_class.should_not include(Net::HTTP::ProxyDelta)
         
     | 
| 
      
 247 
     | 
    
         
            +
              end
         
     | 
| 
      
 248 
     | 
    
         
            +
             
     | 
| 
      
 249 
     | 
    
         
            +
              it "logs a get request" do
         
     | 
| 
      
 250 
     | 
    
         
            +
                RestClient::Request.new(:method => :get, :url => 'http://url').request_log.should ==
         
     | 
| 
      
 251 
     | 
    
         
            +
                'RestClient.get "http://url"'
         
     | 
| 
      
 252 
     | 
    
         
            +
              end
         
     | 
| 
      
 253 
     | 
    
         
            +
             
     | 
| 
      
 254 
     | 
    
         
            +
              it "logs a post request with a small payload" do
         
     | 
| 
      
 255 
     | 
    
         
            +
                RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').request_log.should ==
         
     | 
| 
      
 256 
     | 
    
         
            +
                'RestClient.post "http://url", "foo"'
         
     | 
| 
      
 257 
     | 
    
         
            +
              end
         
     | 
| 
      
 258 
     | 
    
         
            +
             
     | 
| 
      
 259 
     | 
    
         
            +
              it "logs a post request with a large payload" do
         
     | 
| 
      
 260 
     | 
    
         
            +
                RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).request_log.should ==
         
     | 
| 
      
 261 
     | 
    
         
            +
                'RestClient.post "http://url", "(1000 byte payload)"'
         
     | 
| 
      
 262 
     | 
    
         
            +
              end
         
     | 
| 
      
 263 
     | 
    
         
            +
             
     | 
| 
      
 264 
     | 
    
         
            +
              it "logs input headers as a hash" do
         
     | 
| 
      
 265 
     | 
    
         
            +
                RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' }).request_log.should ==
         
     | 
| 
      
 266 
     | 
    
         
            +
                'RestClient.get "http://url", :accept=>"text/plain"'
         
     | 
| 
      
 267 
     | 
    
         
            +
              end
         
     | 
| 
      
 268 
     | 
    
         
            +
             
     | 
| 
      
 269 
     | 
    
         
            +
              it "logs a response including the status code, content type, and result body size in bytes" do
         
     | 
| 
      
 270 
     | 
    
         
            +
                res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
         
     | 
| 
      
 271 
     | 
    
         
            +
                res.stub!(:[]).with('Content-type').and_return('text/html')
         
     | 
| 
      
 272 
     | 
    
         
            +
                @request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
         
     | 
| 
      
 273 
     | 
    
         
            +
              end
         
     | 
| 
      
 274 
     | 
    
         
            +
             
     | 
| 
      
 275 
     | 
    
         
            +
              it "logs a response with a nil Content-type" do
         
     | 
| 
      
 276 
     | 
    
         
            +
                res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
         
     | 
| 
      
 277 
     | 
    
         
            +
                res.stub!(:[]).with('Content-type').and_return(nil)
         
     | 
| 
      
 278 
     | 
    
         
            +
                @request.response_log(res).should == "# => 200 OK |  4 bytes"
         
     | 
| 
      
 279 
     | 
    
         
            +
              end
         
     | 
| 
      
 280 
     | 
    
         
            +
             
     | 
| 
      
 281 
     | 
    
         
            +
              it "strips the charset from the response content type" do
         
     | 
| 
      
 282 
     | 
    
         
            +
                res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
         
     | 
| 
      
 283 
     | 
    
         
            +
                res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
         
     | 
| 
      
 284 
     | 
    
         
            +
                @request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
         
     | 
| 
      
 285 
     | 
    
         
            +
              end
         
     | 
| 
      
 286 
     | 
    
         
            +
             
     | 
| 
      
 287 
     | 
    
         
            +
              it "displays the log to stdout" do
         
     | 
| 
      
 288 
     | 
    
         
            +
                RestClient.stub!(:log).and_return('stdout')
         
     | 
| 
      
 289 
     | 
    
         
            +
                STDOUT.should_receive(:puts).with('xyz')
         
     | 
| 
      
 290 
     | 
    
         
            +
                @request.display_log('xyz')
         
     | 
| 
      
 291 
     | 
    
         
            +
              end
         
     | 
| 
      
 292 
     | 
    
         
            +
             
     | 
| 
      
 293 
     | 
    
         
            +
              it "displays the log to stderr" do
         
     | 
| 
      
 294 
     | 
    
         
            +
                RestClient.stub!(:log).and_return('stderr')
         
     | 
| 
      
 295 
     | 
    
         
            +
                STDERR.should_receive(:puts).with('xyz')
         
     | 
| 
      
 296 
     | 
    
         
            +
                @request.display_log('xyz')
         
     | 
| 
      
 297 
     | 
    
         
            +
              end
         
     | 
| 
      
 298 
     | 
    
         
            +
             
     | 
| 
      
 299 
     | 
    
         
            +
              it "append the log to the requested filename" do
         
     | 
| 
      
 300 
     | 
    
         
            +
                RestClient.stub!(:log).and_return('/tmp/restclient.log')
         
     | 
| 
      
 301 
     | 
    
         
            +
                f = mock('file handle')
         
     | 
| 
      
 302 
     | 
    
         
            +
                File.should_receive(:open).with('/tmp/restclient.log', 'a').and_yield(f)
         
     | 
| 
      
 303 
     | 
    
         
            +
                f.should_receive(:puts).with('xyz')
         
     | 
| 
      
 304 
     | 
    
         
            +
                @request.display_log('xyz')
         
     | 
| 
      
 305 
     | 
    
         
            +
              end
         
     | 
| 
      
 306 
     | 
    
         
            +
             
     | 
| 
      
 307 
     | 
    
         
            +
              it "set read_timeout" do
         
     | 
| 
      
 308 
     | 
    
         
            +
                @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123)
         
     | 
| 
      
 309 
     | 
    
         
            +
                @http.stub!(:request)
         
     | 
| 
      
 310 
     | 
    
         
            +
                @request.stub!(:process_result)
         
     | 
| 
      
 311 
     | 
    
         
            +
                @request.stub!(:response_log)
         
     | 
| 
      
 312 
     | 
    
         
            +
             
     | 
| 
      
 313 
     | 
    
         
            +
                @net.should_receive(:read_timeout=).with(123)
         
     | 
| 
      
 314 
     | 
    
         
            +
             
     | 
| 
      
 315 
     | 
    
         
            +
                @request.transmit(@uri, 'req', nil)
         
     | 
| 
      
 316 
     | 
    
         
            +
              end
         
     | 
| 
      
 317 
     | 
    
         
            +
             
     | 
| 
      
 318 
     | 
    
         
            +
              it "set open_timeout" do
         
     | 
| 
      
 319 
     | 
    
         
            +
                @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :open_timeout => 123)
         
     | 
| 
      
 320 
     | 
    
         
            +
                @http.stub!(:request)
         
     | 
| 
      
 321 
     | 
    
         
            +
                @request.stub!(:process_result)
         
     | 
| 
      
 322 
     | 
    
         
            +
                @request.stub!(:response_log)
         
     | 
| 
      
 323 
     | 
    
         
            +
             
     | 
| 
      
 324 
     | 
    
         
            +
                @net.should_receive(:open_timeout=).with(123)
         
     | 
| 
      
 325 
     | 
    
         
            +
             
     | 
| 
      
 326 
     | 
    
         
            +
                @request.transmit(@uri, 'req', nil)
         
     | 
| 
      
 327 
     | 
    
         
            +
              end
         
     | 
| 
      
 328 
     | 
    
         
            +
             
     | 
| 
      
 329 
     | 
    
         
            +
              it "should default to not verifying ssl certificates" do
         
     | 
| 
      
 330 
     | 
    
         
            +
                @request.verify_ssl.should == false
         
     | 
| 
      
 331 
     | 
    
         
            +
              end
         
     | 
| 
      
 332 
     | 
    
         
            +
             
     | 
| 
      
 333 
     | 
    
         
            +
              it "should set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is false" do
         
     | 
| 
      
 334 
     | 
    
         
            +
                @net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
         
     | 
| 
      
 335 
     | 
    
         
            +
                @http.stub!(:request)
         
     | 
| 
      
 336 
     | 
    
         
            +
                @request.stub!(:process_result)
         
     | 
| 
      
 337 
     | 
    
         
            +
                @request.stub!(:response_log)
         
     | 
| 
      
 338 
     | 
    
         
            +
                @request.transmit(@uri, 'req', 'payload')
         
     | 
| 
      
 339 
     | 
    
         
            +
              end
         
     | 
| 
      
 340 
     | 
    
         
            +
             
     | 
| 
      
 341 
     | 
    
         
            +
              it "should not set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is true" do
         
     | 
| 
      
 342 
     | 
    
         
            +
                @request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true)
         
     | 
| 
      
 343 
     | 
    
         
            +
                @net.should_not_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
         
     | 
| 
      
 344 
     | 
    
         
            +
                @http.stub!(:request)
         
     | 
| 
      
 345 
     | 
    
         
            +
                @request.stub!(:process_result)
         
     | 
| 
      
 346 
     | 
    
         
            +
                @request.stub!(:response_log)
         
     | 
| 
      
 347 
     | 
    
         
            +
                @request.transmit(@uri, 'req', 'payload')
         
     | 
| 
      
 348 
     | 
    
         
            +
              end
         
     | 
| 
      
 349 
     | 
    
         
            +
             
     | 
| 
      
 350 
     | 
    
         
            +
              it "should default to not having an ssl_client_cert" do
         
     | 
| 
      
 351 
     | 
    
         
            +
                @request.ssl_client_cert.should be(nil)
         
     | 
| 
      
 352 
     | 
    
         
            +
              end
         
     | 
| 
      
 353 
     | 
    
         
            +
             
     | 
| 
      
 354 
     | 
    
         
            +
              it "should set the ssl_client_cert if provided" do
         
     | 
| 
      
 355 
     | 
    
         
            +
                @request = RestClient::Request.new(
         
     | 
| 
      
 356 
     | 
    
         
            +
                  :method => :put, 
         
     | 
| 
      
 357 
     | 
    
         
            +
                  :url => 'https://some/resource', 
         
     | 
| 
      
 358 
     | 
    
         
            +
                  :payload => 'payload',
         
     | 
| 
      
 359 
     | 
    
         
            +
                  :ssl_client_cert => "whatsupdoc!"
         
     | 
| 
      
 360 
     | 
    
         
            +
                )
         
     | 
| 
      
 361 
     | 
    
         
            +
                @net.should_receive(:cert=).with("whatsupdoc!")
         
     | 
| 
      
 362 
     | 
    
         
            +
                @http.stub!(:request)
         
     | 
| 
      
 363 
     | 
    
         
            +
                @request.stub!(:process_result)
         
     | 
| 
      
 364 
     | 
    
         
            +
                @request.stub!(:response_log)
         
     | 
| 
      
 365 
     | 
    
         
            +
                @request.transmit(@uri, 'req', 'payload')
         
     | 
| 
      
 366 
     | 
    
         
            +
              end
         
     | 
| 
      
 367 
     | 
    
         
            +
             
     | 
| 
      
 368 
     | 
    
         
            +
              it "should not set the ssl_client_cert if it is not provided" do
         
     | 
| 
      
 369 
     | 
    
         
            +
                @request = RestClient::Request.new(
         
     | 
| 
      
 370 
     | 
    
         
            +
                  :method => :put, 
         
     | 
| 
      
 371 
     | 
    
         
            +
                  :url => 'https://some/resource', 
         
     | 
| 
      
 372 
     | 
    
         
            +
                  :payload => 'payload'
         
     | 
| 
      
 373 
     | 
    
         
            +
                )
         
     | 
| 
      
 374 
     | 
    
         
            +
                @net.should_not_receive(:cert=).with("whatsupdoc!")
         
     | 
| 
      
 375 
     | 
    
         
            +
                @http.stub!(:request)
         
     | 
| 
      
 376 
     | 
    
         
            +
                @request.stub!(:process_result)
         
     | 
| 
      
 377 
     | 
    
         
            +
                @request.stub!(:response_log)
         
     | 
| 
      
 378 
     | 
    
         
            +
                @request.transmit(@uri, 'req', 'payload')
         
     | 
| 
      
 379 
     | 
    
         
            +
              end
         
     | 
| 
      
 380 
     | 
    
         
            +
             
     | 
| 
      
 381 
     | 
    
         
            +
              it "should default to not having an ssl_client_key" do
         
     | 
| 
      
 382 
     | 
    
         
            +
                @request.ssl_client_key.should be(nil)
         
     | 
| 
      
 383 
     | 
    
         
            +
              end
         
     | 
| 
      
 384 
     | 
    
         
            +
             
     | 
| 
      
 385 
     | 
    
         
            +
              it "should set the ssl_client_key if provided" do
         
     | 
| 
      
 386 
     | 
    
         
            +
                @request = RestClient::Request.new(
         
     | 
| 
      
 387 
     | 
    
         
            +
                  :method => :put, 
         
     | 
| 
      
 388 
     | 
    
         
            +
                  :url => 'https://some/resource', 
         
     | 
| 
      
 389 
     | 
    
         
            +
                  :payload => 'payload',
         
     | 
| 
      
 390 
     | 
    
         
            +
                  :ssl_client_key => "whatsupdoc!"
         
     | 
| 
      
 391 
     | 
    
         
            +
                )
         
     | 
| 
      
 392 
     | 
    
         
            +
                @net.should_receive(:key=).with("whatsupdoc!")
         
     | 
| 
      
 393 
     | 
    
         
            +
                @http.stub!(:request)
         
     | 
| 
      
 394 
     | 
    
         
            +
                @request.stub!(:process_result)
         
     | 
| 
      
 395 
     | 
    
         
            +
                @request.stub!(:response_log)
         
     | 
| 
      
 396 
     | 
    
         
            +
                @request.transmit(@uri, 'req', 'payload')
         
     | 
| 
      
 397 
     | 
    
         
            +
              end
         
     | 
| 
      
 398 
     | 
    
         
            +
             
     | 
| 
      
 399 
     | 
    
         
            +
              it "should not set the ssl_client_key if it is not provided" do
         
     | 
| 
      
 400 
     | 
    
         
            +
                @request = RestClient::Request.new(
         
     | 
| 
      
 401 
     | 
    
         
            +
                  :method => :put, 
         
     | 
| 
      
 402 
     | 
    
         
            +
                  :url => 'https://some/resource', 
         
     | 
| 
      
 403 
     | 
    
         
            +
                  :payload => 'payload'
         
     | 
| 
      
 404 
     | 
    
         
            +
                )
         
     | 
| 
      
 405 
     | 
    
         
            +
                @net.should_not_receive(:key=).with("whatsupdoc!")
         
     | 
| 
      
 406 
     | 
    
         
            +
                @http.stub!(:request)
         
     | 
| 
      
 407 
     | 
    
         
            +
                @request.stub!(:process_result)
         
     | 
| 
      
 408 
     | 
    
         
            +
                @request.stub!(:response_log)
         
     | 
| 
      
 409 
     | 
    
         
            +
                @request.transmit(@uri, 'req', 'payload')
         
     | 
| 
      
 410 
     | 
    
         
            +
              end
         
     | 
| 
      
 411 
     | 
    
         
            +
              
         
     | 
| 
      
 412 
     | 
    
         
            +
              it "should default to not having an ssl_ca_file" do
         
     | 
| 
      
 413 
     | 
    
         
            +
                @request.ssl_ca_file.should be(nil)
         
     | 
| 
      
 414 
     | 
    
         
            +
              end
         
     | 
| 
      
 415 
     | 
    
         
            +
             
     | 
| 
      
 416 
     | 
    
         
            +
              it "should set the ssl_ca_file if provided" do
         
     | 
| 
      
 417 
     | 
    
         
            +
                @request = RestClient::Request.new(
         
     | 
| 
      
 418 
     | 
    
         
            +
                  :method => :put, 
         
     | 
| 
      
 419 
     | 
    
         
            +
                  :url => 'https://some/resource', 
         
     | 
| 
      
 420 
     | 
    
         
            +
                  :payload => 'payload',
         
     | 
| 
      
 421 
     | 
    
         
            +
                  :ssl_ca_file => "Certificate Authority File"
         
     | 
| 
      
 422 
     | 
    
         
            +
                )
         
     | 
| 
      
 423 
     | 
    
         
            +
                @net.should_receive(:ca_file=).with("Certificate Authority File")
         
     | 
| 
      
 424 
     | 
    
         
            +
                @http.stub!(:request)
         
     | 
| 
      
 425 
     | 
    
         
            +
                @request.stub!(:process_result)
         
     | 
| 
      
 426 
     | 
    
         
            +
                @request.stub!(:response_log)
         
     | 
| 
      
 427 
     | 
    
         
            +
                @request.transmit(@uri, 'req', 'payload')
         
     | 
| 
      
 428 
     | 
    
         
            +
              end
         
     | 
| 
      
 429 
     | 
    
         
            +
             
     | 
| 
      
 430 
     | 
    
         
            +
              it "should not set the ssl_ca_file if it is not provided" do
         
     | 
| 
      
 431 
     | 
    
         
            +
                @request = RestClient::Request.new(
         
     | 
| 
      
 432 
     | 
    
         
            +
                  :method => :put, 
         
     | 
| 
      
 433 
     | 
    
         
            +
                  :url => 'https://some/resource', 
         
     | 
| 
      
 434 
     | 
    
         
            +
                  :payload => 'payload'
         
     | 
| 
      
 435 
     | 
    
         
            +
                )
         
     | 
| 
      
 436 
     | 
    
         
            +
                @net.should_not_receive(:ca_file=).with("Certificate Authority File")
         
     | 
| 
      
 437 
     | 
    
         
            +
                @http.stub!(:request)
         
     | 
| 
      
 438 
     | 
    
         
            +
                @request.stub!(:process_result)
         
     | 
| 
      
 439 
     | 
    
         
            +
                @request.stub!(:response_log)
         
     | 
| 
      
 440 
     | 
    
         
            +
                @request.transmit(@uri, 'req', 'payload')
         
     | 
| 
      
 441 
     | 
    
         
            +
              end
         
     | 
| 
      
 442 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,75 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require File.dirname(__FILE__) + '/base'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe RestClient::Resource do
         
     | 
| 
      
 4 
     | 
    
         
            +
              before do
         
     | 
| 
      
 5 
     | 
    
         
            +
                @resource = RestClient::Resource.new('http://some/resource', :user => 'jane', :password => 'mypass', :headers => { 'X-Something' => '1'})
         
     | 
| 
      
 6 
     | 
    
         
            +
              end
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
              context "Resource delegation" do
         
     | 
| 
      
 9 
     | 
    
         
            +
                it "GET" do
         
     | 
| 
      
 10 
     | 
    
         
            +
                  RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => { 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
         
     | 
| 
      
 11 
     | 
    
         
            +
                  @resource.get
         
     | 
| 
      
 12 
     | 
    
         
            +
                end
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
                it "POST" do
         
     | 
| 
      
 15 
     | 
    
         
            +
                  RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
         
     | 
| 
      
 16 
     | 
    
         
            +
                  @resource.post 'abc', :content_type => 'image/jpg'
         
     | 
| 
      
 17 
     | 
    
         
            +
                end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                it "PUT" do
         
     | 
| 
      
 20 
     | 
    
         
            +
                  RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
         
     | 
| 
      
 21 
     | 
    
         
            +
                  @resource.put 'abc', :content_type => 'image/jpg'
         
     | 
| 
      
 22 
     | 
    
         
            +
                end
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
                it "DELETE" do
         
     | 
| 
      
 25 
     | 
    
         
            +
                  RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => { 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
         
     | 
| 
      
 26 
     | 
    
         
            +
                  @resource.delete
         
     | 
| 
      
 27 
     | 
    
         
            +
                end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                it "overrides resource headers" do
         
     | 
| 
      
 30 
     | 
    
         
            +
                  RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => { 'X-Something' => '2'}, :user => 'jane', :password => 'mypass')
         
     | 
| 
      
 31 
     | 
    
         
            +
                  @resource.get 'X-Something' => '2'
         
     | 
| 
      
 32 
     | 
    
         
            +
                end
         
     | 
| 
      
 33 
     | 
    
         
            +
              end
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
              it "can instantiate with no user/password" do
         
     | 
| 
      
 36 
     | 
    
         
            +
                @resource = RestClient::Resource.new('http://some/resource')
         
     | 
| 
      
 37 
     | 
    
         
            +
              end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
              it "is backwards compatible with previous constructor" do
         
     | 
| 
      
 40 
     | 
    
         
            +
                @resource = RestClient::Resource.new('http://some/resource', 'user', 'pass')
         
     | 
| 
      
 41 
     | 
    
         
            +
                @resource.user.should == 'user'
         
     | 
| 
      
 42 
     | 
    
         
            +
                @resource.password.should == 'pass'
         
     | 
| 
      
 43 
     | 
    
         
            +
              end
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
              it "concatinates urls, inserting a slash when it needs one" do
         
     | 
| 
      
 46 
     | 
    
         
            +
                @resource.concat_urls('http://example.com', 'resource').should == 'http://example.com/resource'
         
     | 
| 
      
 47 
     | 
    
         
            +
              end
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
              it "concatinates urls, using no slash if the first url ends with a slash" do
         
     | 
| 
      
 50 
     | 
    
         
            +
                @resource.concat_urls('http://example.com/', 'resource').should == 'http://example.com/resource'
         
     | 
| 
      
 51 
     | 
    
         
            +
              end
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
              it "concatinates urls, using no slash if the second url starts with a slash" do
         
     | 
| 
      
 54 
     | 
    
         
            +
                @resource.concat_urls('http://example.com', '/resource').should == 'http://example.com/resource'
         
     | 
| 
      
 55 
     | 
    
         
            +
              end
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
              it "concatinates even non-string urls, :posts + 1 => 'posts/1'" do
         
     | 
| 
      
 58 
     | 
    
         
            +
                @resource.concat_urls(:posts, 1).should == 'posts/1'
         
     | 
| 
      
 59 
     | 
    
         
            +
              end
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
              it "offers subresources via []" do
         
     | 
| 
      
 62 
     | 
    
         
            +
                parent = RestClient::Resource.new('http://example.com')
         
     | 
| 
      
 63 
     | 
    
         
            +
                parent['posts'].url.should == 'http://example.com/posts'
         
     | 
| 
      
 64 
     | 
    
         
            +
              end
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
              it "transports options to subresources" do
         
     | 
| 
      
 67 
     | 
    
         
            +
                parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password')
         
     | 
| 
      
 68 
     | 
    
         
            +
                parent['posts'].user.should == 'user'
         
     | 
| 
      
 69 
     | 
    
         
            +
                parent['posts'].password.should == 'password'
         
     | 
| 
      
 70 
     | 
    
         
            +
              end
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
              it "prints its url with to_s" do
         
     | 
| 
      
 73 
     | 
    
         
            +
                RestClient::Resource.new('x').to_s.should == 'x'
         
     | 
| 
      
 74 
     | 
    
         
            +
              end
         
     | 
| 
      
 75 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,16 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require File.dirname(__FILE__) + '/base'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe RestClient::Response do
         
     | 
| 
      
 4 
     | 
    
         
            +
              before do
         
     | 
| 
      
 5 
     | 
    
         
            +
                @net_http_res = mock('net http response')
         
     | 
| 
      
 6 
     | 
    
         
            +
                @response = RestClient::Response.new('abc', @net_http_res)
         
     | 
| 
      
 7 
     | 
    
         
            +
              end
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
              it "behaves like string" do
         
     | 
| 
      
 10 
     | 
    
         
            +
                @response.should == 'abc'
         
     | 
| 
      
 11 
     | 
    
         
            +
              end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
              it "accepts nil strings and sets it to empty for the case of HEAD" do
         
     | 
| 
      
 14 
     | 
    
         
            +
                RestClient::Response.new(nil, @net_http_res).should == ""
         
     | 
| 
      
 15 
     | 
    
         
            +
              end
         
     | 
| 
      
 16 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,53 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require File.dirname(__FILE__) + '/base'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe RestClient do
         
     | 
| 
      
 4 
     | 
    
         
            +
              describe "API" do
         
     | 
| 
      
 5 
     | 
    
         
            +
                it "GET" do
         
     | 
| 
      
 6 
     | 
    
         
            +
                  RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {})
         
     | 
| 
      
 7 
     | 
    
         
            +
                  RestClient.get('http://some/resource')
         
     | 
| 
      
 8 
     | 
    
         
            +
                end
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
                it "POST" do
         
     | 
| 
      
 11 
     | 
    
         
            +
                  RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'payload', :headers => {})
         
     | 
| 
      
 12 
     | 
    
         
            +
                  RestClient.post('http://some/resource', 'payload')
         
     | 
| 
      
 13 
     | 
    
         
            +
                end
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                it "PUT" do
         
     | 
| 
      
 16 
     | 
    
         
            +
                  RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'payload', :headers => {})
         
     | 
| 
      
 17 
     | 
    
         
            +
                  RestClient.put('http://some/resource', 'payload')
         
     | 
| 
      
 18 
     | 
    
         
            +
                end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
                it "DELETE" do
         
     | 
| 
      
 21 
     | 
    
         
            +
                  RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {})
         
     | 
| 
      
 22 
     | 
    
         
            +
                  RestClient.delete('http://some/resource')
         
     | 
| 
      
 23 
     | 
    
         
            +
                end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                it "HEAD" do
         
     | 
| 
      
 26 
     | 
    
         
            +
                  RestClient::Request.should_receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {})
         
     | 
| 
      
 27 
     | 
    
         
            +
                  RestClient.head('http://some/resource')
         
     | 
| 
      
 28 
     | 
    
         
            +
                end
         
     | 
| 
      
 29 
     | 
    
         
            +
              end
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
              describe "logging" do
         
     | 
| 
      
 32 
     | 
    
         
            +
                after do
         
     | 
| 
      
 33 
     | 
    
         
            +
                  RestClient.log = nil
         
     | 
| 
      
 34 
     | 
    
         
            +
                end
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
                it "gets the log source from the RESTCLIENT_LOG environment variable" do
         
     | 
| 
      
 37 
     | 
    
         
            +
                  ENV.stub!(:[]).with('RESTCLIENT_LOG').and_return('from env')
         
     | 
| 
      
 38 
     | 
    
         
            +
                  RestClient.log = 'from class method'
         
     | 
| 
      
 39 
     | 
    
         
            +
                  RestClient.log.should == 'from env'
         
     | 
| 
      
 40 
     | 
    
         
            +
                end
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
                it "sets a destination for log output, used if no environment variable is set" do
         
     | 
| 
      
 43 
     | 
    
         
            +
                  ENV.stub!(:[]).with('RESTCLIENT_LOG').and_return(nil)
         
     | 
| 
      
 44 
     | 
    
         
            +
                  RestClient.log = 'from class method'
         
     | 
| 
      
 45 
     | 
    
         
            +
                  RestClient.log.should == 'from class method'
         
     | 
| 
      
 46 
     | 
    
         
            +
                end
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
                it "returns nil (no logging) if neither are set (default)" do
         
     | 
| 
      
 49 
     | 
    
         
            +
                  ENV.stub!(:[]).with('RESTCLIENT_LOG').and_return(nil)
         
     | 
| 
      
 50 
     | 
    
         
            +
                  RestClient.log.should == nil
         
     | 
| 
      
 51 
     | 
    
         
            +
                end
         
     | 
| 
      
 52 
     | 
    
         
            +
              end
         
     | 
| 
      
 53 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,79 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification 
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: sevenwire-rest-client
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version 
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.9.3
         
     | 
| 
      
 5 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 6 
     | 
    
         
            +
            authors: 
         
     | 
| 
      
 7 
     | 
    
         
            +
            - Adam Wiggins
         
     | 
| 
      
 8 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 9 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 10 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2009-04-16 00:00:00 -07:00
         
     | 
| 
      
 13 
     | 
    
         
            +
            default_executable: restclient
         
     | 
| 
      
 14 
     | 
    
         
            +
            dependencies: []
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            description: 
         
     | 
| 
      
 17 
     | 
    
         
            +
            email: adam@heroku.com
         
     | 
| 
      
 18 
     | 
    
         
            +
            executables: 
         
     | 
| 
      
 19 
     | 
    
         
            +
            - restclient
         
     | 
| 
      
 20 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
            extra_rdoc_files: 
         
     | 
| 
      
 23 
     | 
    
         
            +
            - README.rdoc
         
     | 
| 
      
 24 
     | 
    
         
            +
            files: 
         
     | 
| 
      
 25 
     | 
    
         
            +
            - README.rdoc
         
     | 
| 
      
 26 
     | 
    
         
            +
            - Rakefile
         
     | 
| 
      
 27 
     | 
    
         
            +
            - VERSION.yml
         
     | 
| 
      
 28 
     | 
    
         
            +
            - bin/restclient
         
     | 
| 
      
 29 
     | 
    
         
            +
            - lib/rest_client.rb
         
     | 
| 
      
 30 
     | 
    
         
            +
            - lib/restclient.rb
         
     | 
| 
      
 31 
     | 
    
         
            +
            - lib/restclient/exceptions.rb
         
     | 
| 
      
 32 
     | 
    
         
            +
            - lib/restclient/mixin/response.rb
         
     | 
| 
      
 33 
     | 
    
         
            +
            - lib/restclient/raw_response.rb
         
     | 
| 
      
 34 
     | 
    
         
            +
            - lib/restclient/request.rb
         
     | 
| 
      
 35 
     | 
    
         
            +
            - lib/restclient/resource.rb
         
     | 
| 
      
 36 
     | 
    
         
            +
            - lib/restclient/response.rb
         
     | 
| 
      
 37 
     | 
    
         
            +
            - spec/base.rb
         
     | 
| 
      
 38 
     | 
    
         
            +
            - spec/exceptions_spec.rb
         
     | 
| 
      
 39 
     | 
    
         
            +
            - spec/mixin/response_spec.rb
         
     | 
| 
      
 40 
     | 
    
         
            +
            - spec/raw_response_spec.rb
         
     | 
| 
      
 41 
     | 
    
         
            +
            - spec/request_spec.rb
         
     | 
| 
      
 42 
     | 
    
         
            +
            - spec/resource_spec.rb
         
     | 
| 
      
 43 
     | 
    
         
            +
            - spec/response_spec.rb
         
     | 
| 
      
 44 
     | 
    
         
            +
            - spec/restclient_spec.rb
         
     | 
| 
      
 45 
     | 
    
         
            +
            has_rdoc: true
         
     | 
| 
      
 46 
     | 
    
         
            +
            homepage: http://rest-client.heroku.com/
         
     | 
| 
      
 47 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 48 
     | 
    
         
            +
            rdoc_options: 
         
     | 
| 
      
 49 
     | 
    
         
            +
            - --charset=UTF-8
         
     | 
| 
      
 50 
     | 
    
         
            +
            require_paths: 
         
     | 
| 
      
 51 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 52 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 53 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 54 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 55 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 56 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 57 
     | 
    
         
            +
              version: 
         
     | 
| 
      
 58 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 59 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 60 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 61 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 62 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 63 
     | 
    
         
            +
              version: 
         
     | 
| 
      
 64 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
            rubyforge_project: 
         
     | 
| 
      
 67 
     | 
    
         
            +
            rubygems_version: 1.2.0
         
     | 
| 
      
 68 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 69 
     | 
    
         
            +
            specification_version: 2
         
     | 
| 
      
 70 
     | 
    
         
            +
            summary: Simple REST client for Ruby, inspired by microframework syntax for specifying actions.
         
     | 
| 
      
 71 
     | 
    
         
            +
            test_files: 
         
     | 
| 
      
 72 
     | 
    
         
            +
            - spec/base.rb
         
     | 
| 
      
 73 
     | 
    
         
            +
            - spec/exceptions_spec.rb
         
     | 
| 
      
 74 
     | 
    
         
            +
            - spec/mixin/response_spec.rb
         
     | 
| 
      
 75 
     | 
    
         
            +
            - spec/raw_response_spec.rb
         
     | 
| 
      
 76 
     | 
    
         
            +
            - spec/request_spec.rb
         
     | 
| 
      
 77 
     | 
    
         
            +
            - spec/resource_spec.rb
         
     | 
| 
      
 78 
     | 
    
         
            +
            - spec/response_spec.rb
         
     | 
| 
      
 79 
     | 
    
         
            +
            - spec/restclient_spec.rb
         
     |