rest-man 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/{multi-matrix-test.yml → ci.yml} +10 -1
  3. data/.github/workflows/single-matrix-test.yml +1 -0
  4. data/.gitignore +2 -0
  5. data/.rubocop-disables.yml +4 -29
  6. data/AUTHORS +5 -1
  7. data/CHANGELOG.md +14 -0
  8. data/Gemfile +3 -0
  9. data/README.md +30 -37
  10. data/Rakefile +1 -59
  11. data/_doc/lib/restman/abstract_response/_follow_redirection.rdoc +7 -0
  12. data/_doc/lib/restman/abstract_response/beautify_headers.rdoc +24 -0
  13. data/_doc/lib/restman/abstract_response/cookie_jar.rdoc +4 -0
  14. data/_doc/lib/restman/abstract_response/cookies.rdoc +12 -0
  15. data/_doc/lib/restman/abstract_response/follow_get_redirection.rdoc +2 -0
  16. data/_doc/lib/restman/abstract_response/follow_redirection.rdoc +2 -0
  17. data/_doc/lib/restman/abstract_response/headers.rdoc +2 -0
  18. data/_doc/lib/restman/abstract_response/response_set_vars.rdoc +5 -0
  19. data/_doc/lib/restman/abstract_response/return.rdoc +9 -0
  20. data/_doc/lib/restman/add_before_execution_proc.rdoc +2 -0
  21. data/_doc/lib/restman/create_log.rdoc +2 -0
  22. data/_doc/lib/restman/exception.rdoc +6 -0
  23. data/_doc/lib/restman/exceptions/timeout.rdoc +4 -0
  24. data/_doc/lib/restman/exceptions.rdoc +4 -0
  25. data/_doc/lib/restman/log=.rdoc +3 -0
  26. data/_doc/lib/restman/params_array/new.rdoc +20 -0
  27. data/_doc/lib/restman/params_array/process_pair.rdoc +4 -0
  28. data/_doc/lib/restman/params_array.rdoc +11 -0
  29. data/_doc/lib/restman/platform/jruby?.rdoc +4 -0
  30. data/_doc/lib/restman/platform/mac_mri?.rdoc +5 -0
  31. data/_doc/lib/restman/proxy.rdoc +2 -0
  32. data/_doc/lib/restman/proxy_set?.rdoc +5 -0
  33. data/_doc/lib/restman/raw_response/new.rdoc +6 -0
  34. data/_doc/lib/restman/raw_response.rdoc +10 -0
  35. data/_doc/lib/restman/request/cookie_jar.rdoc +3 -0
  36. data/_doc/lib/restman/request/cookies.rdoc +11 -0
  37. data/_doc/lib/restman/request/default_headers.rdoc +5 -0
  38. data/_doc/lib/restman/request/default_ssl_cert_store.rdoc +8 -0
  39. data/_doc/lib/restman/request/init/cookie_jar.rdoc +55 -0
  40. data/_doc/lib/restman/request/init/http_method.rdoc +15 -0
  41. data/_doc/lib/restman/request/make_cookie_header.rdoc +8 -0
  42. data/_doc/lib/restman/request/make_headers.rdoc +25 -0
  43. data/_doc/lib/restman/request/maybe_convert_extension.rdoc +18 -0
  44. data/_doc/lib/restman/request/process_result.rdoc +4 -0
  45. data/_doc/lib/restman/request/proxy_uri.rdoc +7 -0
  46. data/_doc/lib/restman/request/stringify_headers.rdoc +9 -0
  47. data/_doc/lib/restman/request/use_ssl.rdoc +4 -0
  48. data/_doc/lib/restman/request.rdoc +46 -0
  49. data/_doc/lib/restman/reset_before_execution_procs.rdoc +1 -0
  50. data/_doc/lib/restman/resource/[].rdoc +25 -0
  51. data/_doc/lib/restman/resource.rdoc +33 -0
  52. data/_doc/lib/restman/response/body.rdoc +7 -0
  53. data/_doc/lib/restman/response/create.rdoc +10 -0
  54. data/_doc/lib/restman/response/fix_encoding.rdoc +2 -0
  55. data/_doc/lib/restman/statuses.rdoc +11 -0
  56. data/_doc/lib/restman/utils/cgi_parse_header.rdoc +6 -0
  57. data/_doc/lib/restman/utils/encode_query_string.rdoc +90 -0
  58. data/_doc/lib/restman/utils/escape.rdoc +11 -0
  59. data/_doc/lib/restman/utils/flatten_params.rdoc +16 -0
  60. data/_doc/lib/restman/utils/get_encoding_from_headers.rdoc +24 -0
  61. data/_doc/lib/restman.rdoc +43 -0
  62. data/bin/console +15 -0
  63. data/lib/restman/abstract_response.rb +13 -60
  64. data/lib/restman/exception.rb +43 -0
  65. data/lib/restman/exceptions/exception_with_response.rb +7 -0
  66. data/lib/restman/exceptions/exceptions_map.rb +26 -0
  67. data/lib/restman/exceptions/request_failed.rb +15 -0
  68. data/lib/restman/exceptions/server_broke_connection.rb +13 -0
  69. data/lib/restman/exceptions/timeout.rb +37 -0
  70. data/lib/restman/params_array/process_pair.rb +39 -0
  71. data/lib/restman/params_array.rb +3 -48
  72. data/lib/restman/payload/base.rb +57 -0
  73. data/lib/restman/payload/multipart/write_content_disposition.rb +88 -0
  74. data/lib/restman/payload/multipart.rb +56 -0
  75. data/lib/restman/payload/streamed.rb +22 -0
  76. data/lib/restman/payload/url_encoded.rb +14 -0
  77. data/lib/restman/payload.rb +14 -196
  78. data/lib/restman/platform.rb +2 -18
  79. data/lib/restman/raw_response.rb +2 -14
  80. data/lib/restman/request/default_ssl_cert_store.rb +13 -0
  81. data/lib/restman/request/fetch_body_to_tempfile.rb +58 -0
  82. data/lib/restman/request/init/cookie_jar.rb +65 -0
  83. data/lib/restman/request/init/ssl_opts.rb +70 -0
  84. data/lib/restman/request/init/url/add_query_from_headers.rb +51 -0
  85. data/lib/restman/request/init/url/normalize_url.rb +19 -0
  86. data/lib/restman/request/init/url.rb +40 -0
  87. data/lib/restman/request/init.rb +106 -0
  88. data/lib/restman/request/log_request.rb +46 -0
  89. data/lib/restman/request/make_cookie_header.rb +16 -0
  90. data/lib/restman/request/make_headers.rb +39 -0
  91. data/lib/restman/request/maybe_convert_extension.rb +28 -0
  92. data/lib/restman/request/net_http_object.rb +25 -0
  93. data/lib/restman/request/process_result.rb +36 -0
  94. data/lib/restman/request/proxy_uri.rb +31 -0
  95. data/lib/restman/request/stringify_headers.rb +36 -0
  96. data/lib/restman/request/transmit.rb +152 -0
  97. data/lib/restman/request.rb +60 -745
  98. data/lib/restman/resource.rb +2 -60
  99. data/lib/restman/response.rb +3 -21
  100. data/lib/restman/statuses.rb +75 -0
  101. data/lib/restman/statuses_compatibility.rb +18 -0
  102. data/lib/restman/utils.rb +10 -206
  103. data/lib/restman/version.rb +1 -1
  104. data/lib/restman.rb +24 -62
  105. data/matrixeval.yml +19 -1
  106. data/rest-man.gemspec +4 -10
  107. data/spec/integration/capath_digicert/ce5e74ef.0 +1 -1
  108. data/spec/integration/request_spec.rb +13 -1
  109. data/spec/spec_helper.rb +11 -0
  110. data/spec/unit/abstract_response_spec.rb +14 -0
  111. data/spec/unit/exception_spec.rb +64 -0
  112. data/spec/unit/exceptions/backwards_campatibility_spec.rb +29 -0
  113. data/spec/unit/exceptions/exceptions_map_spec.rb +89 -0
  114. data/spec/unit/exceptions/request_failed_spec.rb +51 -0
  115. data/spec/unit/exceptions/server_broke_connection_spec.rb +8 -0
  116. data/spec/unit/exceptions/timeout_spec.rb +59 -0
  117. data/spec/unit/params_array/process_pair_spec.rb +59 -0
  118. data/spec/unit/params_array_spec.rb +15 -10
  119. data/spec/unit/payload/multipart_spec.rb +116 -0
  120. data/spec/unit/payload/streamed_spec.rb +48 -0
  121. data/spec/unit/payload/url_encoded_spec.rb +65 -0
  122. data/spec/unit/payload_spec.rb +0 -208
  123. data/spec/unit/request/init/url/add_query_from_headers_spec.rb +40 -0
  124. data/spec/unit/request/init/url/normalize_url_spec.rb +25 -0
  125. data/spec/unit/request/init_spec.rb +83 -0
  126. data/spec/unit/request_spec.rb +143 -151
  127. data/spec/unit/utils_spec.rb +96 -104
  128. metadata +132 -16
  129. data/lib/restman/exceptions.rb +0 -238
  130. data/lib/restman/windows/root_certs.rb +0 -105
  131. data/lib/restman/windows.rb +0 -8
  132. data/spec/unit/exceptions_spec.rb +0 -108
  133. data/spec/unit/windows/root_certs_spec.rb +0 -22
