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
data/rest-man.gemspec CHANGED
@@ -23,19 +23,13 @@ Gem::Specification.new do |s|
23
23
  s.add_development_dependency('pry', '~> 0')
24
24
  s.add_development_dependency('pry-doc', '~> 0')
25
25
  s.add_development_dependency('rdoc', '>= 2.4.2', '< 7.0')
26
- s.add_development_dependency('rubocop', '~> 0.49')
26
+ s.add_development_dependency('rubocop', '~> 1.50.2')
27
27
 
28
28
  s.add_dependency('http-accept', '>= 1.7.0', '< 3.0')
29
29
  s.add_dependency('http-cookie', '>= 1.0.2', '< 2.0')
30
- s.add_dependency('mime-types', '>= 1.16', '< 4.0')
30
+ s.add_dependency('mime-types', '>= 3.0', '< 4.0')
31
31
  s.add_dependency('netrc', '~> 0.8')
32
+ s.add_dependency('active_method', '~> 1.3')
32
33
 
33
- s.required_ruby_version = '>= 2.0.0'
34
-
35
- case ENV['BUILD_PLATFORM'] || RUBY_PLATFORM
36
- when /(mingw32|mswin32)/
37
- # ffi is needed for RestMan::Windows::RootCerts
38
- s.add_dependency('ffi', '~> 1.9')
39
- s.platform = platform
40
- end
34
+ s.required_ruby_version = '>= 2.6.0'
41
35
  end
@@ -1 +1 @@
1
- spec/integration/capath_digicert/digicert.crt
1
+ digicert.crt
@@ -62,7 +62,7 @@ describe RestMan::Request do
62
62
 
63
63
 
64
64
  # verify_callback is not works well with VCR
65
- # it "executes the verify_callback", focus: true do
65
+ # it "executes the verify_callback" do
66
66
  # ran_callback = false
67
67
  # request = RestMan::Request.new(
68
68
  # :method => :get,
@@ -129,6 +129,18 @@ describe RestMan::Request do
129
129
  expect { request.execute }.to(
130
130
  raise_error(RestMan::Exceptions::ReadTimeout))
131
131
  end
132
+
133
+ it "raises WriteTimeout when it hits a write timeout via :write_timeout" do
134
+ allow_any_instance_of(Net::HTTP).to receive(:request).and_raise(Net::WriteTimeout.new)
135
+
136
+ request = RestMan::Request.new(
137
+ :method => :post,
138
+ :url => 'https://www.mozilla.org',
139
+ :write_timeout => 1e-10,
140
+ )
141
+ expect { request.execute }.to(
142
+ raise_error(RestMan::Exceptions::WriteTimeout))
143
+ end
132
144
  end
133
145
 
134
146
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,14 @@
1
+ require 'simplecov'
2
+
3
+ if ENV['UPLOAD_COVERAGE_TO_CODACY']
4
+ require 'simplecov-cobertura'
5
+ SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter
6
+ end
7
+
8
+ SimpleCov.start do
9
+ add_filter "/spec"
10
+ end
11
+
1
12
  require 'webmock/rspec'
2
13
  require 'rest-man'
3
14
 
@@ -26,6 +26,20 @@ describe RestMan::AbstractResponse, :include_helpers do
26
26
  expect(@response.code).to eq 200
27
27
  end
28
28
 
29
+ [200, 255, 299].each do |code|
30
+ it "#success? - return true when response code is #{code}" do
31
+ expect(@net_http_res).to receive(:code).and_return(code.to_s)
32
+ expect(@response.success?).to eq true
33
+ end
34
+ end
35
+
36
+ [100, 199, 300, 399, 400, 499, 500, 599].each do |code|
37
+ it "#success? - return false when response code is #{code}" do
38
+ expect(@net_http_res).to receive(:code).and_return(code.to_s)
39
+ expect(@response.success?).to eq false
40
+ end
41
+ end
42
+
29
43
  it "has a nice description" do
30
44
  expect(@net_http_res).to receive(:to_hash).and_return({'Content-Type' => ['application/pdf']})
31
45
  expect(@net_http_res).to receive(:code).and_return('200')
