flunk 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/flunk.rb +119 -28
  3. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a617a93d04bbd6c20b131f21271ed6d9c372bc3d
4
- data.tar.gz: 33e9a15f41753fe0fd543d581c21154cc2daf8e6
3
+ metadata.gz: fbfaf66ff6b6869fdd639cc3c5c789232ba7a1ca
4
+ data.tar.gz: 93e06a64ffe38ff97f6cd77ebcec094eef85e441
5
5
  SHA512:
6
- metadata.gz: c15a389b2b9c0b56df060115d0c6129347fca4f8920041194316895f1d7c35ddd1ec028d435efd468a3bd92da74fdf44838baf5ec1d71f1cd9fae44b94e2f88d
7
- data.tar.gz: a233c0b670c259fe4f5a162a76650084b4dc4078f313351b21b453faeb9dfa434375ecba3de0aa4804aa95fd0ec1d616987ca87ead489d00e1ab2c35cabb7e10
6
+ metadata.gz: 69e745859d1458782bcfa680df4750ad6ff89304a03f81129fbd0ac28255de8bfc41b7a258f982f3e744b500795d690af1be3f88b74233ac56103f0df64eb499
7
+ data.tar.gz: 17c6790e060ccdc1f913dab6b90b62dd00634585911e43c99826b9f44b55aeda69361fbff06a99be7724b832df3cc2191f8f3adac5e03fd4ef021c2e460af7a6
@@ -5,10 +5,10 @@ class Flunk < ActionDispatch::IntegrationTest
5
5
  def self.test(resource, action, &block)
6
6
 
7
7
  if action.class == Hash
8
- name = action[:name]
9
- action = action[:action]
8
+ name = action[:name]
9
+ action = action[:action]
10
10
  end
11
-
11
+
12
12
  new_proc = Proc.new do
13
13
  @resource ||= resource
14
14
  @action ||= action
@@ -38,11 +38,11 @@ class Flunk < ActionDispatch::IntegrationTest
38
38
  @method ||= self.class.config.read_method
39
39
  @ssl ||= self.class.config.read_ssl
40
40
 
41
+ @headers ||= {}
42
+
41
43
  if @username || @password
42
- @headers ||= {}
43
44
  @headers["HTTP_AUTHORIZATION"] = "Basic #{Base64.encode64(@username.to_s + ":" + @password.to_s)}".strip
44
45
  elsif @auth_token
45
- @headers ||= {}
46
46
  @headers["HTTP_AUTHORIZATION"] = "Token token=\"#{@auth_token}\"".strip
47
47
  end
48
48
 
@@ -50,20 +50,38 @@ class Flunk < ActionDispatch::IntegrationTest
50
50
 
51
51
  @response = response
52
52
 
53
- assert_response @status, @response.body
53
+ expected_status_code = Rack::Utils::SYMBOL_TO_STATUS_CODE[@status]
54
+
55
+ if response.status == 422 and expected_status_code != 422
56
+ puts "VALIDATION ERRORS:"
57
+ puts JSON.pretty_generate(JSON.parse(response.body))
58
+ end
59
+
60
+ if response.status.to_i != expected_status_code
61
+ puts "STATUS MISMATCH:"
62
+ puts "path: #{@path}"
63
+ puts "headers: #{@headers.to_json}"
64
+ puts "body: #{@body}"
65
+ end
66
+
67
+ assert_response @status
54
68
 
55
69
  unless response.body.blank?
70
+ @result = response.body
56
71
  if response.content_type == 'application/json'
57
- json = ActiveSupport::JSON.decode(response.body)
58
- rec_symbolize( json )
59
- @result = json
72
+ begin
73
+ json = ActiveSupport::JSON.decode(response.body)
74
+ rec_symbolize( json )
75
+ @result = json
76
+ rescue => e
77
+ end
60
78
  end
61
79
  end
62
80
 
63
81
  if not @desc.nil?
64
82
  make_doc @resource, @action, @desc, @path, @method, @auth_token, @headers, @body, @status, @result