@@ -0,0 +1,65 @@
1
+ # encoding: binary
2
+
3
+ require_relative '../_lib'
4
+
5
+ describe RestMan::Payload::UrlEncoded, :include_helpers do
6
+
7
+ it "should use standard enctype as default content-type" do
8
+ expect(RestMan::Payload::UrlEncoded.new({}).headers['Content-Type']).
9
+ to eq 'application/x-www-form-urlencoded'
10
+ end
11
+
12
+ it "should form properly encoded params" do
13
+ expect(RestMan::Payload::UrlEncoded.new({:foo => 'bar'}).to_s).
14
+ to eq "foo=bar"
15
+ expect(["foo=bar&baz=qux", "baz=qux&foo=bar"]).to include(
16
+ RestMan::Payload::UrlEncoded.new({:foo => 'bar', :baz => 'qux'}).to_s)
17
+ end
18
+
19
+ it "should escape parameters" do
20
+ expect(RestMan::Payload::UrlEncoded.new({'foo + bar' => 'baz'}).to_s).
21
+ to eq "foo+%2B+bar=baz"
22
+ end
23
+
24
+ it "should properly handle hashes as parameter" do
25
+ expect(RestMan::Payload::UrlEncoded.new({:foo => {:bar => 'baz'}}).to_s).
26
+ to eq "foo[bar]=baz"
27
+ expect(RestMan::Payload::UrlEncoded.new({:foo => {:bar => {:baz => 'qux'}}}).to_s).
28
+ to eq "foo[bar][baz]=qux"
29
+ end
30
+
31
+ it "should handle many attributes inside a hash" do
32
+ parameters = RestMan::Payload::UrlEncoded.new({:foo => {:bar => 'baz', :baz => 'qux'}}).to_s
33
+ expect(parameters).to eq 'foo[bar]=baz&foo[baz]=qux'
34
+ end
35
+
36
+ it "should handle attributes inside an array inside an hash" do
37
+ parameters = RestMan::Payload::UrlEncoded.new({"foo" => [{"bar" => 'baz'}, {"bar" => 'qux'}]}).to_s
38
+ expect(parameters).to eq 'foo[][bar]=baz&foo[][bar]=qux'
39
+ end
40
+
41
+ it "should handle arrays inside a hash inside a hash" do
42
+ parameters = RestMan::Payload::UrlEncoded.new({"foo" => {'even' => [0, 2], 'odd' => [1, 3]}}).to_s
43
+ expect(parameters).to eq 'foo[even][]=0&foo[even][]=2&foo[odd][]=1&foo[odd][]=3'
44
+ end
45
+
46
+ it "should form properly use symbols as parameters" do
47
+ expect(RestMan::Payload::UrlEncoded.new({:foo => :bar}).to_s).
48
+ to eq "foo=bar"
49
+ expect(RestMan::Payload::UrlEncoded.new({:foo => {:bar => :baz}}).to_s).
50
+ to eq "foo[bar]=baz"
51
+ end
52
+
53
+ it "should properly handle arrays as repeated parameters" do
54
+ expect(RestMan::Payload::UrlEncoded.new({:foo => ['bar']}).to_s).
55
+ to eq "foo[]=bar"
56
+ expect(RestMan::Payload::UrlEncoded.new({:foo => ['bar', 'baz']}).to_s).
57
+ to eq "foo[]=bar&foo[]=baz"
58
+ end
59
+
60
+ it 'should not close if stream already closed' do
61
+ p = RestMan::Payload::UrlEncoded.new({'foo ' => 'bar'})
62
+ 3.times {p.close}
63
+ end
64
+
65
+ end
@@ -11,214 +11,6 @@ describe RestMan::Payload, :include_helpers do
11
11
  end
