rest-client 1.1.0 → 1.2.0
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.
Potentially problematic release.
This version of rest-client might be problematic. Click here for more details.
- data/README.rdoc +3 -0
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/restclient.rb +32 -31
- data/lib/restclient/exceptions.rb +70 -69
- data/lib/restclient/mixin/response.rb +40 -40
- data/lib/restclient/net_http_ext.rb +11 -11
- data/lib/restclient/payload.rb +175 -168
- data/lib/restclient/raw_response.rb +22 -22
- data/lib/restclient/request.rb +283 -248
- data/lib/restclient/resource.rb +132 -132
- data/lib/restclient/response.rb +13 -13
- data/spec/exceptions_spec.rb +45 -45
- data/spec/mixin/response_spec.rb +38 -38
- data/spec/payload_spec.rb +92 -92
- data/spec/raw_response_spec.rb +11 -11
- data/spec/request_spec.rb +517 -493
- data/spec/resource_spec.rb +57 -57
- data/spec/response_spec.rb +15 -15
- data/spec/restclient_spec.rb +49 -49
- metadata +2 -2
    
        data/spec/payload_spec.rb
    CHANGED
    
    | @@ -1,43 +1,43 @@ | |
| 1 1 | 
             
            require File.dirname(__FILE__) + "/base"
         | 
| 2 2 |  | 
| 3 3 | 
             
            describe RestClient::Payload do
         | 
| 4 | 
            -
             | 
| 5 | 
            -
             | 
| 6 | 
            -
             | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 4 | 
            +
              context "A regular Payload" do
         | 
| 5 | 
            +
                it "should use standard enctype as default content-type" do
         | 
| 6 | 
            +
                  RestClient::Payload::UrlEncoded.new({}).headers['Content-Type'].
         | 
| 7 | 
            +
                          should == 'application/x-www-form-urlencoded'
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                it "should form properly encoded params" do
         | 
| 11 | 
            +
                  RestClient::Payload::UrlEncoded.new({:foo => 'bar'}).to_s.
         | 
| 12 | 
            +
                          should == "foo=bar"
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                it "should properly handle hashes as parameter" do
         | 
| 16 | 
            +
                  RestClient::Payload::UrlEncoded.new({:foo => {:bar => 'baz' }}).to_s.
         | 
| 17 | 
            +
                          should == "foo[bar]=baz"
         | 
| 18 | 
            +
                  RestClient::Payload::UrlEncoded.new({:foo => {:bar => {:baz => 'qux' }}}).to_s.
         | 
| 19 | 
            +
                          should == "foo[bar][baz]=qux"
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                it "should form properly use symbols as parameters" do
         | 
| 23 | 
            +
                  RestClient::Payload::UrlEncoded.new({:foo => :bar}).to_s.
         | 
| 24 | 
            +
                          should == "foo=bar"
         | 
| 25 | 
            +
                  RestClient::Payload::UrlEncoded.new({:foo => {:bar => :baz }}).to_s.
         | 
| 26 | 
            +
                          should == "foo[bar]=baz"
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
              context "A multipart Payload" do
         | 
| 32 | 
            +
                it "should use standard enctype as default content-type" do
         | 
| 33 | 
            +
                  m = RestClient::Payload::Multipart.new({})
         | 
| 34 | 
            +
                  m.stub!(:boundary).and_return(123)
         | 
| 35 | 
            +
                  m.headers['Content-Type'].should == 'multipart/form-data; boundary="123"'
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                it "should form properly separated multipart data" do
         | 
| 39 | 
            +
                  m = RestClient::Payload::Multipart.new([[:bar, "baz"], [:foo, "bar"]])
         | 
| 40 | 
            +
                  m.to_s.should == <<-EOS
         | 
| 41 41 | 
             
            --#{m.boundary}\r
         | 
| 42 42 | 
             
            Content-Disposition: multipart/form-data; name="bar"\r
         | 
| 43 43 | 
             
            \r
         | 
| @@ -47,85 +47,85 @@ Content-Disposition: multipart/form-data; name="foo"\r | |
| 47 47 | 
             
            \r
         | 
| 48 48 | 
             
            bar\r
         | 
| 49 49 | 
             
            --#{m.boundary}--\r
         | 
| 50 | 
            -
            EOS
         | 
| 51 | 
            -
             | 
| 50 | 
            +
                  EOS
         | 
| 51 | 
            +
                end
         | 
| 52 52 |  | 
| 53 | 
            -
             | 
| 54 | 
            -
             | 
| 55 | 
            -
             | 
| 56 | 
            -
             | 
| 53 | 
            +
                it "should form properly separated multipart data" do
         | 
| 54 | 
            +
                  f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
         | 
| 55 | 
            +
                  m = RestClient::Payload::Multipart.new({:foo => f})
         | 
| 56 | 
            +
                  m.to_s.should == <<-EOS
         | 
| 57 57 | 
             
            --#{m.boundary}\r
         | 
| 58 58 | 
             
            Content-Disposition: multipart/form-data; name="foo"; filename="master_shake.jpg"\r
         | 
| 59 59 | 
             
            Content-Type: image/jpeg\r
         | 
| 60 60 | 
             
            \r
         | 
| 61 61 | 
             
            #{IO.read(f.path)}\r
         | 
| 62 62 | 
             
            --#{m.boundary}--\r
         | 
| 63 | 
            -
            EOS
         | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
             | 
| 63 | 
            +
                  EOS
         | 
| 64 | 
            +
                end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                it "should detect optional (original) content type and filename" do
         | 
| 67 | 
            +
                  f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
         | 
| 68 | 
            +
                  f.instance_eval "def content_type; 'text/plain'; end"
         | 
| 69 | 
            +
                  f.instance_eval "def original_filename; 'foo.txt'; end"
         | 
| 70 | 
            +
                  m = RestClient::Payload::Multipart.new({:foo => f})
         | 
| 71 | 
            +
                  m.to_s.should == <<-EOS
         | 
| 72 72 | 
             
            --#{m.boundary}\r
         | 
| 73 73 | 
             
            Content-Disposition: multipart/form-data; name="foo"; filename="foo.txt"\r
         | 
| 74 74 | 
             
            Content-Type: text/plain\r
         | 
| 75 75 | 
             
            \r
         | 
| 76 76 | 
             
            #{IO.read(f.path)}\r
         | 
| 77 77 | 
             
            --#{m.boundary}--\r
         | 
| 78 | 
            -
            EOS
         | 
| 79 | 
            -
             | 
| 78 | 
            +
                  EOS
         | 
| 79 | 
            +
                end
         | 
| 80 80 |  | 
| 81 | 
            -
             | 
| 82 | 
            -
             | 
| 83 | 
            -
             | 
| 81 | 
            +
                it "should handle hash in hash parameters" do
         | 
| 82 | 
            +
                  m = RestClient::Payload::Multipart.new({:bar => {:baz => "foo"}})
         | 
| 83 | 
            +
                  m.to_s.should == <<-EOS
         | 
| 84 84 | 
             
            --#{m.boundary}\r
         | 
| 85 85 | 
             
            Content-Disposition: multipart/form-data; name="bar[baz]"\r
         | 
| 86 86 | 
             
            \r
         | 
| 87 87 | 
             
            foo\r
         | 
| 88 88 | 
             
            --#{m.boundary}--\r
         | 
| 89 | 
            -
            EOS
         | 
| 89 | 
            +
                  EOS
         | 
| 90 90 |  | 
| 91 | 
            -
             | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 91 | 
            +
                  f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
         | 
| 92 | 
            +
                  f.instance_eval "def content_type; 'text/plain'; end"
         | 
| 93 | 
            +
                  f.instance_eval "def original_filename; 'foo.txt'; end"
         | 
