kronk 1.6.1 → 1.6.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc CHANGED
@@ -1,3 +1,11 @@
1
+ === 1.6.2 / 2011-09-12
2
+
3
+ * Bugfixes
4
+
5
+ * Handling of stream output with Diff#formatted is nil.
6
+
7
+ * Re-enabled support for response uri options.
8
+
1
9
  === 1.6.1 / 2011-09-12
2
10
 
3
11
  * Enhancements:
data/TODO.rdoc CHANGED
@@ -1,5 +1,18 @@
1
1
  = TODO
2
2
 
3
+ == Path
4
+
5
+ * Support move and map for path transactions.
6
+
7
+ == Player
8
+
9
+ * Read full URIs for player input but strip off the protocol and host.
10
+
11
+ * When using player and no host is given, support reading a full URI for
12
+ player input.
13
+
14
+ == Done
15
+
3
16
  * Support data diffing for arrays (including data struct).
4
17
 
5
18
  * Allow for showing diffed sections of a diff only.
data/lib/kronk.rb CHANGED
@@ -14,7 +14,7 @@ require 'yaml'
14
14
  class Kronk
15
15
 
16
16
  # This gem's version.
17
- VERSION = '1.6.1'
17
+ VERSION = '1.6.2'
18
18
 
19
19
  require 'kronk/constants'
20
20
  require 'kronk/player'
@@ -68,6 +68,8 @@ class Kronk
68
68
  # Find a fully qualified ruby namespace/constant.
69
69
 
70
70
  def self.find_const name_or_file, case_insensitive=false
71
+ return name_or_file unless String === name_or_file
72
+
71
73
  if name_or_file =~ /[^:]:([^:]+)$/
72
74
  req_file = $1
73
75
  i = $1.length + 2
@@ -240,8 +242,8 @@ class Kronk
240
242
  # :proxy:: Hash/String - http proxy to use; defaults to nil
241
243
  # :only_data:: String/Array - extracts the data from given data paths
242
244
  # :ignore_data:: String/Array - defines which data points to exclude
243
- # :keep_indicies:: Boolean - indicies of modified arrays display as hashes.
244
- # :with_headers:: Boolean/String/Array - defines which headers to include
245
+ # :keep_indicies:: Boolean - indicies of modified arrays display as hashes
246
+ # :show_headers:: Boolean/String/Array - which headers to show in output
245
247
  # :parser:: Object/String - the parser to use for the body; default nil
246
248
  # :raw:: Boolean - run diff on raw strings
247
249
 
@@ -266,12 +268,12 @@ class Kronk
266
268
 
267
269
  t1 = Thread.new do
268
270
  res1 = retrieve uri1
269
- str1 = res1.stringify @options
271
+ str1 = res1.stringify
270
272
  end
271
273
 
272
274
  t2 = Thread.new do
273
275
  res2 = retrieve uri2
274
- str2 = res2.stringify @options
276
+ str2 = res2.stringify
275
277
  end
276
278
 
277
279
  t1.join
@@ -306,6 +308,9 @@ class Kronk
306
308
  Kronk.history << uri
307
309
  end
308
310
 
311
+ resp.parser = options[:parser] if options[:parser]
312
+ resp.stringify_opts = options
313
+
309
314
  max_rdir = options[:follow_redirects]
310
315
  while resp.redirect? && (max_rdir == true || max_rdir.to_s.to_i > 0)
311
316
  Cmd.verbose "Following redirect..."
@@ -373,9 +378,11 @@ class Kronk
373
378
  end
374
379
 
375
380
  # Response headers - Boolean, String, or Array
376
- when :with_headers
377
- next if out_opts[key] == true || out_opts[key] && val == true
378
- out_opts[key] = [*out_opts[key]] | [*val]
381
+ when :show_headers
382
+ next if out_opts.has_key?(key) &&
383
+ (out_opts[key].class != Array || val == true || val == false)
384
+ out_opts[key] = (val == true || val == false) ? val :
385
+ [*out_opts[key]] | [*val]
379
386
 
