rack-client 0.1.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. data/History.txt +2 -2
  2. data/README.textile +2 -2
  3. data/Rakefile +11 -5
  4. data/demo/demo_spec.rb +3 -3
  5. data/lib/rack/client.rb +29 -25
  6. data/lib/rack/client/adapter.rb +6 -0
  7. data/lib/rack/client/adapter/base.rb +57 -0
  8. data/lib/rack/client/adapter/simple.rb +49 -0
  9. data/lib/rack/client/auth/abstract/challenge.rb +53 -0
  10. data/lib/rack/client/auth/basic.rb +57 -0
  11. data/lib/rack/client/auth/digest/challenge.rb +38 -0
  12. data/lib/rack/client/auth/digest/md5.rb +78 -0
  13. data/lib/rack/client/auth/digest/params.rb +10 -0
  14. data/lib/rack/client/body.rb +12 -0
  15. data/lib/rack/client/cache.rb +19 -0
  16. data/lib/rack/client/cache/cachecontrol.rb +195 -0
  17. data/lib/rack/client/cache/context.rb +95 -0
  18. data/lib/rack/client/cache/entitystore.rb +77 -0
  19. data/lib/rack/client/cache/key.rb +51 -0
  20. data/lib/rack/client/cache/metastore.rb +133 -0
  21. data/lib/rack/client/cache/options.rb +147 -0
  22. data/lib/rack/client/cache/request.rb +46 -0
  23. data/lib/rack/client/cache/response.rb +62 -0
  24. data/lib/rack/client/cache/storage.rb +43 -0
  25. data/lib/rack/client/cookie_jar.rb +17 -0
  26. data/lib/rack/client/cookie_jar/context.rb +59 -0
  27. data/lib/rack/client/cookie_jar/cookie.rb +59 -0
  28. data/lib/rack/client/cookie_jar/cookiestore.rb +36 -0
  29. data/lib/rack/client/cookie_jar/options.rb +43 -0
  30. data/lib/rack/client/cookie_jar/request.rb +15 -0
  31. data/lib/rack/client/cookie_jar/response.rb +16 -0
  32. data/lib/rack/client/cookie_jar/storage.rb +34 -0
  33. data/lib/rack/client/dual_band.rb +13 -0
  34. data/lib/rack/client/follow_redirects.rb +47 -20
  35. data/lib/rack/client/handler.rb +10 -0
  36. data/lib/rack/client/handler/em-http.rb +66 -0
  37. data/lib/rack/client/handler/excon.rb +50 -0
  38. data/lib/rack/client/handler/net_http.rb +85 -0
  39. data/lib/rack/client/handler/typhoeus.rb +62 -0
  40. data/lib/rack/client/headers.rb +49 -0
  41. data/lib/rack/client/parser.rb +18 -0
  42. data/lib/rack/client/parser/base.rb +25 -0
  43. data/lib/rack/client/parser/body_collection.rb +50 -0
  44. data/lib/rack/client/parser/context.rb +15 -0
  45. data/lib/rack/client/parser/json.rb +54 -0
  46. data/lib/rack/client/parser/middleware.rb +8 -0
  47. data/lib/rack/client/parser/request.rb +21 -0
  48. data/lib/rack/client/parser/response.rb +19 -0
  49. data/lib/rack/client/parser/yaml.rb +52 -0
  50. data/lib/rack/client/response.rb +9 -0
  51. data/lib/rack/client/version.rb +5 -0
  52. data/spec/apps/example.org.ru +47 -3
  53. data/spec/auth/basic_spec.rb +69 -0
  54. data/spec/auth/digest/md5_spec.rb +69 -0
  55. data/spec/cache_spec.rb +40 -0
  56. data/spec/cookie_jar_spec.rb +37 -0
  57. data/spec/endpoint_spec.rb +4 -13
  58. data/spec/follow_redirect_spec.rb +27 -0
  59. data/spec/handler/async_api_spec.rb +69 -0
  60. data/spec/handler/em_http_spec.rb +22 -0
  61. data/spec/handler/excon_spec.rb +7 -0
  62. data/spec/handler/net_http_spec.rb +8 -0
  63. data/spec/handler/sync_api_spec.rb +55 -0
  64. data/spec/handler/typhoeus_spec.rb +22 -0
  65. data/spec/middleware_helper.rb +37 -0
  66. data/spec/middleware_spec.rb +48 -5
  67. data/spec/parser/json_spec.rb +22 -0
  68. data/spec/parser/yaml_spec.rb +22 -0
  69. data/spec/server_helper.rb +72 -0
  70. data/spec/spec_helper.rb +17 -3
  71. metadata +86 -31
  72. data/lib/rack/client/auth.rb +0 -13
  73. data/lib/rack/client/http.rb +0 -77
  74. data/spec/auth_spec.rb +0 -22
  75. data/spec/core_spec.rb +0 -123
  76. data/spec/redirect_spec.rb +0 -12
