Updated rspec to v3 and replaced rcov with simplecov.
[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 require 'google/api_client/version'
19
20 describe Google::APIClient::Gzip do
21
22   def create_connection(&block)
23     Faraday.new do |b|
24       b.response :gzip
25       b.adapter :test do |stub|
26         stub.get '/', &block
27       end
28     end
29   end  
30
31   it 'should ignore non-zipped content' do
32     conn = create_connection do |env|
33       [200, {}, 'Hello world']
34     end
35     result = conn.get('/')
36     expect(result.body).to eq("Hello world")
37   end
38
39   it 'should decompress gziped content' do
40     conn = create_connection do |env|
41       [200, { 'Content-Encoding' => 'gzip'}, Base64.decode64('H4sICLVGwlEAA3RtcADzSM3JyVcozy/KSeECANXgObcMAAAA')]
42     end
43     result = conn.get('/')
44     expect(result.body).to eq("Hello world\n")
45   end
46   
47   describe 'with API Client' do
48
49     before do
50       @client = Google::APIClient.new(:application_name => 'test')
51       @client.authorization = nil
52     end
53     
54     
55     it 'should send gzip in user agent' do
56       conn = create_connection do |env|
57         agent = env[:request_headers]['User-Agent']
58         expect(agent).not_to be_nil
59         expect(agent).to include 'gzip'
60         [200, {}, 'Hello world']
61       end
62       @client.execute(:uri => 'http://www.example.com/', :connection => conn)
63     end
64
65     it 'should send gzip in accept-encoding' do
66       conn = create_connection do |env|
67         encoding = env[:request_headers]['Accept-Encoding']
68         expect(encoding).not_to be_nil
69         expect(encoding).to include 'gzip'
70         [200, {}, 'Hello world']
71       end
72       @client.execute(:uri => 'http://www.example.com/', :connection => conn)
73     end
74     
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         expect(encoding).not_to include('gzip') unless encoding.nil?
79         [200, {}, 'Hello world']
80       end
81       response = @client.execute(:uri => 'http://www.example.com/', :gzip => false, :connection => conn)
82       puts response.status
83     end
84     
85   end
86 end