380
387
  # String or Array
381
388
  when :only_data, :ignore_data
data/lib/kronk/cmd.rb CHANGED
@@ -57,7 +57,7 @@ class Kronk
57
57
  :player => {},
58
58
  :proxy => {},
59
59
  :uris => [],
60
- :with_headers => false
60
+ :show_headers => false
61
61
  }
62
62
 
63
63
  options = parse_data_path_args options, argv
@@ -139,13 +139,13 @@ Parse and run diffs against data from live and cached http responses.
139
139
 
140
140
  opt.on('-i', '--include [HEADER1,HEADER2]', Array,
141
141
  'Include all or given headers in response') do |value|
142
- options[:with_headers] ||= []
142
+ options[:show_headers] ||= []
143
143
 
144
144
  if value
145
- options[:with_headers].concat value if
146
- Array === options[:with_headers]
145
+ options[:show_headers].concat value if
146
+ Array === options[:show_headers]
147
147
  else
148
- options[:with_headers] = true
148
+ options[:show_headers] = true
149
149
  end
150
150
 
151
151
  options[:no_body] = false
@@ -154,13 +154,13 @@ Parse and run diffs against data from live and cached http responses.
154
154
 
155
155
  opt.on('-I', '--head [HEADER1,HEADER2]', Array,
156
156
  'Use all or given headers only in the response') do |value|
157
- options[:with_headers] ||= []
157
+ options[:show_headers] ||= []
158
158
 
159
159
  if value
160
- options[:with_headers].concat value if
161
- Array === options[:with_headers]
160
+ options[:show_headers].concat value if
161
+ Array === options[:show_headers]
162
162
  else
163
- options[:with_headers] = true
163
+ options[:show_headers] = true
164
164
  end
165
165
 
166
166
  options[:no_body] = true
@@ -29,9 +29,11 @@ class Kronk
29
29
  kronk.diff.formatted
30
30
 
31
31
  elsif kronk.response
32
- kronk.response.stringify kronk.options
32
+ kronk.response.stringify
33
33
  end
34
34
 
35
+ return unless output
36
+
35
37
  output = "#{output.length}\r\n#{output}\r\n"
36
38
 
37
39
  mutex.synchronize do
@@ -27,7 +27,7 @@ class Kronk
27
27
  elsif kronk.response
28
28
  begin
29
29
  # Make sure response is parsable
30
- kronk.response.stringify kronk.options
30
+ kronk.response.stringify
31
31
  rescue => e
32
32
  error e, kronk, mutex
33
33
  return
@@ -37,10 +37,10 @@ class Kronk
37
37
  end
38
38
 
39
39
 
40
- attr_accessor :body, :bytes, :byterate, :code, :headers, :parser,
41
- :raw, :request, :uri
40
+ attr_accessor :body, :bytes, :byterate, :code, :headers,
41
+ :raw, :stringify_opts, :request, :uri
42
42
 
43
- attr_reader :encoding, :time
43
+ attr_reader :encoding, :parser, :time
44
44
 
45
45
  alias to_hash headers
46
46
  alias to_s raw
@@ -89,6 +89,8 @@ class Kronk
89
89
  @uri = URI.parse io.path if File === io
90
90
 
91
91
  @byterate = 0
92
+
93
+ @stringify_opts = {}
92
94
  end
93
95
 
94
96
 
@@ -155,14 +157,14 @@ class Kronk
155
157
 
156
158
  return @parsed_body if @parsed_body && !parser
157
159
 
160
+ parser ||= @parser
161
+
158
162
  begin
159
163
  parser = Kronk.parser_for(parser) || Kronk.find_const(parser)
160
164
  rescue NameError
161
165
  raise InvalidParser, "No such parser: #{parser}"
