weary 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe Weary::Adapter::Excon do
4
+ before do
5
+ @url = "http://github.com/api/v2/json/repos/show/mwunsch/weary"
6
+ @request = Weary::Request.new @url
7
+ end
8
+
9
+ describe "class methods" do
10
+ it_behaves_like "an Adapter" do
11
+ before do
12
+ stub_request(:get, @url).
13
+ to_return(:status => 200, :body => "", :headers => {})
14
+ end
15
+
16
+ subject { described_class }
17
+ let(:env) { @request.env }
18
+ end
19
+
20
+ describe ".call" do
21
+ it "performs the request through the connect method" do
22
+ described_class.stub(:connect) { Rack::Response.new("", 200, {})}
23
+ described_class.should_receive :connect
24
+ described_class.call(@request.env)
25
+ end
26
+ end
27
+
28
+ describe ".connect" do
29
+ before do
30
+ stub_request(:get, @url)
31
+ end
32
+
33
+ it "performs the http request" do
34
+ req = Rack::Request.new(@request.env)
35
+ described_class.connect(req)
36
+ a_request(:get, @url).should have_been_made
37
+ end
38
+
39
+ it "sets appropriate request headers" do
40
+ @request.headers 'User-Agent' => Weary::USER_AGENTS['Lynx 2.8.4rel.1 on Linux']
41
+ req = Rack::Request.new(@request.env)
42
+ described_class.connect(req)
43
+ a_request(:get, @url).with(:headers => @request.headers).should have_been_made
44
+ end
45
+
46
+ it "sets the body of the request" do
47
+ stub_request(:post, @url)
48
+
49
+ @request.method = "POST"
50
+ @request.params :foo => "baz"
51
+ req = Rack::Request.new(@request.env)
52
+ described_class.connect(req)
53
+ a_request(:post, @url).with(:body => "foo=baz").should have_been_made
54
+ end
55
+ end
56
+ end
57
+
58
+ describe "#connect" do
59
+ it "calls the class method `.connect`" do
60
+ described_class.stub(:connect) { [200, {'Content-Type' => 'text/plain'}, [""]] }
61
+ described_class.should_receive(:connect)
62
+ described_class.new.connect(Rack::Request.new(@request.env))
63
+ end
64
+ end
65
+
66
+ describe "#call" do
67
+ it "uses the overriden `#connect` method" do
68
+ instance = described_class.new
69
+ instance.stub(:connect) { Rack::Response.new [""], 501, {"Content-Type" => "text/plain"} }
70
+ instance.should_receive(:connect)
71
+ instance.call(@request.env)
72
+ end
73
+ end
74
+
75
+ it_behaves_like "an Adapter" do
76
+ before do
77
+ stub_request(:get, @url).
78
+ to_return(:status => 200, :body => "", :headers => {})
79
+ end
80
+
81
+ subject { described_class }
82
+ let(:env) { @request.env }
83
+ end
84
+
85
+
86
+ end
@@ -6,83 +6,92 @@ describe Weary::Adapter::NetHttp do
6
6
  @request = Weary::Request.new @url
7
7
  end
8
8
 
9
- describe ".call" do
10
- before do
11
- stub_request(:get, @url).
12
- to_return(:status => 200, :body => "", :headers => {})
13
- end
9
+ describe "class methods" do
10
+ it_behaves_like "an Adapter" do
11
+ before do
12
+ stub_request(:get, @url).
13
+ to_return(:status => 200, :body => "", :headers => {})
14
+ end
14
15
 
15
- it_behaves_like "a Rack application" do
16
16
  subject { described_class }
17
17
  let(:env) { @request.env }
18
18
  end
19
19
 
20
- it "performs the request through the connect method" do
21
- described_class.stub(:connect) { Rack::Response.new("", 200, {})}
22
- described_class.should_receive :connect
23
- described_class.call(@request.env)
20
+ describe ".call" do
21
+ it "performs the request through the connect method" do
22
+ described_class.stub(:connect) { Rack::Response.new("", 200, {})}
23
+ described_class.should_receive :connect
24
+ described_class.call(@request.env)
25
+ end
24
26
  end
25
- end
26
27
 