@@ -0,0 +1,64 @@
1
+ require_relative '_lib'
2
+
3
+ describe RestMan::Exception do
4
+
5
+ it "contains exceptions in RestMan" do
6
+ expect(RestMan::Unauthorized.new).to be_a_kind_of(RestMan::Exception)
7
+ expect(RestMan::ServerBrokeConnection.new).to be_a_kind_of(RestMan::Exception)
8
+ end
9
+
10
+ describe '#message' do
11
+ it "returns a 'message' equal to the class name if the message is not set, because 'message' should not be nil" do
12
+ e = RestMan::Exception.new
13
+ expect(e.message).to eq "RestMan::Exception"
14
+ end
15
+
16
+ it "returns the 'message' that was set" do
17
+ e = RestMan::Exception.new
18
+ message = "An explicitly set message"
19
+ e.message = message
20
+ expect(e.message).to eq message
21
+ end
22
+
23
+ it "sets the exception message to ErrorMessage" do
24
+ expect(RestMan::ResourceNotFound.new.message).to eq 'Not Found'
25
+ end
26
+ end
27
+
28
+ describe "#http_code" do
29
+ it 'return the initial_response_code' do
30
+ e = RestMan::Exception.new(nil, 111)
31
+ expect(e.http_code).to eq(111)
32
+ end
33
+
34
+ it 'return from response.code' do
35
+ e = RestMan::Exception.new(double("response", code: '111'))
36
+ expect(e.http_code).to eq(111)
37
+ end
38
+ end
39
+
40
+ describe "#http_headers" do
41
+ it 'return from response.headers' do
42
+ e = RestMan::Exception.new(double("response", headers: 'headers'))
43
+ expect(e.http_headers).to eq("headers")
44
+ end
45
+
46
+ it 'return nil when response is nil' do
47
+ e = RestMan::Exception.new
48
+ expect(e.http_headers).to be_nil
49
+ end
50
+ end
51
+
52
+ describe "#http_body" do
53
+ it 'return from response.body' do
54
+ e = RestMan::Exception.new(double("response", body: 'body'))
55
+ expect(e.http_body).to eq("body")
56
+ end
57
+
58
+ it 'return nil when response is nil' do
59
+ e = RestMan::Exception.new
60
+ expect(e.http_body).to be_nil
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,29 @@
1
+ require_relative '../_lib'
2
+
3
+ describe "backwards compatibility" do
4
+ it 'aliases RestMan::NotFound as ResourceNotFound' do
5
+ expect(RestMan::ResourceNotFound).to eq RestMan::NotFound
6
+ end
7
+
8
+ it 'aliases old names for HTTP 413, 414, 416' do
9
+ expect(RestMan::RequestEntityTooLarge).to eq RestMan::PayloadTooLarge
10
+ expect(RestMan::RequestURITooLong).to eq RestMan::URITooLong
11
+ expect(RestMan::RequestedRangeNotSatisfiable).to eq RestMan::RangeNotSatisfiable
12
+ end
13
+
14
+ it 'subclasses NotFound from RequestFailed, ExceptionWithResponse' do
15
+ expect(RestMan::NotFound).to be < RestMan::RequestFailed
16
+ expect(RestMan::NotFound).to be < RestMan::ExceptionWithResponse
17
+ end
18
+
19
+ it 'subclasses timeout from RestMan::RequestTimeout, RequestFailed, EWR' do
20
+ expect(RestMan::Exceptions::OpenTimeout).to be < RestMan::Exceptions::Timeout
21
+ expect(RestMan::Exceptions::ReadTimeout).to be < RestMan::Exceptions::Timeout
22
+
23
+ expect(RestMan::Exceptions::Timeout).to be < RestMan::RequestTimeout
24
+ expect(RestMan::Exceptions::Timeout).to be < RestMan::RequestFailed
25
+ expect(RestMan::Exceptions::Timeout).to be < RestMan::ExceptionWithResponse
26
+ end
27
+
28
+ end
29
+
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestMan::Exceptions::EXCEPTIONS_MAP do
4
+
5
+ it 'can success map for 1xx code' do
6
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[100]).to eq(RestMan::Continue)
7
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[101]).to eq(RestMan::SwitchingProtocols)
8
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[102]).to eq(RestMan::Processing)
9
+ end
10
+
11
+ it 'can success map for 2xx code' do
12
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[200]).to eq(RestMan::OK)
13
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[201]).to eq(RestMan::Created)
14
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[202]).to eq(RestMan::Accepted)
15
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[203]).to eq(RestMan::NonAuthoritativeInformation)
16
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[204]).to eq(RestMan::NoContent)
17
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[205]).to eq(RestMan::ResetContent)
18
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[206]).to eq(RestMan::PartialContent)
19
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[207]).to eq(RestMan::MultiStatus)
20
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[208]).to eq(RestMan::AlreadyReported)
21
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[226]).to eq(RestMan::IMUsed)
22
+ end
23
+
24
+ it 'can success map for 3xx code' do
25
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[300]).to eq(RestMan::MultipleChoices)
26
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[301]).to eq(RestMan::MovedPermanently)
27
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[302]).to eq(RestMan::Found)
28
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[303]).to eq(RestMan::SeeOther)
29
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[304]).to eq(RestMan::NotModified)
30
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[305]).to eq(RestMan::UseProxy)
31
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[306]).to eq(RestMan::SwitchProxy)
32
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[307]).to eq(RestMan::TemporaryRedirect)
33
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[308]).to eq(RestMan::PermanentRedirect)
34
+ end
35
+
36
+ it 'can success map for 4xx code' do
37
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[400]).to eq(RestMan::BadRequest)
38
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[401]).to eq(RestMan::Unauthorized)
39
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[402]).to eq(RestMan::PaymentRequired)
40
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[403]).to eq(RestMan::Forbidden)
41
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[404]).to eq(RestMan::NotFound)
42
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[405]).to eq(RestMan::MethodNotAllowed)
43
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[406]).to eq(RestMan::NotAcceptable)
44
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[407]).to eq(RestMan::ProxyAuthenticationRequired)
45
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[408]).to eq(RestMan::RequestTimeout)
46
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[409]).to eq(RestMan::Conflict)
47
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[410]).to eq(RestMan::Gone)
48
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[411]).to eq(RestMan::LengthRequired)
49
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[412]).to eq(RestMan::PreconditionFailed)
50
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[413]).to eq(RestMan::PayloadTooLarge)
51
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[414]).to eq(RestMan::URITooLong)
52
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[415]).to eq(RestMan::UnsupportedMediaType)
53
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[416]).to eq(RestMan::RangeNotSatisfiable)
54
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[417]).to eq(RestMan::ExpectationFailed)
55
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[418]).to eq(RestMan::ImATeapot)
56
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[421]).to eq(RestMan::TooManyConnectionsFromThisIP)
57
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[422]).to eq(RestMan::UnprocessableEntity)
58
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[423]).to eq(RestMan::Locked)
59
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[424]).to eq(RestMan::FailedDependency)
60
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[425]).to eq(RestMan::UnorderedCollection)
61
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[426]).to eq(RestMan::UpgradeRequired)
62
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[428]).to eq(RestMan::PreconditionRequired)
63
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[429]).to eq(RestMan::TooManyRequests)
64
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[431]).to eq(RestMan::RequestHeaderFieldsTooLarge)
65
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[449]).to eq(RestMan::RetryWith)
66
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[450]).to eq(RestMan::BlockedByWindowsParentalControls)
67
+
68
+ expect(RestMan::ResourceNotFound).to eq(RestMan::NotFound)
69
+ expect(RestMan::RequestEntityTooLarge).to eq(RestMan::PayloadTooLarge)
70
+ expect(RestMan::RequestURITooLong).to eq(RestMan::URITooLong)
71
+ expect(RestMan::RequestedRangeNotSatisfiable).to eq(RestMan::RangeNotSatisfiable)
72
+ end
73
+
74
+ it 'can success map for 5xx code' do
75
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[500]).to eq(RestMan::InternalServerError)
76
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[501]).to eq(RestMan::NotImplemented)
77
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[502]).to eq(RestMan::BadGateway)
78
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[503]).to eq(RestMan::ServiceUnavailable)
79
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[504]).to eq(RestMan::GatewayTimeout)
80
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[505]).to eq(RestMan::HTTPVersionNotSupported)
81
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[506]).to eq(RestMan::VariantAlsoNegotiates)
82
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[507]).to eq(RestMan::InsufficientStorage)
83
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[508]).to eq(RestMan::LoopDetected)
84
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[509]).to eq(RestMan::BandwidthLimitExceeded)
85
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[510]).to eq(RestMan::NotExtended)
86
+ expect(RestMan::Exceptions::EXCEPTIONS_MAP[511]).to eq(RestMan::NetworkAuthenticationRequired)
87
+ end
88
+
89
+ end
@@ -0,0 +1,51 @@
1
+ require_relative '../_lib'
2
+
3
+ describe RestMan::RequestFailed do
4
+ before do
5
+ @response = double('HTTP Response', :code => '502')
6
+ end
7
+
8
+ it "stores the http response on the exception" do
9
+ response = "response"
10
+ begin
11
+ raise RestMan::RequestFailed, response
12
+ rescue RestMan::RequestFailed => e
13
+ expect(e.response).to eq response
14
+ end
15
+ end
16
+
17
+ it "http_code convenience method for fetching the code as an integer" do
18
+ expect(RestMan::RequestFailed.new(@response).http_code).to eq 502
19
+ end
20
+
21
+ it "http_body convenience method for fetching the body (decoding when necessary)" do
22
+ expect(RestMan::RequestFailed.new(@response).http_code).to eq 502
23
+ expect(RestMan::RequestFailed.new(@response).message).to eq 'HTTP status code 502'
24
+ end
25
+
26
+ it "shows the status code in the message" do
27
+ expect(RestMan::RequestFailed.new(@response).to_s).to match(/502/)
28
+ end
29
+ end
30
+
31
+ describe RestMan::ResourceNotFound do
32
+ it "also has the http response attached" do
33
+ response = "response"
34
+ begin
35
+ raise RestMan::ResourceNotFound, response
36
+ rescue RestMan::ResourceNotFound => e
37
+ expect(e.response).to eq response
38
+ end
39
+ end
40
+
41
+ it 'stores the body on the response of the exception' do
42
+ body = "body"
43
+ stub_request(:get, "www.example.com").to_return(:body => body, :status => 404)
44
+ begin
45
+ RestMan.get "www.example.com"
46
+ raise
47
+ rescue RestMan::ResourceNotFound => e
48
+ expect(e.response.body).to eq body
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,8 @@
1
+ require_relative '../_lib'
2
+
3
+ describe RestMan::ServerBrokeConnection do
4
+ it "should have a default message of 'Server broke connection'" do
5
+ e = RestMan::ServerBrokeConnection.new
6
+ expect(e.message).to eq 'Server broke connection'
7
+ end
8
+ end
@@ -0,0 +1,59 @@
1
+ require_relative '../_lib'
2
+
3
+ describe RestMan::Exceptions::Timeout do
4
+
5
+ describe 'OpenTimeout' do
6
+ it '#message' do
7
+ e = RestMan::Exceptions::OpenTimeout.new("error message...")
8
+ expect(e.message).to eq("error message...")
9
+ end
10
+
11
+ it "#message - should have a default message of 'Timed out connection to server'" do
12
+ e = RestMan::Exceptions::OpenTimeout.new
13
+ expect(e.message).to eq("Timed out connecting to server")
14
+ end
15
+
16
+ it "#original_exception" do
17
+ original_exception = StandardError.new('someting wrong')
18
+ e = RestMan::Exceptions::OpenTimeout.new('error message...', original_exception)
19
+ expect(e.original_exception).to eq(original_exception)
20
+ end
21
+ end
22
+
23
+ describe 'ReadTimeout' do
24
+ it '#message' do
25
+ e = RestMan::Exceptions::ReadTimeout.new("error message...")
26
+ expect(e.message).to eq("error message...")
27
+ end
28
+
29
+ it "#message - should have a default message of 'Timed out connection to server'" do
30
+ e = RestMan::Exceptions::ReadTimeout.new
31
+ expect(e.message).to eq("Timed out reading data from server")
32
+ end
33
+
34
+ it "#original_exception" do
35
+ original_exception = StandardError.new('someting wrong')
36
+ e = RestMan::Exceptions::ReadTimeout.new('error message...', original_exception)
37
+ expect(e.original_exception).to eq(original_exception)
38
+ end
39
+ end
40
+
41
+ describe 'WriteTimeout' do
42
+ it '#message' do
43
+ e = RestMan::Exceptions::WriteTimeout.new("error message...")
44
+ expect(e.message).to eq("error message...")
45
+ end
46
+
47
+ it "#message - should have a default message of 'Timed out connection to server'" do
48
+ e = RestMan::Exceptions::WriteTimeout.new
49
+ expect(e.message).to eq("Timed out writing data to server")
50
+ end
51
+
52
+ it "#original_exception" do
53
+ original_exception = StandardError.new('someting wrong')
54
+ e = RestMan::Exceptions::WriteTimeout.new('error message...', original_exception)
55
+ expect(e.original_exception).to eq(original_exception)
56
+ end
57
+ end
58
+
59
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestMan::ParamsArray::ProcessPair do
4
+
5
+ let(:process_pair) { RestMan::ParamsArray::ProcessPair.call(pair) }
6
+
7
+ context 'when pair is a hash' do
8
+ context 'which containts only one key-value pair' do
9
+ let(:pair) { {a: 1} }
10
+
11
+ it "return the array [key, value]" do
12
+ expect(process_pair).to eq([:a, 1])
13
+ end
14
+ end
15
+
16
+ context 'which containts multiple key-value pairs' do
17
+ let(:pair) { { a: 1, b: 2 } }
18
+
19
+ it "raise ArgumentError" do
20
+ expect{process_pair}.to raise_error(ArgumentError, "Bad # of fields for pair: {:a=>1, :b=>2}")
21
+ end
22
+ end
23
+ end
24
+
25
+ context 'when pair is an array' do
26
+ context 'which containts only one item like [key]' do
27
+ let(:pair) { [:a] }
28
+
29
+ it "return the array [key, nil]" do
30
+ expect(process_pair).to eq([:a, nil])
31
+ end
32
+ end
33
+
34
+ context 'which containts two items like [key, value]' do
35
+ let(:pair) { [:a, 1] }
36
+
37
+ it "return the array [key, value]" do
38
+ expect(process_pair).to eq([:a, 1])
39
+ end
40
+ end
41
+
42
+ context 'which containts more than two items like [a, b, c]' do
43
+ let(:pair) { [:a, 1, :b] }
44
+
45
+ it "raise ArgumentError" do
46
+ expect{process_pair}.to raise_error(ArgumentError, "Bad # of fields for pair: [:a, 1, :b]")
47
+ end
48
+ end
49
+ end
50
+
51
+ context 'when pair is an array like object' do
52
+ let(:pair) { double('ArrayLikeObject', to_a: [:a, 1]) }
53
+
54
+ it 'convert it to array' do
55
+ expect(process_pair).to eq([:a, 1])
56
+ end
57
+ end
58
+
59
+ end
@@ -2,19 +2,19 @@ require_relative '_lib'
2
2
 