12
12
  end
13
13
 
14
- context "A regular Payload" do
15
- it "should use standard enctype as default content-type" do
16
- expect(RestMan::Payload::UrlEncoded.new({}).headers['Content-Type']).
17
- to eq 'application/x-www-form-urlencoded'
18
- end
19
-
20
- it "should form properly encoded params" do
21
- expect(RestMan::Payload::UrlEncoded.new({:foo => 'bar'}).to_s).
22
- to eq "foo=bar"
23
- expect(["foo=bar&baz=qux", "baz=qux&foo=bar"]).to include(
24
- RestMan::Payload::UrlEncoded.new({:foo => 'bar', :baz => 'qux'}).to_s)
25
- end
26
-
27
- it "should escape parameters" do
28
- expect(RestMan::Payload::UrlEncoded.new({'foo + bar' => 'baz'}).to_s).
29
- to eq "foo+%2B+bar=baz"
30
- end
31
-
32
- it "should properly handle hashes as parameter" do
33
- expect(RestMan::Payload::UrlEncoded.new({:foo => {:bar => 'baz'}}).to_s).
34
- to eq "foo[bar]=baz"
35
- expect(RestMan::Payload::UrlEncoded.new({:foo => {:bar => {:baz => 'qux'}}}).to_s).
36
- to eq "foo[bar][baz]=qux"
37
- end
38
-
39
- it "should handle many attributes inside a hash" do
40
- parameters = RestMan::Payload::UrlEncoded.new({:foo => {:bar => 'baz', :baz => 'qux'}}).to_s
41
- expect(parameters).to eq 'foo[bar]=baz&foo[baz]=qux'
42
- end
43
-
44
- it "should handle attributes inside an array inside an hash" do
45
- parameters = RestMan::Payload::UrlEncoded.new({"foo" => [{"bar" => 'baz'}, {"bar" => 'qux'}]}).to_s
46
- expect(parameters).to eq 'foo[][bar]=baz&foo[][bar]=qux'
47
- end
48
-
49
- it "should handle arrays inside a hash inside a hash" do
50
- parameters = RestMan::Payload::UrlEncoded.new({"foo" => {'even' => [0, 2], 'odd' => [1, 3]}}).to_s
51
- expect(parameters).to eq 'foo[even][]=0&foo[even][]=2&foo[odd][]=1&foo[odd][]=3'
52
- end
53
-
54
- it "should form properly use symbols as parameters" do
55
- expect(RestMan::Payload::UrlEncoded.new({:foo => :bar}).to_s).
56
- to eq "foo=bar"
57
- expect(RestMan::Payload::UrlEncoded.new({:foo => {:bar => :baz}}).to_s).
58
- to eq "foo[bar]=baz"
59
- end
60
-
61
- it "should properly handle arrays as repeated parameters" do
62
- expect(RestMan::Payload::UrlEncoded.new({:foo => ['bar']}).to_s).
63
- to eq "foo[]=bar"
64
- expect(RestMan::Payload::UrlEncoded.new({:foo => ['bar', 'baz']}).to_s).
65
- to eq "foo[]=bar&foo[]=baz"
66
- end
67
-
68
- it 'should not close if stream already closed' do
69
- p = RestMan::Payload::UrlEncoded.new({'foo ' => 'bar'})
70
- 3.times {p.close}
71
- end
72
-
73
- end
74
-
75
- context "A multipart Payload" do
76
- it "should use standard enctype as default content-type" do
77
- m = RestMan::Payload::Multipart.new({})
78
- allow(m).to receive(:boundary).and_return(123)
79
- expect(m.headers['Content-Type']).to eq 'multipart/form-data; boundary=123'
80
- end
81
-
82
- it 'should not error on close if stream already closed' do
83
- m = RestMan::Payload::Multipart.new(:file => File.new(test_image_path))
84
- 3.times {m.close}
85
- end
86
-
87
- it "should form properly separated multipart data" do
88
- m = RestMan::Payload::Multipart.new([[:bar, "baz"], [:foo, "bar"]])
89
- expect(m.to_s).to eq <<-EOS
90
- --#{m.boundary}\r
91
- Content-Disposition: form-data; name="bar"\r
92
- \r
93
- baz\r
94
- --#{m.boundary}\r
95
- Content-Disposition: form-data; name="foo"\r
96
- \r
97
- bar\r
98
- --#{m.boundary}--\r
99
- EOS
100
- end
101
-
102
- it "should not escape parameters names" do
103
- m = RestMan::Payload::Multipart.new([["bar ", "baz"]])
104
- expect(m.to_s).to eq <<-EOS
105
- --#{m.boundary}\r
106
- Content-Disposition: form-data; name="bar "\r
107
- \r
108
- baz\r
109
- --#{m.boundary}--\r
110
- EOS
111
- end
112
-
113
- it "should form properly separated multipart data" do
114
- f = File.new(test_image_path)
115
- m = RestMan::Payload::Multipart.new({:foo => f})
116
- expect(m.to_s).to eq <<-EOS
117
- --#{m.boundary}\r
118
- Content-Disposition: form-data; name="foo"; filename="ISS.jpg"\r
119
- Content-Type: image/jpeg\r
120
- \r
121
- #{File.open(f.path, 'rb'){|bin| bin.read}}\r
122
- --#{m.boundary}--\r
123
- EOS
124
- end
125
-
126
- it "should ignore the name attribute when it's not set" do
127
- f = File.new(test_image_path)
128
- m = RestMan::Payload::Multipart.new({nil => f})
129
- expect(m.to_s).to eq <<-EOS
130
- --#{m.boundary}\r
131
- Content-Disposition: form-data; filename="ISS.jpg"\r
132
- Content-Type: image/jpeg\r
133
- \r
134
- #{File.open(f.path, 'rb'){|bin| bin.read}}\r
135
- --#{m.boundary}--\r
136
- EOS
137
- end
138
-
139
- it "should detect optional (original) content type and filename" do
140
- f = File.new(test_image_path)
141
- expect(f).to receive(:content_type).and_return('text/plain')
142
- expect(f).to receive(:original_filename).and_return('foo.txt')
143
- m = RestMan::Payload::Multipart.new({:foo => f})
144
- expect(m.to_s).to eq <<-EOS
145
- --#{m.boundary}\r
146
- Content-Disposition: form-data; name="foo"; filename="foo.txt"\r
147
- Content-Type: text/plain\r
148
- \r
149
- #{File.open(f.path, 'rb'){|bin| bin.read}}\r
150
- --#{m.boundary}--\r
151
- EOS
152
- end
153
-
154
- it "should handle hash in hash parameters" do
155
- m = RestMan::Payload::Multipart.new({:bar => {:baz => "foo"}})
156
- expect(m.to_s).to eq <<-EOS
157
- --#{m.boundary}\r
158
- Content-Disposition: form-data; name="bar[baz]"\r
159
- \r
160
- foo\r
161
- --#{m.boundary}--\r
162
- EOS
163
-
164
- f = File.new(test_image_path)
165
- f.instance_eval "def content_type; 'text/plain'; end"
166
- f.instance_eval "def original_filename; 'foo.txt'; end"
167
- m = RestMan::Payload::Multipart.new({:foo => {:bar => f}})
168
- expect(m.to_s).to eq <<-EOS
169
- --#{m.boundary}\r
170
- Content-Disposition: form-data; name="foo[bar]"; filename="foo.txt"\r
171
- Content-Type: text/plain\r
172
- \r
173
- #{File.open(f.path, 'rb'){|bin| bin.read}}\r
174
- --#{m.boundary}--\r
175
- EOS
176
- end
177
-
178
- it 'should correctly format hex boundary' do
179
- allow(SecureRandom).to receive(:base64).with(12).and_return('TGs89+ttw/xna6TV')
180
- f = File.new(test_image_path)
181
- m = RestMan::Payload::Multipart.new({:foo => f})
182
- expect(m.boundary).to eq('-' * 4 + 'RubyFormBoundary' + 'TGs89AttwBxna6TV')
183
- end
184
-
185
- end
186
-
187
- context "streamed payloads" do
188
- it "should properly determine the size of file payloads" do
189
- f = File.new(test_image_path)
190
- payload = RestMan::Payload.generate(f)
191
- expect(payload.size).to eq 72_463
192
- expect(payload.length).to eq 72_463
193
- end
194
-
195
- it "should properly determine the size of other kinds of streaming payloads" do
196
- s = StringIO.new 'foo'
197
- payload = RestMan::Payload.generate(s)
198
- expect(payload.size).to eq 3
199
- expect(payload.length).to eq 3
200
-
201
- begin
202
- f = Tempfile.new "rest-man"
203
- f.write 'foo bar'
204
-
205
- payload = RestMan::Payload.generate(f)
206
- expect(payload.size).to eq 7
207
- expect(payload.length).to eq 7
208
- ensure
209
- f.close
210
- end
211
- end
212
-
213
- it "should have a closed? method" do
214
- f = File.new(test_image_path)
215
- payload = RestMan::Payload.generate(f)
216
- expect(payload.closed?).to be_falsey
217
- payload.close
218
- expect(payload.closed?).to be_truthy
219
- end
220
- end
221
-
222
14
  context "Payload generation" do
