Skip to content

Commit

Permalink
Add tests for error classes
Browse files Browse the repository at this point in the history
  • Loading branch information
waits committed Jun 14, 2016
1 parent 45ffcd3 commit e353155
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/dropbox/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Dropbox
class Client
def initialize(access_token)
unless access_token =~ /^[a-z0-9_-]{64}$/i
raise ClientError.new('Access token is invalid.')
raise ClientError.invalid_access_token
end

@access_token = access_token
Expand Down Expand Up @@ -65,7 +65,7 @@ def save_url(path, url)
when 'async_job_id'
resp['async_job_id']
else
raise ClientError.unknown_response_type
raise ClientError.unknown_response_type(resp['.tag'])
end
end

Expand All @@ -82,7 +82,7 @@ def object_from_response(resp, tag=resp['.tag'])
when 'folder'
FolderMetadata.new(resp['id'], resp['path_lower'])
else
raise ClientError.unknown_response_type
raise ClientError.unknown_response_type(tag)
end
end

Expand Down
6 changes: 5 additions & 1 deletion lib/dropbox/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ module Dropbox
class ClientError < StandardError
attr_reader :message

def self.unknown_response_type
def self.invalid_access_token
self.new("Invalid access token")
end

def self.unknown_response_type(str)
self.new("Unknown response type '#{str}'")
end

Expand Down
27 changes: 27 additions & 0 deletions test/test_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'minitest/autorun'
require 'http'
require 'dropbox'

class DropboxErrorTest < Minitest::Test
def test_client_error
err = Dropbox::ClientError.invalid_access_token
assert_equal "Invalid access token", err.to_s

err = Dropbox::ClientError.unknown_response_type('link')
assert_equal "Unknown response type 'link'", err.to_s
end

def test_api_error
resp = HTTP::Response.new(status: 404, version: '1.1',
headers: {'Content-Type' => 'application/json'},
body: '{"error_summary": "Resource not found"}')
err = Dropbox::APIError.new(resp)
assert_equal 'Resource not found', err.to_s

resp = HTTP::Response.new(status: 500, version: '1.1',
headers: {'Content-Type' => 'text/html'},
body: '<html><body>Server error</body></html>')
err = Dropbox::APIError.new(resp)
assert_equal '<html><body>Server error</body></html>', err.to_s
end
end

0 comments on commit e353155

Please sign in to comment.