21700: Install Bundler system-wide in Rails postinst
[arvados.git] / sdk / ruby-google-api-client / spec / google / api_client / gzip_spec.rb
1 # Encoding: utf-8
2 # Copyright 2012 Google Inc.
3 #
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
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15
16 require 'spec_helper'
17
18 require 'google/api_client'
19
20 RSpec.describe Google::APIClient::Gzip do
21
22   def create_connection(&block)
23     Faraday.new do |b|
24       b.response :charset
25       b.response :gzip
26       b.adapter :test do |stub|
27         stub.get '/', &block
28       end
29     end
30   end  
31
32   it 'should ignore non-zipped content' do
33     conn = create_connection do |env|
34       [200, {}, 'Hello world']
35     end
36     result = conn.get('/')
37     expect(result.body).to eq("Hello world")
38   end
39
40   it 'should decompress gziped content' do
41     conn = create_connection do |env|
42       [200, { 'Content-Encoding' => 'gzip'}, Base64.decode64('H4sICLVGwlEAA3RtcADzSM3JyVcozy/KSeECANXgObcMAAAA')]
43     end
44     result = conn.get('/')
45     expect(result.body).to eq("Hello world\n")
46   end
47   
48   it 'should inflate with the correct charset encoding' do
49     conn = create_connection do |env|
50       [200, 
51         { 'Content-Encoding' => 'deflate', 'Content-Type' => 'application/json;charset=BIG5'}, 
52         Base64.decode64('eJxb8nLp7t2VAA8fBCI=')]
53     end
54     result = conn.get('/')
55     expect(result.body.encoding).to eq(Encoding::BIG5)
56     expect(result.body).to eq('日本語'.encode("BIG5"))
57   end
58
59   describe 'with API Client' do
60
61     before do
62       @client = Google::APIClient.new(:application_name => 'test')
63       @client.authorization = nil
64     end
65     
66     
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']
73       end
74       @client.execute(:uri => 'http://www.example.com/', :connection => conn)
75     end
76
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']
83       end
84       @client.execute(:uri => 'http://www.example.com/', :connection => conn)
85     end
86     
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']
92       end
93       response = @client.execute(:uri => 'http://www.example.com/', :gzip => false, :connection => conn)
94       puts response.status
95     end
96     
97   end
98 end