Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8568: added_deflate_support.diff

File added_deflate_support.diff, 2.5 kB (added by foeken, 2 years ago)

Properly, with tests

  • test/connection_test.rb

    old new  
    11require "#{File.dirname(__FILE__)}/abstract_unit" 
    22require 'base64' 
     3require 'zlib' 
    34 
    45class ConnectionTest < Test::Unit::TestCase 
    56  ResponseCodeStub = Struct.new(:code) 
     
    2223      mock.get    "/people_single_elements.xml", {}, @people_single 
    2324      mock.get    "/people_empty_elements.xml", {}, @people_empty 
    2425      mock.get    "/people/1.xml", {}, @matz 
     26      mock.get    "/people/3.xml", {}, Zlib::Deflate.deflate(@matz), 200, { 'content-encoding' => 'deflate' } 
    2527      mock.put    "/people/1.xml", {}, nil, 204 
    2628      mock.put    "/people/2.xml", {}, @header, 204 
    2729      mock.delete "/people/1.xml", {}, nil, 200 
     
    9698    matz = @conn.get("/people/1.xml") 
    9799    assert_equal "Matz", matz["name"] 
    98100  end 
    99  
     101   
    100102  def test_get_with_header 
    101103    david = @conn.get("/people/2.xml", @header) 
    102104    assert_equal "David", david["name"] 
    103105  end 
     106   
     107  def test_get_deflated_header 
     108    matz = @conn.get("/people/3.xml") 
     109    assert_equal "Matz", matz["name"] 
     110  end 
    104111 
    105112  def test_get_collection 
    106113    people = @conn.get("/people.xml") 
  • lib/active_resource/connection.rb

    old new  
    8181    end 
    8282 
    8383    def xml_from_response(response) 
    84       if response = from_xml_data(Hash.from_xml(response.body)) 
     84             
     85      # Check the content-encoding header and act accordingly 
     86      case response.header["content-encoding"] 
     87        when "deflate" then response_body = Zlib::Inflate.inflate(response.body) 
     88        else                response_body = response.body 
     89      end 
     90 
     91      if response = from_xml_data(Hash.from_xml(response_body)) 
    8592        response.first 
    8693      else 
    8794        nil 
    8895      end 
     96       
    8997    end 
    9098 
    9199 
  • lib/active_resource/http_mock.rb

    old new  
    9898      @body, @message, @headers = body, message.to_s, headers 
    9999      @code = @message[0,3].to_i 
    100100    end 
     101     
     102    # Added this, since in real life the headers are called header :/ 
     103    def header 
     104      headers 
     105    end 
     106     
     107    def header=(header) 
     108      headers 
     109    end       
    101110 
    102111    def success? 
    103112      (200..299).include?(code)