helmet 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a879ae7efe16ee0b0697501f4075fea37abe07b1
4
- data.tar.gz: 448ee6bb790963c0cd5b5b2faabc7ad1fa585b19
3
+ metadata.gz: 3c0fa561a63d2be52841a64cc90103db7cdbdd7e
4
+ data.tar.gz: 31c33f5bbe5b145b04e3955c0cf128e7f880ea19
5
5
  SHA512:
6
- metadata.gz: 15ec47ac0967d6492915529c865507451bb62e1236ec10fdf7c7c5a89af1d0e351bf0e75bfe190a4ebda7409de922a19a7b9961a3c2b536adb6dec979774c9b4
7
- data.tar.gz: 495a7c1a4006bc21d140f226744983b68d09ea7c021839521c16a20b17714870a50047b275840d93eaaf99427a9172571ff96b142a6abd1828261988f1538d14
6
+ metadata.gz: 887635bc3aefd6badbfe428df978f7984382bdb1cea93337f8cac9d218970b5ae9e4bda353e3506e09ec271f3578dc3899d301077c9b18ccfea61c542b065875
7
+ data.tar.gz: de5a117cbe50bd4f21563500ea2311d9256d03f84e350b481faee2ec727d87f9cbde6e5687f83f3324309ca58113a02ebe808d5e8d6bd480448bca11c84d0276
data/HISTORY.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # HISTORY
2
2
 
3
- ## v0.0.2
3
+ ## v0.2.1
4
+ - content-type support
5
+ - Fix template bugs
6
+ - Include HttpRouter params into env params
7
+ - Rename config method to settings
8
+ - Include config into handler
9
+
10
+ ## v0.2.0
4
11
 
5
12
  - Replace router by http_router gem.
6
13
  - Include LICENSE file
data/LICENSE CHANGED
@@ -16,3 +16,52 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
16
  THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