@@ -1,13 +0,0 @@
1
- module Rack::Client::Auth
2
- class Basic
3
- def initialize(app, username, password)
4
- @app, @username, @password = app, username, password
5
- end
6
-
7
- def call(env)
8
- encoded_login = ["#{@username}:#{@password}"].pack("m*")
9
- env['HTTP_AUTHORIZATION'] = "Basic #{encoded_login}"
10
- @app.call(env)
11
- end
12
- end
13
- end
@@ -1,77 +0,0 @@
1
- require 'net/http'
2
-
3
- class Rack::Client::HTTP
4
- def self.call(env)
5
- new(env).run
6
- end
7
-
8
- def initialize(env)
9
- @env = env
10
- end
11
-
12
- def run
13
- case request.request_method
14
- when "HEAD"
15
- head = Net::HTTP::Head.new(request.path, request_headers)
16
- http.request(head) do |response|
17
- return parse(response)
18
- end
19
- when "GET"
20
- get = Net::HTTP::Get.new(request.path, request_headers)
21
- http.request(get) do |response|
22
- return parse(response)
23
- end
24
- when "POST"
25
- post = Net::HTTP::Post.new(request.path, request_headers)
26
- post.body = @env["rack.input"].read
27
- http.request(post) do |response|
28
- return parse(response)
29
- end
30
- when "PUT"
31
- put = Net::HTTP::Put.new(request.path, request_headers)
32
- put.body = @env["rack.input"].read
33
- http.request(put) do |response|
34
- return parse(response)
35
- end
36
- when "DELETE"
37
- delete = Net::HTTP::Delete.new(request.path, request_headers)
38
- http.request(delete) do |response|
39
- return parse(response)
40
- end
41
- else
42
- raise "Unsupported method: #{request.request_method.inspect}"
43
- end
44
- end
45
-
46
- def http
47
- Net::HTTP.new(request.host, request.port)
48
- end
49
-
50
- def parse(response)
51
- status = response.code.to_i
52
- headers = {}
53
- response.each do |key,value|
54
- key = key.gsub(/(\w+)/) do |matches|
55
- matches.sub(/^./) do |char|
56
- char.upcase
57
- end
58
- end
59
- headers[key] = value
60
- end
61
- [status, headers, response.body.to_s]
62
- end
63
-
64
- def request
65
- @request ||= Rack::Request.new(@env)
66
- end
67
-
68
- def request_headers
69
- headers = {}
70
- @env.each do |k,v|
71
- if k =~ /^HTTP_(.*)$/
72
- headers[$1] = v
73
- end
74
- end
75
- headers
76
- end
77
- end
data/spec/auth_spec.rb DELETED
@@ -1,22 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
-
3
- describe Rack::Client, "with an Auth::Basic middleware" do
4
- it "succeeds with authorization" do
5
- client = Rack::Client.new do
6
- use Rack::Client::Auth::Basic, "username", "password"
7
- end
8
- response = client.get("http://localhost:9292/auth/ping")
9
- response.status.should == 200
10
- response.headers["Content-Type"].should == "text/html"
11
- response.body.should == "pong"
12
- end
13
-
14
- it "fails with authorization" do
15
- client = Rack::Client.new do
16
- use Rack::Client::Auth::Basic, "username", "fail"
17
- end
18
- response = client.get("http://localhost:9292/auth/ping")
19
- response.status.should == 401
20
- response.body.should == ""
21
- end
22
- end
data/spec/core_spec.rb DELETED
@@ -1,123 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
-
3
- describe Rack::Client, "without middleware" do
4
- context "at the instance level" do
5
- it "returns an empty body" do
6
- response = Rack::Client.new.get("http://localhost:9292/empty")
7
- response.status.should == 200
8
- response.headers["Content-Type"].should == "text/html"
9
- response.headers["Content-Length"].should == "0"
10
- response.body.should == ""
11
- end
12
-
13
- it "returns a 302" do
14
- response = Rack::Client.new.get("http://localhost:9292/redirect")
15
- response.status.should == 302
16
- response["Location"].should == "/after-redirect"
17
- end
18
-
19
- it "heads data" do
20
- response = Rack::Client.new.head "http://localhost:9292/shelf"
21
- response.status.should == 200
22
- response["ETag"].should == "828ef3fdfa96f00ad9f27c383fc9ac7f"
23
- end
24
-
25
- it "puts data" do
26
- response = Rack::Client.new.put "http://localhost:9292/shelf/ctm", "some data"
27
- response.status.should == 200
28
- response["Location"].should == "/shelf/ctm"
29
- end
30
-
31
- it "deletes data" do
32
- response = Rack::Client.new.delete "http://localhost:9292/shelf/ctm"
33
- response.status.should == 204
34
- end
35
-
36
- it "posts data" do
37
- response = Rack::Client.new.post("http://localhost:9292/posted", "some data")
38
- response.status.should == 201
39
- response["Created"].should == "awesome"
40
- end
41
- end
42
-
43
- context "at the class level" do
44
- it "returns an empty body" do
45
- response = Rack::Client.get("http://localhost:9292/empty")
46
- response.status.should == 200
47
- response.headers["Content-Type"].should == "text/html"
48
- response.headers["Content-Length"].should == "0"
49
- response.body.should == ""
50
- end
51
-
52
- it "returns a 302" do
53
- response = Rack::Client.get("http://localhost:9292/redirect")
54
- response.status.should == 302
55
- response["Location"].should == "/after-redirect"
56
- end
57
-
58
- it "heads data" do
59
- response = Rack::Client.head "http://localhost:9292/shelf"
60
- response.status.should == 200
61
- response["ETag"].should == "828ef3fdfa96f00ad9f27c383fc9ac7f"
62
- end
63
-
64
- it "puts data" do
65
- response = Rack::Client.put "http://localhost:9292/shelf/ctm", "some data"
66
- response.status.should == 200
67
- response["Location"].should == "/shelf/ctm"
68
- end
69
-
70
- it "deletes data" do
71
- response = Rack::Client.delete "http://localhost:9292/shelf/ctm"
72
- response.status.should == 204
73
- end
74
-
75
- it "posts data" do
76
- response = Rack::Client.post("http://localhost:9292/posted", "some data")
77
- response.status.should == 201
78
- response["Created"].should == "awesome"
79
- end
80
- end
81
-
82
- context "at the rack-test level" do
83
- include Rack::Test::Methods
84
- def app() Rack::Client.new end
85
-
86
- it "returns an empty body" do
87
- get "http://localhost:9292/empty"
88
- last_response.status.should == 200
89
- last_response.headers["Content-Type"].should == "text/html"
90
- last_response.headers["Content-Length"].should == "0"
91
- last_response.body.should == ""
92
- end
93
-
94
- it "returns a 302" do
95
- get "http://localhost:9292/redirect"
96
- last_response.status.should == 302
97
- last_response["Location"].should == "/after-redirect"
98
- end
99
-
100
- it "heads data" do
101
- head "http://localhost:9292/shelf"
102
- last_response.status.should == 200
103
- last_response["ETag"].should == "828ef3fdfa96f00ad9f27c383fc9ac7f"
104
- end
105
-
106
- it "puts data" do
107
- put "http://localhost:9292/shelf/ctm", "some data"
108
- last_response.status.should == 200
109
- last_response["Location"].should == "/shelf/ctm"
110
- end
111
-
112
- it "deletes data" do
113
- delete "http://localhost:9292/shelf/ctm"
114
- last_response.status.should == 204
115
- end
116
-
117
- it "posts data" do
118
- post "http://localhost:9292/posted", "some data"
119
- last_response.status.should == 201
120
- last_response["Created"].should == "awesome"
121
- end
122
- end
123
- end
@@ -1,12 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
-
3
- describe Rack::Client, "with a FollowRedirects middleware" do
4
- it "follows redirects" do
5
- client = Rack::Client.new do
6
- use Rack::Client::FollowRedirects
7
- end
8
- response = client.get("http://localhost:9292/redirect")
9
- response.status.should == 200
10
- response.body.should == "after redirect"
11
- end
12
- end