Skip to content

Commit 5992586

Browse files
committed
Merge pull request rest-client#499 from rest-client/ab-rspec-3
Upgrade to RSpec 3.0
2 parents 718c5ff + 51cdf04 commit 5992586

23 files changed

+733
-713
lines changed

.rubocop-disables.yml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ Lint/Eval:
2121
Exclude:
2222
- rest-client.windows.gemspec
2323

24+
Lint/HandleExceptions:
25+
Exclude:
26+
- lib/restclient/utils.rb
27+
28+
Lint/UselessAccessModifier:
29+
Exclude:
30+
- lib/restclient/windows/root_certs.rb
31+
2432
# Offense count: 4
2533
# Cop supports --auto-correct.
2634
Style/Alias:
@@ -35,7 +43,7 @@ Style/AndOr:
3543
# TODO
3644
# Offense count: 3
3745
# Cop supports --auto-correct.
38-
Style/Blocks:
46+
Style/BlockDelimiters:
3947
Enabled: false
4048

4149
# Offense count: 48
@@ -90,6 +98,9 @@ Style/CollectionMethods:
9098
Style/ColonMethodCall:
9199
Enabled: false
92100

101+
Style/ConditionalAssignment:
102+
EnforcedStyle: assign_inside_condition
103+
93104
# Offense count: 2
94105
Style/ConstantName:
95106
Enabled: false
@@ -161,12 +172,11 @@ Style/FileName:
161172
Style/FormatString:
162173
Enabled: false
163174

164-
# TODO: configure
165-
# Offense count: 514
175+
# TODO: enable
166176
# Cop supports --auto-correct.
167177
# Configuration parameters: SupportedStyles.
168178
Style/HashSyntax:
169-
EnforcedStyle: hash_rockets
179+
Enabled: false
170180

171181
# NOTABUG
172182
# Offense count: 8
@@ -337,10 +347,9 @@ Style/SpaceInsideParens:
337347
Style/StringLiterals:
338348
Enabled: false
339349

340-
# NOTABUG
341-
# Offense count: 14
342-
# Configuration parameters: EnforcedStyleForMultiline, SupportedStyles.
343-
Style/TrailingComma:
350+
Style/TrailingCommaInLiteral:
351+
EnforcedStyleForMultiline: comma
352+
Style/TrailingCommaInArguments:
344353
Enabled: false
345354

346355
# TODO: configure

lib/restclient.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ def self.options(url, headers={}, &block)
9494
# A global proxy URL to use for all requests. This can be overridden on a
9595
# per-request basis by passing `:proxy` to RestClient::Request.
9696
def self.proxy
97-
@proxy
97+
@proxy ||= nil
9898
end
99+
99100
def self.proxy=(value)
100101
@proxy = value
101102
@proxy_set = true

lib/restclient/abstract_response.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def cookies
6464
# @return [HTTP::CookieJar]
6565
#
6666
def cookie_jar
67-
return @cookie_jar if @cookie_jar
67+
return @cookie_jar if defined?(@cookie_jar) && @cookie_jar
6868

6969
jar = @request.cookie_jar.dup
7070
headers.fetch(:set_cookie, []).each do |cookie|
@@ -152,7 +152,7 @@ def follow_get_redirection(&block)
152152
#
153153
def self.beautify_headers(headers)
154154
headers.inject({}) do |out, (key, value)|
155-
key_sym = key.gsub(/-/, '_').downcase.to_sym
155+
key_sym = key.tr('-', '_').downcase.to_sym
156156

157157
# Handle Set-Cookie specially since it cannot be joined by comma.
158158
if key.downcase == 'set-cookie'

lib/restclient/payload.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def mime_for(path)
174174
end
175175

176176
def boundary
177-
return @boundary if @boundary
177+
return @boundary if defined?(@boundary) && @boundary
178178

179179
# Use the same algorithm used by WebKit: generate 16 random
180180
# alphanumeric characters, replacing `+` `/` with `A` `B` (included in

lib/restclient/request.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ def initialize args
124124
else
125125
raise ArgumentError, "must pass :url"
126126
end
127+
128+
@user = @password = nil
127129
parse_url_with_auth!(url)
128130