| 94 | 
            +
                  m = RestClient::Payload::Multipart.new({:foo => {:bar => f}})
         | 
| 95 | 
            +
                  m.to_s.should == <<-EOS
         | 
| 96 96 | 
             
            --#{m.boundary}\r
         | 
| 97 97 | 
             
            Content-Disposition: multipart/form-data; name="foo[bar]"; filename="foo.txt"\r
         | 
| 98 98 | 
             
            Content-Type: text/plain\r
         | 
| 99 99 | 
             
            \r
         | 
| 100 100 | 
             
            #{IO.read(f.path)}\r
         | 
| 101 101 | 
             
            --#{m.boundary}--\r
         | 
| 102 | 
            -
            EOS
         | 
| 103 | 
            -
             | 
| 104 | 
            -
             | 
| 105 | 
            -
             | 
| 106 | 
            -
             | 
| 107 | 
            -
             | 
| 108 | 
            -
             | 
| 109 | 
            -
             | 
| 110 | 
            -
             | 
| 111 | 
            -
             | 
| 112 | 
            -
             | 
| 113 | 
            -
             | 
| 114 | 
            -
             | 
| 115 | 
            -
             | 
| 116 | 
            -
             | 
| 117 | 
            -
             | 
| 118 | 
            -
             | 
| 119 | 
            -
             | 
| 120 | 
            -
             | 
| 121 | 
            -
             | 
| 122 | 
            -
             | 
| 123 | 
            -
             | 
| 124 | 
            -
             | 
| 125 | 
            -
             | 
| 126 | 
            -
             | 
| 127 | 
            -
             | 
| 128 | 
            -
             | 
| 129 | 
            -
             | 
| 130 | 
            -
             | 
| 102 | 
            +
                  EOS
         | 
| 103 | 
            +
                end
         | 
| 104 | 
            +
             | 
| 105 | 
            +
              end
         | 
| 106 | 
            +
             | 
| 107 | 
            +
              context "Payload generation" do
         | 
| 108 | 
            +
                it "should recognize standard urlencoded params" do
         | 
| 109 | 
            +
                  RestClient::Payload.generate({"foo" => 'bar'}).should be_kind_of(RestClient::Payload::UrlEncoded)
         | 
| 110 | 
            +
                end
         | 
| 111 | 
            +
             | 
| 112 | 
            +
                it "should recognize multipart params" do
         | 
| 113 | 
            +
                  f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
         | 
| 114 | 
            +
                  RestClient::Payload.generate({"foo" => f}).should be_kind_of(RestClient::Payload::Multipart)
         | 
| 115 | 
            +
                end
         | 
| 116 | 
            +
             | 
| 117 | 
            +
                it "should be multipart if forced" do
         | 
| 118 | 
            +
                  RestClient::Payload.generate({"foo" => "bar", :multipart => true}).should be_kind_of(RestClient::Payload::Multipart)
         | 
| 119 | 
            +
                end
         | 
| 120 | 
            +
             | 
| 121 | 
            +
                it "should return data if no of the above" do
         | 
| 122 | 
            +
                  RestClient::Payload.generate("data").should be_kind_of(RestClient::Payload::Base)
         | 
| 123 | 
            +
                end
         | 
| 124 | 
            +
             | 
| 125 | 
            +
                it "should recognize nested multipart payloads" do
         | 
| 126 | 
            +
                  f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
         | 
| 127 | 
            +
                  RestClient::Payload.generate({"foo" => {"file" => f}}).should be_kind_of(RestClient::Payload::Multipart)
         | 
| 128 | 
            +
                end
         | 
| 129 | 
            +
             | 
| 130 | 
            +
              end
         | 
| 131 131 | 
             
            end
         | 
    
        data/spec/raw_response_spec.rb
    CHANGED
    
    | @@ -1,17 +1,17 @@ | |
| 1 1 | 
             
            require File.dirname(__FILE__) + '/base'
         | 
| 2 2 |  | 
| 3 3 | 
             
            describe RestClient::RawResponse do
         | 
| 4 | 
            -
             | 
| 5 | 
            -
             | 
| 6 | 
            -
             | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 4 | 
            +
              before do
         | 
| 5 | 
            +
                @tf = mock("Tempfile", :read => "the answer is 42", :open => true)
         | 
| 6 | 
            +
                @net_http_res = mock('net http response')
         | 
| 7 | 
            +
                @response = RestClient::RawResponse.new(@tf, @net_http_res)
         | 
| 8 | 
            +
              end
         | 
| 9 9 |  | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 10 | 
            +
              it "behaves like string" do
         | 
| 11 | 
            +
                @response.to_s.should == 'the answer is 42'
         | 
| 12 | 
            +
              end
         | 
| 13 13 |  | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 14 | 
            +
              it "exposes a Tempfile" do
         | 
| 15 | 
            +
                @response.file.should == @tf
         | 
| 16 | 
            +
              end
         | 
| 17 17 | 
             
            end
         | 
    
        data/spec/request_spec.rb
    CHANGED
    
    | @@ -1,497 +1,521 @@ | |
| 1 1 | 
             
            require File.dirname(__FILE__) + '/base'
         | 
| 2 2 |  | 
| 3 3 | 
             
            describe RestClient::Request do
         | 
| 4 | 
            -
             | 
| 5 | 
            -
             | 
| 6 | 
            -
             | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 41 | 
            -
             | 
| 42 | 
            -
             | 
| 43 | 
            -
             | 
| 44 | 
            -
             | 
| 45 | 
            -
             | 
| 46 | 
            -
             | 
| 47 | 
            -
             | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 50 | 
            -
             | 
| 51 | 
            -
             | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 54 | 
            -
             | 
| 55 | 
            -
             | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 58 | 
            -
             | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
             | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 74 | 
            -
             | 
| 75 | 
            -
             | 
| 76 | 
            -
             | 
| 77 | 
            -
             | 
| 78 | 
            -
             | 
| 79 | 
            -
             | 
| 80 | 
            -
             | 
| 81 | 
            -
             | 
| 82 | 
            -
             | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 85 | 
            -
             | 
| 86 | 
            -
             | 
| 87 | 
            -
             | 
| 88 | 
            -
             | 
| 89 | 
            -
             | 
| 90 | 
            -
             | 
| 91 | 
            -
             | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 98 | 
            -
             | 
| 99 | 
            -
             | 
| 100 | 
            -
             | 
| 101 | 
            -
             | 
| 102 | 
            -
             | 
| 103 | 
            -
             | 
| 104 | 
            -
             | 
| 105 | 
            -
             | 
| 106 | 
            -
             | 
| 107 | 
            -
             | 
| 108 | 
            -
             | 
| 109 | 
            -
             | 
| 110 | 
            -
             | 
| 111 | 
            -
             | 
| 112 | 
            -
             | 
| 113 | 
            -
             | 
| 114 | 
            -
             | 
| 115 | 
            -
             | 
| 116 | 
            -
             | 
| 117 | 
            -
             | 
| 118 | 
            -
             | 
| 119 | 
            -
             | 
| 120 | 
            -
             | 
| 121 | 
            -
             | 
| 122 | 
            -
             | 
| 123 | 
            -
             | 
| 124 | 
            -
             | 
| 125 | 
            -
             | 
| 126 | 
            -
             | 
| 127 | 
            -
             | 
| 128 | 
            -
             | 
| 129 | 
            -
             | 
| 130 | 
            -
             | 
| 131 | 
            -
             | 
| 132 | 
            -
             | 
| 133 | 
            -
             | 
| 134 | 
            -
             | 
| 135 | 
            -
             | 
| 136 | 
            -
             | 
| 137 | 
            -
             | 
| 138 | 
            -
             | 
| 139 | 
            -
             | 
| 140 | 
            -
             | 
| 141 | 
            -
             | 