162
166
  end if String === parser
163
167
 
164
- parser ||= @parser
165
-
166
168
  raise MissingParser,
167
169
  "No parser for Content-Type: #{@_res['Content-Type']}" unless parser
168
170
 
@@ -203,6 +205,16 @@ class Kronk
203
205
  end
204
206
 
205
207
 
208
+ ##
209
+ # Assign the parser.
210
+
211
+ def parser= parser
212
+ @parser = Kronk.parser_for(parser) || Kronk.find_const(parser)
213
+ rescue NameError
214
+ raise InvalidParser, "No such parser: #{parser}"
215
+ end
216
+
217
+
206
218
  ##
207
219
  # Returns the header portion of the raw http response.
208
220
 
@@ -245,13 +257,13 @@ class Kronk
245
257
  # Returns the raw response with selective headers and/or the body of
246
258
  # the response. Supports the following options:
247
259
  # :no_body:: Bool - Don't return the body; default nil
248
- # :with_headers:: Bool/String/Array - Return headers; default nil
260
+ # :show_headers:: Bool/String/Array - Return headers; default nil
249
261
 
250
262
  def selective_string options={}
251
263
  str = @body unless options[:no_body]
252
264
 
253
- if options[:with_headers]
254
- header = raw_header(options[:with_headers])
265
+ if options[:show_headers]
266
+ header = raw_header(options[:show_headers])
255
267
  str = [header, str].compact.join "\r\n"
256
268
  end
257
269
 
@@ -263,7 +275,7 @@ class Kronk
263
275
  # Returns the parsed response with selective headers and/or the body of
264
276
  # the response. Supports the following options:
265
277
  # :no_body:: Bool - Don't return the body; default nil
266
- # :with_headers:: Bool/String/Array - Return headers; default nil
278
+ # :show_headers:: Bool/String/Array - Return headers; default nil
267
279
  # :parser:: Object - The parser to use for the body; default nil
268
280
  # :ignore_data:: String/Array - Removes the data from given data paths
269
281
  # :only_data:: String/Array - Extracts the data from given data paths
@@ -275,8 +287,8 @@ class Kronk
275
287
  data = parsed_body options[:parser]
276
288
  end
277
289
 
278
- if options[:with_headers]
279
- header_data = parsed_header(options[:with_headers])
290
+ if options[:show_headers]
291
+ header_data = parsed_header(options[:show_headers])
280
292
  data &&= [header_data, data]
281
293
  data ||= header_data
282
294
  end
@@ -298,9 +310,11 @@ class Kronk
298
310
  # :ignore_data:: String/Array - defines which data points to exclude
299
311
  # :raw:: Boolean - Force using the unparsed raw response
300
312
  # :keep_indicies:: Boolean - indicies of modified arrays display as hashes.
301
- # :with_headers:: Boolean/String/Array - defines which headers to include
313
+ # :show_headers:: Boolean/String/Array - defines which headers to include
302
314
 
303
315
  def stringify options={}
316
+ options = options.empty? ? @stringify_opts : merge_stringify_opts(options)
317
+
304
318
  if !options[:raw] && (options[:parser] || @parser || options[:no_body])
305
319
  data = selective_data options
306
320
  Diff.ordered_data_string data, options[:struct]
@@ -314,6 +328,30 @@ class Kronk
314
328
  end
315
329
 
316
330
 
331
+ def merge_stringify_opts options # :nodoc:
332
+ options = options.dup
333
+ @stringify_opts.each do |key, val|
334
+ case key
335
+ # Response headers - Boolean, String, or Array
336
+ when :show_headers
337
+ next if options.has_key?(key) &&
338
+ (options[key].class != Array || val == true || val == false)
339
+
340
+ options[key] = (val == true || val == false) ? val :
341
+ [*options[key]] | [*val]
342
+
343
+ # String or Array
344
+ when :only_data, :ignore_data
345
+ options[key] = [*options[key]] | [*val]
346
+
347
+ else
348
+ options[key] = val if options[key].nil?
349
+ end
350
+ end
351
+ options
352
+ end
353
+
354
+
317
355
  ##
