koala 1.2.0beta1 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +27 -2
- data/koala.gemspec +7 -8
- data/lib/koala/graph_api.rb +11 -22
- data/lib/koala/graph_batch_api.rb +11 -1
- data/lib/koala/graph_collection.rb +7 -2
- data/lib/koala/http_service.rb +3 -3
- data/lib/koala/multipart_request.rb +35 -0
- data/lib/koala/oauth.rb +37 -24
- data/lib/koala/realtime_updates.rb +1 -1
- data/lib/koala/rest_api.rb +5 -0
- data/lib/koala/test_users.rb +2 -3
- data/lib/koala/uploadable_io.rb +2 -1
- data/lib/koala/version.rb +3 -0
- data/lib/koala.rb +8 -2
- data/readme.md +44 -7
- data/spec/cases/{api_base_spec.rb → api_spec.rb} +35 -10
- data/spec/cases/graph_and_rest_api_spec.rb +0 -26
- data/spec/cases/graph_api_batch_spec.rb +12 -30
- data/spec/cases/graph_api_spec.rb +0 -20
- data/spec/cases/graph_collection_spec.rb +116 -0
- data/spec/cases/http_service_spec.rb +13 -12
- data/spec/cases/koala_spec.rb +7 -3
- data/spec/cases/multipart_request_spec.rb +66 -0
- data/spec/cases/oauth_spec.rb +227 -102
- data/spec/cases/rest_api_spec.rb +0 -19
- data/spec/cases/test_users_spec.rb +25 -35
- data/spec/cases/uploadable_io_spec.rb +28 -0
- data/spec/fixtures/facebook_data.yml +3 -2
- data/spec/fixtures/mock_facebook_responses.yml +18 -5
- data/spec/support/graph_api_shared_examples.rb +105 -126
- data/spec/support/koala_test.rb +41 -15
- data/spec/support/mock_http_service.rb +2 -1
- data/spec/support/rest_api_shared_examples.rb +69 -25
- metadata +94 -72
|
@@ -5,7 +5,7 @@ describe "Koala::Facebook::API" do
|
|
|
5
5
|
@service = Koala::Facebook::API.new
|
|
6
6
|
end
|
|
7
7
|
|
|
8
|
-
it "
|
|
8
|
+
it "doesn't include an access token if none was given" do
|
|
9
9
|
Koala.should_receive(:make_request).with(
|
|
10
10
|
anything,
|
|
11
11
|
hash_not_including('access_token' => 1),
|
|
@@ -16,7 +16,7 @@ describe "Koala::Facebook::API" do
|
|
|
16
16
|
@service.api('anything')
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
-
it "
|
|
19
|
+
it "includes an access token if given" do
|
|
20
20
|
token = 'adfadf'
|
|
21
21
|
service = Koala::Facebook::API.new token
|
|
22
22
|
|
|
@@ -30,13 +30,13 @@ describe "Koala::Facebook::API" do
|
|
|
30
30
|
service.api('anything')
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
it "
|
|
33
|
+
it "has an attr_reader for access token" do
|
|
34
34
|
token = 'adfadf'
|
|
35
35
|
service = Koala::Facebook::API.new token
|
|
36
36
|
service.access_token.should == token
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
it "
|
|
39
|
+
it "gets the attribute of a Koala::Response given by the http_component parameter" do
|
|
40
40
|
http_component = :method_name
|
|
41
41
|
|
|
42
42
|
response = mock('Mock KoalaResponse', :body => '', :status => 200)
|
|
@@ -47,7 +47,7 @@ describe "Koala::Facebook::API" do
|
|
|
47
47
|
@service.api('anything', {}, 'get', :http_component => http_component)
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
-
it "
|
|
50
|
+
it "returns the body of the request as JSON if no http_component is given" do
|
|
51
51
|
response = stub('response', :body => 'body', :status => 200)
|
|
52
52
|
Koala.stub(:make_request).and_return(response)
|
|
53
53
|
|
|
@@ -57,7 +57,7 @@ describe "Koala::Facebook::API" do
|
|
|
57
57
|
@service.api('anything').should == json_body
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
-
it "
|
|
60
|
+
it "executes an error checking block if provided" do
|
|
61
61
|
body = '{}'
|
|
62
62
|
Koala.stub(:make_request).and_return(Koala::Response.new(200, body, {}))
|
|
63
63
|
|
|
@@ -70,13 +70,13 @@ describe "Koala::Facebook::API" do
|
|
|
70
70
|
end
|
|
71
71
|
end
|
|
72
72
|
|
|
73
|
-
it "
|
|
73
|
+
it "raises an API error if the HTTP response code is greater than or equal to 500" do
|
|
74
74
|
Koala.stub(:make_request).and_return(Koala::Response.new(500, 'response body', {}))
|
|
75
75
|
|
|
76
76
|
lambda { @service.api('anything') }.should raise_exception(Koala::Facebook::APIError)
|
|
77
77
|
end
|
|
78
78
|
|
|
79
|
-
it "
|
|
79
|
+
it "handles rogue true/false as responses" do
|
|
80
80
|
Koala.should_receive(:make_request).and_return(Koala::Response.new(200, 'true', {}))
|
|
81
81
|
@service.api('anything').should be_true
|
|
82
82
|
|
|
@@ -85,17 +85,42 @@ describe "Koala::Facebook::API" do
|
|
|
85
85
|
end
|
|
86
86
|
|
|
87
87
|
describe "with regard to leading slashes" do
|
|
88
|
-
it "
|
|
88
|
+
it "adds a leading / to the path if not present" do
|
|
89
89
|
path = "anything"
|
|
90
90
|
Koala.should_receive(:make_request).with("/#{path}", anything, anything, anything).and_return(Koala::Response.new(200, 'true', {}))
|
|
91
91
|
@service.api(path)
|
|
92
92
|
end
|
|
93
93
|
|
|
94
|
-
it "
|
|
94
|
+
it "doesn't change the path if a leading / is present" do
|
|
95
95
|
path = "/anything"
|
|
96
96
|
Koala.should_receive(:make_request).with(path, anything, anything, anything).and_return(Koala::Response.new(200, 'true', {}))
|
|
97
97
|
@service.api(path)
|
|
98
98
|
end
|
|
99
99
|
end
|
|
100
100
|
|
|
101
|
+
describe "with an access token" do
|
|
102
|
+
before(:each) do
|
|
103
|
+
@api = Koala::Facebook::API.new(@token)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
it_should_behave_like "Koala RestAPI"
|
|
107
|
+
it_should_behave_like "Koala RestAPI with an access token"
|
|
108
|
+
|
|
109
|
+
it_should_behave_like "Koala GraphAPI"
|
|
110
|
+
it_should_behave_like "Koala GraphAPI with an access token"
|
|
111
|
+
it_should_behave_like "Koala GraphAPI with GraphCollection"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
describe "without an access token" do
|
|
115
|
+
before(:each) do
|
|
116
|
+
@api = Koala::Facebook::API.new
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
it_should_behave_like "Koala RestAPI"
|
|
120
|
+
it_should_behave_like "Koala RestAPI without an access token"
|
|
121
|
+
|
|
122
|
+
it_should_behave_like "Koala GraphAPI"
|
|
123
|
+
it_should_behave_like "Koala GraphAPI without an access token"
|
|
124
|
+
it_should_behave_like "Koala GraphAPI with GraphCollection"
|
|
125
|
+
end
|
|
101
126
|
end
|
|
@@ -19,30 +19,4 @@ describe "Koala::Facebook::GraphAndRestAPI" do
|
|
|
19
19
|
api = Koala::Facebook::GraphAndRestAPI.new("token")
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
|
-
|
|
23
|
-
describe "with an access token" do
|
|
24
|
-
before(:each) do
|
|
25
|
-
@api = Koala::Facebook::API.new(@token)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
it_should_behave_like "Koala RestAPI"
|
|
29
|
-
it_should_behave_like "Koala RestAPI with an access token"
|
|
30
|
-
|
|
31
|
-
it_should_behave_like "Koala GraphAPI"
|
|
32
|
-
it_should_behave_like "Koala GraphAPI with an access token"
|
|
33
|
-
it_should_behave_like "Koala GraphAPI with GraphCollection"
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
describe "without an access token" do
|
|
37
|
-
before(:each) do
|
|
38
|
-
@api = Koala::Facebook::API.new
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
it_should_behave_like "Koala RestAPI"
|
|
42
|
-
it_should_behave_like "Koala RestAPI without an access token"
|
|
43
|
-
|
|
44
|
-
it_should_behave_like "Koala GraphAPI"
|
|
45
|
-
it_should_behave_like "Koala GraphAPI without an access token"
|
|
46
|
-
it_should_behave_like "Koala GraphAPI with GraphCollection"
|
|
47
|
-
end
|
|
48
22
|
end
|
|
@@ -430,7 +430,7 @@ describe "Koala::Facebook::GraphAPI in batch mode" do
|
|
|
430
430
|
end
|
|
431
431
|
|
|
432
432
|
describe "usage tests" do
|
|
433
|
-
it "
|
|
433
|
+
it "gets two results at once" do
|
|
434
434
|
me, koppel = @api.batch do |batch_api|
|
|
435
435
|
batch_api.get_object('me')
|
|
436
436
|
batch_api.get_object(KoalaTest.user1)
|
|
@@ -439,7 +439,15 @@ describe "Koala::Facebook::GraphAPI in batch mode" do
|
|
|
439
439
|
koppel['id'].should_not be_nil
|
|
440
440
|
end
|
|
441
441
|
|
|
442
|
-
it '
|
|
442
|
+
it 'makes mixed calls inside of a batch' do
|
|
443
|
+
me, friends = @api.batch do |batch_api|
|
|
444
|
+
batch_api.get_object('me')
|
|
445
|
+
batch_api.get_connections('me', 'friends')
|
|
446
|
+
end
|
|
447
|
+
friends.should be_a(Koala::Facebook::GraphCollection)
|
|
448
|
+
end
|
|
449
|
+
|
|
450
|
+
it 'turns pageable results into GraphCollections' do
|
|
443
451
|
me, friends = @api.batch do |batch_api|
|
|
444
452
|
batch_api.get_object('me')
|
|
445
453
|
batch_api.get_connections('me', 'friends')
|
|
@@ -448,14 +456,14 @@ describe "Koala::Facebook::GraphAPI in batch mode" do
|
|
|
448
456
|
friends.should be_an(Array)
|
|
449
457
|
end
|
|
450
458
|
|
|
451
|
-
it '
|
|
459
|
+
it 'makes a get_picture call inside of a batch' do
|
|
452
460
|
pictures = @api.batch do |batch_api|
|
|
453
461
|
batch_api.get_picture('me')
|
|
454
462
|
end
|
|
455
463
|
pictures.first.should_not be_empty
|
|
456
464
|
end
|
|
457
465
|
|
|
458
|
-
it "
|
|
466
|
+
it "handles requests for two different tokens" do
|
|
459
467
|
me, insights = @api.batch do |batch_api|
|
|
460
468
|
batch_api.get_object('me')
|
|
461
469
|
batch_api.get_connections(@app_id, 'insights', {}, {"access_token" => @app_api.access_token})
|
|
@@ -571,30 +579,4 @@ describe "Koala::Facebook::GraphAPI in batch mode" do
|
|
|
571
579
|
end
|
|
572
580
|
end
|
|
573
581
|
end
|
|
574
|
-
|
|
575
|
-
describe "new interface" do
|
|
576
|
-
it "includes a deprecation warning on GraphAPI" do
|
|
577
|
-
begin
|
|
578
|
-
Koala::Facebook::GraphAPI.batch do
|
|
579
|
-
end
|
|
580
|
-
rescue NoMethodError => @err
|
|
581
|
-
end
|
|
582
|
-
|
|
583
|
-
# verify the message points people to the wiki page
|
|
584
|
-
@err.should
|
|
585
|
-
@err.message.should =~ /https\:\/\/github.com\/arsduo\/koala\/wiki\/Batch-requests/
|
|
586
|
-
end
|
|
587
|
-
|
|
588
|
-
it "includes a deprecation warning on GraphAndRESTAPI" do
|
|
589
|
-
begin
|
|
590
|
-
Koala::Facebook::GraphAndRestAPI.batch do
|
|
591
|
-
end
|
|
592
|
-
rescue NoMethodError => @err
|
|
593
|
-
end
|
|
594
|
-
|
|
595
|
-
# verify the message points people to the wiki page
|
|
596
|
-
@err.should
|
|
597
|
-
@err.message.should =~ /https\:\/\/github.com\/arsduo\/koala\/wiki\/Batch-requests/
|
|
598
|
-
end
|
|
599
|
-
end
|
|
600
582
|
end
|
|
@@ -19,24 +19,4 @@ describe "Koala::Facebook::GraphAPI" do
|
|
|
19
19
|
api = Koala::Facebook::GraphAPI.new("token")
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
|
-
|
|
23
|
-
context "with an access token" do
|
|
24
|
-
before :each do
|
|
25
|
-
@api = Koala::Facebook::API.new(@token)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
it_should_behave_like "Koala GraphAPI"
|
|
29
|
-
it_should_behave_like "Koala GraphAPI with an access token"
|
|
30
|
-
it_should_behave_like "Koala GraphAPI with GraphCollection"
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
context "without an access token" do
|
|
34
|
-
before :each do
|
|
35
|
-
@api = Koala::Facebook::API.new
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
it_should_behave_like "Koala GraphAPI"
|
|
39
|
-
it_should_behave_like "Koala GraphAPI without an access token"
|
|
40
|
-
it_should_behave_like "Koala GraphAPI with GraphCollection"
|
|
41
|
-
end
|
|
42
22
|
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Koala::Facebook::GraphCollection do
|
|
4
|
+
before(:each) do
|
|
5
|
+
@result = {
|
|
6
|
+
"data" => [1, 2, :three],
|
|
7
|
+
"paging" => {:paging => true}
|
|
8
|
+
}
|
|
9
|
+
@api = Koala::Facebook::API.new("123")
|
|
10
|
+
@collection = Koala::Facebook::GraphCollection.new(@result, @api)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "subclasses Array" do
|
|
14
|
+
Koala::Facebook::GraphCollection.ancestors.should include(Array)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "creates an array-like object" do
|
|
18
|
+
Koala::Facebook::GraphCollection.new(@result, @api).should be_an(Array)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "contains the result data" do
|
|
22
|
+
@result["data"].each_with_index {|r, i| @collection[i].should == r}
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "has a read-only paging attribute" do
|
|
26
|
+
@collection.methods.map(&:to_sym).should include(:paging)
|
|
27
|
+
@collection.methods.map(&:to_sym).should_not include(:paging=)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "sets paging to results['paging']" do
|
|
31
|
+
@collection.paging.should == @result["paging"]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "sets raw_response to the original results" do
|
|
35
|
+
@collection.raw_response.should == @result
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "sets the API to the provided API" do
|
|
39
|
+
@collection.api.should == @api
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
describe "when getting a whole page" do
|
|
43
|
+
before(:each) do
|
|
44
|
+
@second_page = {
|
|
45
|
+
"data" => [:second, :page, :data],
|
|
46
|
+
"paging" => {}
|
|
47
|
+
}
|
|
48
|
+
@base = stub("base")
|
|
49
|
+
@args = stub("args")
|
|
50
|
+
@page_of_results = stub("page of results")
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "should return the previous page of results" do
|
|
54
|
+
@collection.should_receive(:previous_page_params).and_return([@base, @args])
|
|
55
|
+
@api.should_receive(:api).with(@base, @args, anything, anything).and_return(@second_page)
|
|
56
|
+
Koala::Facebook::GraphCollection.should_receive(:new).with(@second_page, @api).and_return(@page_of_results)
|
|
57
|
+
@collection.previous_page.should == @page_of_results
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it "should return the next page of results" do
|
|
61
|
+
@collection.should_receive(:next_page_params).and_return([@base, @args])
|
|
62
|
+
@api.should_receive(:api).with(@base, @args, anything, anything).and_return(@second_page)
|
|
63
|
+
Koala::Facebook::GraphCollection.should_receive(:new).with(@second_page, @api).and_return(@page_of_results)
|
|
64
|
+
|
|
65
|
+
@collection.next_page.should == @page_of_results
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "should return nil it there are no other pages" do
|
|
69
|
+
%w{next previous}.each do |this|
|
|
70
|
+
@collection.should_receive("#{this}_page_params".to_sym).and_return(nil)
|
|
71
|
+
@collection.send("#{this}_page").should == nil
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
describe "when parsing page paramters" do
|
|
77
|
+
it "should return the base as the first array entry" do
|
|
78
|
+
base = "url_path"
|
|
79
|
+
@collection.parse_page_url("anything.com/#{base}?anything").first.should == base
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
it "should return the arguments as a hash as the last array entry" do
|
|
83
|
+
args_hash = {"one" => "val_one", "two" => "val_two"}
|
|
84
|
+
@collection.parse_page_url("anything.com/anything?#{args_hash.map {|k,v| "#{k}=#{v}" }.join("&")}").last.should == args_hash
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
describe "#evaluate" do
|
|
89
|
+
it "returns the original result if it's provided a non-hash result" do
|
|
90
|
+
result = []
|
|
91
|
+
Koala::Facebook::GraphCollection.evaluate(result, @api).should == result
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
it "returns the original result if it's provided a nil result" do
|
|
95
|
+
result = nil
|
|
96
|
+
Koala::Facebook::GraphCollection.evaluate(result, @api).should == result
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
it "returns the original result if the result doesn't have a data key" do
|
|
100
|
+
result = {"paging" => {}}
|
|
101
|
+
Koala::Facebook::GraphCollection.evaluate(result, @api).should == result
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
it "returns the original result if the result's data key isn't an array" do
|
|
105
|
+
result = {"data" => {}, "paging" => {}}
|
|
106
|
+
Koala::Facebook::GraphCollection.evaluate(result, @api).should == result
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it "returns a new GraphCollection of the result if it has an array data key and a paging key" do
|
|
110
|
+
result = {"data" => [], "paging" => {}}
|
|
111
|
+
expected = :foo
|
|
112
|
+
Koala::Facebook::GraphCollection.should_receive(:new).with(result, @api).and_return(expected)
|
|
113
|
+
Koala::Facebook::GraphCollection.evaluate(result, @api).should == expected
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -20,6 +20,7 @@ describe "Koala::HTTPService" do
|
|
|
20
20
|
@builder = stub("Faraday connection builder")
|
|
21
21
|
@builder.stub(:request)
|
|
22
22
|
@builder.stub(:adapter)
|
|
23
|
+
@builder.stub(:use)
|
|
23
24
|
end
|
|
24
25
|
|
|
25
26
|
it "is defined" do
|
|
@@ -27,7 +28,7 @@ describe "Koala::HTTPService" do
|
|
|
27
28
|
end
|
|
28
29
|
|
|
29
30
|
it "adds multipart" do
|
|
30
|
-
@builder.should_receive(:
|
|
31
|
+
@builder.should_receive(:use).with(Koala::MultipartRequest)
|
|
31
32
|
Koala::HTTPService::DEFAULT_MIDDLEWARE.call(@builder)
|
|
32
33
|
end
|
|
33
34
|
|
|
@@ -63,12 +64,12 @@ describe "Koala::HTTPService" do
|
|
|
63
64
|
|
|
64
65
|
it "returns the beta REST server if options[:rest_api]" do
|
|
65
66
|
server = Koala::HTTPService.server(@options.merge(:rest_api => true))
|
|
66
|
-
server.should =~ Regexp.new(
|
|
67
|
+
server.should =~ Regexp.new(Koala::Facebook::REST_SERVER.gsub(/\.facebook/, ".beta.facebook"))
|
|
67
68
|
end
|
|
68
69
|
|
|
69
70
|
it "returns the beta rest server if !options[:rest_api]" do
|
|
70
71
|
server = Koala::HTTPService.server(@options)
|
|
71
|
-
server.should =~ Regexp.new(
|
|
72
|
+
server.should =~ Regexp.new(Koala::Facebook::GRAPH_SERVER.gsub(/\.facebook/, ".beta.facebook"))
|
|
72
73
|
end
|
|
73
74
|
end
|
|
74
75
|
|
|
@@ -77,12 +78,12 @@ describe "Koala::HTTPService" do
|
|
|
77
78
|
@options = {:video => true}
|
|
78
79
|
end
|
|
79
80
|
|
|
80
|
-
it "
|
|
81
|
+
it "returns the REST video server if options[:rest_api]" do
|
|
81
82
|
server = Koala::HTTPService.server(@options.merge(:rest_api => true))
|
|
82
83
|
server.should =~ Regexp.new(Koala::Facebook::REST_SERVER.gsub(/\.facebook/, "-video.facebook"))
|
|
83
84
|
end
|
|
84
85
|
|
|
85
|
-
it "
|
|
86
|
+
it "returns the graph video server if !options[:rest_api]" do
|
|
86
87
|
server = Koala::HTTPService.server(@options)
|
|
87
88
|
server.should =~ Regexp.new(Koala::Facebook::GRAPH_SERVER.gsub(/\.facebook/, "-video.facebook"))
|
|
88
89
|
end
|
|
@@ -90,11 +91,11 @@ describe "Koala::HTTPService" do
|
|
|
90
91
|
end
|
|
91
92
|
|
|
92
93
|
describe "#encode_params" do
|
|
93
|
-
it "
|
|
94
|
+
it "returns an empty string if param_hash evaluates to false" do
|
|
94
95
|
Koala::HTTPService.encode_params(nil).should == ''
|
|
95
96
|
end
|
|
96
97
|
|
|
97
|
-
it "
|
|
98
|
+
it "converts values to JSON if the value is not a String" do
|
|
98
99
|
val = 'json_value'
|
|
99
100
|
not_a_string = 'not_a_string'
|
|
100
101
|
not_a_string.stub(:is_a?).and_return(false)
|
|
@@ -113,7 +114,7 @@ describe "Koala::HTTPService" do
|
|
|
113
114
|
end.should be_true
|
|
114
115
|
end
|
|
115
116
|
|
|
116
|
-
it "
|
|
117
|
+
it "escapes all values" do
|
|
117
118
|
args = Hash[*(1..4).map {|i| [i.to_s, "Value #{i}($"]}.flatten]
|
|
118
119
|
|
|
119
120
|
result = Koala::HTTPService.encode_params(args)
|
|
@@ -123,7 +124,7 @@ describe "Koala::HTTPService" do
|
|
|
123
124
|
end
|
|
124
125
|
end
|
|
125
126
|
|
|
126
|
-
it "
|
|
127
|
+
it "converts all keys to Strings" do
|
|
127
128
|
args = Hash[*(1..4).map {|i| [i, "val#{i}"]}.flatten]
|
|
128
129
|
|
|
129
130
|
result = Koala::HTTPService.encode_params(args)
|
|
@@ -427,16 +428,16 @@ describe "Koala::HTTPService" do
|
|
|
427
428
|
:net_http => Koala::NetHTTPService
|
|
428
429
|
}.each_pair do |adapter, module_class|
|
|
429
430
|
describe module_class.to_s do
|
|
430
|
-
it "
|
|
431
|
+
it "responds to deprecated_interface" do
|
|
431
432
|
module_class.should respond_to(:deprecated_interface)
|
|
432
433
|
end
|
|
433
434
|
|
|
434
|
-
it "
|
|
435
|
+
it "issues a deprecation warning" do
|
|
435
436
|
Koala::Utils.should_receive(:deprecate)
|
|
436
437
|
module_class.deprecated_interface
|
|
437
438
|
end
|
|
438
439
|
|
|
439
|
-
it "
|
|
440
|
+
it "sets the default adapter to #{adapter}" do
|
|
440
441
|
module_class.deprecated_interface
|
|
441
442
|
Faraday.default_adapter.should == adapter
|
|
442
443
|
end
|
data/spec/cases/koala_spec.rb
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe "Koala" do
|
|
4
|
+
it "has a version" do
|
|
5
|
+
Koala.const_defined?("VERSION").should be_true
|
|
6
|
+
end
|
|
7
|
+
|
|
4
8
|
it "has an http_service accessor" do
|
|
5
9
|
Koala.should respond_to(:http_service)
|
|
6
10
|
Koala.should respond_to(:http_service=)
|
|
@@ -35,14 +39,14 @@ describe "Koala" do
|
|
|
35
39
|
end
|
|
36
40
|
end
|
|
37
41
|
|
|
38
|
-
|
|
42
|
+
describe "make_request" do
|
|
39
43
|
it "passes all its arguments to the http_service" do
|
|
40
|
-
http_service = stub("http_service")
|
|
41
44
|
path = "foo"
|
|
42
45
|
args = {:a => 2}
|
|
43
46
|
verb = "get"
|
|
44
47
|
options = {:c => :d}
|
|
45
|
-
|
|
48
|
+
|
|
49
|
+
Koala.http_service.should_receive(:make_request).with(path, args, verb, options)
|
|
46
50
|
Koala.make_request(path, args, verb, options)
|
|
47
51
|
end
|
|
48
52
|
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Koala::MultipartRequest do
|
|
4
|
+
it "is a subclass of Faraday::Request::Multipart" do
|
|
5
|
+
Koala::MultipartRequest.superclass.should == Faraday::Request::Multipart
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "defines mime_type as multipart/form-data" do
|
|
9
|
+
Koala::MultipartRequest.mime_type.should == 'multipart/form-data'
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe ".process_request?" do
|
|
13
|
+
before :each do
|
|
14
|
+
@env = {}
|
|
15
|
+
@multipart = Koala::MultipartRequest.new
|
|
16
|
+
@multipart.stub(:request_type).and_return("")
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# no way to test the call to super, unfortunately
|
|
20
|
+
it "returns true if env[:body] is a hash with at least one hash in its values" do
|
|
21
|
+
@env[:body] = {:a => {:c => 2}}
|
|
22
|
+
@multipart.process_request?(@env).should be_true
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "returns true if env[:body] is a hash with at least one array in its values" do
|
|
26
|
+
@env[:body] = {:a => [:c, 2]}
|
|
27
|
+
@multipart.process_request?(@env).should be_true
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "returns true if env[:body] is a hash with mixed objects in its values" do
|
|
31
|
+
@env[:body] = {:a => [:c, 2], :b => {:e => :f}}
|
|
32
|
+
@multipart.process_request?(@env).should be_true
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "returns false if env[:body] is a string" do
|
|
36
|
+
@env[:body] = "my body"
|
|
37
|
+
@multipart.process_request?(@env).should be_false
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "returns false if env[:body] is a hash without an array or hash value" do
|
|
41
|
+
@env[:body] = {:a => 3}
|
|
42
|
+
@multipart.process_request?(@env).should be_false
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
describe ".process_params" do
|
|
47
|
+
before :each do
|
|
48
|
+
@parent = Faraday::Request::Multipart.new
|
|
49
|
+
@multipart = Koala::MultipartRequest.new
|
|
50
|
+
@block = lambda {|k, v| "#{k}=#{v}"}
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "is identical to the parent for requests without a prefix" do
|
|
54
|
+
hash = {:a => 2, :c => "3"}
|
|
55
|
+
@multipart.process_params(hash, &@block).should == @parent.process_params(hash, &@block)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "replaces encodes [ and ] if the request has a prefix" do
|
|
59
|
+
hash = {:a => 2, :c => "3"}
|
|
60
|
+
prefix = "foo"
|
|
61
|
+
# process_params returns an array
|
|
62
|
+
@multipart.process_params(hash, prefix, &@block).join("&").should == @parent.process_params(hash, prefix, &@block).join("&").gsub(/\[/, "%5B").gsub(/\]/, "%5D")
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
end
|