This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
/
api_spec.rb
59 lines (49 loc) · 1.59 KB
/
api_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Tumblife::API do
def create_response(status, msg)
Hashie::Mash.new({
:body => {
:meta => {
:status => status,
:msg => msg
},
:response => {}
}.to_json
})
end
before do
@api = Tumblife::API.new
end
context :request do
before do
@token = @api.access_token
end
it 'should get' do
@api.should_receive(:access_token).and_return(@token)
@token.should_receive(:get).with('/path/to?key1=param1&key2=param2', @api.header).and_return(create_response(200, 'OK'))
@api.get('/path/to', :key1 => 'param1', :key2 => 'param2')
end
it 'should post' do
@api.should_receive(:access_token).and_return(@token)
@token.should_receive(:post).with('/path/to', {'key1' => 'param1', 'key2' => 'param2'}, @api.header).and_return(create_response(200, 'OK'))
@api.post('/path/to', :key1 => 'param1', :key2 => 'param2')
end
end
context :error do
it 'should raise APIError' do
lambda {
@api.parse_response(create_response(200, 'OK'))
}.should_not raise_error(Tumblife::APIError)
lambda {
@api.parse_response(create_response(301, 'Found'))
}.should_not raise_error(Tumblife::APIError)
lambda {
@api.parse_response(create_response(401, 'Not Authorized'))
}.should raise_error(Tumblife::APIError)
lambda {
@api.parse_response(create_response(404, 'Not Found'))
}.should raise_error(Tumblife::APIError)
end
end
end