223
15
  it "should recognize standard urlencoded params" do
224
16
  expect(RestMan::Payload.generate({"foo" => 'bar'})).to be_kind_of(RestMan::Payload::UrlEncoded)
@@ -0,0 +1,40 @@
1
+ require_relative "../../../_lib"
2
+
3
+ RSpec.describe RestMan::Request::Init::Url::AddQueryFromHeaders do
4
+
5
+ it 'should handle basic URL params' do
6
+ expect(RestMan::Request::Init::Url::AddQueryFromHeaders.call('https://example.com/foo', params: {key1: 123, key2: 'abc'})).
7
+ to eq 'https://example.com/foo?key1=123&key2=abc'
8
+
9
+ expect(RestMan::Request::Init::Url::AddQueryFromHeaders.call('https://example.com/foo', params: {'key1' => 123})).
10
+ to eq 'https://example.com/foo?key1=123'
11
+
12
+ expect(RestMan::Request::Init::Url::AddQueryFromHeaders.call('https://example.com/path',
13
+ params: {foo: 'one two', bar: 'three + four == seven'})).
14
+ to eq 'https://example.com/path?foo=one+two&bar=three+%2B+four+%3D%3D+seven'
15
+ end
16
+
17
+ it 'should combine with & when URL params already exist' do
18
+ expect(RestMan::Request::Init::Url::AddQueryFromHeaders.call('https://example.com/path?foo=1', params: {bar: 2})).
19
+ to eq 'https://example.com/path?foo=1&bar=2'
20
+ end
21
+
22
+ it 'should handle complex nested URL params per Rack / Rails conventions' do
23
+ expect(RestMan::Request::Init::Url::AddQueryFromHeaders.call('https://example.com/', params: {
24
+ foo: [1,2,3],
25
+ null: nil,
26
+ falsy: false,
27
+ math: '2+2=4',
28
+ nested: {'key + escaped' => 'value + escaped', other: [], arr: [1,2]},
29
+ })).to eq 'https://example.com/?foo[]=1&foo[]=2&foo[]=3&null&falsy=false&math=2%2B2%3D4' \
30
+ '&nested[key+%2B+escaped]=value+%2B+escaped&nested[other]' \
31
+ '&nested[arr][]=1&nested[arr][]=2'
32
+ end
33
+
34
+ it 'should handle ParamsArray objects' do
35
+ expect(RestMan::Request::Init::Url::AddQueryFromHeaders.call('https://example.com/',
36
+ params: RestMan::ParamsArray.new([[:foo, 1], [:foo, 2]])
37
+ )).to eq 'https://example.com/?foo=1&foo=2'
38
+ end
39
+
40
+ end
@@ -0,0 +1,25 @@
1
+ require_relative "../../../_lib"
2
+
3
+ RSpec.describe RestMan::Request::Init::Url::NormalizeUrl do
4
+
5
+ it "adds http:// to the front of resources specified in the syntax example.com/resource" do
6
+ expect(RestMan::Request::Init::Url::NormalizeUrl.call('example.com/resource')).to eq 'http://example.com/resource'
7
+ end
8
+
9
+ it 'adds http:// to resources containing a colon' do
10
+ expect(RestMan::Request::Init::Url::NormalizeUrl.call('example.com:1234')).to eq 'http://example.com:1234'
11
+ end
12
+
13
+ it 'does not add http:// to the front of https resources' do
14
+ expect(RestMan::Request::Init::Url::NormalizeUrl.call('https://example.com/resource')).to eq 'https://example.com/resource'
15
+ end
16
+
17
+ it 'does not add http:// to the front of capital HTTP resources' do
18
+ expect(RestMan::Request::Init::Url::NormalizeUrl.call('HTTP://example.com/resource')).to eq 'HTTP://example.com/resource'
19
+ end
20
+
21
+ it 'does not add http:// to the front of capital HTTPS resources' do
22
+ expect(RestMan::Request::Init::Url::NormalizeUrl.call('HTTPS://example.com/resource')).to eq 'HTTPS://example.com/resource'
23
+ end
24
+
25
+ end
@@ -0,0 +1,83 @@
1
+ require_relative '../_lib'
2
+
3
+ RSpec.describe "RestMan::Request::Init" do
4
+
5
+ describe ".http_method" do
6
+ context 'when :method is nil' do
7
+ let(:args) { {} }
8
+
9
+ it "raise ArgumentError" do
10
+ expect {
11
+ RestMan::Request::Init.http_method(args)
12
+ }.to raise_error(ArgumentError, "must pass :method")
13
+ end
14
+ end
15
+
16
+ context 'when :method is not nil' do
17
+ let(:args) { {method: :POST} }
18
+
19
+ it "return post" do
20
+ expect(RestMan::Request::Init.http_method(args)).to eq('post')
21
+ end
22
+ end
23
+ end
24
+
25
+ describe ".headers" do
26
+ let(:args) { {} }
27
+
28
+ context "when :headers is nil" do
29
+ it "return {}" do
30
+ expect(RestMan::Request::Init.headers(args)).to eq({})
31
+ end
32
+ end
33
+
34
+ context "when :headers is not nil" do
35
+ let(:args) { { headers: { foo: 'bar' } } }
36
+
37
+ it "return args[:headers].dup" do
38
+ expect(RestMan::Request::Init.headers(args)).to eq({foo: 'bar'})
39
+ expect(RestMan::Request::Init.headers(args).object_id).not_to eq(args[:headers])
40
+ end
41
+ end
42
+ end
43
+
44
+ describe ".uri" do
45
+ it 'should parse to an URI' do
46
+ uri = RestMan::Request::Init.uri('http://example.com')
47
+ expect(uri).to be_a(URI)
48
+ end
49
+
50
+ it 'should reject valid URIs with no hostname' do
51
+ expect {
52
+ RestMan::Request::Init.uri('http:///')
53
+ }.to raise_error(URI::InvalidURIError, "bad URI(no host provided): http:///")
54
+ end
55
+
56
+ it 'should reject invalid URIs' do
57
+ expect {
58
+ RestMan::Request::Init.uri('http://::')
59
+ }.to raise_error(URI::InvalidURIError)
60
+ end
61
+ end
62
+
63
+ describe ".auth" do
64
+ it "doesn't overwrite user and password (which may have already been set by the Resource constructor) if there is no user/password in the url" do
65
+ user, password = RestMan::Request::Init.auth(URI.parse('http://example.com/resource'), {user: 'beth', password: 'pass2'})
66
+ expect(user).to eq 'beth'
67
+ expect(password).to eq 'pass2'
68
+ end
69
+
70
+ it 'uses the username and password from the URL' do
71
+ user, password = RestMan::Request::Init.auth(URI.parse('http://person:secret@example.com/resource'), {})
72
+ expect(user).to eq 'person'
73
+ expect(password).to eq 'secret'
74
+ end
75
+
76
+ it 'overrides URL user/pass with explicit options' do
77
+ user, password = RestMan::Request::Init.auth(URI.parse('http://person:secret@example.com/resource'), {user: 'beth', password: 'pass2'})
78
+ expect(user).to eq 'beth'
79
+ expect(password).to eq 'pass2'
80
+ end
81
+ end
82
+
83
+ end