3
3
  describe RestMan::ParamsArray do
4
4
 
5
- describe '.new' do
5
+ describe '.new + .to_a' do
6
6
  it 'accepts various types of containers' do
7
- as_array = [[:foo, 123], [:foo, 456], [:bar, 789], [:empty, nil]]
8
- [
9
- [[:foo, 123], [:foo, 456], [:bar, 789], [:empty, nil]],
10
- [{foo: 123}, {foo: 456}, {bar: 789}, {empty: nil}],
11
- [{foo: 123}, {foo: 456}, {bar: 789}, {empty: nil}],
12
- [{foo: 123}, [:foo, 456], {bar: 789}, {empty: nil}],
13
- [{foo: 123}, [:foo, 456], {bar: 789}, [:empty]],
14
- ].each do |input|
15
- expect(RestMan::ParamsArray.new(input).to_a).to eq as_array
7
+ parse = lambda do |input, to:|
8
+ expect(RestMan::ParamsArray.new(input).to_a).to eq to
16
9
  end
17
10
 
11
+ parse.([[:foo, 123], [:foo, 456], [:bar, 789], [:empty, nil]], to: [[:foo, 123], [:foo, 456], [:bar, 789], [:empty, nil]])
12
+ parse.([{foo: 123}, {foo: 456}, {bar: 789}, {empty: nil}], to: [[:foo, 123], [:foo, 456], [:bar, 789], [:empty, nil]])
13
+ parse.([{foo: 123}, {foo: 456}, {bar: 789}, {empty: nil}], to: [[:foo, 123], [:foo, 456], [:bar, 789], [:empty, nil]])
14
+ parse.([{foo: 123}, [:foo, 456], {bar: 789}, {empty: nil}], to: [[:foo, 123], [:foo, 456], [:bar, 789], [:empty, nil]])
15
+ parse.([{foo: 123}, [:foo, 456], {bar: 789}, [:empty]], to: [[:foo, 123], [:foo, 456], [:bar, 789], [:empty, nil]])
16
+ parse.([], to: [])
17
+
18
18
  expect(RestMan::ParamsArray.new([]).to_a).to eq []
