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'
19 describe Google::APIClient::Gzip do
21 def create_connection(&block)
24 b.adapter :test do |stub|
30 it 'should ignore non-zipped content' do
31 conn = create_connection do |env|
32 [200, {}, 'Hello world']
34 result = conn.get('/')
35 expect(result.body).to eq("Hello world")
38 it 'should decompress gziped content' do
39 conn = create_connection do |env|
40 [200, { 'Content-Encoding' => 'gzip'}, Base64.decode64('H4sICLVGwlEAA3RtcADzSM3JyVcozy/KSeECANXgObcMAAAA')]
42 result = conn.get('/')
43 expect(result.body).to eq("Hello world\n")
46 describe 'with API Client' do
49 @client = Google::APIClient.new(:application_name => 'test')
50 @client.authorization = nil
54 it 'should send gzip in user agent' do
55 conn = create_connection do |env|
56 agent = env[:request_headers]['User-Agent']
57 expect(agent).not_to be_nil
58 expect(agent).to include 'gzip'
59 [200, {}, 'Hello world']
61 @client.execute(:uri => 'http://www.example.com/', :connection => conn)
64 it 'should send gzip in accept-encoding' do
65 conn = create_connection do |env|
66 encoding = env[:request_headers]['Accept-Encoding']
67 expect(encoding).not_to be_nil
68 expect(encoding).to include 'gzip'
69 [200, {}, 'Hello world']
71 @client.execute(:uri => 'http://www.example.com/', :connection => conn)
74 it 'should not send gzip in accept-encoding if disabled for request' do
75 conn = create_connection do |env|
76 encoding = env[:request_headers]['Accept-Encoding']
77 expect(encoding).not_to include('gzip') unless encoding.nil?
78 [200, {}, 'Hello world']
80 response = @client.execute(:uri => 'http://www.example.com/', :gzip => false, :connection => conn)