129131
# process cookie arguments found in headers or args
@@ -815,7 +817,7 @@ def fetch_body(http_response)
815817
# Kudos to _why!
816818
@tf = Tempfile.new('rest-client.')
817819
@tf.binmode
818-
size, total = 0, http_response.header['Content-Length'].to_i
820+
size, total = 0, http_response['Content-Length'].to_i
819821
http_response.read_body do |chunk|
820822
@tf.write chunk
821823
size += chunk.size

lib/restclient/response.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ def self.create(body, net_http_res, request)
4747
result
4848
end
4949

50-
private
51-
5250
def self.fix_encoding(response)
5351
charset = RestClient::Utils.get_encoding_from_headers(response.headers)
5452
encoding = nil
@@ -68,6 +66,8 @@ def self.fix_encoding(response)
6866
response
6967
end
7068

69+
private
70+
7171
def body_truncated(length)
7272
b = body
7373
if b.length > length

rest-client.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
1717
s.summary = 'Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions.'
1818

1919
s.add_development_dependency('webmock', '~> 2.0')
20-
s.add_development_dependency('rspec', '~> 2.99')
20+
s.add_development_dependency('rspec', '~> 3.0')
2121
s.add_development_dependency('pry', '~> 0')
2222
s.add_development_dependency('pry-doc', '~> 0')
2323
s.add_development_dependency('rdoc', '>= 2.4.2', '< 5.0')

spec/integration/httpbin_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,31 @@ def execute_httpbin_json(suffix, opts={})
4141
describe '.execute' do
4242
it 'sends a user agent' do
4343
data = execute_httpbin_json('user-agent', method: :get)
44-
data['user-agent'].should match(/rest-client/)
44+
expect(data['user-agent']).to match(/rest-client/)
4545
end
4646

4747
it 'receives cookies on 302' do
4848
expect {
4949
execute_httpbin('cookies/set?foo=bar', method: :get, max_redirects: 0)
5050
}.to raise_error(RestClient::Found) { |ex|
51-
ex.http_code.should eq 302
52-
ex.response.cookies['foo'].should eq 'bar'
51+
expect(ex.http_code).to eq 302
52+
expect(ex.response.cookies['foo']).to eq 'bar'
5353
}
5454
end
5555

5656
it 'passes along cookies through 302' do
5757
data = execute_httpbin_json('cookies/set?foo=bar', method: :get)
58-
data.should have_key('cookies')
59-
data['cookies']['foo'].should eq 'bar'
58+
expect(data).to have_key('cookies')
59+
expect(data['cookies']['foo']).to eq 'bar'
6060
end
6161

6262
it 'handles quote wrapped cookies' do
6363
expect {
6464
execute_httpbin('cookies/set?foo=' + CGI.escape('"bar:baz"'),
6565
method: :get, max_redirects: 0)
6666
}.to raise_error(RestClient::Found) { |ex|
67-
ex.http_code.should eq 302
68-
ex.response.cookies['foo'].should eq '"bar:baz"'
67+
expect(ex.http_code).to eq 302
68+
expect(ex.response.cookies['foo']).to eq '"bar:baz"'
6969
}
7070
end
7171
end

spec/integration/integration_spec.rb

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
body = 'abc'
99
stub_request(:get, "www.example.com").to_return(:body => body, :status => 200)
1010
response = RestClient.get "www.example.com"
11-
response.code.should eq 200
12-
response.body.should eq body
11+
expect(response.code).to eq 200
12+
expect(response.body).to eq body
1313
end
1414

1515
it "a simple request with gzipped content" do
1616
stub_request(:get, "www.example.com").with(:headers => { 'Accept-Encoding' => 'gzip, deflate' }).to_return(:body => "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000", :status => 200, :headers => { 'Content-Encoding' => 'gzip' } )
1717
response = RestClient.get "www.example.com"
18-
response.code.should eq 200
19-
response.body.should eq "i'm gziped\n"
18+
expect(response.code).to eq 200
19+
expect(response.body).to eq "i'm gziped\n"
2020
end
2121