| 142 | 
            -
             | 
| 143 | 
            -
             | 
| 144 | 
            -
             | 
| 145 | 
            -
             | 
| 146 | 
            -
             | 
| 147 | 
            -
             | 
| 148 | 
            -
             | 
| 149 | 
            -
             | 
| 150 | 
            -
             | 
| 151 | 
            -
             | 
| 152 | 
            -
             | 
| 153 | 
            -
             | 
| 154 | 
            -
             | 
| 155 | 
            -
             | 
| 156 | 
            -
             | 
| 157 | 
            -
             | 
| 158 | 
            -
             | 
| 159 | 
            -
             | 
| 160 | 
            -
             | 
| 161 | 
            -
             | 
| 162 | 
            -
             | 
| 163 | 
            -
             | 
| 164 | 
            -
             | 
| 165 | 
            -
             | 
| 166 | 
            -
             | 
| 167 | 
            -
             | 
| 168 | 
            -
             | 
| 169 | 
            -
             | 
| 170 | 
            -
             | 
| 171 | 
            -
             | 
| 172 | 
            -
             | 
| 173 | 
            -
             | 
| 174 | 
            -
             | 
| 175 | 
            -
             | 
| 176 | 
            -
             | 
| 177 | 
            -
             | 
| 178 | 
            -
             | 
| 179 | 
            -
             | 
| 180 | 
            -
             | 
| 181 | 
            -
             | 
| 182 | 
            -
             | 
| 183 | 
            -
             | 
| 184 | 
            -
             | 
| 185 | 
            -
             | 
| 186 | 
            -
             | 
| 187 | 
            -
             | 
| 188 | 
            -
             | 
| 189 | 
            -
             | 
| 190 | 
            -
             | 
| 191 | 
            -
             | 
| 192 | 
            -
             | 
| 193 | 
            -
             | 
| 194 | 
            -
             | 
| 195 | 
            -
             | 
| 196 | 
            -
             | 
| 197 | 
            -
             | 
| 198 | 
            -
             | 
| 199 | 
            -
             | 
| 200 | 
            -
             | 
| 201 | 
            -
             | 
| 202 | 
            -
             | 
| 203 | 
            -
             | 
| 204 | 
            -
             | 
| 205 | 
            -
             | 
| 206 | 
            -
             | 
| 207 | 
            -
             | 
| 208 | 
            -
             | 
| 209 | 
            -
             | 
| 210 | 
            -
             | 
| 211 | 
            -
             | 
| 212 | 
            -
             | 
| 213 | 
            -
             | 
| 214 | 
            -
             | 
| 215 | 
            -
             | 
| 216 | 
            -
             | 
| 217 | 
            -
             | 
| 218 | 
            -
             | 
| 219 | 
            -
             | 
| 220 | 
            -
             | 
| 221 | 
            -
             | 
| 222 | 
            -
             | 
| 223 | 
            -
             | 
| 224 | 
            -
             | 
| 225 | 
            -
             | 
| 226 | 
            -
             | 
| 227 | 
            -
             | 
| 228 | 
            -
             | 
| 229 | 
            -
             | 
| 230 | 
            -
             | 
| 231 | 
            -
             | 
| 232 | 
            -
             | 
| 233 | 
            -
             | 
| 234 | 
            -
             | 
| 235 | 
            -
             | 
| 236 | 
            -
             | 
| 237 | 
            -
             | 
| 238 | 
            -
             | 
| 239 | 
            -
             | 
| 240 | 
            -
             | 
| 241 | 
            -
             | 
| 242 | 
            -
             | 
| 243 | 
            -
             | 
| 244 | 
            -
             | 
| 245 | 
            -
             | 
| 246 | 
            -
             | 
| 247 | 
            -
             | 
| 248 | 
            -
             | 
| 249 | 
            -
             | 
| 250 | 
            -
             | 
| 251 | 
            -
             | 
| 252 | 
            -
             | 
| 253 | 
            -
             | 
| 254 | 
            -
             | 
| 255 | 
            -
             | 
| 256 | 
            -
             | 
| 257 | 
            -
             | 
| 258 | 
            -
             | 
| 259 | 
            -
             | 
| 260 | 
            -
             | 
| 261 | 
            -
             | 
| 262 | 
            -
             | 
| 263 | 
            -
             | 
| 264 | 
            -
             | 
| 265 | 
            -
             | 
| 266 | 
            -
             | 
| 267 | 
            -
             | 
| 268 | 
            -
             | 
| 269 | 
            -
             | 
| 270 | 
            -
             | 
| 271 | 
            -
             | 
| 272 | 
            -
             | 
| 273 | 
            -
             | 
| 274 | 
            -
             | 
| 275 | 
            -
             | 
| 276 | 
            -
             | 
| 277 | 
            -
             | 
| 278 | 
            -
             | 
| 279 | 
            -
             | 
| 280 | 
            -
             | 
| 281 | 
            -
             | 
| 282 | 
            -
             | 
| 283 | 
            -
             | 
| 284 | 
            -
             | 
| 285 | 
            -
             | 
| 286 | 
            -
             | 
| 287 | 
            -
             | 
| 288 | 
            -
             | 
| 289 | 
            -
             | 
| 290 | 
            -
             | 
| 291 | 
            -
             | 
| 292 | 
            -
             | 
| 293 | 
            -
             | 
| 294 | 
            -
             | 
| 295 | 
            -
             | 
| 296 | 
            -
             | 
| 297 | 
            -
             | 
| 298 | 
            -
             | 
| 299 | 
            -
             | 
| 300 | 
            -
             | 
| 301 | 
            -
             | 
| 302 | 
            -
             | 
| 303 | 
            -
             | 
| 304 | 
            -
             | 
| 305 | 
            -
             | 
| 306 | 
            -
             | 
| 307 | 
            -
             | 
| 308 | 
            -
             | 
| 309 | 
            -
             | 
| 310 | 
            -
             | 
| 311 | 
            -
             | 
| 312 | 
            -
             | 
| 313 | 
            -
             | 
| 314 | 
            -
             | 
| 315 | 
            -
             | 
| 316 | 
            -
             | 
| 317 | 
            -
             | 
| 318 | 
            -
             | 
| 319 | 
            -
             | 
| 320 | 
            -
             | 
| 321 | 
            -
             | 
| 322 | 
            -
             | 
| 323 | 
            -
             | 
| 324 | 
            -
             | 
| 325 | 
            -
             | 
| 326 | 
            -
             | 
| 327 | 
            -
             | 
| 328 | 
            -
             | 
| 329 | 
            -
             | 
| 330 | 
            -
             | 
| 331 | 
            -
             | 
| 332 | 
            -
             | 
| 333 | 
            -
             | 
| 334 | 
            -
             | 
| 335 | 
            -
             | 
| 336 | 
            -
             | 
| 337 | 
            -
             | 
| 338 | 
            -
             | 
| 339 | 
            -
             | 
| 340 | 
            -
             | 
| 341 | 
            -
             | 
| 342 | 
            -
             | 
| 343 | 
            -
             | 
| 344 | 
            -
             | 
| 345 | 
            -
             | 
| 346 | 
            -
             | 
| 347 | 
            -
             | 
| 348 | 
            -
             | 
| 349 | 
            -
             | 
| 350 | 
            -
             | 
| 351 | 
            -
             | 
| 352 | 
            -
             | 
| 353 | 
            -
             | 
| 354 | 
            -
             | 
| 355 | 
            -
             | 
| 356 | 
            -
             | 
| 357 | 
            -
             | 
| 358 | 
            -
             | 
| 359 | 
            -
             | 
| 360 | 
            -
             | 
| 361 | 
            -
             | 
| 362 | 
            -
             | 
| 363 | 
            -
             | 
| 364 | 
            -
             | 
