1 # Copyright 2012 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 require 'google/api_client'
18 require 'google/api_client/version'
20 describe Google::APIClient::Gzip do
22 def create_connection(&block)
25 b.adapter :test do |stub|
31 it 'should ignore non-zipped content' do
32 conn = create_connection do |env|
33 [200, {}, 'Hello world']
35 result = conn.get('/')
36 result.body.should == "Hello world"
39 it 'should decompress gziped content' do
40 conn = create_connection do |env|
41 [200, { 'Content-Encoding' => 'gzip'}, Base64.decode64('H4sICLVGwlEAA3RtcADzSM3JyVcozy/KSeECANXgObcMAAAA')]
43 result = conn.get('/')
44 result.body.should == "Hello world\n"
47 describe 'with API Client' do
50 @client = Google::APIClient.new(:application_name => 'test')
51 @client.authorization = nil
55 it 'should send gzip in user agent' do
56 conn = create_connection do |env|
57 agent = env[:request_headers]['User-Agent']
58 agent.should_not be_nil
59 agent.should include 'gzip'
60 [200, {}, 'Hello world']
62 @client.execute(:uri => 'http://www.example.com/', :connection => conn)
65 it 'should send gzip in accept-encoding' do
66 conn = create_connection do |env|
67 encoding = env[:request_headers]['Accept-Encoding']
68 encoding.should_not be_nil
69 encoding.should include 'gzip'
70 [200, {}, 'Hello world']
72 @client.execute(:uri => 'http://www.example.com/', :connection => conn)
75 it 'should not send gzip in accept-encoding if disabled for request' do
76 conn = create_connection do |env|
77 encoding = env[:request_headers]['Accept-Encoding']
78 encoding.should_not include('gzip') unless encoding.nil?
79 [200, {}, 'Hello world']
81 response = @client.execute(:uri => 'http://www.example.com/', :gzip => false, :connection => conn)