rack-api 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rack-api (0.1.1)
4
+ rack-api (0.1.2)
5
5
  activesupport (~> 3.0.6)
6
6
  rack (~> 1.2.1)
7
7
  rack-mount (~> 0.6.14)
@@ -80,7 +80,7 @@ Then you can go to your spec file, say, <tt>spec/api_spec.rb</tt>. You need to d
80
80
 
81
81
  it "renders status page" do
82
82
  get "/api/v1/status"
83
- JSON.load(last_response.body).should == {:status => "running"}
83
+ last_response.body.should == {:status => "running"}.to_json
84
84
  last_response.status.should == 200
85
85
  end
86
86
  end
@@ -2,6 +2,7 @@ module Rack
2
2
  class API
3
3
  module Middleware
4
4
  autoload :Format, "rack/api/middleware/format"
5
+ autoload :SSL, "rack/api/middleware/ssl"
5
6
  end
6
7
  end
7
8
  end
@@ -0,0 +1,21 @@
1
+ module Rack
2
+ class API
3
+ module Middleware
4
+ class SSL
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ request = Rack::Request.new(env)
11
+
12
+ if env["rack.url_scheme"] == "https"
13
+ @app.call(env)
14
+ else
15
+ [400, {"Content-Type" => "text/plain"}, ["Only HTTPS requests are supported by now."]]
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -211,7 +211,7 @@ module Rack
211
211
  builder.use Rack::API::Middleware::Format, option(:formats)
212
212
 
213
213
  # Add middlewares to executation stack.
214
- option(:middlewares, :merged).each {|middleware| builder.use(*middleware)}
214
+ option(:middlewares, :merge).each {|middleware| builder.use(*middleware)}
215
215
 
216
216
  # Apply helpers to app.
217
217
  helpers = option(:helpers)
@@ -3,7 +3,7 @@ module Rack
3
3
  module Version
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- PATCH = 1
6
+ PATCH = 2
7
7
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
8
8
  end
9
9
  end
@@ -43,7 +43,7 @@ describe Rack::API, "Basic Authentication" do
43
43
  get "/v1/", {}, "HTTP_AUTHORIZATION" => basic_auth("admin", "test")
44
44
 
45
45
  last_response.status.should == 200
46
- JSON.load(last_response.body).should == {"success" => true}
46
+ last_response.body.should == {"success" => true}.to_json
47
47
  end
48
48
  end
49
49
 
@@ -52,7 +52,7 @@ describe Rack::API, "Basic Authentication" do
52
52
  get "/v2/"
53
53
 
54
54
  last_response.status.should == 200
55
- JSON.load(last_response.body).should == {"success" => true}
55
+ last_response.body.should == {"success" => true}.to_json
56
56
  end
57
57
  end
58
58
 
@@ -69,17 +69,17 @@ describe Rack::API, "Basic Authentication" do
69
69
  get "/v3/", {}, "HTTP_AUTHORIZATION" => basic_auth("john", "test")
70
70
 
71
71
  last_response.status.should == 200
72
- JSON.load(last_response.body).should == {"success" => true}
72
+ last_response.body.should == {"success" => true}.to_json
73
73
  end
74
74
  end
75
75
 
76
76
  it "returns credentials" do
77
77
  get "/v2/credentials", {}, "HTTP_AUTHORIZATION" => basic_auth("admin", "test")
78
- JSON.load(last_response.body).should == ["admin", "test"]
78
+ last_response.body.should == ["admin", "test"].to_json
79
79
  end
80
80
 
81
81
  it "returns empty array when no credentials are provided" do
82
82
  get "/v2/credentials"
83
- JSON.load(last_response.body).should == []
83
+ last_response.body.should == [].to_json
84
84
  end
85
85
  end
@@ -62,14 +62,14 @@ describe Rack::API, "Format" do
62
62
  get "/v1", :format => "json"
63
63
 
64
64
  last_response.status.should == 200
65
- JSON.load(last_response.body).should == {"success" => true}
65
+ last_response.body.should == {"success" => true}.to_json
66
66
  end
67
67
 
68
68
  it "renders when set through extension" do
69
69
  get "/v1/users.json"
70
70
 
71
71
  last_response.status.should == 200
72
- JSON.load(last_response.body).should == {"users" => []}
72
+ last_response.body.should == {"users" => []}.to_json
73
73
  end
74
74
 
75
75
  it "sends header" do
@@ -23,11 +23,11 @@ describe Rack::API, "Helpers" do
23
23
 
24
24
  it "adds module helper" do
25
25
  get "/v1"
26
- JSON.load(last_response.body).should include("module")
26
+ json(last_response.body).should include("module")
27
27
  end
28
28
 
29
29
  it "adds block helper" do
30
30
  get "/v1"
31
- JSON.load(last_response.body).should include("block")
31
+ json(last_response.body).should include("block")
32
32
  end
33
33
  end
@@ -17,7 +17,7 @@ describe Rack::API, "HTTP Methods" do
17
17
  it "renders #{method}" do
18
18
  send method, "/v1/#{method}"
19
19
  last_response.status.should == 200
20
- JSON.load(last_response.body).should == {method => true}
20
+ last_response.body.should == {method => true}.to_json
21
21
  end
22
22
  end