| 365 | 
            -
             | 
| 366 | 
            -
             | 
| 367 | 
            -
             | 
| 368 | 
            -
             | 
| 369 | 
            -
             | 
| 370 | 
            -
             | 
| 371 | 
            -
             | 
| 372 | 
            -
             | 
| 373 | 
            -
             | 
| 374 | 
            -
             | 
| 375 | 
            -
             | 
| 376 | 
            -
             | 
| 377 | 
            -
             | 
| 378 | 
            -
             | 
| 379 | 
            -
             | 
| 380 | 
            -
             | 
| 381 | 
            -
             | 
| 382 | 
            -
             | 
| 383 | 
            -
             | 
| 384 | 
            -
             | 
| 385 | 
            -
             | 
| 386 | 
            -
             | 
| 387 | 
            -
             | 
| 388 | 
            -
             | 
| 389 | 
            -
             | 
| 390 | 
            -
             | 
| 391 | 
            -
             | 
| 392 | 
            -
             | 
| 393 | 
            -
             | 
| 394 | 
            -
             | 
| 395 | 
            -
             | 
| 396 | 
            -
             | 
| 397 | 
            -
             | 
| 398 | 
            -
             | 
| 399 | 
            -
             | 
| 400 | 
            -
             | 
| 401 | 
            -
             | 
| 402 | 
            -
             | 
| 403 | 
            -
             | 
| 404 | 
            -
             | 
| 405 | 
            -
             | 
| 406 | 
            -
             | 
| 407 | 
            -
             | 
| 408 | 
            -
             | 
| 409 | 
            -
             | 
| 410 | 
            -
             | 
| 411 | 
            -
             | 
| 412 | 
            -
             | 
| 413 | 
            -
             | 
| 414 | 
            -
             | 
| 415 | 
            -
             | 
| 416 | 
            -
             | 
| 417 | 
            -
             | 
| 418 | 
            -
             | 
| 419 | 
            -
             | 
| 420 | 
            -
             | 
| 421 | 
            -
             | 
| 422 | 
            -
             | 
| 423 | 
            -
             | 
| 424 | 
            -
             | 
| 425 | 
            -
             | 
| 426 | 
            -
             | 
| 427 | 
            -
             | 
| 428 | 
            -
             | 
| 429 | 
            -
             | 
| 430 | 
            -
             | 
| 431 | 
            -
             | 
| 432 | 
            -
             | 
| 433 | 
            -
             | 
| 434 | 
            -
             | 
| 435 | 
            -
             | 
| 436 | 
            -
             | 
| 437 | 
            -
             | 
| 438 | 
            -
             | 
| 439 | 
            -
             | 
| 440 | 
            -
             | 
| 441 | 
            -
             | 
| 442 | 
            -
             | 
| 443 | 
            -
             | 
| 444 | 
            -
             | 
| 445 | 
            -
             | 
| 446 | 
            -
             | 
| 447 | 
            -
             | 
| 448 | 
            -
             | 
| 449 | 
            -
             | 
| 450 | 
            -
             | 
| 451 | 
            -
             | 
| 452 | 
            -
             | 
| 453 | 
            -
             | 
| 454 | 
            -
             | 
| 455 | 
            -
             | 
| 456 | 
            -
             | 
| 457 | 
            -
             | 
| 458 | 
            -
             | 
| 459 | 
            -
             | 
| 460 | 
            -
             | 
| 461 | 
            -
             | 
| 462 | 
            -
             | 
| 463 | 
            -
             | 
| 464 | 
            -
             | 
| 465 | 
            -
             | 
| 466 | 
            -
             | 
| 467 | 
            -
             | 
| 468 | 
            -
             | 
| 469 | 
            -
             | 
| 470 | 
            -
             | 
| 471 | 
            -
             | 
| 472 | 
            -
             | 
| 473 | 
            -
             | 
| 474 | 
            -
             | 
| 475 | 
            -
             | 
| 476 | 
            -
             | 
| 477 | 
            -
             | 
| 478 | 
            -
             | 
| 479 | 
            -
             | 
| 480 | 
            -
             | 
| 481 | 
            -
             | 
| 482 | 
            -
             | 
| 483 | 
            -
             | 
| 484 | 
            -
             | 
| 485 | 
            -
             | 
| 486 | 
            -
             | 
| 487 | 
            -
             | 
| 488 | 
            -
             | 
| 489 | 
            -
             | 
| 490 | 
            -
             | 
| 491 | 
            -
             | 
| 492 | 
            -
             | 
| 493 | 
            -
             | 
| 494 | 
            -
             | 
| 495 | 
            -
             | 
| 496 | 
            -
             | 
| 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 | 
            +
                RestClient.log = 'test.log'
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              it "accept */* mimetype, preferring xml" do
         | 
| 22 | 
            +
                @request.default_headers[:accept].should == '*/*; q=0.5, application/xml'
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
              it "decodes an uncompressed result body by passing it straight through" do
         | 
| 26 | 
            +
                RestClient::Request.decode(nil, 'xyz').should == 'xyz'
         | 
| 27 | 
            +
              end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
              it "decodes a gzip body" do
         | 
| 30 | 
            +
                RestClient::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"
         | 
| 31 | 
            +
              end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
              it "ingores gzip for empty bodies" do
         | 
| 34 | 
            +
                RestClient::Request.decode('gzip', '').should be_empty
         | 
| 35 | 
            +
              end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
              it "decodes a deflated body" do
         | 
| 38 | 
            +
                RestClient::Request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363").should == "some deflated text"
         | 
| 39 | 
            +
              end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
              it "processes a successful result" do
         | 
| 42 | 
            +
                res = mock("result")
         | 
| 43 | 
            +
                res.stub!(:code).and_return("200")
         | 
| 44 | 
            +
                res.stub!(:body).and_return('body')
         | 
| 45 | 
            +
                res.stub!(:[]).with('content-encoding').and_return(nil)
         | 
| 46 | 
            +
                @request.process_result(res).should == 'body'
         | 
| 47 | 
            +
              end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
              it "doesn't classify successful requests as failed" do
         | 
| 50 | 
            +
                203.upto(206) do |code|
         | 
| 51 | 
            +
                  res = mock("result")
         | 
| 52 | 
            +
                  res.stub!(:code).and_return(code.to_s)
         | 
| 53 | 
            +
                  res.stub!(:body).and_return("")
         | 
| 54 | 
            +
                  res.stub!(:[]).with('content-encoding').and_return(nil)
         | 
| 55 | 
            +
                  @request.process_result(res).should be_empty
         | 
| 56 | 
            +
                end
         | 
| 57 | 
            +
              end
         | 
| 58 | 
            +
             | 
| 59 | 
            +
              it "parses a url into a URI object" do
         | 
| 60 | 
            +
                URI.should_receive(:parse).with('http://example.com/resource')
         | 
| 61 | 
            +
                @request.parse_url('http://example.com/resource')
         | 
| 62 | 
            +
              end
         | 
| 63 | 
            +
             | 
| 64 | 
            +
              it "adds http:// to the front of resources specified in the syntax example.com/resource" do
         | 
| 65 | 
            +
                URI.should_receive(:parse).with('http://example.com/resource')
         | 
| 66 | 
            +
                @request.parse_url('example.com/resource')
         | 
| 67 | 
            +
              end
         | 
| 68 | 
            +
             | 
| 69 | 
            +
              it "extracts the username and password when parsing http://user:password@example.com/" do
         | 
| 70 | 
            +
                URI.stub!(:parse).and_return(mock('uri', :user => 'joe', :password => 'pass1'))
         | 
| 71 | 
            +
                @request.parse_url_with_auth('http://joe:pass1@example.com/resource')
         | 
| 72 | 
            +
                @request.user.should == 'joe'
         | 