65
83
  end
66
-
84
+
67
85
  end
68
86
 
69
87
  @result
@@ -72,7 +90,7 @@ class Flunk < ActionDispatch::IntegrationTest
72
90
  def desc(desc)
73
91
  @desc = desc
74
92
  end
75
-
93
+
76
94
  def read_desc
77
95
  @desc
78
96
  end
@@ -86,7 +104,10 @@ class Flunk < ActionDispatch::IntegrationTest
86
104
  end
87
105
 
88
106
  def path(path)
89
- @path = path
107
+ uri = URI.parse path
108
+ uri.path = uri.path[0] == "/" ? uri.path : "/#{uri.path}"
109
+ uri.path = uri.path[-5..-1] == ".json" ? uri.path : "#{uri.path}.json"
110
+ @path = uri.to_s
90
111
  end
91
112
 
92
113
  def read_path
@@ -151,7 +172,7 @@ class Flunk < ActionDispatch::IntegrationTest
151
172
 
152
173
  def header(key, value)
153
174
  @headers ||= {}
154
- @headers = self.class.config.read_headers.merge @headers
175
+ @headers = self.class.config.read_headers.merge @headers if self.class.config.read_headers
155
176
  @headers[key] = value
156
177
  end
157
178
 
@@ -170,7 +191,7 @@ class Flunk < ActionDispatch::IntegrationTest
170
191
 
171
192
  def doc_directory(doc_directory)
172
193
  FileUtils.rm_r(doc_directory) if File.exists?(doc_directory)
173
- FileUtils.mkdir_p(doc_directory)
194
+ FileUtils.mkdir_p(doc_directory)
174
195
  @doc_directory = doc_directory
175
196
  end
176
197
 
@@ -184,6 +205,18 @@ class Flunk < ActionDispatch::IntegrationTest
184
205
  @doc_directory
185
206
  end
186
207
 
208
+ def doc_base_url(doc_base_url)
209
+ @doc_base_url = doc_base_url
210
+ end
211
+
212
+ def read_config_doc_base_url
213
+ @doc_base_url
214
+ end
215
+
216
+ def read_doc_base_url
217
+ self.class.config.read_config_doc_base_url || "http://www.example.com/"
218
+ end
219
+
187
220
 
188
221
 
189
222
 
@@ -223,46 +256,104 @@ class Flunk < ActionDispatch::IntegrationTest
223
256
  def make_doc resource, action, desc, path, method, auth_token, headers, body, status, response
224
257
  body = body.class == String ? JSON.parse(body) : body
225
258
  url = File.join(@@config.read_base_url.to_s, path.to_s)
259
+
260
+ headers ||= {}
261
+ headers["Content-Type"] = "application/json"
262
+ headers["Accept"] = "application/json"
263
+
226
264
  contents = ""
265
+
227
266
  contents += "# #{action.humanize}\n\n"
267
+
228
268
  contents += "#{desc.humanize}\n\n"
269
+
229
270
  contents += "## Request\n\n"
271
+
230
272
  if not auth_token.nil?
231
- contents += "- Requires Authentication\n"
273
+ contents += "- **Requires Authentication**\n"
232
274
  end
233
- contents += "- HTTP Method: #{method.to_s.upcase}\n"
234
- contents += "- URL: #{url}\n"
275
+
276
+ contents += "- **Method:** #{method.to_s.upcase}\n"
277
+
278
+ # if not headers.nil?
279
+ # headers_strings = headers.map {|k,v| " - #{k}: #{v}" }
280
+ # contents += "- **Headers:**\n#{headers_strings.join("\n")}\n"
281
+ # end
282
+
283
+ contents += "- **URL:** #{url}\n"
284
+
235
285
  if not body.nil?
236
- contents += "- Body:\n\n```js\n#{JSON.pretty_generate(body)}\n```\n\n"
286
+ contents += "- **Body:**\n\n```json\n#{pretty(body)}\n```\n\n"
237
287
  else