23
23
 
@@ -7,6 +7,6 @@ describe Rack::API do
7
7
 
8
8
  it "renders action from MyApp" do
9
9
  get "/v1"
10
- JSON.load(last_response.body).should == {"myapp" => true}
10
+ last_response.body.should == {"myapp" => true}.to_json
11
11
  end
12
12
  end
@@ -3,6 +3,8 @@ require "spec_helper"
3
3
  describe Rack::API, "Middlewares" do
4
4
  before do
5
5
  Rack::API.app do
6
+ use ZOMGMiddleware
7
+
6
8
  version :v1 do
7
9
  use AwesomeMiddleware
8
10
  get("/") {}
@@ -13,5 +15,6 @@ describe Rack::API, "Middlewares" do
13
15
  it "sends custom headers" do
14
16
  get "/v1"
15
17
  last_response.headers["X-Awesome"].should == "U R Awesome"
18
+ last_response.headers["X-ZOMG"].should == "ZOMG!"
16
19
  end
17
20
  end
@@ -12,16 +12,16 @@ describe Rack::API, "Params" do
12
12
 
13
13
  it "detects optional names from routing params" do
14
14
  get "/v1/users/1.json"
15
- JSON.load(last_response.body).should == {"id" => "1", "format" => "json"}
15
+ json(last_response.body).should == {"id" => "1", "format" => "json"}
16
16
  end
17
17
 
18
18
  it "detects query string params" do
19
19
  get "/v1/users/1?include=articles"
20
- JSON.load(last_response.body).should == {"id" => "1", "include" => "articles"}
20
+ json(last_response.body).should == {"id" => "1", "include" => "articles"}
21
21
  end
22
22
 
23
23
  it "detects post params" do
24
24
  post "/v1/users", :name => "John Doe"
25
- JSON.load(last_response.body).should == {"name" => "John Doe"}
25
+ last_response.body.should == {"name" => "John Doe"}.to_json
26
26
  end
27
27
  end
@@ -0,0 +1,21 @@
1
+ require "spec_helper"
2
+
3
+ describe Rack::API::Middleware::SSL do
4
+ let(:action) { proc {|env| [200, {}, ["success"]] } }
5
+
6
+ it "denies http requests" do
7
+ env = Rack::MockRequest.env_for("/v1", "rack.url_scheme" => "http")
8
+ status, headers, response = Rack::API::Middleware::SSL.new(action).call(env)
9
+
10
+ status.should == 400
11
+ headers["Content-Type"].should == "text/plain"
12
+ response.should include("Only HTTPS requests are supported by now.")
13
+ end
14
+
15
+ it "accepts https requests" do
16
+ env = Rack::MockRequest.env_for("/v1", "rack.url_scheme" => "https")
17
+ status, headers, response = Rack::API::Middleware::SSL.new(action).call(env)
18
+
19
+ status.should == 200
20
+ end
21
+ end
@@ -6,4 +6,8 @@ module Helpers
6
6
  def basic_auth(username, password)
7
7
  "Basic " + Base64.encode64("#{username}:#{password}")
8
8
  end
9
+
10
+ def json(string)
11
+ JSON.load(string)
12
+ end
9
13
  end
@@ -0,0 +1,10 @@
1
+ class ZOMGMiddleware
2
+ def initialize(app)
3
+ @app = app
4
+ end
5
+
6
+ def call(env)
7
+ status, headers, response = @app.call(env)
8
+ [status, headers.merge("X-ZOMG" => "ZOMG!"), response]
9
+ end
10
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rack-api
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Nando Vieira
@@ -110,6 +110,7 @@ files:
110
110
  - lib/rack/api/formatter/jsonp.rb
111
111
  - lib/rack/api/middleware.rb
112
112
  - lib/rack/api/middleware/format.rb
113
+ - lib/rack/api/middleware/ssl.rb
113
114
  - lib/rack/api/response.rb
114
115
  - lib/rack/api/runner.rb
115
116
  - lib/rack/api/version.rb
@@ -126,11 +127,13 @@ files:
126
127
  - spec/rack-api/runner_spec.rb
127
128
  - spec/rack-api/settings_spec.rb
128
129
  - spec/rack-api/short_circuit_spec.rb
130
+ - spec/rack-api/ssl_spec.rb
129
131
  - spec/spec_helper.rb
130
132
  - spec/support/awesome_middleware.rb
131
133
  - spec/support/core_ext.rb
132
134
  - spec/support/helpers.rb
133
135
  - spec/support/myapp.rb
136
+ - spec/support/zomg_middleware.rb
134
137
  homepage: http://rubygems.org/gems/rack-api
135
138
  licenses: []
136
139
 
@@ -171,8 +174,10 @@ test_files:
171
174
  - spec/rack-api/runner_spec.rb
172
175
  - spec/rack-api/settings_spec.rb
173
176
  - spec/rack-api/short_circuit_spec.rb
177
+ - spec/rack-api/ssl_spec.rb
174
178
  - spec/spec_helper.rb
175
179
  - spec/support/awesome_middleware.rb
176
180
  - spec/support/core_ext.rb
177
181
  - spec/support/helpers.rb
178
182
  - spec/support/myapp.rb
183
+ - spec/support/zomg_middleware.rb