318
356
  # Check if this is a 2XX response.
319
357
 
@@ -83,8 +83,8 @@ class Kronk
83
83
  # Supports all options of Kronk.compare.
84
84
 
85
85
  def assert_equal_responses uri1, uri2, options={}
86
- resp1 = Kronk.retrieve(uri1, options).stringify options
87
- resp2 = Kronk.retrieve(uri2, options).stringify options
86
+ resp1 = Kronk.retrieve(uri1, options).stringify
87
+ resp2 = Kronk.retrieve(uri2, options).stringify
88
88
 
89
89
  assert_equal resp1, resp2
90
90
  end
data/test/test_cmd.rb CHANGED
@@ -201,19 +201,19 @@ class TestCmd < Test::Unit::TestCase
201
201
 
202
202
  def test_parse_args_http_headers
203
203
  opts = Kronk::Cmd.parse_args %w{uri -i FOO -i BAR -iTWO,PART}
204
- assert_equal %w{FOO BAR TWO PART}, opts[:with_headers]
204
+ assert_equal %w{FOO BAR TWO PART}, opts[:show_headers]
205
205
  assert_equal false, opts[:no_body]
206
206
 
207
207
  opts = Kronk::Cmd.parse_args %w{uri -I}
208
- assert_equal true, opts[:with_headers]
208
+ assert_equal true, opts[:show_headers]
209
209
  assert_equal true, opts[:no_body]
210
210
 
211
211
  opts = Kronk::Cmd.parse_args %w{uri -I FOO -I BAR -ITWO,PART}
212
- assert_equal %w{FOO BAR TWO PART}, opts[:with_headers]
212
+ assert_equal %w{FOO BAR TWO PART}, opts[:show_headers]
213
213
  assert_equal true, opts[:no_body]
214
214
 
215
215
  opts = Kronk::Cmd.parse_args %w{uri -i}
216
- assert_equal true, opts[:with_headers]
216
+ assert_equal true, opts[:show_headers]
217
217
  assert_equal false, opts[:no_body]
218
218
  end
219
219
 
data/test/test_kronk.rb CHANGED
@@ -313,31 +313,31 @@ class TestKronk < Test::Unit::TestCase
313
313
  end
314
314
 
315
315
 
316
- def test_options_for_uri_with_headers
316
+ def test_options_for_uri_show_headers
317
317
  with_uri_options do
318
318
  %w{withhdrs withstrhdrs withtruehdrs}.each do |type|
319
- opts = Kronk.new(:with_headers => true).
319
+ opts = Kronk.new(:show_headers => true).
320
320
  options_for_uri "http://#{type}.com"
321
321
 
322
- assert_equal true, opts[:with_headers]
322
+ assert_equal true, opts[:show_headers]
323
323
  end
324
324
  end
325
325
  end
326
326
 
327
327
 
328
- def test_options_for_uri_with_headers_arr
328
+ def test_options_for_uri_show_headers_arr
329
329
  with_uri_options do
330
330
  %w{withhdrs withstrhdrs}.each do |type|
331
- opts = Kronk.new(:with_headers => %w{hdr2 hdr3}).
331
+ opts = Kronk.new(:show_headers => %w{hdr2 hdr3}).
332
332
  options_for_uri "http://#{type}.com"
333
333
 
334
- assert_equal %w{hdr1 hdr2 hdr3}.sort, opts[:with_headers].sort
334
+ assert_equal %w{hdr1 hdr2 hdr3}.sort, opts[:show_headers].sort
335
335
  end
336
336
 
337
- opts = Kronk.new(:with_headers => %w{hdr2 hdr3}).
337
+ opts = Kronk.new(:show_headers => %w{hdr2 hdr3}).
338
338
  options_for_uri "http://withtruehdrs.com"