238
288
  contents += "\n"
239
289
  end
290
+
240
291
  contents += "## Response\n\n"
241
- contents += "- Status: #{status} #{status.to_s.humanize}\n"
292
+
293
+ contents += "- **Status:** #{Rack::Utils::SYMBOL_TO_STATUS_CODE[status]} #{status.to_s.humanize}\n"
294
+
242
295
  if not response.nil?
243
- contents += "- Body:\n\n```js\n#{JSON.pretty_generate(response)}\n```\n\n"
296
+ contents += "- **Body:**\n\n```json\n#{pretty(response)}\n```\n\n"
244
297
  else
245
298
  contents += "\n"
246
299
  end
300
+
247
301
  contents += "## Example\n\n"
248
- contents +=
302
+
303
+ contents +=
249
304
  "```bash
250
305
  curl -X #{method.to_s.upcase} \\\n"
251
- headers.to_h.each do |key, value|
252
- contents +=
253
- " -H \"#{key}: #{value}\" \\\n"
306
+ headers.to_h.each do |key, value|
307
+ contents +=
308
+ " -H \'#{key}: #{value}\' \\\n"
254
309
  end
255
310
  if not body.nil?
256
- contents +=
257
- " -d '#{body.to_json}' \\"
311
+ contents +=
312
+ " -d '#{pretty(body).gsub /\n/, "\n "}' \\\n"
258
313
  end
259
- contents +=
260
- " \"#{url}\"
314
+ contents +=
315
+ " \"#{ URI::join(read_doc_base_url, url) }\"
261
316
  ```"
317
+
318
+ save_doc resource, action, contents
319
+ end
320
+
321
+
322
+ # custom attributes must follow this form:
323
+ # customer_attributes =
324
+ # key:
325
+ # kind: [text | code]
326
+ # content: [text]
327
+ def make_custom_doc resource, action, desc, custom_attributes
328
+ contents = ""
329
+ contents += "# #{action.humanize}\n\n"
330
+ contents += "#{desc.humanize}\n\n"
331
+
332
+ custom_attributes.each do |key, value|
333
+ if value[:kind] == :text
334
+ contents += "- **#{key.to_s.humanize}:** #{value[:content]}\n"
335
+ elsif value[:kind] == :code
336
+ contents += "- **#{key.to_s.humanize}:**\n\n```\n#{value[:content]}\n```\n\n"
337
+ end
338
+ end
339
+
340
+ save_doc resource, action, contents
341
+ end
342
+
343
+
344
+ def save_doc resource, action, contents
262
345
  resource_directory = File.join( read_doc_directory, resource.pluralize.capitalize )
263
346
  FileUtils.mkdir_p(resource_directory) unless File.exists?( resource_directory )
264
347
  file_path = File.join( resource_directory, "#{action.capitalize}.md" )
265
348
  File.open(file_path, 'w') {|f| f.write(contents) }
266
349
  end
267
350
 
351
+ def pretty json
352
+ begin
353
+ pretty_json = JSON.pretty_generate(json)
354
+ rescue => e
355
+ pretty_json = json
356
+ end
357
+ end
358
+
268
359
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flunk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Kirk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-03 00:00:00.000000000 Z
11
+ date: 2014-12-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A gem for testing Ruby on Rails web APIs by simulating a client.
14
14
  email: atomkirk@gmail.com
@@ -30,17 +30,17 @@ require_paths:
30
30
  - lib
31
31
  required_ruby_version: !ruby/object:Gem::Requirement
32
32
  requirements:
33
- - - '>='
33
+ - - ">="
34
34
  - !ruby/object:Gem::Version
35
35
  version: '0'
36
36
  required_rubygems_version: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  requirements: []
42
42
  rubyforge_project:
43
- rubygems_version: 2.2.1
43
+ rubygems_version: 2.2.2
44
44
  signing_key:
45
45
  specification_version: 4
46
46
  summary: A gem for testing Ruby on Rails web APIs by simulating a client.