2 # Copyright 2012 Google Inc.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 require 'google/api_client'
20 RSpec.describe Google::APIClient::Gzip do
22 def create_connection(&block)
26 b.adapter :test do |stub|
32 it 'should ignore non-zipped content' do
33 conn = create_connection do |env|
34 [200, {}, 'Hello world']
36 result = conn.get('/')
37 expect(result.body).to eq("Hello world")
40 it 'should decompress gziped content' do
41 conn = create_connection do |env|
42 [200, { 'Content-Encoding' => 'gzip'}, Base64.decode64('H4sICLVGwlEAA3RtcADzSM3JyVcozy/KSeECANXgObcMAAAA')]
44 result = conn.get('/')
45 expect(result.body).to eq("Hello world\n")
48 it 'should inflate with the correct charset encoding' do
49 conn = create_connection do |env|
51 { 'Content-Encoding' => 'deflate', 'Content-Type' => 'application/json;charset=BIG5'},
52 Base64.decode64('eJxb8nLp7t2VAA8fBCI=')]
54 result = conn.get('/')
55 expect(result.body.encoding).to eq(Encoding::BIG5)
56 expect(result.body).to eq('日本語'.encode("BIG5"))
59 describe 'with API Client' do
62 @client = Google::APIClient.new(:application_name => 'test')
63 @client.authorization = nil
67 it 'should send gzip in user agent' do
68 conn = create_connection do |env|
69 agent = env[:request_headers]['User-Agent']
70 expect(agent).not_to be_nil
71 expect(agent).to include 'gzip'
72 [200, {}, 'Hello world']
74 @client.execute(:uri => 'http://www.example.com/', :connection => conn)
77 it 'should send gzip in accept-encoding' do
78 conn = create_connection do |env|
79 encoding = env[:request_headers]['Accept-Encoding']
80 expect(encoding).not_to be_nil
81 expect(encoding).to include 'gzip'
82 [200, {}, 'Hello world']
84 @client.execute(:uri => 'http://www.example.com/', :connection => conn)
87 it 'should not send gzip in accept-encoding if disabled for request' do
88 conn = create_connection do |env|
89 encoding = env[:request_headers]['Accept-Encoding']
90 expect(encoding).not_to include('gzip') unless encoding.nil?
91 [200, {}, 'Hello world']
93 response = @client.execute(:uri => 'http://www.example.com/', :gzip => false, :connection => conn)