wd_sinatra 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Weasel Diesel Sinatra Changelog
2
2
 
3
+ ## 0.2.4
4
+
5
+ * Fixed the documentation template to support namespaced input params.
6
+
3
7
  ## 0.2.3
4
8
 
5
9
  * Fixed the example of the `params_exception_handler` hook added in the
data/README.md CHANGED
@@ -57,8 +57,69 @@ To generate documentation for the APIs you created in the api folder.
57
57
 
58
58
  ### Testing
59
59
 
60
- TODO
61
- see 'wd_sinatra/test_helpers'
60
+ Helpers to test your apps are provided. When you generate your first
61
+ app, you will see a first example using minitest:
62
+
63
+ ```ruby
64
+ class HelloWorldIntegrationTest < MiniTest::Unit::TestCase
65
+
66
+ def test_default_response
67
+ TestApi.get "/hello_world"
68
+ assert_api_response
69
+ assert_equal "Hello World", TestApi.json_response['message']
70
+ assert Time.parse(TestApi.json_response['at']) < Time.now
71
+ end
72
+
73
+ def test_customized_response
74
+ TestApi.get "/hello_world", :name => "Matt"
75
+ assert_api_response
76
+ assert_equal "Hello Matt", TestApi.json_response['message']
77
+ assert Time.parse(TestApi.json_response['at']) < Time.now
78
+ end
79
+
80
+ end
81
+ ```
82
+
83
+ The `TestApi` module is used to call a service. The call will go through
84
+ the entire app stack including middleware.
85
+ You can then look at the response object via the `TestApi` module using
86
+ one of the many provided methods such as `last_response`,
87
+ `json_response` and then methods on `last_response` provided by [Rack](http://rack.rubyforge.org/doc/Rack/MockResponse.html):
88
+
89
+ * status
90
+ * headers
91
+ * body
92
+ * errors
93
+
94
+ The `TestApi` interface allows you to dispatch all the calls, and to
95
+ send custom parameters and headers, set cookies and everything you need to do proper integration
96
+ tests.
97
+
98
+ Because we opted to describe our responses, and this framework is based
99
+ on the concept that we want to communicate about our apis, it is
100
+ critical to test the service responses. For that, a JSON response helper
101
+ is provided (testunit/minitest only for now) so you can easily check
102
+ that the structure of the response matches the description.
103
+
104
+ This is what the `assert_api_response` helper does.
105
+
106
+ This helper is to be used after you dispatched an API call.
107
+ The last response is being analyzed and the JSON structure should match
108
+ the description provided in the service.
109
+ Note that this helper doesn't check the content of the structure, that
110
+ is something you need to do yourself with custom tests as shown in the
111
+ example.
112
+
113
+ ### Tips
114
+
115
+ When dispatching a test api call using an url with a placeholder such as
116
+ `/people/:id', you need to pass the id as a param and the url will be
117
+ properly composed:
118
+
119
+ ```ruby
120
+ TestApi.post '/people/:id', :id => 123
121
+ ```
122
+
62
123
 
63
124
  ## Writing a service
64
125
 
data/bin/wd_sinatra CHANGED
@@ -35,6 +35,10 @@ class WdSinatra < Thor::Group
35
35
  directory "api", "#{name}/api"
36
36
  end
37
37
 
38
+ def create_test_directory
39
+ directory "test", "#{name}/test"
40
+ end
41
+
38
42
  def create_files
39
43
  copy_file "Rakefile", "#{name}/Rakefile"
40
44
  copy_file "Gemfile", "#{name}/Gemfile"
@@ -1,5 +1,5 @@
1
1
  module WD
2
2
  module Sinatra
3
- VERSION = "0.2.3"
3
+ VERSION = "0.2.4"
4
4
  end
5
5
  end
@@ -2,7 +2,7 @@
2
2
  <html lang="en">
3
3
  <head>
4
4
  <meta charset="utf-8">
5
- <title>Bootstrap, from Twitter</title>
5
+ <title>Weasel Diesel - API documentation</title>
6
6
  <meta name="description" content="">
7
7
  <meta name="author" content="">
8
8
 
@@ -75,7 +75,32 @@
75
75
  <% end %>
76
76
  </ul>
77
77
  <% end %>
78
- <%# TODO support for namespaced params %>
78
+
79
+ <h3>Input parameters description</h3>
80
+ <%# TODO support for nested namespaced params %>
81
+ <% api.params.namespaced_params.each do |params| %>
82
+ <div class='well'>
83
+ <h4>Namespace: <span style='font-style: italic'><%= params.space_name %></span></h4>
84
+ <div style='padding-left: 40px'>
85
+ <% [["Required", :list_required], ["Optional", :list_optional]].each do |label,rule_meth| %>
86
+ <% unless params.send(rule_meth).empty? %>
87
+ <h4><%= label %> Params</h4>
88
+ <ul>
89
+ <% params.send(rule_meth).each do |rule| %>
90
+ <li>
91
+ <span class='label notice'><%= rule.name %></span> of type <span class='label success'><%= rule.options[:type] || 'String' %></span>
92
+ <% if desc = (api.doc.params_doc[rule.name.to_sym] || rule.options[:doc]) %>
93
+ <%= desc %>&nbsp;
94
+ <% end %>
95
+ </li>
96
+ <% end %>
97
+ </ul>
98
+ <% end %>
99
+ <% end %>
100
+ </div>
101
+ </div>
102
+ <% end %>
103
+
79
104
  <% [["Required", :required_rules], ["Optional", :optional_rules]].each do |label,rule_meth| %>
80
105
  <% unless api.send(rule_meth).empty? %>
81
106
  <h3><%= label %> Params</h3>
@@ -97,20 +122,23 @@
97
122
  </ul>
98
123
  <% end %>
99
124
  <% end %>
125
+
100
126
  </div>
101
127
 
102
128
  <div>
103
129
  <% if api.response.nodes.any? %>
104
130
  <h3>Response description</h3>
105
- <% api.response.elements.each do |el| %>
106
- <%= el.to_html %>
107
- <% end %>
108
- <% api.response.arrays.each do |arr| %>
109
- <%= arr.to_html %>
110
- <% end %>
131
+ <div class='well'>
132
+ <% api.response.elements.each do |el| %>
133
+ <%= el.to_html %>
134
+ <% end %>
135
+ <% api.response.arrays.each do |arr| %>
136
+ <%= arr.to_html %>
137
+ <% end %>
138
+ </div>
111
139
  </ul>
112
140
  <h4>Response example</h4>
113
- <p><code><%= api.response.to_json %></code></p>
141
+ <div class='well'><code><%= api.response.to_json %></code></div>
114
142
  <% end %>
115
143
  </div>
116
144
 
@@ -0,0 +1,19 @@
1
+ require 'test_helpers'
2
+
3
+ class HelloWorldIntegrationTest < MiniTest::Unit::TestCase
4
+
5
+ def test_default_response
6
+ TestApi.get "/hello_world"
7
+ assert_api_response
8
+ assert_equal "Hello World", TestApi.json_response['message']
9
+ assert Time.parse(TestApi.json_response['at']) < Time.now
10
+ end
11
+
12
+ def test_customized_response
13
+ TestApi.get "/hello_world", :name => "Matt"
14
+ assert_api_response
15
+ assert_equal "Hello Matt", TestApi.json_response['message']
16
+ assert Time.parse(TestApi.json_response['at']) < Time.now
17
+ end
18
+
19
+ end
@@ -0,0 +1,39 @@
1
+ ENV['RACK_ENV'] ||= 'test'
2
+ require 'test/unit'
3
+ require 'rack'
4
+ require 'rack/test'
5
+ require 'json'
6
+ require 'wd_sinatra/test_helpers'
7
+
8
+ ENV['DONT_PRINT_ROUTES'] = 'true'
9
+ root = File.expand_path('..', File.dirname(__FILE__))
10
+ WDSinatra::AppLoader.server(root)
11
+ WeaselDiesel.send(:include, JSONResponseVerification)
12
+
13
+ if RUBY_VERSION =~ /1.8/
14
+ require 'minitest/autorun'
15
+ end
16
+
17
+ require 'wd_sinatra/test_unit_helpers'
18
+ include TestUnitHelpers
19
+
20
+ TestApi.auth_request_x_header = WDSinatraHooks::INTERNAL_X_HEADER[/HTTP_(.*)/, 1] # strip the header marker added by Rack
21
+ TestApi.mobile_request_x_header = WDSinatraHooks::MOBILE_X_HEADER[/HTTP_(.*)/, 1] # strip the header marker added by Rack
22
+
23
+ module TestApi
24
+
25
+ # Edit this method to reflect your own Auth strategy
26
+ def self.valid_internal_api_headers(headers)
27
+ custom_headers = {TestApi.auth_request_x_header => WDSinatraHooks::ALLOWED_API_KEYS[0]}
28
+ custom_headers.merge!(headers) if headers
29
+ custom_headers
30
+ end
31
+
32
+ # Edit this method to reflect your own Auth strategy
33
+ def self.mobile_headers(headers)
34
+ custom_headers = {TestApi.mobile_request_x_header => 'fake_example'}
35
+ custom_headers.merge!(headers) if headers
36
+ custom_headers
37
+ end
38
+
39
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wd_sinatra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-14 00:00:00.000000000 Z
12
+ date: 2012-05-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: weasel_diesel
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &70286849559140 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
24
+ version_requirements: *70286849559140
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: thor
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &70286849558620 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ! '>='
@@ -37,12 +32,7 @@ dependencies:
37
32
  version: '0'
38
33
  type: :runtime
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
35
+ version_requirements: *70286849558620
46
36
  description: Weasel-Diesel Sinatra app gem, allowing you to generate/update sinatra
47
37
  apps using the Weasel Diesel DSL
48
38
  email:
@@ -136,6 +126,8 @@ files:
136
126
  - templates/lib/tasks/doc_generator/bootstrap/lib/variables.less
137
127
  - templates/lib/tasks/doc_generator/template.erb
138
128
  - templates/public/favicon.ico
129
+ - templates/test/integration/hello_world_test.rb
130
+ - templates/test/test_helpers.rb
139
131
  - wd-sinatra.gemspec
140
132
  homepage: https://github.com/mattetti/wd_sinatra
141
133
  licenses: []
@@ -157,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
149
  version: '0'
158
150
  requirements: []
159
151
  rubyforge_project:
160
- rubygems_version: 1.8.24
152
+ rubygems_version: 1.8.16
161
153
  signing_key:
162
154
  specification_version: 3
163
155
  summary: Weasel-Diesel Sinatra app gem, allowing you to generate/update sinatra apps