27
- describe "#call" do
28
- it "calls the class method `.call`" do
29
- described_class.stub(:call) { [200, {'Content-Type' => 'text/plain'}, [""]] }
30
- described_class.should_receive(:call)
31
- described_class.new.call(@request.env)
32
- end
33
- end
28
+ describe ".connect" do
29
+ before do
30
+ stub_request(:get, @url)
31
+ end
34
32
 
35
- describe ".connect" do
36
- before do
37
- stub_request(:get, @url)
38
- end
33
+ it "performs the http request" do
34
+ req = Rack::Request.new(@request.env)
35
+ described_class.connect(req)
36
+ a_request(:get, @url).should have_been_made
37
+ end
39
38
 
40
- it "performs the http request" do
41
- req = Rack::Request.new(@request.env)
42
- described_class.connect(req)
43
- a_request(:get, @url).should have_been_made
44
- end
39
+ it "sets appropriate request headers" do
40
+ @request.headers 'User-Agent' => Weary::USER_AGENTS['Lynx 2.8.4rel.1 on Linux']
41
+ req = Rack::Request.new(@request.env)
42
+ described_class.connect(req)
43
+ a_request(:get, @url).with(:headers => @request.headers).should have_been_made
44
+ end
45
45
 
46
- it "returns a Rack::Response" do
47
- req = Rack::Request.new(@request.env)
48
- described_class.connect(req).should be_kind_of Rack::Response
46
+ it "sets the body of the request" do
47
+ stub_request(:post, @url)
48
+ @request.method = "POST"
49
+ @request.params :foo => "baz"
50
+ req = Rack::Request.new(@request.env)
51
+ described_class.connect(req)
52
+ a_request(:post, @url).with(:body => "foo=baz").should have_been_made
53
+ end
49
54
  end
50
55
 
51
- it "sets appropriate request headers" do
52
- @request.headers 'User-Agent' => Weary::USER_AGENTS['Lynx 2.8.4rel.1 on Linux']
53
- req = Rack::Request.new(@request.env)
54
- described_class.connect(req)
55
- a_request(:get, @url).with(:headers => @request.headers).should have_been_made
56
+ describe ".socket" do
57
+ it "sets up the http connection" do
58
+ req = Rack::Request.new(@request.env)
59
+ described_class.socket(req).should be_kind_of Net::HTTP
60
+ end
56
61
  end
57
62
 
58
- it "sets the body of the request" do
59
- stub_request(:post, @url)
60
- @request.method = "POST"
61
- @request.params :foo => "baz"
62
- req = Rack::Request.new(@request.env)
63
- described_class.connect(req)
64
- a_request(:post, @url).with(:body => "foo=baz").should have_been_made
63
+ describe ".request_class" do
64
+ it "gets the Net::HTTP request class for the request method" do
65
+ described_class.request_class("POST").should be Net::HTTP::Post
66
+ end
65
67
  end
66
68
  end
67
69
 
68
- describe ".normalize_request_headers" do
69
- it "removes the HTTP_ prefix from request headers" do
70
- @request.headers 'User-Agent' => Weary::USER_AGENTS['Lynx 2.8.4rel.1 on Linux']
71
- headers = described_class.normalize_request_headers(@request.env)
72
- headers.should have_key "User-Agent"
70
+ describe "#connect" do
71
+ it "calls the class method `.connect`" do
72
+ described_class.stub(:connect) { [200, {'Content-Type' => 'text/plain'}, [""]] }
73
+ described_class.should_receive(:connect)
74
+ described_class.new.connect(Rack::Request.new(@request.env))
73
75
  end
74
76
  end
75
77
 
76
- describe ".socket" do
77
- it "sets up the http connection" do
78
- req = Rack::Request.new(@request.env)
79
- described_class.socket(req).should be_kind_of Net::HTTP
78
+ describe "#call" do
79
+ it "uses the overriden `#connect` method" do
80
+ instance = described_class.new
81
+ instance.stub(:connect) { Rack::Response.new [""], 501, {"Content-Type" => "text/plain"} }
82
+ instance.should_receive(:connect)
83
+ instance.call(@request.env)
80
84
  end
81
85
  end
82
86
 
83
- describe ".request_class" do
84
- it "gets the Net::HTTP request class for the request method" do
85
- described_class.request_class("POST").should be Net::HTTP::Post
87
+ it_behaves_like "an Adapter" do
88
+ before do
89
+ stub_request(:get, @url).
90
+ to_return(:status => 200, :body => "", :headers => {})
86
91
  end