339
339
 
340
- assert_equal %w{hdr2 hdr3}, opts[:with_headers]
340
+ assert_equal %w{hdr2 hdr3}, opts[:show_headers]
341
341
  end
342
342
  end
343
343
 
@@ -362,19 +362,19 @@ class TestKronk < Test::Unit::TestCase
362
362
  def test_compare_raw
363
363
  diff = Kronk.compare "test/mocks/200_response.json",
364
364
  "test/mocks/200_response.xml",
365
- :with_headers => true,
365
+ :show_headers => true,
366
366
  :raw => true
367
367
 
368
368
  resp1 = Kronk.retrieve "test/mocks/200_response.json",
369
- :with_headers => true,
369
+ :show_headers => true,
370
370
  :raw => true
371
371
 
372
372
  resp2 = Kronk.retrieve "test/mocks/200_response.xml",
373
- :with_headers => true,
373
+ :show_headers => true,
374
374
  :raw => true
375
375
 
376
- exp_diff = Kronk::Diff.new resp1.selective_string(:with_headers => true),
377
- resp2.selective_string(:with_headers => true),
376
+ exp_diff = Kronk::Diff.new resp1.selective_string(:show_headers => true),
377
+ resp2.selective_string(:show_headers => true),
378
378
  :labels => [
379
379
  "test/mocks/200_response.json",
380
380
  "test/mocks/200_response.xml"
@@ -444,17 +444,17 @@ class TestKronk < Test::Unit::TestCase
444
444
  def test_compare_data
445
445
  diff = Kronk.compare "test/mocks/200_response.json",
446
446
  "test/mocks/200_response.xml",
447
- :with_headers => true
447
+ :show_headers => true
448
448
 
449
449
  resp1 = Kronk.retrieve "test/mocks/200_response.json",
450
- :with_headers => true
450
+ :show_headers => true
451
451
 
452
452
  resp2 = Kronk.retrieve "test/mocks/200_response.xml",
453
- :with_headers => true
453
+ :show_headers => true
454
454
 
455
455
  exp_diff = Kronk::Diff.new_from_data \
456
- resp1.selective_data(:with_headers => true),
457
- resp2.selective_data(:with_headers => true),
456
+ resp1.selective_data(:show_headers => true),
457
+ resp2.selective_data(:show_headers => true),
458
458
  :labels => [
459
459
  "test/mocks/200_response.json",
460
460
  "test/mocks/200_response.xml"
@@ -523,7 +523,7 @@ class TestKronk < Test::Unit::TestCase
523
523
 
524
524
 
525
525
  def test_compare_data_inst
526
- kronk = Kronk.new :with_headers => true
526
+ kronk = Kronk.new :show_headers => true
527
527
  diff = kronk.compare "test/mocks/200_response.json",
528
528
  "test/mocks/200_response.xml"
529
529
 
@@ -545,8 +545,8 @@ class TestKronk < Test::Unit::TestCase
545
545
  assert_equal nil, kronk.diff
546
546
 
547
547
  exp_diff = Kronk::Diff.new_from_data \
548
- resp2.selective_data(:with_headers => true),
549
- resp1.selective_data(:with_headers => true),
548
+ resp2.selective_data(:show_headers => true),
549
+ resp1.selective_data(:show_headers => true),
550
550
  :labels => [
551
551
  "test/mocks/200_response.json",
552
552
  "test/mocks/200_response.xml"
@@ -618,13 +618,13 @@ class TestKronk < Test::Unit::TestCase
618
618
  :proxy => "someproxy.com"
619
619
  },
620
620
  'withhdrs' => {
621
- :with_headers => %w{hdr1 hdr2 hdr3}
621
+ :show_headers => %w{hdr1 hdr2 hdr3}
622
622
  },
623
623
  'withstrhdrs' => {
624
- :with_headers => "hdr1"
624
+ :show_headers => "hdr1"
625
625
  },
626
626
  'withtruehdrs' => {
627
- :with_headers => true
627
+ :show_headers => true
628
628
  },
629
629
  'focus_data' => {
630
630
  :only_data => %w{path1 path2},
@@ -208,7 +208,7 @@ class TestResponse < Test::Unit::TestCase
208
208
 
209
209
  assert_equal "#{@json_resp.raw.split("\r\n\r\n")[0]}\r\n",
210
210
  @json_resp.selective_string(:no_body => true,
211
- :with_headers => true)
211
+ :show_headers => true)
212
212
  end
213
213
 
214
214
 
@@ -217,7 +217,7 @@ class TestResponse < Test::Unit::TestCase
217
217
 
218
218
  expected = "Content-Type: application/json; charset=utf-8\r\n\r\n#{body}"
219
219
  assert_equal expected,
220
- @json_resp.selective_string(:with_headers => "Content-Type")
220
+ @json_resp.selective_string(:show_headers => "Content-Type")
221
221
  end
222
222
 
223
223
 
@@ -227,12 +227,12 @@ class TestResponse < Test::Unit::TestCase
227
227
  expected = "Date: Fri, 03 Dec 2010 21:49:00 GMT\r\nContent-Type: application/json; charset=utf-8\r\n\r\n#{body}"
228
228
  assert_equal expected,
229
229
  @json_resp.selective_string(
230
- :with_headers => ["Content-Type", "Date"])
230
+ :show_headers => ["Content-Type", "Date"])
231
231
 
232
232
  expected = "Date: Fri, 03 Dec 2010 21:49:00 GMT\r\nContent-Type: application/json; charset=utf-8\r\n"
233
233
  assert_equal expected,
234
234
  @json_resp.selective_string(:no_body => true,
235
- :with_headers => ["Content-Type", "Date"])
235
+ :show_headers => ["Content-Type", "Date"])
236
236
  end
