cli_test.rb
6.81 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
require File.dirname(__FILE__) + '/test_helper'
class Twurl::CLI::OptionParsingTest < Minitest::Test
TEST_PATH = '/1.1/url/does/not/matter.json'
module CommandParsingTests
def test_no_command_specified_falls_to_default_command
options = Twurl::CLI.parse_options([TEST_PATH])
assert_equal Twurl::CLI::DEFAULT_COMMAND, options.command
end
def test_supported_command_specified_extracts_the_command
expected_command = Twurl::CLI::SUPPORTED_COMMANDS.first
options = Twurl::CLI.parse_options([expected_command])
assert_equal expected_command, options.command
end
def test_unsupported_command_specified_sets_default_command
unsupported_command = 'unsupported'
options = Twurl::CLI.parse_options([TEST_PATH, unsupported_command])
assert_equal Twurl::CLI::DEFAULT_COMMAND, options.command
end
end
include CommandParsingTests
module PathParsingTests
def test_missing_path_throws_no_path_found
stub(Twurl::CLI).puts
assert_raises Twurl::CLI::NoPathFound do
Twurl::CLI.parse_options([])
end
end
def test_uri_params_are_encoded
options = Twurl::CLI.parse_options(["/1.1/url?baz=bamf:rofl"])
assert_equal options.path, "/1.1/url?baz=bamf%3Arofl"
end
end
include PathParsingTests
module RequestMethodParsingTests
def test_request_method_is_default_if_unspecified
options = Twurl::CLI.parse_options([TEST_PATH])
assert_equal Twurl::Options::DEFAULT_REQUEST_METHOD, options.request_method
end
def test_specifying_a_request_method_extracts_and_normalizes_request_method
variations = [%w[-X put], %w[-X PUT], %w[--request-method PUT], %w[--request-method put]]
variations.each do |option_variation|
order_variant_1 = [option_variation, TEST_PATH].flatten
order_variant_2 = [TEST_PATH, option_variation].flatten
[order_variant_1, order_variant_2].each do |args|
options = Twurl::CLI.parse_options(args)
assert_equal 'put', options.request_method
end
end
end
def test_specifying_unsupported_request_method_returns_an_error
Twurl::CLI.parse_options([TEST_PATH, '-X', 'UNSUPPORTED'])
end
end
include RequestMethodParsingTests
module OAuthClientOptionParsingTests
def test_extracting_the_consumer_key
mock(Twurl::CLI).prompt_for('Consumer key').never
options = Twurl::CLI.parse_options([TEST_PATH, '-c', 'the-key'])
assert_equal 'the-key', options.consumer_key
end
def test_consumer_key_option_with_no_value_prompts_user_for_value
mock(Twurl::CLI).prompt_for('Consumer key').times(1) { 'inputted-key'}
options = Twurl::CLI.parse_options([TEST_PATH, '-c'])
assert_equal 'inputted-key', options.consumer_key
end
end
include OAuthClientOptionParsingTests
module DataParsingTests
def test_extracting_a_single_key_value_pair
options = Twurl::CLI.parse_options([TEST_PATH, '-d', 'key=value'])
assert_equal({'key' => 'value'}, options.data)
options = Twurl::CLI.parse_options([TEST_PATH, '--data', 'key=value'])
assert_equal({'key' => 'value'}, options.data)
end
def test_passing_data_and_no_explicit_request_method_defaults_request_method_to_post
options = Twurl::CLI.parse_options([TEST_PATH, '-d', 'key=value'])
assert_equal 'post', options.request_method
end
def test_passing_data_and_an_explicit_request_method_uses_the_specified_method
options = Twurl::CLI.parse_options([TEST_PATH, '-d', 'key=value', '-X', 'DELETE'])
assert_equal({'key' => 'value'}, options.data)
assert_equal 'delete', options.request_method
end
def test_multiple_pairs_when_option_is_specified_multiple_times_on_command_line_collects_all
options = Twurl::CLI.parse_options([TEST_PATH, '-d', 'key=value', '-d', 'another=pair'])
assert_equal({'key' => 'value', 'another' => 'pair'}, options.data)
end
def test_multiple_pairs_separated_by_ampersand_are_all_captured
options = Twurl::CLI.parse_options([TEST_PATH, '-d', 'key=value&another=pair'])
assert_equal({'key' => 'value', 'another' => 'pair'}, options.data)
end
def test_extracting_an_empty_key_value_pair
options = Twurl::CLI.parse_options([TEST_PATH, '-d', 'key='])
assert_equal({'key' => ''}, options.data)
options = Twurl::CLI.parse_options([TEST_PATH, '--data', 'key='])
assert_equal({'key' => ''}, options.data)
end
end
include DataParsingTests
module HeaderParsingTests
def test_extracting_a_single_header
options = Twurl::CLI.parse_options([TEST_PATH, '-A', 'Key: Value'])
assert_equal({'Key' => 'Value'}, options.headers)
options = Twurl::CLI.parse_options([TEST_PATH, '--header', 'Key: Value'])
assert_equal({'Key' => 'Value'}, options.headers)
end
def test_multiple_headers_when_option_is_specified_multiple_times_on_command_line_collects_all
options = Twurl::CLI.parse_options([TEST_PATH, '-A', 'Key: Value', '-A', 'Another: Pair'])
assert_equal({'Key' => 'Value', 'Another' => 'Pair'}, options.headers)
end
end
include HeaderParsingTests
module SSLDisablingTests
def test_ssl_is_on_by_default
options = Twurl::CLI.parse_options([TEST_PATH])
assert options.ssl?
end
def test_passing_no_ssl_option_disables_ssl
['-U', '--no-ssl'].each do |switch|
options = Twurl::CLI.parse_options([TEST_PATH, switch])
assert !options.ssl?
end
end
end
include SSLDisablingTests
module HostOptionTests
def test_not_specifying_host_sets_it_to_the_default
options = Twurl::CLI.parse_options([TEST_PATH])
assert_equal Twurl::Options::DEFAULT_HOST, options.host
end
def test_setting_host_updates_to_requested_value
custom_host = 'localhost:3000'
assert Twurl::Options::DEFAULT_HOST != custom_host
[[TEST_PATH, '-H', custom_host], [TEST_PATH, '--host', custom_host]].each do |option_combination|
options = Twurl::CLI.parse_options(option_combination)
assert_equal custom_host, options.host
end
end
def test_protocol_is_stripped_from_host
custom_host = 'localhost:3000'
options = Twurl::CLI.parse_options([TEST_PATH, '-H', "https://"+custom_host])
assert_equal custom_host, options.host
end
end
include HostOptionTests
module ProxyOptionTests
def test_not_specifying_proxy_sets_it_to_nil
options = Twurl::CLI.parse_options([TEST_PATH])
assert_equal nil, options.proxy
end
def test_setting_proxy_updates_to_requested_value
custom_proxy = 'localhost:80'
[[TEST_PATH, '-P', custom_proxy], [TEST_PATH, '--proxy', custom_proxy]].each do |option_combination|
options = Twurl::CLI.parse_options(option_combination)
assert_equal custom_proxy, options.proxy
end
end
end
include ProxyOptionTests
end