| 73 | 
            +
                @request.password.should == 'pass1'
         | 
| 74 | 
            +
              end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
              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
         | 
| 77 | 
            +
                URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
         | 
| 78 | 
            +
                @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :user => 'beth', :password => 'pass2')
         | 
| 79 | 
            +
                @request.parse_url_with_auth('http://example.com/resource')
         | 
| 80 | 
            +
                @request.user.should == 'beth'
         | 
| 81 | 
            +
                @request.password.should == 'pass2'
         | 
| 82 | 
            +
              end
         | 
| 83 | 
            +
             | 
| 84 | 
            +
              it "correctly formats cookies provided to the constructor" do
         | 
| 85 | 
            +
                URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
         | 
| 86 | 
            +
                @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:session_id => '1' })
         | 
| 87 | 
            +
                @request.should_receive(:default_headers).and_return({'foo' => 'bar'})
         | 
| 88 | 
            +
                headers = @request.make_headers({}).should == { 'Foo' => 'bar', 'Cookie' => 'session_id=1'}
         | 
| 89 | 
            +
              end
         | 
| 90 | 
            +
             | 
| 91 | 
            +
              it "determines the Net::HTTP class to instantiate by the method name" do
         | 
| 92 | 
            +
                @request.net_http_request_class(:put).should == Net::HTTP::Put
         | 
| 93 | 
            +
              end
         | 
| 94 | 
            +
             | 
| 95 | 
            +
              it "merges user headers with the default headers" do
         | 
| 96 | 
            +
                @request.should_receive(:default_headers).and_return({ '1' => '2' })
         | 
| 97 | 
            +
                headers = @request.make_headers('3' => '4')
         | 
| 98 | 
            +
                headers.should have_key('1')
         | 
| 99 | 
            +
                headers['1'].should == '2'
         | 
| 100 | 
            +
                headers.should have_key('3')
         | 
| 101 | 
            +
                headers['3'].should == '4'
         | 
| 102 | 
            +
              end
         | 
| 103 | 
            +
             | 
| 104 | 
            +
              it "prefers the user header when the same header exists in the defaults" do
         | 
| 105 | 
            +
                @request.should_receive(:default_headers).and_return({ '1' => '2' })
         | 
| 106 | 
            +
                headers = @request.make_headers('1' => '3')
         | 
| 107 | 
            +
                headers.should have_key('1')
         | 
| 108 | 
            +
                headers['1'].should == '3'
         | 
| 109 | 
            +
              end
         | 
| 110 | 
            +
             | 
| 111 | 
            +
              it "converts header symbols from :content_type to 'Content-type'" do
         | 
| 112 | 
            +
                @request.should_receive(:default_headers).and_return({})
         | 
| 113 | 
            +
                headers = @request.make_headers(:content_type => 'abc')
         | 
| 114 | 
            +
                headers.should have_key('Content-type')
         | 
| 115 | 
            +
                headers['Content-type'].should == 'abc'
         | 
| 116 | 
            +
              end
         | 
| 117 | 
            +
             | 
| 118 | 
            +
              it "converts content-type from extension to real content-type" do
         | 
| 119 | 
            +
                @request.should_receive(:default_headers).and_return({})
         | 
| 120 | 
            +
                headers = @request.make_headers(:content_type => 'json')
         | 
| 121 | 
            +
                headers.should have_key('Content-type')
         | 
| 122 | 
            +
                headers['Content-type'].should == 'application/json'
         | 
| 123 | 
            +
              end
         | 
| 124 | 
            +
             | 
| 125 | 
            +
              it "converts accept from extension(s) to real content-type(s)" do
         | 
| 126 | 
            +
                @request.should_receive(:default_headers).and_return({})
         | 
| 127 | 
            +
                headers = @request.make_headers(:accept => 'json, mp3')
         | 
| 128 | 
            +
                headers.should have_key('Accept')
         | 
| 129 | 
            +
                headers['Accept'].should == 'application/json, audio/mpeg'
         | 
| 130 | 
            +
             | 
| 131 | 
            +
                @request.should_receive(:default_headers).and_return({})
         | 
| 132 | 
            +
                headers = @request.make_headers(:accept => :json)
         | 
| 133 | 
            +
                headers.should have_key('Accept')
         | 
| 134 | 
            +
                headers['Accept'].should == 'application/json'
         | 
| 135 | 
            +
              end
         | 
| 136 | 
            +
             | 
| 137 | 
            +
              it "converts header values to strings" do
         | 
| 138 | 
            +
                @request.make_headers('A' => 1)['A'].should == '1'
         | 
| 139 | 
            +
              end
         | 
| 140 | 
            +
             | 
| 141 | 
            +
              it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
         | 
| 142 | 
            +
                @request.should_receive(:parse_url_with_auth).with('http://some/resource').and_return(@uri)
         | 
| 143 | 
            +
                klass = mock("net:http class")
         | 
| 144 | 
            +
                @request.should_receive(:net_http_request_class).with(:put).and_return(klass)
         | 
| 145 | 
            +
                klass.should_receive(:new).and_return('result')
         | 
| 146 | 
            +
                @request.should_receive(:transmit).with(@uri, 'result', kind_of(RestClient::Payload::Base))
         | 
| 147 | 
            +
                @request.execute_inner
         | 
| 148 | 
            +
              end
         | 
| 149 | 
            +
             | 
| 150 | 
            +
              it "transmits the request with Net::HTTP" do
         | 
| 151 | 
            +
                @http.should_receive(:request).with('req', 'payload')
         | 
| 152 | 
            +
                @request.should_receive(:process_result)
         | 
| 153 | 
            +
                @request.should_receive(:response_log)
         | 
| 154 | 
            +
                @request.transmit(@uri, 'req', 'payload')
         | 
| 155 | 
            +
              end
         | 
| 156 | 
            +
             | 
| 157 | 
            +
              it "uses SSL when the URI refers to a https address" do
         | 
| 158 | 
            +
                @uri.stub!(:is_a?).with(URI::HTTPS).and_return(true)
         | 
| 159 | 
            +
                @net.should_receive(:use_ssl=).with(true)
         | 
| 160 | 
            +
                @http.stub!(:request)
         | 
| 161 | 
            +
                @request.stub!(:process_result)
         | 
| 162 | 
            +
                @request.stub!(:response_log)
         | 
| 163 | 
            +
                @request.transmit(@uri, 'req', 'payload')
         | 
| 164 | 
            +
              end
         | 
| 165 | 
            +
             | 
| 166 | 
            +
              it "sends nil payloads" do
         | 
| 167 | 
            +
                @http.should_receive(:request).with('req', nil)
         | 
| 168 | 
            +
                @request.should_receive(:process_result)
         | 
| 169 | 
            +
                @request.stub!(:response_log)
         | 
| 170 | 
            +
                @request.transmit(@uri, 'req', nil)
         | 
| 171 | 
            +
              end
         | 
| 172 | 
            +
             | 
| 173 | 
            +
              it "passes non-hash payloads straight through" do
         | 
| 174 | 
            +
                @request.process_payload("x").should == "x"
         | 
| 175 | 
            +
              end
         | 
| 176 | 
            +
             | 
| 177 | 
            +
              it "converts a hash payload to urlencoded data" do
         | 
| 178 | 
            +
                @request.process_payload(:a => 'b c+d').should == "a=b%20c%2Bd"
         | 
| 179 | 
            +
              end
         | 
| 180 | 
            +
             | 
| 181 | 
            +
              it "accepts nested hashes in payload" do
         | 
| 182 | 
            +
                payload = @request.process_payload(:user => { :name => 'joe', :location => { :country => 'USA', :state => 'CA' }})
         | 
| 183 | 
            +
                payload.should include('user[name]=joe')
         | 
