koala 0.9.0 → 1.0.0

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.
Files changed (66) hide show
  1. data/.gitignore +3 -0
  2. data/CHANGELOG +47 -7
  3. data/Gemfile +3 -0
  4. data/LICENSE +1 -1
  5. data/Manifest +10 -15
  6. data/Rakefile +13 -13
  7. data/koala.gemspec +36 -16
  8. data/lib/koala/graph_api.rb +188 -123
  9. data/lib/koala/http_services.rb +93 -18
  10. data/lib/koala/rest_api.rb +73 -6
  11. data/lib/koala/test_users.rb +85 -0
  12. data/lib/koala/uploadable_io.rb +115 -0
  13. data/lib/koala.rb +114 -116
  14. data/readme.md +32 -18
  15. data/spec/cases/api_base_spec.rb +101 -0
  16. data/spec/cases/graph_and_rest_api_spec.rb +31 -0
  17. data/spec/cases/graph_api_spec.rb +25 -0
  18. data/spec/cases/http_services/http_service_spec.rb +54 -0
  19. data/spec/cases/http_services/net_http_service_spec.rb +350 -0
  20. data/spec/cases/http_services/typhoeus_service_spec.rb +144 -0
  21. data/spec/cases/oauth_spec.rb +409 -0
  22. data/spec/cases/realtime_updates_spec.rb +184 -0
  23. data/spec/cases/rest_api_spec.rb +25 -0
  24. data/spec/cases/test_users_spec.rb +221 -0
  25. data/spec/cases/uploadable_io_spec.rb +151 -0
  26. data/spec/fixtures/beach.jpg +0 -0
  27. data/spec/{facebook_data.yml → fixtures/facebook_data.yml} +18 -14
  28. data/spec/{mock_facebook_responses.yml → fixtures/mock_facebook_responses.yml} +314 -241
  29. data/spec/spec_helper.rb +18 -0
  30. data/spec/support/graph_api_shared_examples.rb +424 -0
  31. data/spec/support/live_testing_data_helper.rb +40 -0
  32. data/spec/{mock_http_service.rb → support/mock_http_service.rb} +94 -80
  33. data/spec/support/rest_api_shared_examples.rb +161 -0
  34. data/spec/support/setup_mocks_or_live.rb +52 -0
  35. data/spec/support/uploadable_io_shared_examples.rb +76 -0
  36. metadata +140 -55
  37. data/examples/oauth_playground/Capfile +0 -2
  38. data/examples/oauth_playground/LICENSE +0 -22
  39. data/examples/oauth_playground/Rakefile +0 -4
  40. data/examples/oauth_playground/config/deploy.rb +0 -39
  41. data/examples/oauth_playground/config/facebook.yml +0 -13
  42. data/examples/oauth_playground/config.ru +0 -27
  43. data/examples/oauth_playground/lib/load_facebook.rb +0 -3
  44. data/examples/oauth_playground/lib/oauth_playground.rb +0 -187
  45. data/examples/oauth_playground/readme.md +0 -8
  46. data/examples/oauth_playground/spec/oauth_playground_spec.rb +0 -35
  47. data/examples/oauth_playground/spec/spec_helper.rb +0 -36
  48. data/examples/oauth_playground/tmp/restart.txt +0 -0
  49. data/examples/oauth_playground/views/index.erb +0 -206
  50. data/examples/oauth_playground/views/layout.erb +0 -39
  51. data/init.rb +0 -2
  52. data/spec/koala/api_base_tests.rb +0 -95
  53. data/spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb +0 -10
  54. data/spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb +0 -11
  55. data/spec/koala/graph_api/graph_api_no_access_token_tests.rb +0 -114
  56. data/spec/koala/graph_api/graph_api_with_access_token_tests.rb +0 -150
  57. data/spec/koala/graph_api/graph_collection_tests.rb +0 -104
  58. data/spec/koala/live_testing_data_helper.rb +0 -15
  59. data/spec/koala/net_http_service_tests.rb +0 -181
  60. data/spec/koala/oauth/oauth_tests.rb +0 -440
  61. data/spec/koala/realtime_updates/realtime_updates_tests.rb +0 -187
  62. data/spec/koala/rest_api/rest_api_no_access_token_tests.rb +0 -94
  63. data/spec/koala/rest_api/rest_api_with_access_token_tests.rb +0 -36
  64. data/spec/koala_spec.rb +0 -18
  65. data/spec/koala_spec_helper.rb +0 -31
  66. data/spec/koala_spec_without_mocks.rb +0 -19