2222
it "a 404" do
@@ -26,10 +26,10 @@
2626
RestClient.get "www.example.com"
2727
raise
2828
rescue RestClient::ResourceNotFound => e
29-
e.http_code.should eq 404
30-
e.response.code.should eq 404
31-
e.response.body.should eq body
32-
e.http_body.should eq body
29+
expect(e.http_code).to eq 404
30+
expect(e.response.code).to eq 404
31+
expect(e.response.body).to eq body
32+
expect(e.http_body).to eq body
3333
end
3434
end
3535

@@ -41,8 +41,8 @@
4141
'Content-Type' => 'text/plain; charset=UTF-8'
4242
})
4343
response = RestClient.get "www.example.com"
44-
response.encoding.should eq Encoding::UTF_8
45-
response.valid_encoding?.should eq true
44+
expect(response.encoding).to eq Encoding::UTF_8
45+
expect(response.valid_encoding?).to eq true
4646
end
4747

4848
it 'handles windows-1252' do
@@ -52,9 +52,9 @@
5252
'Content-Type' => 'text/plain; charset=windows-1252'
5353
})
5454
response = RestClient.get "www.example.com"
55-
response.encoding.should eq Encoding::WINDOWS_1252
56-
response.encode('utf-8').should eq "ÿ"
57-
response.valid_encoding?.should eq true
55+
expect(response.encoding).to eq Encoding::WINDOWS_1252
56+
expect(response.encode('utf-8')).to eq "ÿ"
57+
expect(response.valid_encoding?).to eq true
5858
end
5959

6060
it 'handles binary' do
@@ -64,28 +64,28 @@
6464
'Content-Type' => 'application/octet-stream; charset=binary'
6565
})
6666
response = RestClient.get "www.example.com"
67-
response.encoding.should eq Encoding::BINARY
68-
lambda {
67+
expect(response.encoding).to eq Encoding::BINARY
68+
expect {
6969
response.encode('utf-8')
70-
}.should raise_error(Encoding::UndefinedConversionError)
71-
response.valid_encoding?.should eq true
70+
}.to raise_error(Encoding::UndefinedConversionError)
71+
expect(response.valid_encoding?).to eq true
7272
end
7373

7474
it 'handles euc-jp' do
7575
body = "\xA4\xA2\xA4\xA4\xA4\xA6\xA4\xA8\xA4\xAA".
7676
force_encoding(Encoding::BINARY)
7777
body_utf8 = 'あいうえお'
78-
body_utf8.encoding.should eq Encoding::UTF_8
78+
expect(body_utf8.encoding).to eq Encoding::UTF_8
7979

8080
stub_request(:get, 'www.example.com').to_return(
8181
:body => body, :status => 200, :headers => {
8282
'Content-Type' => 'text/plain; charset=EUC-JP'
8383
})
8484
response = RestClient.get 'www.example.com'
85-
response.encoding.should eq Encoding::EUC_JP
86-
response.valid_encoding?.should eq true
87-
response.length.should eq 5
88-
response.encode('utf-8').should eq body_utf8
85+
expect(response.encoding).to eq Encoding::EUC_JP
86+
expect(response.valid_encoding?).to eq true
87+
expect(response.length).to eq 5
88+
expect(response.encode('utf-8')).to eq body_utf8
8989
end
9090

9191
it 'defaults to Encoding.default_external' do
@@ -95,7 +95,7 @@
9595
})
9696

9797
response = RestClient.get 'www.example.com'
98-
response.encoding.should eq Encoding.default_external
98+
expect(response.encoding).to eq Encoding.default_external
9999
end
100100

101101
it 'handles invalid encoding' do
@@ -105,7 +105,7 @@
105105
})
106106

107107
response = RestClient.get 'www.example.com'
108-
response.encoding.should eq Encoding.default_external
108+
expect(response.encoding).to eq Encoding.default_external
109109
end
110110

111111
it 'leaves images as binary' do
@@ -117,7 +117,7 @@
117117
})
118118

119119
response = RestClient.get 'www.example.com'
120-
response.encoding.should eq Encoding::BINARY
120+
expect(response.encoding).to eq Encoding::BINARY
121121
end
122122
end
123123
end

spec/integration/request_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
},
7676
)
7777
expect {request.execute }.to_not raise_error
78-
ran_callback.should eq(true)
78+
expect(ran_callback).to eq(true)
7979
end
8080

8181
it "fails verification when the callback returns false",

0 commit comments

Comments
 (0)