| 184 | 
            +
                payload.should include('user[location][country]=USA')
         | 
| 185 | 
            +
                payload.should include('user[location][state]=CA')
         | 
| 186 | 
            +
              end
         | 
| 187 | 
            +
             | 
| 188 | 
            +
              it "set urlencoded content_type header on hash payloads" do
         | 
| 189 | 
            +
                @request.process_payload(:a => 1)
         | 
| 190 | 
            +
                @request.headers[:content_type].should == 'application/x-www-form-urlencoded'
         | 
| 191 | 
            +
              end
         | 
| 192 | 
            +
             | 
| 193 | 
            +
              it "sets up the credentials prior to the request" do
         | 
| 194 | 
            +
                @http.stub!(:request)
         | 
| 195 | 
            +
                @request.stub!(:process_result)
         | 
| 196 | 
            +
                @request.stub!(:response_log)
         | 
| 197 | 
            +
             | 
| 198 | 
            +
                @request.stub!(:user).and_return('joe')
         | 
| 199 | 
            +
                @request.stub!(:password).and_return('mypass')
         | 
| 200 | 
            +
                @request.should_receive(:setup_credentials).with('req')
         | 
| 201 | 
            +
             | 
| 202 | 
            +
                @request.transmit(@uri, 'req', nil)
         | 
| 203 | 
            +
              end
         | 
| 204 | 
            +
             | 
| 205 | 
            +
              it "does not attempt to send any credentials if user is nil" do
         | 
| 206 | 
            +
                @request.stub!(:user).and_return(nil)
         | 
| 207 | 
            +
                req = mock("request")
         | 
| 208 | 
            +
                req.should_not_receive(:basic_auth)
         | 
| 209 | 
            +
                @request.setup_credentials(req)
         | 
| 210 | 
            +
              end
         | 
| 211 | 
            +
             | 
| 212 | 
            +
              it "setup credentials when there's a user" do
         | 
| 213 | 
            +
                @request.stub!(:user).and_return('joe')
         | 
| 214 | 
            +
                @request.stub!(:password).and_return('mypass')
         | 
| 215 | 
            +
                req = mock("request")
         | 
| 216 | 
            +
                req.should_receive(:basic_auth).with('joe', 'mypass')
         | 
| 217 | 
            +
                @request.setup_credentials(req)
         | 
| 218 | 
            +
              end
         | 
| 219 | 
            +
             | 
| 220 | 
            +
              it "catches EOFError and shows the more informative ServerBrokeConnection" do
         | 
| 221 | 
            +
                @http.stub!(:request).and_raise(EOFError)
         | 
| 222 | 
            +
                lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::ServerBrokeConnection)
         | 
| 223 | 
            +
              end
         | 
| 224 | 
            +
             | 
| 225 | 
            +
              it "execute calls execute_inner" do
         | 
| 226 | 
            +
                @request.should_receive(:execute_inner)
         | 
| 227 | 
            +
                @request.execute
         | 
| 228 | 
            +
              end
         | 
| 229 | 
            +
             | 
| 230 | 
            +
              it "class method execute wraps constructor" do
         | 
| 231 | 
            +
                req = mock("rest request")
         | 
| 232 | 
            +
                RestClient::Request.should_receive(:new).with(1 => 2).and_return(req)
         | 
| 233 | 
            +
                req.should_receive(:execute)
         | 
| 234 | 
            +
                RestClient::Request.execute(1 => 2)
         | 
| 235 | 
            +
              end
         | 
| 236 | 
            +
             | 
| 237 | 
            +
              it "raises a Redirect with the new location when the response is in the 30x range" do
         | 
| 238 | 
            +
                res = mock('response', :code => '301', :header => { 'Location' => 'http://new/resource' })
         | 
| 239 | 
            +
                lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://new/resource'}
         | 
| 240 | 
            +
              end
         | 
| 241 | 
            +
             | 
| 242 | 
            +
              it "handles redirects with relative paths" do
         | 
| 243 | 
            +
                res = mock('response', :code => '301', :header => { 'Location' => 'index' })
         | 
| 244 | 
            +
                lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
         | 
| 245 | 
            +
              end
         | 
| 246 | 
            +
             | 
| 247 | 
            +
              it "handles redirects with absolute paths" do
         | 
| 248 | 
            +
                @request.instance_variable_set('@url', 'http://some/place/else')
         | 
| 249 | 
            +
                res = mock('response', :code => '301', :header => { 'Location' => '/index' })
         | 
| 250 | 
            +
                lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
         | 
| 251 | 
            +
              end
         | 
| 252 | 
            +
             | 
| 253 | 
            +
              it "uses GET and clears payload when following 30x redirects" do
         | 
| 254 | 
            +
                url = "http://example.com/redirected"
         | 
| 255 | 
            +
             | 
| 256 | 
            +
                @request.should_receive(:execute_inner).once.ordered.and_raise(RestClient::Redirect.new(url))
         | 
| 257 | 
            +
             | 
| 258 | 
            +
                @request.should_receive(:execute_inner).once.ordered do
         | 
| 259 | 
            +
                  @request.url.should == url
         | 
| 260 | 
            +
                  @request.method.should == :get
         | 
| 261 | 
            +
                  @request.payload.should be_nil
         | 
| 262 | 
            +
                end
         | 
| 263 | 
            +
             | 
| 264 | 
            +
                @request.execute
         | 
| 265 | 
            +
              end
         | 
| 266 | 
            +
             | 
| 267 | 
            +
              it "raises Unauthorized when the response is 401" do
         | 
| 268 | 
            +
                res = mock('response', :code => '401')
         | 
| 269 | 
            +
                lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized)
         | 
| 270 | 
            +
              end
         | 
| 271 | 
            +
             | 
| 272 | 
            +
              it "raises ResourceNotFound when the response is 404" do
         | 
| 273 | 
            +
                res = mock('response', :code => '404')
         | 
| 274 | 
            +
                lambda { @request.process_result(res) }.should raise_error(RestClient::ResourceNotFound)
         | 
| 275 | 
            +
              end
         | 
| 276 | 
            +
             | 
| 277 | 
            +
              it "raises RequestFailed otherwise" do
         | 
| 278 | 
            +
                res = mock('response', :code => '500')
         | 
| 279 | 
            +
                lambda { @request.process_result(res) }.should raise_error(RestClient::RequestFailed)
         | 
| 280 | 
            +
              end
         | 
| 281 | 
            +
             | 
| 282 | 
            +
              it "creates a proxy class if a proxy url is given" do
         | 
| 283 | 
            +
                RestClient.stub!(:proxy).and_return("http://example.com/")
         | 
| 284 | 
            +
                @request.net_http_class.should include(Net::HTTP::ProxyDelta)
         | 
| 285 | 
            +
              end
         | 
| 286 | 
            +
             | 
| 287 | 
            +
              it "creates a non-proxy class if a proxy url is not given" do
         | 
| 288 | 
            +
                @request.net_http_class.should_not include(Net::HTTP::ProxyDelta)
         | 
| 289 | 
            +
              end
         | 
| 290 | 
            +
             | 
| 291 | 
            +
              it "logs a get request" do
         | 
| 292 | 
            +
                ['RestClient.get "http://url", headers: {"Accept-encoding"=>"gzip, deflate", "Accept"=>"*/*; q=0.5, application/xml"}',
         | 
| 293 | 
            +
                 'RestClient.get "http://url", headers: {"Accept"=>"*/*; q=0.5, application/xml", "Accept-encoding"=>"gzip, deflate}'].should include
         | 
| 294 | 
            +
                RestClient::Request.new(:method => :get, :url => 'http://url').request_log
         | 
| 295 | 
            +
              end
         | 
| 296 | 
            +
             | 
| 297 | 
            +
              it "logs a post request with a small payload" do
         | 
