tweetwine 0.4.1 → 0.4.2
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.rdoc +6 -0
- data/Rakefile +22 -23
- data/lib/tweetwine/cli.rb +167 -165
- data/lib/tweetwine/config.rb +3 -2
- data/lib/tweetwine/exceptions.rb +1 -0
- data/lib/tweetwine/version.rb +1 -1
- data/project.rb +16 -16
- data/test/fixture/{config_example.yaml → config_integration.yaml} +0 -0
- data/test/fixture/oauth.rb +11 -11
- data/test/{test_helper.rb → helper.rb} +16 -7
- data/test/{example/authorization_example.rb → integration/authorization_test.rb} +17 -17
- data/test/integration/global_options_test.rb +67 -0
- data/test/integration/helper.rb +70 -0
- data/test/integration/invalid_config_file_test.rb +28 -0
- data/test/integration/search_statuses_test.rb +81 -0
- data/test/integration/show_followers_test.rb +24 -0
- data/test/integration/show_friends_test.rb +24 -0
- data/test/integration/show_home_test.rb +47 -0
- data/test/integration/show_mentions_test.rb +24 -0
- data/test/integration/show_user_test.rb +48 -0
- data/test/integration/update_status_test.rb +199 -0
- data/test/integration/use_http_proxy_test.rb +71 -0
- data/test/{example/user_help_example.rb → integration/user_help_test.rb} +36 -36
- data/test/unit/character_encoding_test.rb +19 -15
- data/test/unit/cli_test.rb +9 -10
- data/test/unit/config_test.rb +73 -71
- data/test/unit/helper.rb +108 -0
- data/test/unit/http_test.rb +39 -39
- data/test/unit/oauth_test.rb +15 -16
- data/test/unit/obfuscate_test.rb +4 -4
- data/test/unit/option_parser_test.rb +12 -12
- data/test/unit/promise_test.rb +10 -10
- data/test/unit/support_test.rb +44 -45
- data/test/unit/tweet_helper.rb +1 -1
- data/test/unit/tweet_test.rb +42 -42
- data/test/unit/twitter_test.rb +300 -303
- data/test/unit/ui_test.rb +310 -312
- data/test/unit/uri_test.rb +7 -7
- data/test/unit/url_shortener_test.rb +77 -79
- data/tweetwine.gemspec +6 -15
- metadata +55 -145
- data/test/example/example_helper.rb +0 -58
- data/test/example/global_options_example.rb +0 -64
- data/test/example/search_statuses_example.rb +0 -76
- data/test/example/show_followers_example.rb +0 -24
- data/test/example/show_friends_example.rb +0 -24
- data/test/example/show_home_example.rb +0 -44
- data/test/example/show_mentions_example.rb +0 -24
- data/test/example/show_user_example.rb +0 -44
- data/test/example/update_status_example.rb +0 -183
- data/test/example/use_http_proxy_example.rb +0 -68
- data/test/unit/unit_helper.rb +0 -111
data/test/unit/config_test.rb
CHANGED
@@ -1,18 +1,17 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'unit/helper'
|
4
|
+
require 'fileutils'
|
5
|
+
require 'tempfile'
|
6
|
+
require 'yaml'
|
4
7
|
|
5
|
-
|
6
|
-
require "tempfile"
|
7
|
-
require "yaml"
|
8
|
+
module Tweetwine::Test::Unit
|
8
9
|
|
9
|
-
|
10
|
+
class ConfigTest < TestCase
|
11
|
+
CONFIG_FILE = fixture_path 'config_unit.yaml'
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
context "when given command line arguments, no environment variables, no config file" do
|
15
|
-
setup do
|
13
|
+
describe "when given command line arguments, no environment variables, no config file" do
|
14
|
+
before do
|
16
15
|
@args = %w{--opt cmd_opt --defopt cmd_defopt left overs}
|
17
16
|
default_config = {:defopt => 'defopt'}
|
18
17
|
@config = Config.read(@args, default_config) do |args|
|
@@ -21,21 +20,21 @@ class ConfigTest < UnitTestCase
|
|
21
20
|
end
|
22
21
|
end
|
23
22
|
|
24
|
-
|
23
|
+
it "has option defined from command line" do
|
25
24
|
assert_equal 'cmd_opt', @config[:opt]
|
26
25
|
end
|
27
26
|
|
28
|
-
|
27
|
+
it "overrides option default value from command line" do
|
29
28
|
assert_equal 'cmd_defopt', @config[:defopt]
|
30
29
|
end
|
31
30
|
|
32
|
-
|
31
|
+
it "leaves remaining command line arguments unconsumed" do
|
33
32
|
assert_equal %w{left overs}, @args
|
34
33
|
end
|
35
34
|
end
|
36
35
|
|
37
|
-
|
38
|
-
|
36
|
+
describe "when given command line arguments, environment variables, no config file" do
|
37
|
+
before do
|
39
38
|
ENV['opt'] = 'env_opt'
|
40
39
|
ENV['defopt'] = 'env_defopt'
|
41
40
|
ENV['envopt'] = 'env_envopt'
|
@@ -47,27 +46,27 @@ class ConfigTest < UnitTestCase
|
|
47
46
|
end
|
48
47
|
end
|
49
48
|
|
50
|
-
|
51
|
-
ENV
|
52
|
-
ENV
|
53
|
-
ENV
|
49
|
+
after do
|
50
|
+
ENV.delete 'opt'
|
51
|
+
ENV.delete 'defopt'
|
52
|
+
ENV.delete 'envopt'
|
54
53
|
end
|
55
54
|
|
56
|
-
|
55
|
+
it "has option defined from environment variable" do
|
57
56
|
assert_equal 'env_envopt', @config[:envopt]
|
58
57
|
end
|
59
58
|
|
60
|
-
|
59
|
+
it "overrides option value from command line over environment variable" do
|
61
60
|
assert_equal 'cmd_opt', @config[:opt]
|
62
61
|
end
|
63
62
|
|
64
|
-
|
63
|
+
it "overrides option default value from environment variable" do
|
65
64
|
assert_equal 'env_defopt', @config[:defopt]
|
66
65
|
end
|
67
66
|
end
|
68
67
|
|
69
|
-
|
70
|
-
|
68
|
+
describe "when given command line arguments, no environment variables, config file" do
|
69
|
+
before do
|
71
70
|
@args = %w{--opt cmd_opt}
|
72
71
|
default_config = {:config_file => CONFIG_FILE, :defopt => 'defopt'}
|
73
72
|
@config = Config.read(@args, default_config) do |args|
|
@@ -76,21 +75,21 @@ class ConfigTest < UnitTestCase
|
|
76
75
|
end
|
77
76
|
end
|
78
77
|
|
79
|
-
|
78
|
+
it "has option defined from config file" do
|
80
79
|
assert_equal 'file_fileopt', @config[:fileopt]
|
81
80
|
end
|
82
81
|
|
83
|
-
|
82
|
+
it "overrides option value from command line over config file" do
|
84
83
|
assert_equal 'cmd_opt', @config[:opt]
|
85
84
|
end
|
86
85
|
|
87
|
-
|
86
|
+
it "overrides option default value from config file" do
|
88
87
|
assert_equal 'file_defopt', @config[:defopt]
|
89
88
|
end
|
90
89
|
end
|
91
90
|
|
92
|
-
|
93
|
-
|
91
|
+
describe "when given command line arguments, environment variables, config file" do
|
92
|
+
before do
|
94
93
|
@args = %w{--opt2 cmd_opt2}
|
95
94
|
ENV['opt'] = 'env_opt'
|
96
95
|
ENV['opt2'] = 'env_opt2'
|
@@ -100,67 +99,67 @@ class ConfigTest < UnitTestCase
|
|
100
99
|
end
|
101
100
|
end
|
102
101
|
|
103
|
-
|
104
|
-
ENV
|
105
|
-
ENV
|
102
|
+
after do
|
103
|
+
ENV.delete 'opt'
|
104
|
+
ENV.delete 'opt2'
|
106
105
|
end
|
107
106
|
|
108
|
-
|
107
|
+
it "overrides option value from environment variable over config file" do
|
109
108
|
assert_equal 'env_opt', @config[:opt]
|
110
109
|
end
|
111
110
|
|
112
|
-
|
111
|
+
it "overrides option value from command line over environment variable and config file" do
|
113
112
|
assert_equal 'cmd_opt2', @config[:opt2]
|
114
113
|
end
|
115
114
|
end
|
116
115
|
|
117
|
-
|
118
|
-
|
116
|
+
describe "when handling command line arguments without parser" do
|
117
|
+
before do
|
119
118
|
ENV['opt'] = 'env_opt'
|
120
119
|
@args = %w{--opt cmd_opt --defopt cmd_defopt}
|
121
120
|
default_config = {:config_file => CONFIG_FILE, :defopt => 'defopt', :env_lookouts => [:opt]}
|
122
121
|
@config = Config.read(@args, default_config)
|
123
122
|
end
|
124
123
|
|
125
|
-
|
126
|
-
ENV
|
124
|
+
after do
|
125
|
+
ENV.delete 'opt'
|
127
126
|
end
|
128
127
|
|
129
|
-
|
128
|
+
it "ignores command line arguments, using environment variables and config file for options if available" do
|
130
129
|
assert_equal 'env_opt', @config[:opt]
|
131
130
|
assert_equal 'file_defopt', @config[:defopt]
|
132
131
|
end
|
133
132
|
end
|
134
133
|
|
135
|
-
|
136
|
-
|
134
|
+
describe "when handling environment variables" do
|
135
|
+
before do
|
137
136
|
ENV['visible'] = 'env_visible'
|
138
137
|
ENV['hidden'] = 'env_hidden'
|
139
138
|
ENV['empty'] = ''
|
140
139
|
@config = Config.read([], :env_lookouts => [:visible, :empty])
|
141
140
|
end
|
142
141
|
|
143
|
-
|
144
|
-
ENV
|
145
|
-
ENV
|
146
|
-
ENV
|
142
|
+
after do
|
143
|
+
ENV.delete 'visible'
|
144
|
+
ENV.delete 'hidden'
|
145
|
+
ENV.delete 'empty'
|
147
146
|
end
|
148
147
|
|
149
|
-
|
148
|
+
it "considers only specified environment variables that are nonempty" do
|
150
149
|
assert_equal 'env_visible', @config[:visible]
|
151
150
|
end
|
152
151
|
|
153
|
-
|
152
|
+
it "does not consider empty environment variables" do
|
154
153
|
assert_equal nil, @config[:empty]
|
155
154
|
end
|
156
155
|
|
157
|
-
|
156
|
+
it "does not consider unspecified environment variables" do
|
158
157
|
assert_equal nil, @config[:hidden]
|
159
158
|
end
|
160
159
|
end
|
161
160
|
|
162
|
-
|
163
|
-
|
161
|
+
describe "when handling the config file" do
|
162
|
+
it "allows specifying configuration file from command line arguments" do
|
164
163
|
@args = ['-f', CONFIG_FILE]
|
165
164
|
@config = Config.read(@args, {}) do
|
166
165
|
@args.slice!(0..1)
|
@@ -170,13 +169,21 @@ class ConfigTest < UnitTestCase
|
|
170
169
|
assert_equal 'file_defopt', @config[:defopt]
|
171
170
|
end
|
172
171
|
|
173
|
-
|
172
|
+
it "raises exception when trying to save and no config file is specified" do
|
174
173
|
@config = Config.read()
|
175
|
-
|
174
|
+
assert_raises(RuntimeError) { @config.save }
|
176
175
|
end
|
177
176
|
|
178
|
-
|
179
|
-
|
177
|
+
it "raises exception when trying to read invalid config file" do
|
178
|
+
Tempfile.open('.tweetwine') do |tf|
|
179
|
+
file = tf.path
|
180
|
+
FileUtils.touch file
|
181
|
+
assert_raises(ConfigError) { Config.read [], :config_file => file }
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
describe "when config file does not exist" do
|
186
|
+
before do
|
180
187
|
@tmp_dir = Dir.mktmpdir
|
181
188
|
@file = @tmp_dir + '/.tweetwine'
|
182
189
|
@excludes = [:secret]
|
@@ -185,22 +192,22 @@ class ConfigTest < UnitTestCase
|
|
185
192
|
@expected_config = {'new_opt' => 'to_file'}
|
186
193
|
end
|
187
194
|
|
188
|
-
|
195
|
+
after do
|
189
196
|
FileUtils.rm_rf @tmp_dir
|
190
197
|
end
|
191
198
|
|
192
|
-
|
199
|
+
it "ignores nonexisting config file for initial read" do
|
193
200
|
assert_contains_exactly @default_config.keys, @config.keys
|
194
201
|
end
|
195
202
|
|
196
|
-
|
203
|
+
it "saves config to the file, implicitly without config file, env lookouts, and excludes set itself" do
|
197
204
|
@config[:new_opt] = 'to_file'
|
198
205
|
@config.save
|
199
206
|
stored = YAML.load_file @file
|
200
207
|
assert_equal(@expected_config, stored)
|
201
208
|
end
|
202
209
|
|
203
|
-
|
210
|
+
it "saves config to the file, explicitly without excluded entries" do
|
204
211
|
@config[@excludes.first] = 'password'
|
205
212
|
@config[:new_opt] = 'to_file'
|
206
213
|
@config.save
|
@@ -208,7 +215,7 @@ class ConfigTest < UnitTestCase
|
|
208
215
|
assert_equal(@expected_config, stored)
|
209
216
|
end
|
210
217
|
|
211
|
-
|
218
|
+
it "modifying exclusions after initial read has no effect on config file location and exclusions" do
|
212
219
|
@config[:config_file] = @tmp_dir + '/.tweetwine.another'
|
213
220
|
@config[:excludes] << :new_opt
|
214
221
|
@config[:new_opt] = 'to_file'
|
@@ -217,22 +224,17 @@ class ConfigTest < UnitTestCase
|
|
217
224
|
assert_equal(@expected_config, stored)
|
218
225
|
end
|
219
226
|
|
220
|
-
|
227
|
+
it "sets config file permissions accessible only to the user when saving" do
|
221
228
|
@config.save
|
222
229
|
assert_equal 0600, file_mode(@file)
|
223
230
|
end
|
224
231
|
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
should "not set config file permissions when saving" do
|
233
|
-
@config.save
|
234
|
-
assert_equal @original_mode, file_mode(@file)
|
235
|
-
end
|
232
|
+
it "does not change config file permissions when saving over existing file" do
|
233
|
+
FileUtils.touch @file
|
234
|
+
original_mode = 0644
|
235
|
+
File.chmod original_mode, @file
|
236
|
+
@config.save
|
237
|
+
assert_equal original_mode, file_mode(@file)
|
236
238
|
end
|
237
239
|
end
|
238
240
|
end
|
data/test/unit/helper.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'helper'
|
4
|
+
require 'mocha'
|
5
|
+
|
6
|
+
Mocha::Configuration.prevent(:stubbing_non_existent_method)
|
7
|
+
|
8
|
+
module Tweetwine::Test
|
9
|
+
module Unit
|
10
|
+
module Assertions
|
11
|
+
# Asserts whether an Enumeration-like object contains all the elements.
|
12
|
+
# Fails unless +actual+ contains the same elements as +expected+,
|
13
|
+
# ignoring the order of the elements.
|
14
|
+
def assert_contains_exactly(expected, actual, msg_diff_size = nil, msg_diff_elems = nil)
|
15
|
+
assert_equal(expected.size, actual.size, message(msg_diff_size) {
|
16
|
+
'Expected %s to be of same size as %s' % [actual.inspect, expected.inspect]
|
17
|
+
})
|
18
|
+
assert(enumerable_minus_each_element(actual, expected).empty?, message(msg_diff_elems) {
|
19
|
+
'Expected %s to contain all the elements of %s' % [actual.inspect, expected.inspect]
|
20
|
+
})
|
21
|
+
end
|
22
|
+
|
23
|
+
# Fails unless +str+ is a full match to +regex+.
|
24
|
+
def assert_full_match(regex, str, msg = nil)
|
25
|
+
match_data = regex.match(str)
|
26
|
+
assert(str == match_data.to_s, message(msg) {
|
27
|
+
'Expected %s to be a full match to %s' % [str, regex.inspect]
|
28
|
+
})
|
29
|
+
end
|
30
|
+
|
31
|
+
# Fails if +str+ is a full match to +regex+.
|
32
|
+
def assert_no_full_match(regex, str, msg = nil)
|
33
|
+
match_data = regex.match(str)
|
34
|
+
assert(str != match_data.to_s, message(msg) {
|
35
|
+
'Expected %s not to be a full match to %s' % [str, regex.inspect]
|
36
|
+
})
|
37
|
+
end
|
38
|
+
|
39
|
+
# Fails unless +fun.call(*args)+ is equal to +expected+ and
|
40
|
+
# +fun.call(*args)+ is equal to +fun.call(*args.reverse)+.
|
41
|
+
def assert_commutative(expected, args, msg_not_expected = nil, msg_not_commutative = nil, &fun)
|
42
|
+
left_args = args
|
43
|
+
left_actual = fun.call(left_args)
|
44
|
+
assert_equal(expected, left_actual, message(msg_not_expected) {
|
45
|
+
'Expected %s, not %s' % [expected.inspect, left_actual.inspect]
|
46
|
+
})
|
47
|
+
right_args = args.reverse
|
48
|
+
right_actual = fun.call(*right_args)
|
49
|
+
assert_equal(left_actual, right_actual, message(msg_not_commutative) {
|
50
|
+
'Expected fun%s => %s to be commutative with fun%s => %s' %
|
51
|
+
[left_args.inspect, left_actual.inspect, right_args.inspect, right_actual.inspect]
|
52
|
+
})
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def enumerable_minus_each_element(enumerable, elements)
|
58
|
+
remaining = enumerable.dup.to_a
|
59
|
+
elements.each do |e|
|
60
|
+
index = remaining.index(e)
|
61
|
+
remaining.delete_at(index) if index
|
62
|
+
end
|
63
|
+
remaining
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
module Doubles
|
68
|
+
def mock_config
|
69
|
+
@config = mock('Config')
|
70
|
+
Tweetwine::CLI.stubs(:config).returns(@config)
|
71
|
+
end
|
72
|
+
|
73
|
+
def mock_http
|
74
|
+
@http = mock('Http')
|
75
|
+
Tweetwine::CLI.stubs(:http).returns(@http)
|
76
|
+
end
|
77
|
+
|
78
|
+
def mock_oauth
|
79
|
+
@oauth = mock('OAuth')
|
80
|
+
Tweetwine::CLI.stubs(:oauth).returns(@oauth)
|
81
|
+
end
|
82
|
+
|
83
|
+
def mock_ui
|
84
|
+
@ui = mock('UI')
|
85
|
+
Tweetwine::CLI.stubs(:ui).returns(@ui)
|
86
|
+
end
|
87
|
+
|
88
|
+
def mock_url_shortener
|
89
|
+
@url_shortener = mock('UrlShortener')
|
90
|
+
Tweetwine::CLI.stubs(:url_shortener).returns(@url_shortener)
|
91
|
+
end
|
92
|
+
|
93
|
+
def stub_config(options = {})
|
94
|
+
@config = options
|
95
|
+
Tweetwine::CLI.stubs(:config).returns(@config)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
class TestCase < MiniTest::Spec
|
100
|
+
include WebMockIntegration
|
101
|
+
include Tweetwine
|
102
|
+
include Assertions
|
103
|
+
include Doubles
|
104
|
+
include Tweetwine::Test::CommonHelper
|
105
|
+
extend Tweetwine::Test::CommonHelper
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
data/test/unit/http_test.rb
CHANGED
@@ -1,51 +1,51 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'unit/helper'
|
4
4
|
|
5
|
-
module Tweetwine::Test
|
5
|
+
module Tweetwine::Test::Unit
|
6
6
|
|
7
|
-
class HttpTest <
|
8
|
-
RESPONSE_BODY
|
9
|
-
CUSTOM_HEADERS
|
10
|
-
SITE_URL
|
11
|
-
LATEST_ARTICLES_URL
|
12
|
-
LATEST_ARTICLES_URL_SORTED
|
13
|
-
NEW_ARTICLE_URL
|
14
|
-
PAYLOAD
|
7
|
+
class HttpTest < TestCase
|
8
|
+
RESPONSE_BODY = "resp"
|
9
|
+
CUSTOM_HEADERS = {'X-Custom' => 'true'}
|
10
|
+
SITE_URL = "https://a-site.org"
|
11
|
+
LATEST_ARTICLES_URL = "#{SITE_URL}/articles/latest"
|
12
|
+
LATEST_ARTICLES_URL_SORTED = "#{LATEST_ARTICLES_URL}?sort=date"
|
13
|
+
NEW_ARTICLE_URL = "#{SITE_URL}/articles/new"
|
14
|
+
PAYLOAD = {:msg => 'gloomy night'}
|
15
15
|
|
16
|
-
|
16
|
+
before do
|
17
17
|
mock_ui
|
18
18
|
@client = Http::Client.new
|
19
19
|
def @client.sleep(*args); end # speed up tests
|
20
20
|
end
|
21
21
|
|
22
22
|
%w{http https}.each do |scheme|
|
23
|
-
|
24
|
-
url = "#{scheme}://site.org/"
|
23
|
+
it "supports #{scheme} scheme" do
|
24
|
+
url = "#{scheme}://b-site.org/"
|
25
25
|
stub_request(:get, url)
|
26
26
|
@client.get(url)
|
27
27
|
assert_requested(:get, url)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
-
|
31
|
+
it "returns response body when successful response" do
|
32
32
|
stub_request(:get, LATEST_ARTICLES_URL).to_return(:body => RESPONSE_BODY)
|
33
33
|
assert_equal(RESPONSE_BODY, @client.get(LATEST_ARTICLES_URL))
|
34
34
|
end
|
35
35
|
|
36
|
-
|
36
|
+
it "sends GET request with query parameters and custom headers" do
|
37
37
|
stub_request(:get, LATEST_ARTICLES_URL_SORTED)
|
38
38
|
@client.get(LATEST_ARTICLES_URL_SORTED, CUSTOM_HEADERS)
|
39
39
|
assert_requested(:get, LATEST_ARTICLES_URL_SORTED, :headers => CUSTOM_HEADERS)
|
40
40
|
end
|
41
41
|
|
42
|
-
|
42
|
+
it "sends POST request with payload and custom headers" do
|
43
43
|
stub_request(:post, NEW_ARTICLE_URL).with(:body => PAYLOAD, :headers => CUSTOM_HEADERS)
|
44
44
|
@client.post(NEW_ARTICLE_URL, PAYLOAD, CUSTOM_HEADERS)
|
45
45
|
assert_requested(:post, NEW_ARTICLE_URL, :body => PAYLOAD, :headers => CUSTOM_HEADERS)
|
46
46
|
end
|
47
47
|
|
48
|
-
|
48
|
+
it "stores response code and message in HttpError when failed response" do
|
49
49
|
code, message = 501, 'Not Implemented'
|
50
50
|
stub_request(:get, LATEST_ARTICLES_URL).to_return(:status => [code, message])
|
51
51
|
begin
|
@@ -59,23 +59,23 @@ class HttpTest < UnitTestCase
|
|
59
59
|
end
|
60
60
|
|
61
61
|
[:get, :post].each do |method|
|
62
|
-
|
62
|
+
it "raises HttpError when failed response to #{method} request" do
|
63
63
|
stub_request(method, LATEST_ARTICLES_URL).to_return(:status => [500, "Internal Server Error"])
|
64
|
-
|
64
|
+
assert_raises(HttpError) { @client.send(method, LATEST_ARTICLES_URL) }
|
65
65
|
end
|
66
66
|
|
67
|
-
|
67
|
+
it "retries connection upon connection timeout to #{method} request" do
|
68
68
|
stub_request(method, LATEST_ARTICLES_URL).to_timeout.then.to_return(:body => RESPONSE_BODY)
|
69
69
|
@ui.expects(:warn).with("Could not connect -- retrying in 4 seconds")
|
70
70
|
@client.send(method, LATEST_ARTICLES_URL)
|
71
71
|
assert_equal(RESPONSE_BODY, @client.send(method, LATEST_ARTICLES_URL))
|
72
72
|
end
|
73
73
|
|
74
|
-
|
74
|
+
it "retries connection a maximum of certain number of times upon connection timeout to #{method} request" do
|
75
75
|
stub_request(method, LATEST_ARTICLES_URL).to_timeout
|
76
76
|
io_calls = sequence("IO")
|
77
77
|
Http::Client::MAX_RETRIES.times { @ui.expects(:warn).in_sequence(io_calls) }
|
78
|
-
|
78
|
+
assert_raises(TimeoutError) { @client.send(method, LATEST_ARTICLES_URL) }
|
79
79
|
end
|
80
80
|
|
81
81
|
[
|
@@ -83,22 +83,22 @@ class HttpTest < UnitTestCase
|
|
83
83
|
[Errno::ECONNRESET, 'connection reset'],
|
84
84
|
[SocketError, 'socket error']
|
85
85
|
].each do |(error, desc)|
|
86
|
-
|
86
|
+
it "retries connection upon #{desc} to #{method} request" do
|
87
87
|
stub_request(method, LATEST_ARTICLES_URL).to_raise(error).then.to_return(:body => RESPONSE_BODY)
|
88
88
|
@ui.expects(:warn).with("Could not connect -- retrying in 4 seconds")
|
89
89
|
@client.send(method, LATEST_ARTICLES_URL)
|
90
90
|
assert_equal(RESPONSE_BODY, @client.send(method, LATEST_ARTICLES_URL))
|
91
91
|
end
|
92
92
|
|
93
|
-
|
93
|
+
it "retries connection a maximum of certain number of times upon #{desc} to #{method} request" do
|
94
94
|
stub_request(method, LATEST_ARTICLES_URL).to_raise(error)
|
95
95
|
io_calls = sequence("IO")
|
96
96
|
Http::Client::MAX_RETRIES.times { @ui.expects(:warn).in_sequence(io_calls) }
|
97
|
-
|
97
|
+
assert_raises(ConnectionError) { @client.send(method, LATEST_ARTICLES_URL) }
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
-
|
101
|
+
it "allows access to the #{method} request object just before sending it" do
|
102
102
|
stub_request(method, LATEST_ARTICLES_URL)
|
103
103
|
@client.send(method, LATEST_ARTICLES_URL) do |_, request|
|
104
104
|
request['X-Quote'] = 'You monster.'
|
@@ -107,7 +107,7 @@ class HttpTest < UnitTestCase
|
|
107
107
|
end
|
108
108
|
end
|
109
109
|
|
110
|
-
|
110
|
+
describe "for proxy support" do
|
111
111
|
[
|
112
112
|
['proxy.net', 'proxy.net', 8080],
|
113
113
|
['http://proxy.net', 'proxy.net', 8080],
|
@@ -115,7 +115,7 @@ class HttpTest < UnitTestCase
|
|
115
115
|
['http://proxy.net:8080', 'proxy.net', 8080],
|
116
116
|
['http://proxy.net:8182', 'proxy.net', 8182]
|
117
117
|
].each do |proxy_url, expected_host, expected_port|
|
118
|
-
|
118
|
+
it "supports proxy, when its URL is #{proxy_url}" do
|
119
119
|
proxy = Http::Client.new(:http_proxy => proxy_url)
|
120
120
|
net_http = proxy.instance_variable_get(:@http)
|
121
121
|
assert(net_http.proxy_class?)
|
@@ -124,49 +124,49 @@ class HttpTest < UnitTestCase
|
|
124
124
|
end
|
125
125
|
end
|
126
126
|
|
127
|
-
|
128
|
-
|
127
|
+
it "raises CommandLineError if given invalid port" do
|
128
|
+
assert_raises(CommandLineError) do
|
129
129
|
Http::Client.new(:http_proxy => 'http://proxy.net:asdf')
|
130
130
|
end
|
131
131
|
end
|
132
132
|
end
|
133
133
|
|
134
|
-
|
135
|
-
|
134
|
+
describe "when using client as resource" do
|
135
|
+
before do
|
136
136
|
@resource = @client.as_resource(SITE_URL)
|
137
137
|
end
|
138
138
|
|
139
|
-
|
139
|
+
it "sends GET request with custom headers to the base URL" do
|
140
140
|
stub_request(:get, SITE_URL)
|
141
141
|
@resource.get(CUSTOM_HEADERS)
|
142
142
|
assert_requested(:get, SITE_URL, :headers => CUSTOM_HEADERS)
|
143
143
|
end
|
144
144
|
|
145
|
-
|
145
|
+
it "sends POST request with payload and custom headers to the base URL" do
|
146
146
|
stub_request(:post, SITE_URL).with(:body => PAYLOAD, :headers => CUSTOM_HEADERS)
|
147
147
|
@resource.post(PAYLOAD, CUSTOM_HEADERS)
|
148
148
|
assert_requested(:post, SITE_URL, :body => PAYLOAD, :headers => CUSTOM_HEADERS)
|
149
149
|
end
|
150
150
|
|
151
|
-
|
152
|
-
|
151
|
+
describe "when constructing new resource from the original as a sub-URL" do
|
152
|
+
before do
|
153
153
|
@news_resource = @resource['news']
|
154
154
|
@expected_news_url = "#{SITE_URL}/news"
|
155
155
|
end
|
156
156
|
|
157
|
-
|
157
|
+
it "sends GET request with custom headers to the sub-URL" do
|
158
158
|
stub_request(:get, @expected_news_url)
|
159
159
|
@news_resource.get(CUSTOM_HEADERS)
|
160
160
|
assert_requested(:get, @expected_news_url, :headers => CUSTOM_HEADERS)
|
161
161
|
end
|
162
162
|
|
163
|
-
|
163
|
+
it "sends POST request with payload and custom headers to the sub-URL" do
|
164
164
|
stub_request(:post, @expected_news_url).with(:body => PAYLOAD, :headers => CUSTOM_HEADERS)
|
165
165
|
@news_resource.post(PAYLOAD, CUSTOM_HEADERS)
|
166
166
|
assert_requested(:post, @expected_news_url, :body => PAYLOAD, :headers => CUSTOM_HEADERS)
|
167
167
|
end
|
168
168
|
|
169
|
-
|
169
|
+
it "further constructs a new resource from the original" do
|
170
170
|
news_resource = @news_resource['top']
|
171
171
|
expected_url = "#{SITE_URL}/news/top"
|
172
172
|
stub_request(:get, expected_url)
|
data/test/unit/oauth_test.rb
CHANGED
@@ -1,22 +1,21 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'unit/helper'
|
4
|
+
require 'fixture/oauth'
|
5
|
+
require 'net/http'
|
5
6
|
|
6
|
-
|
7
|
+
module Tweetwine::Test::Unit
|
7
8
|
|
8
|
-
|
9
|
+
class OAuthTest < TestCase
|
10
|
+
include Test::Fixture::OAuth
|
9
11
|
|
10
|
-
|
11
|
-
include Fixture::OAuth
|
12
|
-
|
13
|
-
setup do
|
12
|
+
before do
|
14
13
|
mock_http
|
15
14
|
mock_ui
|
16
15
|
@oauth = OAuth.new
|
17
16
|
end
|
18
17
|
|
19
|
-
|
18
|
+
it "authorizes with PIN code so that request can be signed" do
|
20
19
|
expect_complete_oauth_dance
|
21
20
|
@oauth.authorize
|
22
21
|
connection, request = *fake_http_connection_and_request
|
@@ -25,26 +24,26 @@ class OAuthTest < UnitTestCase
|
|
25
24
|
assert_match(/oauth_token="#{ACCESS_TOKEN_KEY}"/, request['Authorization'])
|
26
25
|
end
|
27
26
|
|
28
|
-
|
27
|
+
it "raises AuthorizationError if OAuth dance fails due to HTTP 4xx response" do
|
29
28
|
@http.expects(:post).
|
30
29
|
with(REQUEST_TOKEN_URL).
|
31
30
|
raises(HttpError.new(401, 'Unauthorized'))
|
32
|
-
|
31
|
+
assert_raises(AuthorizationError) { @oauth.authorize }
|
33
32
|
end
|
34
33
|
|
35
|
-
|
34
|
+
it "passes other exceptions than due to HTTP 4xx responses through" do
|
36
35
|
@http.expects(:post).
|
37
36
|
with(REQUEST_TOKEN_URL).
|
38
37
|
raises(HttpError.new(503, 'Service Unavailable'))
|
39
|
-
|
38
|
+
assert_raises(HttpError) { @oauth.authorize }
|
40
39
|
end
|
41
40
|
|
42
|
-
|
43
|
-
|
41
|
+
describe "when access token is known" do
|
42
|
+
before do
|
44
43
|
@oauth = OAuth.new(Obfuscate.write("#{ACCESS_TOKEN_KEY}:2"))
|
45
44
|
end
|
46
45
|
|
47
|
-
|
46
|
+
it "signs request with it" do
|
48
47
|
connection, request = *fake_http_connection_and_request
|
49
48
|
@oauth.request_signer.call(connection, request)
|
50
49
|
assert_match(/^OAuth /, request['Authorization'])
|