92
+
93
+ subject { described_class.new }
94
+ let(:env) { @request.env }
87
95
  end
96
+
88
97
  end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ begin
4
+ describe Weary::Adapter::Typhoeus do
5
+ before do
6
+ @url = "http://github.com/api/v2/json/repos/show/mwunsch/weary"
7
+ @request = Weary::Request.new @url
8
+ end
9
+
10
+ describe "class methods" do
11
+ it_behaves_like "an Adapter" do
12
+ before do
13
+ stub_request(:get, @url).
14
+ to_return(:status => 200, :body => "", :headers => {})
15
+ end
16
+
17
+ subject { described_class }
18
+ let(:env) { @request.env }
19
+ end
20
+
21
+ describe ".call" do
22
+ it "performs the request through the connect method" do
23
+ described_class.stub(:connect) { Rack::Response.new("", 200, {})}
24
+ described_class.should_receive :connect
25
+ described_class.call(@request.env)
26
+ end
27
+ end
28
+ end
29
+
30
+ describe "#connect" do
31
+ it "calls the class method `.connect`" do
32
+ described_class.stub(:connect) { [200, {'Content-Type' => 'text/plain'}, [""]] }
33
+ described_class.should_receive(:connect)
34
+ described_class.new.connect(Rack::Request.new(@request.env))
35
+ end
36
+ end
37
+
38
+ describe "#call" do
39
+ it "uses the overriden `#connect` method" do
40
+ instance = described_class.new
41
+ instance.stub(:connect) { Rack::Response.new [""], 501, {"Content-Type" => "text/plain"} }
42
+ instance.should_receive(:connect)
43
+ instance.call(@request.env)
44
+ end
45
+ end
46
+
47
+ it_behaves_like "an Adapter" do
48
+ before do
49
+ stub_request(:get, @url).
50
+ to_return(:status => 200, :body => "", :headers => {})
51
+ end
52
+
53
+ subject { described_class }
54
+ let(:env) { @request.env }
55
+ end
56
+
57
+ end
58
+
59
+ rescue LoadError => e
60
+ warn <<-MSG
61
+ [warn] Received a LoadError when attempting to load the Typhoeus adapter,
62
+ and skipping the specs.
63
+
64
+ #{e.message}
65
+
66
+ Make sure Typhoeus is in the $LOAD_PATH.
67
+ MSG
68
+ end
@@ -2,6 +2,10 @@ require 'spec_helper'
2
2
  require 'rack/lobster'
3
3
 
4
4
  describe Weary::Client do
5
+ it_behaves_like "a Requestable" do
6
+ subject { Class.new(Weary::Client) }
7
+ end
8
+
5
9
  describe "::resource" do
6
10
  before do
7
11
  @url = "http://github.com/api/v2/json/repos/show/mwunsch/weary"
@@ -20,7 +20,7 @@ describe Weary::Deferred do
20
20
 
21
21
  it "with a factory method" do
22
22
  deferred = described_class.new @request.perform, @struct, lambda {|model, response| response.status }
23
- deferred.should eq 501
23
+ deferred.should eql 501
24
24
  end
25
25
  end
26
26
 
@@ -10,12 +10,12 @@ describe Weary::Middleware::BasicAuth do
10
10
  end
11
11
 
12
12
  it_behaves_like "a Rack application" do
13
- subject { described_class.new(@request, ["mwunsch", "secret"]) }
13
+ subject { described_class.new(@request, "mwunsch", "secret") }
14
14
  let(:env) { @request.env }
15
15
  end
16
16
 
17
17
  it "prepares the Authorization header for the request" do
18
- middleware = described_class.new(@request, ["mwunsch", "secret"])
18
+ middleware = described_class.new(@request, "mwunsch", "secret")
19
19
  middleware.call(@request.env)
20
20
  a_request(:get, @url).should have_been_made
21
21
  end