| 298 | 
            +
                ['RestClient.post "http://url", headers: {"Accept-encoding"=>"gzip, deflate", "Content-Length"=>"3", "Accept"=>"*/*; q=0.5, application/xml"}, paylod: "foo"',
         | 
| 299 | 
            +
                 'RestClient.post "http://url", headers: {"Accept"=>"*/*; q=0.5, application/xml", "Accept-encoding"=>"gzip, deflate", "Content-Length"=>"3"}, paylod: "foo"'].should include
         | 
| 300 | 
            +
                RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').request_log
         | 
| 301 | 
            +
              end
         | 
| 302 | 
            +
             | 
| 303 | 
            +
              it "logs a post request with a large payload" do
         | 
| 304 | 
            +
                ['RestClient.post "http://url", headers: {"Accept-encoding"=>"gzip, deflate", "Content-Length"=>"1000", "Accept"=>"*/*; q=0.5, application/xml"}, paylod: 1000 byte length',
         | 
| 305 | 
            +
                 'RestClient.post "http://url", headers: {"Accept"=>"*/*; q=0.5, application/xml", "Accept-encoding"=>"gzip, deflate", "Content-Length"=>"1000"}, paylod: 1000 byte length'].should include
         | 
| 306 | 
            +
                RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).request_log
         | 
| 307 | 
            +
              end
         | 
| 308 | 
            +
             | 
| 309 | 
            +
              it "logs input headers as a hash" do
         | 
| 310 | 
            +
                ['RestClient.get "http://url", headers: {"Accept-encoding"=>"gzip, deflate", "Accept"=>"text/plain"}',
         | 
| 311 | 
            +
                 'RestClient.get "http://url", headers: {"Accept"=>"text/plain", "Accept-encoding"=>"gzip, deflate"}'].should include
         | 
| 312 | 
            +
                RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' })
         | 
| 313 | 
            +
              end
         | 
| 314 | 
            +
             | 
| 315 | 
            +
              it "logs a response including the status code, content type, and result body size in bytes" do
         | 
| 316 | 
            +
                res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
         | 
| 317 | 
            +
                res.stub!(:[]).with('Content-type').and_return('text/html')
         | 
| 318 | 
            +
                @request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
         | 
| 319 | 
            +
              end
         | 
| 320 | 
            +
             | 
| 321 | 
            +
              it "logs a response with a nil Content-type" do
         | 
| 322 | 
            +
                res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
         | 
| 323 | 
            +
                res.stub!(:[]).with('Content-type').and_return(nil)
         | 
| 324 | 
            +
                @request.response_log(res).should == "# => 200 OK |  4 bytes"
         | 
| 325 | 
            +
              end
         | 
| 326 | 
            +
             | 
| 327 | 
            +
              it "logs a response with a nil body" do
         | 
| 328 | 
            +
                res = mock('result', :code => '200', :class => Net::HTTPOK, :body => nil)
         | 
| 329 | 
            +
                res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
         | 
| 330 | 
            +
                @request.response_log(res).should == "# => 200 OK | text/html 0 bytes"
         | 
| 331 | 
            +
              end
         | 
| 332 | 
            +
             | 
| 333 | 
            +
              it "strips the charset from the response content type" do
         | 
| 334 | 
            +
                res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
         | 
| 335 | 
            +
                res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
         | 
| 336 | 
            +
                @request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
         | 
| 337 | 
            +
              end
         | 
| 338 | 
            +
             | 
| 339 | 
            +
              it "displays the log to stdout" do
         | 
| 340 | 
            +
                RestClient.stub!(:log).and_return('stdout')
         | 
| 341 | 
            +
                STDOUT.should_receive(:puts).with('xyz')
         | 
| 342 | 
            +
                @request.display_log('xyz')
         | 
| 343 | 
            +
              end
         | 
| 344 | 
            +
             | 
| 345 | 
            +
              it "displays the log to stderr" do
         | 
| 346 | 
            +
                RestClient.stub!(:log).and_return('stderr')
         | 
| 347 | 
            +
                STDERR.should_receive(:puts).with('xyz')
         | 
| 348 | 
            +
                @request.display_log('xyz')
         | 
| 349 | 
            +
              end
         | 
| 350 | 
            +
             | 
| 351 | 
            +
              it "append the log to the requested filename" do
         | 
| 352 | 
            +
                RestClient.stub!(:log).and_return('/tmp/restclient.log')
         | 
| 353 | 
            +
                f = mock('file handle')
         | 
| 354 | 
            +
                File.should_receive(:open).with('/tmp/restclient.log', 'a').and_yield(f)
         | 
| 355 | 
            +
                f.should_receive(:puts).with('xyz')
         | 
| 356 | 
            +
                @request.display_log('xyz')
         | 
| 357 | 
            +
              end
         | 
| 358 | 
            +
             | 
| 359 | 
            +
              it "set read_timeout" do
         | 
| 360 | 
            +
                @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123)
         | 
| 361 | 
            +
                @http.stub!(:request)
         | 
| 362 | 
            +
                @request.stub!(:process_result)
         | 
| 363 | 
            +
                @request.stub!(:response_log)
         | 
| 364 | 
            +
             | 
| 365 | 
            +
                @net.should_receive(:read_timeout=).with(123)
         | 
| 366 | 
            +
             | 
| 367 | 
            +
                @request.transmit(@uri, 'req', nil)
         | 
| 368 | 
            +
              end
         | 
| 369 | 
            +
             | 
| 370 | 
            +
              it "set open_timeout" do
         | 
| 371 | 
            +
                @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :open_timeout => 123)
         | 
| 372 | 
            +
                @http.stub!(:request)
         | 
| 373 | 
            +
                @request.stub!(:process_result)
         | 
| 374 | 
            +
                @request.stub!(:response_log)
         | 
| 375 | 
            +
             | 
| 376 | 
            +
                @net.should_receive(:open_timeout=).with(123)
         | 
| 377 | 
            +
             | 
| 378 | 
            +
                @request.transmit(@uri, 'req', nil)
         | 
| 379 | 
            +
              end
         | 
| 380 | 
            +
             | 
| 381 | 
            +
              it "should default to not verifying ssl certificates" do
         | 
| 382 | 
            +
                @request.verify_ssl.should == false
         | 
| 383 | 
            +
              end
         | 
| 384 | 
            +
             | 
| 385 | 
            +
              it "should set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is false" do
         | 
| 386 | 
            +
                @net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
         | 
| 387 | 
            +
                @http.stub!(:request)
         | 
| 388 | 
            +
                @request.stub!(:process_result)
         | 
| 389 | 
            +
                @request.stub!(:response_log)
         | 
| 390 | 
            +
                @request.transmit(@uri, 'req', 'payload')
         | 
| 391 | 
            +
              end
         | 
| 392 | 
            +
             | 
| 393 | 
            +
              it "should not set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is true" do
         | 
| 394 | 
            +
                @request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true)
         | 
| 395 | 
            +
                @net.should_not_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
         | 
| 396 | 
            +
                @http.stub!(:request)
         | 
| 397 | 
            +
                @request.stub!(:process_result)
         | 
| 398 | 
            +
                @request.stub!(:response_log)
         | 
| 399 | 
            +
                @request.transmit(@uri, 'req', 'payload')
         | 
| 400 | 
            +
              end
         | 
| 401 | 
            +
             | 
| 402 | 
            +
              it "should set net.verify_mode to the passed value if verify_ssl is an OpenSSL constant" do
         | 
| 403 | 
            +
                mode = OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
         | 
| 404 | 
            +
                @request = RestClient::Request.new( :method => :put,
         | 
| 405 | 
            +
                                                    :url => 'https://some/resource',
         | 
| 406 | 
            +
                                                    :payload => 'payload',
         | 
| 407 | 
            +
                                                    :verify_ssl => mode )
         | 
