So many versions!!
[arvados.git] / spec / google / api_client / gzip_spec.rb
1 # Copyright 2012 Google Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 require 'spec_helper'
16
17 require 'google/api_client'
18
19 describe Google::APIClient::Gzip do
20
21   def create_connection(&block)
22     Faraday.new do |b|
23       b.response :gzip
24       b.adapter :test do |stub|
25         stub.get '/', &block
26       end
27     end
28   end  
29
30   it 'should ignore non-zipped content' do
31     conn = create_connection do |env|
32       [200, {}, 'Hello world']
33     end
34     result = conn.get('/')
35     expect(result.body).to eq("Hello world")
36   end
37
38   it 'should decompress gziped content' do
39     conn = create_connection do |env|
40       [200, { 'Content-Encoding' => 'gzip'}, Base64.decode64('H4sICLVGwlEAA3RtcADzSM3JyVcozy/KSeECANXgObcMAAAA')]
41     end
42     result = conn.get('/')
43     expect(result.body).to eq("Hello world\n")
44   end
45   
46   describe 'with API Client' do
47
48     before do
49       @client = Google::APIClient.new(:application_name => 'test')
50       @client.authorization = nil
51     end
52     
53     
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']
60       end
61       @client.execute(:uri => 'http://www.example.com/', :connection => conn)
62     end
63
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']
70       end
71       @client.execute(:uri => 'http://www.example.com/', :connection => conn)
72     end
73     
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']
79       end
80       response = @client.execute(:uri => 'http://www.example.com/', :gzip => false, :connection => conn)
81       puts response.status
82     end
83     
84   end
85 end