17
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
18
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
+
20
+ Portions of this code are from the Goliath project (https://github.com/postrank-labs/goliath) and under the following license:
21
+ Copyright (c) 2011-2012 PostRank Inc.
22
+
23
+ Permission is hereby granted, free of charge, to any person
24
+ obtaining a copy of this software and associated documentation
25
+ files (the "Software"), to deal in the Software without
26
+ restriction, including without limitation the rights to use,
27
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
28
+ copies of the Software, and to permit persons to whom the
29
+ Software is furnished to do so, subject to the following
30
+ conditions:
31
+
32
+ The above copyright notice and this permission notice shall be
33
+ included in all copies or substantial portions of the Software.
34
+
35
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
36
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
37
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
38
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
39
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
40
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
41
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
42
+ OTHER DEALINGS IN THE SOFTWARE.
43
+
44
+ Portions of this code are from the Sinatra project (https://github.com/bmizerany/sinatra) and under the following license:
45
+ Copyright (c) 2007, 2008, 2009, 2010, 2011 Blake Mizerany
46
+
47
+ Permission is hereby granted, free of charge, to any person
48
+ obtaining a copy of this software and associated documentation
49
+ files (the "Software"), to deal in the Software without
50
+ restriction, including without limitation the rights to use,
51
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
52
+ copies of the Software, and to permit persons to whom the
53
+ Software is furnished to do so, subject to the following
54
+ conditions:
55
+
56
+ The above copyright notice and this permission notice shall be
57
+ included in all copies or substantial portions of the Software.
58
+
59
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
60
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
61
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
62
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
63
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
64
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
65
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
66
+ OTHER DEALINGS IN THE SOFTWARE.
67
+
data/Rakefile CHANGED
@@ -1,10 +1,27 @@
1
1
  require 'rake'
2
2
  require 'rake/testtask'
3
3
 
4
- task :default => [:test]
4
+ require './lib/helmet/version'
5
5
 
6
6
  desc 'run tests'
7
7
  Rake::TestTask.new(:test) do |test|
8
8
  test.pattern = 'test/**/*_test.rb'
9
9
  test.verbose = true
10
10
  end
11
+
12
+ namespace :gem do
13
+ desc 'build gem file'
14
+ task :build do
15
+ system 'gem build helmet.gemspec'
16
+ end
17
+
18
+ desc 'install gem file'
19
+ task :install do
20
+ system "gem install --local helmet-#{Helmet::VERSION}.gem"
21
+ end
22
+ end
23
+
24
+ desc 'build and install gem file'
25
+ task :gem => ['gem:build', 'gem:install']
26
+
27
+ task :default => [:test]
@@ -1,7 +1,6 @@
1
1
  require 'goliath'
2
2
 
3
3
  require 'helmet/api'
4
- require 'helmet/version'
5
4
 
6
5
  module Helmet
7
6
  end
@@ -4,20 +4,12 @@ require 'http_router'
4
4
 
5
5
  require 'helmet/handler'
6
6
 
7
- require 'pry'
8
-
9
7
  module Helmet
10
8
  class API < Goliath::API
11
9
 
12
10
  class << self
13
-
14
- def set(key, value)
15
- @config[key.to_sym] = value
16
- end
17
11
 
18
- def config(key)
19
- @config[key.to_sym]
20
- end
12
+ attr_accessor :settings
21
13
 
22
14
  def before(route, opts = {}, &block)
23
15
  @before_filters.add(route, opts, &block)
@@ -78,8 +70,9 @@ module Helmet
78
70
 
79
71
  # compute public/ views folder
80
72
  base = File.expand_path(File.dirname(caller.first[/^[^:]*/]))
81
- klass.set :public_folder, File.join(base, 'public')
82
- klass.set :views_folder, File.join(base, 'views')
73
+
74
+ klass.settings[:public_folder] = File.join(base, 'public')
75
+ klass.settings[:views_folder] = File.join(base, 'views')
83
76
 
84
77
  super # update Goliath::Application.app_class
85
78
  end
@@ -91,7 +84,7 @@ module Helmet
91
84
  # Handle before filters
92
85
  @before_filters = HttpRouter.new
93
86
 
94
- @config = {}
87
+ @settings = {}
95
88
 
96
89
  @helpers = Module.new
97
90
  end
@@ -113,15 +106,26 @@ module Helmet
113
106
 
114
107
  catch(:halt) do
115
108
  # evaluate any route match
116
- filters = self.class.before_filters.recognize(env).first || []
109
+ filters = self.class.before_filters.recognize(env).first
110
+
117
111
  filters.each do |f|
112
+ # include route params into env
113
+ route_params = f.params
114
+ env.params.merge! route_params
115
+
118
116
  handler.handle! &f.route.dest
119
- end
117
+
118
+ # remove after usage
119
+ env.params.delete_if {|k,v| route_params[k] == v}
120
+ end if filters
120
121
 
121
122
  routes_recognized = self.class.routes.recognize(env).first
122
123
  if routes_recognized
123
124
  # Use the first matched route
124
- handler.handle! &routes_recognized.first.route.dest
125
+ http_router_response = routes_recognized.first
126
+ # Bind params to the env
127
+ env.params.merge! http_router_response.params
128
+ handler.handle! &http_router_response.route.dest
125
129
  else
126
130
  handler.handle! do
127
131
  status 404
@@ -1,6 +1,8 @@
1
1
  require 'helmet/templates'
2
2
  require 'helmet/response'
3
3
 
4
+ require 'rack/mime'
5
+
4
6
  module Helmet
5
7
 
6
8
  # Handle each request and provide usefull methods for request handling
@@ -67,9 +69,27 @@ module Helmet
67
69
  def params
68
70
  env.params || {}
69
71
  end
72
+
73
+ # @return [Hash] config object
74
+ def config
75
+ env.config
76
+ end
70
77
 
71
- def content_type(type)
72
- # TODO implement content type header
78
+ # Set content-type header based on extension
79
+ #
80
+ # @param [String, Symbol] ext mime extension
81
+ # @return [String] mime type (@see mime_type)
82
+ def content_type ext
83
+ header['Content-Type'] = mime_type(ext)
84
+ end
85
+
86
+ # Return mime type based on extension
87
+ # Uses Rack::Mime library.
88
+ #
89
+ # @param [String, Symbol] ext mime type extension
90
+ # @return [String] mime type
91
+ def mime_type ext
92
+ Rack::Mime.mime_type ".#{ext}"
73
93
  end
74
94
  end
75
95
  end
@@ -6,11 +6,12 @@ module Helmet
6
6
  @@template_cache = Tilt::Cache.new
7
7
 
8
8
  def erb(template, options = {}, locals = {})
9
- render(:erb, template, options, locals)
9
+ render(:erb, template, options, locals)
10
10
  end
11
11
 
12
12
  def render(engine, data, options = {}, locals = {}, &block)
13
- layout = options.delete(:layout)
13
+ layout = options[:layout]
14
+ layout = :layout if layout.nil?
14
15
  # force template update
15
16
  @@template_cache.clear unless Goliath.env == :production
16
17
  compiled_template = @@template_cache.fetch(data, options) do
@@ -18,7 +19,12 @@ module Helmet
18
19
  end
19
20
  output = compiled_template.render(self, locals, &block)
20
21
  if layout
21
- return render(engine, layout, options, locals) {output}
22
+ begin
23
+ options.merge!(layout: false)
24
+ return render(engine, layout, options, locals) {output}
25
+ rescue
26
+ # Do nothing
27
+ end
22
28
  end
23
29
  output
24
30
  end
@@ -27,7 +33,8 @@ module Helmet
27
33
 
28
34
  def find_template(engine, template)
29
35
  filename = "#{template.to_s}.#{engine.to_s}"
30
- File.join(@klass.config(:views_folder), filename)
36
+ File.join(@klass.settings[:views_folder], filename)
31
37
  end
32
38
  end
39
+
33
40
  end
@@ -1,3 +1,3 @@
1
1
  module Helmet
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -1,18 +1,23 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
3
  require 'uri'
4
+ require 'rack/mime'
4
5
 
5
6
  class Simple < Helmet::API
6
7
 
7
8
  use Rack::Session::Cookie, :secret => 'remove_security_warning'
8
9
 
9
- before '/xx' do
10
+ before '/filtered' do
10
11
  halt 'filtered!'
11
12
  end
12
13
 
13
14
  get '/' do
14
15
  'get'
15
16
  end
17
+
18
+ get '/filtered' do
19
+ 'should not be here!'
20
+ end
16
21
 
17
22
  post '/' do
18
23
  'post'
@@ -33,6 +38,11 @@ class Simple < Helmet::API
33
38
  get '/redirect' do
34
39
  redirect '/redirected'
35
40
  end
41
+
42
+ get '/ct' do
43
+ ct = params['ct'].to_sym
44
+ content_type ct
45
+ end
36
46
  end
37
47
 
38
48
  class APITest < Test::Unit::TestCase
@@ -77,7 +87,7 @@ class APITest < Test::Unit::TestCase
77
87
 
78
88
  def test_filter
79
89
  with_api(Simple) do
80
- get_request({:path => '/xx'}, @err) do |c|
90
+ get_request({:path => '/filtered'}, @err) do |c|
81
91
  assert_equal 'filtered!', c.response
82
92
  end
83
93
  end
@@ -93,6 +103,30 @@ class APITest < Test::Unit::TestCase
93
103
  end
94
104
  end
95
105
 
106
+ def test_content_type_json
107
+ with_api(Simple) do
108
+ get_request({:path => '/ct', :query => {ct: 'json'}}, @err) do |c|
109
+ assert_equal c.response_header['Content-Type'], Rack::Mime.mime_type('.json')
110
+ end
111
+ end
112
+ end
113
+
114
+ def test_content_type_js
115
+ with_api(Simple) do
116
+ get_request({:path => '/ct', :query => {ct: 'js'}}, @err) do |c|
117
+ assert_equal c.response_header['Content-Type'], Rack::Mime.mime_type('.js')
118
+ end
119
+ end
120
+ end
121
+
122
+ def test_content_type_html
123
+ with_api(Simple) do
124
+ get_request({:path => '/ct', :query => {ct: 'html'}}, @err) do |c|
125
+ assert_equal c.response_header['Content-Type'], Rack::Mime.mime_type('.html')
126
+ end
127
+ end
128
+ end
129
+
96
130
  # def test_raise
97
131
  # with_api(Simple) do
98
132
  # get_request({:path => '/raise'}, @err) do |c|
@@ -0,0 +1,58 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class RouterAPI < Helmet::API
4
+
5
+ before '/user/:id' do
6
+ halt "Halt user #{params[:id]}" if params[:id] == '11'
7
+ end
8
+
9
+ before '/clean/:user/1' do
10
+ "Clean/ #{params[:user] || 'nil'}/ #{params[:id] || 'nil'}"
11
+ end
12
+
13
+ before '/clean/test/:id' do
14
+ [body, "Clean/ #{params[:user] || 'nil'}/ #{params[:id] || 'nil'}"] * ','
15
+ end
16
+
17
+ get '/user/:id' do
18
+ "Hello user #{params[:id]}"
19
+ end
20
+
21
+ get '/clean/:user/:id' do
22
+ body
23
+ end
24
+
25
+ end
26
+
27
+ class HttpRouterTest < Test::Unit::TestCase
28
+ include Goliath::TestHelper
29
+
30
+ def setup
31
+ @err = Proc.new { assert false, "API request failed" }
32
+ end
33
+
34
+ def test_url_params
35
+ with_api(RouterAPI) do
36
+ get_request({path: '/user/3'}, @err) do |c|
37
+ assert_equal 'Hello user 3', c.response
38
+ end
39
+ end
40
+ end
41
+
42
+ def test_before_filters_params
43
+ with_api(RouterAPI) do
44
+ get_request({path: '/user/11'}, @err) do |c|
45
+ assert_equal 'Halt user 11', c.response
46
+ end
47
+ end
48
+ end
49
+
50
+ def test_clean_params_for_each_route
51
+ with_api(RouterAPI) do
52
+ get_request({path: '/clean/test/1'}, @err) do |c|
53
+ assert_equal 'Clean/ test/ nil,Clean/ nil/ 1', c.response
54
+ end
55
+ end
56
+ end
57
+
58
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helmet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thiago Lewin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-20 00:00:00.000000000 Z
11
+ date: 2013-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: goliath
@@ -101,6 +101,7 @@ files:
101
101
  - examples/views/layout.erb
102
102
  - test/api_test.rb
103
103
  - test/helper_test.rb
104
+ - test/http_router_test.rb
104
105
  - test/template_test.rb
105
106
  - test/test_helper.rb
106
107
  - test/views/test.erb
@@ -137,6 +138,7 @@ summary: Simple web framework for Goliath web server.
137
138
  test_files:
138
139
  - test/api_test.rb
139
140
  - test/helper_test.rb
141
+ - test/http_router_test.rb
140
142
  - test/template_test.rb
141
143
  - test/test_helper.rb
142
144
  - test/views/test.erb