| 408 | 
            +
                @net.should_receive(:verify_mode=).with(mode)
         | 
| 409 | 
            +
                @http.stub!(:request)
         | 
| 410 | 
            +
                @request.stub!(:process_result)
         | 
| 411 | 
            +
                @request.stub!(:response_log)
         | 
| 412 | 
            +
                @request.transmit(@uri, 'req', 'payload')
         | 
| 413 | 
            +
              end
         | 
| 414 | 
            +
             | 
| 415 | 
            +
              it "should default to not having an ssl_client_cert" do
         | 
| 416 | 
            +
                @request.ssl_client_cert.should be(nil)
         | 
| 417 | 
            +
              end
         | 
| 418 | 
            +
             | 
| 419 | 
            +
              it "should set the ssl_client_cert if provided" do
         | 
| 420 | 
            +
                @request = RestClient::Request.new(
         | 
| 421 | 
            +
                        :method => :put,
         | 
| 422 | 
            +
                        :url => 'https://some/resource',
         | 
| 423 | 
            +
                        :payload => 'payload',
         | 
| 424 | 
            +
                        :ssl_client_cert => "whatsupdoc!"
         | 
| 425 | 
            +
                )
         | 
| 426 | 
            +
                @net.should_receive(:cert=).with("whatsupdoc!")
         | 
| 427 | 
            +
                @http.stub!(:request)
         | 
| 428 | 
            +
                @request.stub!(:process_result)
         | 
| 429 | 
            +
                @request.stub!(:response_log)
         | 
| 430 | 
            +
                @request.transmit(@uri, 'req', 'payload')
         | 
| 431 | 
            +
              end
         | 
| 432 | 
            +
             | 
| 433 | 
            +
              it "should not set the ssl_client_cert if it is not provided" do
         | 
| 434 | 
            +
                @request = RestClient::Request.new(
         | 
| 435 | 
            +
                        :method => :put,
         | 
| 436 | 
            +
                        :url => 'https://some/resource',
         | 
| 437 | 
            +
                        :payload => 'payload'
         | 
| 438 | 
            +
                )
         | 
| 439 | 
            +
                @net.should_not_receive(:cert=).with("whatsupdoc!")
         | 
| 440 | 
            +
                @http.stub!(:request)
         | 
| 441 | 
            +
                @request.stub!(:process_result)
         | 
| 442 | 
            +
                @request.stub!(:response_log)
         | 
| 443 | 
            +
                @request.transmit(@uri, 'req', 'payload')
         | 
| 444 | 
            +
              end
         | 
| 445 | 
            +
             | 
| 446 | 
            +
              it "should default to not having an ssl_client_key" do
         | 
| 447 | 
            +
                @request.ssl_client_key.should be(nil)
         | 
| 448 | 
            +
              end
         | 
| 449 | 
            +
             | 
| 450 | 
            +
              it "should set the ssl_client_key if provided" do
         | 
| 451 | 
            +
                @request = RestClient::Request.new(
         | 
| 452 | 
            +
                        :method => :put,
         | 
| 453 | 
            +
                        :url => 'https://some/resource',
         | 
| 454 | 
            +
                        :payload => 'payload',
         | 
| 455 | 
            +
                        :ssl_client_key => "whatsupdoc!"
         | 
| 456 | 
            +
                )
         | 
| 457 | 
            +
                @net.should_receive(:key=).with("whatsupdoc!")
         | 
| 458 | 
            +
                @http.stub!(:request)
         | 
| 459 | 
            +
                @request.stub!(:process_result)
         | 
| 460 | 
            +
                @request.stub!(:response_log)
         | 
| 461 | 
            +
                @request.transmit(@uri, 'req', 'payload')
         | 
| 462 | 
            +
              end
         | 
| 463 | 
            +
             | 
| 464 | 
            +
              it "should not set the ssl_client_key if it is not provided" do
         | 
| 465 | 
            +
                @request = RestClient::Request.new(
         | 
| 466 | 
            +
                        :method => :put,
         | 
| 467 | 
            +
                        :url => 'https://some/resource',
         | 
| 468 | 
            +
                        :payload => 'payload'
         | 
| 469 | 
            +
                )
         | 
| 470 | 
            +
                @net.should_not_receive(:key=).with("whatsupdoc!")
         | 
| 471 | 
            +
                @http.stub!(:request)
         | 
| 472 | 
            +
                @request.stub!(:process_result)
         | 
| 473 | 
            +
                @request.stub!(:response_log)
         | 
| 474 | 
            +
                @request.transmit(@uri, 'req', 'payload')
         | 
| 475 | 
            +
              end
         | 
| 476 | 
            +
             | 
| 477 | 
            +
              it "should default to not having an ssl_ca_file" do
         | 
| 478 | 
            +
                @request.ssl_ca_file.should be(nil)
         | 
| 479 | 
            +
              end
         | 
| 480 | 
            +
             | 
| 481 | 
            +
              it "should set the ssl_ca_file if provided" do
         | 
| 482 | 
            +
                @request = RestClient::Request.new(
         | 
| 483 | 
            +
                        :method => :put,
         | 
| 484 | 
            +
                        :url => 'https://some/resource',
         | 
| 485 | 
            +
                        :payload => 'payload',
         | 
| 486 | 
            +
                        :ssl_ca_file => "Certificate Authority File"
         | 
| 487 | 
            +
                )
         | 
| 488 | 
            +
                @net.should_receive(:ca_file=).with("Certificate Authority File")
         | 
| 489 | 
            +
                @http.stub!(:request)
         | 
| 490 | 
            +
                @request.stub!(:process_result)
         | 
| 491 | 
            +
                @request.stub!(:response_log)
         | 
| 492 | 
            +
                @request.transmit(@uri, 'req', 'payload')
         | 
| 493 | 
            +
              end
         | 
| 494 | 
            +
             | 
| 495 | 
            +
              it "should not set the ssl_ca_file if it is not provided" do
         | 
| 496 | 
            +
                @request = RestClient::Request.new(
         | 
| 497 | 
            +
                        :method => :put,
         | 
| 498 | 
            +
                        :url => 'https://some/resource',
         | 
| 499 | 
            +
                        :payload => 'payload'
         | 
| 500 | 
            +
                )
         | 
| 501 | 
            +
                @net.should_not_receive(:ca_file=).with("Certificate Authority File")
         | 
| 502 | 
            +
                @http.stub!(:request)
         | 
| 503 | 
            +
                @request.stub!(:process_result)
         | 
| 504 | 
            +
                @request.stub!(:response_log)
         | 
| 505 | 
            +
                @request.transmit(@uri, 'req', 'payload')
         | 
| 506 | 
            +
              end
         | 
| 507 | 
            +
             | 
| 508 | 
            +
              it "should still return a response object for 204 No Content responses" do
         | 
| 509 | 
            +
                @request = RestClient::Request.new(
         | 
| 510 | 
            +
                        :method => :put,
         | 
| 511 | 
            +
                        :url => 'https://some/resource',
         | 
| 512 | 
            +
                        :payload => 'payload'
         | 
| 513 | 
            +
                )
         | 
| 514 | 
            +
                net_http_res = Net::HTTPNoContent.new("", "204", "No Content")
         | 
| 515 | 
            +
                net_http_res.stub(:read_body).and_return(nil)
         | 
| 516 | 
            +
                @http.should_receive(:request).and_return(@request.fetch_body(net_http_res))
         | 
| 517 | 
            +
                response = @request.transmit(@uri, 'req', 'payload')
         | 
| 518 | 
            +
                response.should_not be_nil
         | 
| 519 | 
            +
                response.code.should equal(204)
         | 
| 520 | 
            +
              end
         | 
| 497 521 | 
             
            end
         |