19
19
  expect(RestMan::ParamsArray.new([]).empty?).to eq true
20
20
  end
@@ -33,4 +33,9 @@ describe RestMan::ParamsArray do
33
33
  }.to raise_error(NoMethodError)
34
34
  end
35
35
  end
36
+
37
+ it '#emtpy?' do
38
+ expect(RestMan::ParamsArray.new([]).empty?).to eq true
39
+ expect(RestMan::ParamsArray.new({foo: 1}).empty?).to eq false
40
+ end
36
41
  end
@@ -0,0 +1,116 @@
1
+ # encoding: binary
2
+
3
+ require_relative '../_lib'
4
+
5
+ describe RestMan::Payload::Multipart, :include_helpers do
6
+
7
+ it "should use standard enctype as default content-type" do
8
+ m = RestMan::Payload::Multipart.new({})
9
+ allow(m).to receive(:boundary).and_return(123)
10
+ expect(m.headers['Content-Type']).to eq 'multipart/form-data; boundary=123'
11
+ end
12
+
13
+ it 'should not error on close if stream already closed' do
14
+ m = RestMan::Payload::Multipart.new(:file => File.new(test_image_path))
15
+ 3.times {m.close}
16
+ end
17
+
18
+ it "should form properly separated multipart data" do
19
+ m = RestMan::Payload::Multipart.new([[:bar, "baz"], [:foo, "bar"]])
20
+ expect(m.to_s).to eq <<~EOS
21
+ --#{m.boundary}\r
22
+ Content-Disposition: form-data; name="bar"\r
23
+ \r
24
+ baz\r
25
+ --#{m.boundary}\r
26
+ Content-Disposition: form-data; name="foo"\r
27
+ \r
28
+ bar\r
29
+ --#{m.boundary}--\r
30
+ EOS
31
+ end
32
+
33
+ it "should not escape parameters names" do
34
+ m = RestMan::Payload::Multipart.new([["bar ", "baz"]])
35
+ expect(m.to_s).to eq <<~EOS
36
+ --#{m.boundary}\r
37
+ Content-Disposition: form-data; name="bar "\r
38
+ \r
39
+ baz\r
40
+ --#{m.boundary}--\r
41
+ EOS
42
+ end
43
+
44
+ it "should form properly separated multipart data" do
45
+ f = File.new(test_image_path)
46
+ m = RestMan::Payload::Multipart.new({:foo => f})
47
+ expect(m.to_s).to eq <<~EOS
48
+ --#{m.boundary}\r
49
+ Content-Disposition: form-data; name="foo"; filename="ISS.jpg"\r
50
+ Content-Type: image/jpeg\r
51
+ \r
52
+ #{File.open(f.path, 'rb'){|bin| bin.read}}\r
53
+ --#{m.boundary}--\r
54
+ EOS
55
+ end
56
+
57
+ it "should ignore the name attribute when it's not set" do
58
+ f = File.new(test_image_path)
59
+ m = RestMan::Payload::Multipart.new({nil => f})
60
+ expect(m.to_s).to eq <<~EOS
61
+ --#{m.boundary}\r
62
+ Content-Disposition: form-data; filename="ISS.jpg"\r
63
+ Content-Type: image/jpeg\r
64
+ \r
65
+ #{File.open(f.path, 'rb'){|bin| bin.read}}\r
66
+ --#{m.boundary}--\r
67
+ EOS
68
+ end
69
+
70
+ it "should detect optional (original) content type and filename" do
71
+ f = File.new(test_image_path)
72
+ expect(MIME::Types).to receive(:type_for).with(f.path).and_return("")
73
+ expect(f).to receive(:original_filename).and_return('foo.txt')
74
+ m = RestMan::Payload::Multipart.new({:foo => f})
75
+ expect(m.to_s).to eq <<~EOS
76
+ --#{m.boundary}\r
77
+ Content-Disposition: form-data; name="foo"; filename="foo.txt"\r
78
+ Content-Type: text/plain\r
79
+ \r
80
+ #{File.open(f.path, 'rb'){|bin| bin.read}}\r
81
+ --#{m.boundary}--\r
82
+ EOS
83
+ end
84
+
85
+ it "should handle hash in hash parameters" do
86
+ m = RestMan::Payload::Multipart.new({:bar => {:baz => "foo"}})
87
+ expect(m.to_s).to eq <<~EOS
88
+ --#{m.boundary}\r
89
+ Content-Disposition: form-data; name="bar[baz]"\r
90
+ \r
91
+ foo\r
92
+ --#{m.boundary}--\r
93
+ EOS
94
+
95
+ f = File.new(test_image_path)
96
+ f.instance_eval "def content_type; 'text/plain'; end"
97
+ f.instance_eval "def original_filename; 'foo.txt'; end"
98
+ m = RestMan::Payload::Multipart.new({:foo => {:bar => f}})
99
+ expect(m.to_s).to eq <<~EOS
100
+ --#{m.boundary}\r
101
+ Content-Disposition: form-data; name="foo[bar]"; filename="foo.txt"\r
102
+ Content-Type: text/plain\r
103
+ \r
104
+ #{File.open(f.path, 'rb'){|bin| bin.read}}\r
105
+ --#{m.boundary}--\r
106
+ EOS
107
+ end
108
+
109
+ it 'should correctly format hex boundary' do
110
+ allow(SecureRandom).to receive(:base64).with(12).and_return('TGs89+ttw/xna6TV')
111
+ f = File.new(test_image_path)
112
+ m = RestMan::Payload::Multipart.new({:foo => f})
113
+ expect(m.boundary).to eq('-' * 4 + 'RubyFormBoundary' + 'TGs89AttwBxna6TV')
114
+ end
115
+
116
+ end
@@ -0,0 +1,48 @@
1
+ # encoding: binary
2
+
3
+ require_relative '../_lib'
4
+
5
+ describe RestMan::Payload::Streamed, :include_helpers do
6
+
7
+ it "should properly determine the size of file payloads" do
8
+ f = File.new(test_image_path)
9
+ payload = RestMan::Payload.generate(f)
10
+ expect(payload.size).to eq 72_463
11
+ expect(payload.length).to eq 72_463
12
+ end
13
+
14
+ it "should properly determine the size of other kinds of streaming payloads" do
15
+ s = StringIO.new 'foo'
16
+ payload = RestMan::Payload.generate(s)
17
+ expect(payload.size).to eq 3
18
+ expect(payload.length).to eq 3
19
+
20
+ begin
21
+ f = Tempfile.new "rest-man"
22
+ f.write 'foo bar'
23
+
24
+ payload = RestMan::Payload.generate(f)
25
+ expect(payload.size).to eq 7
26
+ expect(payload.length).to eq 7
27
+ ensure
28
+ f.close
29
+ end
30
+ end
31
+
32
+ it "should preperly determin the size of an IO like paylod" do
33
+ io = IO.new(0)
34
+ allow(io).to receive(:stat).and_return(double("IO::Stat", size: 10))
35
+ payload = RestMan::Payload.generate(io)
36
+ expect(payload.size).to eq 10
37
+ expect(payload.length).to eq 10
38
+ end
39
+
40
+ it "should have a closed? method" do
41
+ f = File.new(test_image_path)
42
+ payload = RestMan::Payload.generate(f)
43
+ expect(payload.closed?).to be_falsey
44
+ payload.close
45
+ expect(payload.closed?).to be_truthy
46
+ end
47
+
48
+ end