237
237
 
238
238
 
@@ -246,7 +246,7 @@ class TestResponse < Test::Unit::TestCase
246
246
 
247
247
  assert_equal "#{@json_resp.raw.split("\r\n\r\n")[0]}\r\n",
248
248
  @json_resp.selective_string(:no_body => true,
249
- :with_headers => true)
249
+ :show_headers => true)
250
250
  end
251
251
 
252
252
 
@@ -265,7 +265,7 @@ class TestResponse < Test::Unit::TestCase
265
265
  [{'content-type' => 'application/json; charset=utf-8'}, body]
266
266
 
267
267
  assert_equal expected,
268
- @json_resp.selective_data(:with_headers => "Content-Type")
268
+ @json_resp.selective_data(:show_headers => "Content-Type")
269
269
  end
270
270
 
271
271
 
@@ -278,7 +278,7 @@ class TestResponse < Test::Unit::TestCase
278
278
 
279
279
  assert_equal expected,
280
280
  @json_resp.selective_data(
281
- :with_headers => ["Content-Type", "Date"])
281
+ :show_headers => ["Content-Type", "Date"])
282
282
  end
283
283
 
284
284
 
@@ -291,7 +291,7 @@ class TestResponse < Test::Unit::TestCase
291
291
 
292
292
  assert_equal expected,
293
293
  @json_resp.selective_data(:no_body => true,
294
- :with_headers => ["Content-Type", "Date"])
294
+ :show_headers => ["Content-Type", "Date"])
295
295
  end
296
296
 
297
297
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: kronk
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.6.1
5
+ version: 1.6.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jeremie Castagna
@@ -169,7 +169,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
169
169
  requirements:
170
170
  - - ">="
171
171
  - !ruby/object:Gem::Version
172
- hash: 2162238022894922072
172
+ hash: -3702389185383603132
173
173
  segments:
174
174
  - 0
175
175
  version: "0"