Merge branch 'master' of ../martinsarsale-support-for-repeats
[arvados.git] / spec / google / api_client / parsers / json_parser_spec.rb
1 # Copyright 2010 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 'json'
18 require 'google/api_client/parsers/json_parser'
19 require 'google/api_client/parsers/json/error_parser'
20 require 'google/api_client/parsers/json/pagination'
21
22 describe Google::APIClient::JSONParser, 'with error data' do
23   before do
24     @data = {
25       'error' => {
26         'code' => 401,
27         'message' => 'Token invalid - Invalid AuthSub token.',
28         'errors' => [
29           {
30             'location' => 'Authorization',
31             'domain' => 'global',
32             'locationType' => 'header',
33             'reason' => 'authError',
34             'message' => 'Token invalid - Invalid AuthSub token.'
35           }
36         ]
37       }
38     }
39   end
40
41   it 'should correctly match as an error' do
42     parser = Google::APIClient::JSONParser.match(@data)
43     parser.should == Google::APIClient::JSON::ErrorParser
44   end
45
46   it 'should be automatically handled as an error when parsed' do
47     data = Google::APIClient::JSONParser.parse(@data)
48     data.should be_kind_of(Google::APIClient::JSON::ErrorParser)
49   end
50
51   it 'should correctly expose error message' do
52     data = Google::APIClient::JSONParser.parse(@data)
53     data.error.should == 'Token invalid - Invalid AuthSub token.'
54   end
55 end