@@ -0,0 +1,24 @@
1
+ require 'weary/middleware/timeout'
2
+ require 'spec_helper'
3
+
4
+ describe Weary::Middleware::Timeout do
5
+ describe "#call" do
6
+ before do
7
+ @url = "http://github.com/api/v2/json/repos/show/mwunsch/weary"
8
+ @request = Weary::Request.new @url
9
+ stub_request(:get, @request.uri.to_s).to_timeout
10
+ end
11
+
12
+ it_behaves_like "a Rack application" do
13
+ subject { described_class.new(@request) }
14
+ let(:env) { @request.env }
15
+ end
16
+
17
+ it "returns a 504 (Gateway Timeout) when no response is received in time" do
18
+ middleware = described_class.new(@request)
19
+ status, header, body = middleware.call(@request.env)
20
+ status.should eql 504
21
+ end
22
+
23
+ end
24
+ end
@@ -1,6 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Weary::Request do
4
+ it_behaves_like "a Requestable" do
5
+ subject { described_class.new "http://github.com/api/v2/json/repos/show/mwunsch/weary" }
6
+ end
7
+
4
8
  describe "#uri" do
5
9
  subject { described_class.new "http://github.com/api/v2/json/repos/show/mwunsch/weary" }
6
10
 
@@ -97,16 +101,6 @@ describe Weary::Request do
97
101
  subject { described_class.new "http://github.com/api/v2/json/repos/show/mwunsch/weary" }
98
102
  let(:hash) { {'User-Agent' => Weary::USER_AGENTS['Lynx 2.8.4rel.1 on Linux']} }
99
103
 
100
- it "sets headers for the request" do
101
- subject.headers(hash)
102
- subject.instance_variable_get(:@headers).should eql hash
103
- end
104
-
105
- it "gets previously set headers" do
106
- subject.headers(hash)
107
- subject.headers.should eql hash
108
- end
109
-
110
104
  it "updates the env with the Rack-friendly key" do
111
105
  subject.headers(hash)
112
106
  subject.env.should have_key('HTTP_USER_AGENT')
@@ -118,21 +112,6 @@ describe Weary::Request do
118
112
  end
119
113
  end
120
114
 
121
- describe "#user_agent" do
122
- subject { described_class.new "http://github.com/api/v2/json/repos/show/mwunsch/weary" }
123
- let(:agent) { 'RSpec' }
124
-
125
- it "updates the #headers hash with a User-Agent" do
126
- subject.user_agent agent
127
- subject.headers.should have_key 'User-Agent'
128
- end
129
-
130
- it "sets the user agent for the headers" do
131
- subject.user_agent agent
132
- subject.headers['User-Agent'].should be agent
133
- end
134
- end
135
-
136
115
  describe "#params" do
137
116
  it "sets the query string for a GET request" do
138
117
  req = described_class.new "http://api.twitter.com/version/users/show.json"
@@ -142,8 +121,7 @@ describe Weary::Request do
142
121
 
143
122
  it "sets the rack input for a POST request" do
144
123
  req = described_class.new "https://api.github.com/gists", "POST"
145
- req.params :public => true,
146
- :files => { "file1.txt" => { :content => "String file contents"}}
124
+ req.params :public => true
147
125
  req.env['rack.input'].read.should eql req.params
148
126
  end
149
127
 
@@ -196,20 +174,6 @@ describe Weary::Request do
196
174
  end
197
175
  end
198
176
 
199
- describe "#adapter" do
200
- subject { described_class.new "http://github.com/api/v2/json/repos/show/mwunsch/weary" }
201
-
202
- it "sets a new adapter to set the connection" do
203
- klass = Class.new { include Weary::Adapter }
204
- subject.adapter(klass)
205
- subject.adapter.should be klass
206
- end
207
-
208
- it "defaults to the Net::HTTP adapter" do
209
- subject.adapter.should be Weary::Adapter::NetHttp
210
- end
211
- end
212
-
213
177
  describe "#perform" do
214
178
  subject do
215
179
  url = "http://github.com/api/v2/json/repos/show/mwunsch/weary"
@@ -233,7 +197,7 @@ describe Weary::Request do
233
197
  end
234
198
 
235
199
  describe "#use" do
236
- it "adds a middleware to the stack" do
200
+ it "runs middleware through the calling stack" do
237
201
  req = described_class.new "http://github.com/api/v2/json/repos/show/mwunsch/weary"
238
202
  req.adapter(Class.new { include Weary::Adapter })
239
203
  # Rack::Runtime sets an "X-Runtime" response header