@@ -1,114 +0,0 @@
1
- shared_examples_for "Koala GraphAPI without an access token" do
2
- it "should get public data about a user" do
3
- result = @api.get_object("koppel")
4
- # the results should have an ID and a name, among other things
5
- (result["id"] && result["name"]).should
6
- end
7
-
8
- it "should not get private data about a user" do
9
- result = @api.get_object("koppel")
10
- # updated_time should be a pretty fixed test case
11
- result["updated_time"].should be_nil
12
- end
13
-
14
- it "should get public data about a Page" do
15
- result = @api.get_object("contextoptional")
16
- # the results should have an ID and a name, among other things
17
- (result["id"] && result["name"]).should
18
- end
19
-
20
- it "should not be able to get data about 'me'" do
21
- lambda { @api.get_object("me") }.should raise_error(Koala::Facebook::APIError)
22
- end
23
-
24
- it "should be able to get multiple objects" do
25
- results = @api.get_objects(["contextoptional", "naitik"])
26
- results.length.should == 2
27
- end
28
-
29
- it "shouldn't be able to access connections from users" do
30
- lambda { @api.get_connections("lukeshepard", "likes") }.should raise_error(Koala::Facebook::APIError)
31
- end
32
-
33
- it "should be able to access a user's picture" do
34
- @api.get_picture("chris.baclig").should =~ /http\:\/\//
35
- end
36
-
37
- it "should be able to access a user's picture, given a picture type" do
38
- @api.get_picture("chris.baclig", {:type => 'large'}).should =~ /^http\:\/\//
39
- end
40
-
41
- it "should be able to access connections from public Pages" do
42
- result = @api.get_connections("contextoptional", "photos")
43
- result.should be_a(Array)
44
- end
45
-
46
- # paging
47
- # see also graph_collection_tests
48
- it "should make a request for a page when provided a specific set of page params" do
49
- query = [1, 2]
50
- @api.should_receive(:graph_call).with(*query)
51
- @api.get_page(query)
52
- end
53
-
54
- it "should not be able to put an object" do
55
- lambda { @result = @api.put_object("lukeshepard", "feed", :message => "Hello, world") }.should raise_error(Koala::Facebook::APIError)
56
- puts "Error! Object #{@result.inspect} somehow put onto Luke Shepard's wall!" if @result
57
- end
58
-
59
- # these are not strictly necessary as the other put methods resolve to put_object, but are here for completeness
60
- it "should not be able to post to a feed" do
61
- (lambda do
62
- attachment = {:name => "Context Optional", :link => "http://www.contextoptional.com/"}
63
- @result = @api.put_wall_post("Hello, world", attachment, "contextoptional")
64
- end).should raise_error(Koala::Facebook::APIError)
65
- puts "Error! Object #{@result.inspect} somehow put onto Context Optional's wall!" if @result
66
- end
67
-
68
- it "should not be able to comment on an object" do
69
- # random public post on the ContextOptional wall
70
- lambda { @result = @api.put_comment("7204941866_119776748033392", "The hackathon was great!") }.should raise_error(Koala::Facebook::APIError)
71
- puts "Error! Object #{@result.inspect} somehow commented on post 7204941866_119776748033392!" if @result
72
- end
73
-
74
- it "should not be able to like an object" do
75
- lambda { @api.put_like("7204941866_119776748033392") }.should raise_error(Koala::Facebook::APIError)
76
- end
77
-
78
- # DELETE
79
- it "should not be able to delete posts" do
80
- # test post on the Ruby SDK Test application
81
- lambda { @result = @api.delete_object("115349521819193_113815981982767") }.should raise_error(Koala::Facebook::APIError)
82
- end
83
-
84
- # SEARCH
85
- it "should be able to search" do
86
- result = @api.search("facebook")
87
- result.length.should be_an(Integer)
88
- end
89
-
90
- it_should_behave_like "Koala GraphAPI with GraphCollection"
91
-
92
- # API
93
- it "should never use the rest api server" do
94
- Koala.should_receive(:make_request).with(
95
- anything,
96
- anything,
97
- anything,
98
- hash_not_including(:rest_api => true)
99
- ).and_return(Koala::Response.new(200, "", {}))
100
-
101
- @api.api("anything")
102
- end
103
- end
104
-
105
- class FacebookNoAccessTokenTests < Test::Unit::TestCase
106
- describe "Koala GraphAPI without an access token" do
107
- before :each do
108
- @api = Koala::Facebook::GraphAPI.new
109
- end
110
-
111
- it_should_behave_like "Koala GraphAPI without an access token"
112
- end
113
- end
114
-
@@ -1,150 +0,0 @@
1
- shared_examples_for "Koala GraphAPI with an access token" do
2
- it "should get public data about a user" do
3
- result = @api.get_object("koppel")
4
- # the results should have an ID and a name, among other things
5
- (result["id"] && result["name"]).should_not be_nil
6
- end
7
-
8
- it "should get private data about a user" do
9
- result = @api.get_object("koppel")
10
- # updated_time should be a pretty fixed test case
11
- result["updated_time"].should_not be_nil
12
- end
13
-
14
- it "should get public data about a Page" do
15
- result = @api.get_object("contextoptional")
16
- # the results should have an ID and a name, among other things
17
- (result["id"] && result["name"]).should
18
- end
19
-
20
- it "should get data about 'me'" do
21
- result = @api.get_object("me")
22
- result["updated_time"].should
23
- end
24
-
25
- it "should be able to get multiple objects" do
26
- result = @api.get_objects(["contextoptional", "naitik"])
27
- result.length.should == 2
28
- end
29
-
30
- it "should be able to access a user's picture" do
31
- @api.get_picture("chris.baclig").should =~ /http\:\/\//
32
- end
33
-
34
- it "should be able to access a user's picture, given a picture type" do
35
- @api.get_picture("chris.baclig", {:type => 'large'}).should =~ /^http\:\/\//
36
- end
37
-
38
- it "should be able to access connections from users" do
39
- result = @api.get_connections("lukeshepard", "likes")
40
- result.length.should > 0
41
- end
42
-
43
- it "should be able to access connections from public Pages" do
44
- result = @api.get_connections("contextoptional", "photos")
45
- result.should be_a(Array)
46
- end
47
-
48
- # paging
49
- # see also graph_collection_tests
50
- it "should make a request for a page when provided a specific set of page params" do
51
- query = [1, 2]
52
- @api.should_receive(:graph_call).with(*query)
53
- @api.get_page(query)
54
- end
55
-
56
-
57
- # PUT
58
- it "should be able to write an object to the graph" do
59
- result = @api.put_wall_post("Hello, world, from the test suite!")
60
- @temporary_object_id = result["id"]
61
- @temporary_object_id.should_not be_nil
62
- end
63
-
64
- # DELETE
65
- it "should be able to delete posts" do
66
- result = @api.put_wall_post("Hello, world, from the test suite delete method!")
67
- object_id_to_delete = result["id"]
68
- delete_result = @api.delete_object(object_id_to_delete)
69
- delete_result.should == true
70
- end
71
-
72
- # additional put tests
73
- it "should be able to verify messages posted to a wall" do
74
- message = "the cats are asleep"
75
- put_result = @api.put_wall_post(message)
76
- @temporary_object_id = put_result["id"]
77
- get_result = @api.get_object(@temporary_object_id)
78
-
79
- # make sure the message we sent is the message that got posted
80
- get_result["message"].should == message
81
- end
82
-
83
- it "should be able to post a message with an attachment to a feed" do
84
- result = @api.put_wall_post("Hello, world, from the test suite again!", {:name => "Context Optional", :link => "http://www.contextoptional.com/"})
85
- @temporary_object_id = result["id"]
86
- @temporary_object_id.should_not be_nil
87
- end
88
-
89
- it "should be able to verify a message with an attachment posted to a feed" do
90
- attachment = {"name" => "Context Optional", "link" => "http://www.contextoptional.com/"}
91
- result = @api.put_wall_post("Hello, world, from the test suite again!", attachment)
92
- @temporary_object_id = result["id"]
93
- get_result = @api.get_object(@temporary_object_id)
94
-
95
- # make sure the result we fetch includes all the parameters we sent
96
- it_matches = attachment.inject(true) {|valid, param| valid && (get_result[param[0]] == attachment[param[0]])}
97
- it_matches.should == true
98
- end
99
-
100
- it "should be able to comment on an object" do
101
- result = @api.put_wall_post("Hello, world, from the test suite, testing comments!")
102
- @temporary_object_id = result["id"]
103
-
104
- # this will be deleted when the post gets deleted
105
- comment_result = @api.put_comment(@temporary_object_id, "it's my comment!")
106
- comment_result.should_not be_nil
107
- end
108
-
109
- it "should be able to verify a comment posted about an object" do
110
- message_text = "Hello, world, from the test suite, testing comments!"
111
- result = @api.put_wall_post(message_text)
112
- @temporary_object_id = result["id"]
113
-
114
- # this will be deleted when the post gets deleted
115
- comment_text = "it's my comment!"
116
- comment_result = @api.put_comment(@temporary_object_id, comment_text)
117
- get_result = @api.get_object(comment_result["id"])
118
-
119
- # make sure the text of the comment matches what we sent
120
- get_result["message"].should == comment_text
121
- end
122
-
123
- it "should be able to like an object" do
124
- result = @api.put_wall_post("Hello, world, from the test suite, testing comments!")
125
- @temporary_object_id = result["id"]
126
- like_result = @api.put_like(@temporary_object_id)
127
- end
128
-
129
- # SEARCH
130
- it "should be able to search" do
131
- result = @api.search("facebook")
132
- result.length.should be_an(Integer)
133
- end
134
-
135
- # API
136
- # the above tests test this already, but we should consider additional api tests
137
-
138
- it_should_behave_like "Koala GraphAPI with GraphCollection"
139
- end
140
-
141
- class FacebookWithAccessTokenTests < Test::Unit::TestCase
142
- describe "Koala GraphAPI with an access token" do
143
- it_should_behave_like "live testing examples"
144
- it_should_behave_like "Koala GraphAPI with an access token"
145
-
146
- before :each do
147
- @api = Koala::Facebook::GraphAPI.new(@token)
148
- end
149
- end
150
- end
@@ -1,104 +0,0 @@
1
- # GraphCollection
2
- shared_examples_for "Koala GraphAPI with GraphCollection" do
3
-
4
- it "should create an array-like object" do
5
- call = @api.graph_call("contextoptional/photos")
6
- Koala::Facebook::GraphCollection.new(call, @api).should be_an(Array)
7
- end
8
-
9
- describe "when getting a collection" do
10
- # GraphCollection methods
11
- it "should get a GraphCollection when getting connections" do
12
- @result = @api.get_connections("contextoptional", "photos")
13
- @result.should be_a(Koala::Facebook::GraphCollection)
14
- end
15
-
16
- it "should return nil if the get_collections call fails with nil" do
17
- # this happens sometimes
18
- @api.should_receive(:graph_call).and_return(nil)
19
- @api.get_connections("contextoptional", "photos").should be_nil
20
- end
21
-
22
- it "should get a GraphCollection when searching" do
23
- result = @api.search("facebook")
24
- result.should be_a(Koala::Facebook::GraphCollection)
25
- end
26
-
27
- it "should return nil if the search call fails with nil" do
28
- # this happens sometimes
29
- @api.should_receive(:graph_call).and_return(nil)
30
- @api.search("facebook").should be_nil
31
- end
32
-
33
- it "should get a GraphCollection when paging through results" do
34
- @results = @api.get_page(["search", {"q"=>"facebook", "limit"=>"25", "until"=>"2010-09-23T21:17:33+0000"}])
35
- @results.should be_a(Koala::Facebook::GraphCollection)
36
- end
37
-
38
- it "should return nil if the page call fails with nil" do
39
- # this happens sometimes
40
- @api.should_receive(:graph_call).and_return(nil)
41
- @api.get_page(["search", {"q"=>"facebook", "limit"=>"25", "until"=>"2010-09-23T21:17:33+0000"}]).should be_nil
42
- end
43
-
44
- # GraphCollection attributes
45
- describe "the GraphCollection" do
46
- before(:each) do
47
- @result = @api.get_connections("contextoptional", "photos")
48
- end
49
-
50
- it "should have a read-only paging attribute" do
51
- lambda { @result.paging }.should_not raise_error
52
- lambda { @result.paging = "paging" }.should raise_error(NoMethodError)
53
- end
54
-
55
- describe "when getting a whole page" do
56
- before(:each) do
57
- @second_page = stub("page of Fb graph results")
58
- @base = stub("base")
59
- @args = stub("args")
60
- @page_of_results = stub("page of results")
61
- end
62
-
63
- it "should return the previous page of results" do
64
- @result.should_receive(:previous_page_params).and_return([@base, @args])
65
- @api.should_receive(:graph_call).with(@base, @args).and_return(@second_page)
66
- Koala::Facebook::GraphCollection.should_receive(:new).with(@second_page, @api).and_return(@page_of_results)
67
-
68
- @result.previous_page(@api).should == @page_of_results
69
- end
70
-
71
- it "should return the next page of results" do
72
- @result.should_receive(:next_page_params).and_return([@base, @args])
73
- @api.should_receive(:graph_call).with(@base, @args).and_return(@second_page)
74
- Koala::Facebook::GraphCollection.should_receive(:new).with(@second_page, @api).and_return(@page_of_results)
75
-
76
- @result.next_page.should == @page_of_results
77
- end
78
-
79
- it "should return nil it there are no other pages" do
80
- %w{next previous}.each do |this|
81
- @result.should_receive("#{this}_page_params".to_sym).and_return(nil)
82
- @result.send("#{this}_page", @api).should == nil
83
- end
84
- end
85
- end
86
-
87
- describe "when parsing page paramters" do
88
- before(:each) do
89
- @graph_collection = Koala::Facebook::GraphCollection.new({"data" => []}, Koala::Facebook::GraphAPI.new)
90
- end
91
-
92
- it "should return the base as the first array entry" do
93
- base = "url_path"
94
- @graph_collection.parse_page_url("anything.com/#{base}?anything").first.should == base
95
- end
96
-
97
- it "should return the arguments as a hash as the last array entry" do
98
- args_hash = {"one" => "val_one", "two" => "val_two"}
99
- @graph_collection.parse_page_url("anything.com/anything?#{args_hash.map {|k,v| "#{k}=#{v}" }.join("&")}").last.should == args_hash
100
- end
101
- end
102
- end
103
- end
104
- end
@@ -1,15 +0,0 @@
1
- shared_examples_for "live testing examples" do
2
- before :each do
3
- @token = $testing_data["oauth_token"]
4
- raise Exception, "Must supply access token to run FacebookWithAccessTokenTests!" unless @token
5
- end
6
-
7
- after :each do
8
- # clean up any temporary objects
9
- if @temporary_object_id
10
- puts "\nCleaning up temporary object #{@temporary_object_id.to_s}"
11
- result = @api.delete_object(@temporary_object_id)
12
- raise "Unable to clean up temporary Graph object #{@temporary_object_id}!" unless result
13
- end
14
- end
15
- end
@@ -1,181 +0,0 @@
1
- require 'koala/http_services'
2
-
3
- class NetHTTPServiceTests < Test::Unit::TestCase
4
- module Bear
5
- include Koala::NetHTTPService
6
- end
7
-
8
- it "should define a make_request static module method" do
9
- Bear.respond_to?(:make_request).should be_true
10
- end
11
-
12
- describe "when making a request" do
13
- before(:each) do
14
- # Setup stubs for make_request to execute without exceptions
15
- @mock_http_response = stub('Net::HTTPResponse', :code => 1)
16
- @mock_body = stub('Net::HTTPResponse body')
17
- @http_request_result = [@mock_http_response, @mock_body]
18
-
19
- @http_yield_mock = mock('Net::HTTP start yielded object')
20
-
21
- @http_yield_mock.stub(:post).and_return(@http_request_result)
22
- @http_yield_mock.stub(:get).and_return(@http_request_result)
23
-
24
- @http_mock = stub('Net::HTTP object', 'use_ssl=' => true, 'verify_mode=' => true)
25
- @http_mock.stub(:start).and_yield(@http_yield_mock)
26
-
27
- Net::HTTP.stub(:new).and_return(@http_mock)
28
- end
29
-
30
- it "should use POST if verb is not GET" do
31
- @http_yield_mock.should_receive(:post).and_return(@mock_http_response)
32
- @http_mock.should_receive(:start).and_yield(@http_yield_mock)
33
-
34
- Bear.make_request('anything', {}, 'anything')
35
- end
36
-
37
- it "should use port 443" do
38
- Net::HTTP.should_receive(:new).with(anything, 443).and_return(@http_mock)
39
-
40
- Bear.make_request('anything', {}, 'anything')
41
- end
42
-
43
- it "should use SSL" do
44
- @http_mock.should_receive('use_ssl=').with(true)
45
-
46
- Bear.make_request('anything', {}, 'anything')
47
- end
48
-
49
- it "should use the graph server by default" do
50
- Net::HTTP.should_receive(:new).with(Koala::Facebook::GRAPH_SERVER, anything).and_return(@http_mock)
51
- Bear.make_request('anything', {}, 'anything')
52
- end
53
-
54
- it "should use the REST server if the :rest_api option is true" do
55
- Net::HTTP.should_receive(:new).with(Koala::Facebook::REST_SERVER, anything).and_return(@http_mock)
56
- Bear.make_request('anything', {}, 'anything', :rest_api => true)
57
- end
58
-
59
- it "should turn off vertificate validaiton warnings" do
60
- @http_mock.should_receive('verify_mode=').with(OpenSSL::SSL::VERIFY_NONE)
61
-
62
- Bear.make_request('anything', {}, 'anything')
63
- end
64
-
65
- it "should start an HTTP connection" do
66
- @http_mock.should_receive(:start).and_yield(@http_yield_mock)
67
- Bear.make_request('anything', {}, 'anything')
68
- end
69
-
70
- describe "via POST" do
71
- it "should use Net::HTTP to make a POST request" do
72
- @http_yield_mock.should_receive(:post).and_return(@http_request_result)
73
-
74
- Bear.make_request('anything', {}, 'post')
75
- end
76
-
77
- it "should go to the specified path adding a / if it doesn't exist" do
78
- path = mock('Path')
79
- @http_yield_mock.should_receive(:post).with(path, anything).and_return(@http_request_result)
80
-
81
- Bear.make_request(path, {}, 'post')
82
- end
83
-
84
- it "should use encoded parameters" do
85
- args = {}
86
- params = mock('Encoded parameters')
87
- Bear.should_receive(:encode_params).with(args).and_return(params)
88
-
89
- @http_yield_mock.should_receive(:post).with(anything, params).and_return(@http_request_result)
90
-
91
- Bear.make_request('anything', args, 'post')
92
- end
93
- end
94
-
95
- describe "via GET" do
96
- it "should use Net::HTTP to make a GET request" do
97
- @http_yield_mock.should_receive(:get).and_return(@http_request_result)
98
-
99
- Bear.make_request('anything', {}, 'get')
100
- end
101
-
102
- it "should use the correct path, including arguments" do
103
- path = mock('Path')
104
- params = mock('Encoded parameters')
105
- args = {}
106
-
107
- Bear.should_receive(:encode_params).with(args).and_return(params)
108
- @http_yield_mock.should_receive(:get).with("#{path}?#{params}").and_return(@http_request_result)
109
-
110
- Bear.make_request(path, args, 'get')
111
- end
112
- end
113
-
114
- describe "the returned value" do
115
- before(:each) do
116
- @response = Bear.make_request('anything', {}, 'anything')
117
- end
118
-
119
- it "should return a Koala::Response object" do
120
- @response.class.should == Koala::Response
121
- end
122
-
123
- it "should return a Koala::Response with the right status" do
124
- @response.status.should == @mock_http_response.code
125
- end
126
-
127
- it "should reutrn a Koala::Response with the right body" do
128
- @response.body.should == @mock_body
129
- end
130
-
131
- it "should return a Koala::Response with the Net::HTTPResponse object as headers" do
132
- @response.headers.should == @mock_http_response
133
- end
134
- end # describe return value
135
- end # describe when making a request
136
-
137
- describe "when encoding parameters" do
138
- it "should return an empty string if param_hash evaluates to false" do
139
- Bear.encode_params(nil).should == ''
140
- end
141
-
142
- it "should convert values to JSON if the value is not a String" do
143
- val = 'json_value'
144
- not_a_string = 'not_a_string'
145
- not_a_string.stub(:class).and_return('NotAString')
146
- not_a_string.should_receive(:to_json).and_return(val)
147
-
148
- string = "hi"
149
-
150
- args = {
151
- not_a_string => not_a_string,
152
- string => string
153
- }
154
-
155
- result = Bear.encode_params(args)
156
- result.split('&').find do |key_and_val|
157
- key_and_val.match("#{not_a_string}=#{val}")
158
- end.should be_true
159
- end
160
-
161
- it "should escape all values" do
162
- args = Hash[*(1..4).map {|i| [i.to_s, "Value #{i}($"]}.flatten]
163
-
164
- result = Bear.encode_params(args)
165
- result.split('&').each do |key_val|
166
- key, val = key_val.split('=')
167
- val.should == CGI.escape(args[key])
168
- end
169
- end
170
-
171
- it "should convert all keys to Strings" do
172
- args = Hash[*(1..4).map {|i| [i, "val#{i}"]}.flatten]
173
-
174
- result = Bear.encode_params(args)
175
- result.split('&').each do |key_val|
176
- key, val = key_val.split('=')
177
- key.should == args.find{|key_val_arr| key_val_arr.last == val}.first.to_s
178
- end
179
- end
180
- end
181
- end