htty 1.1.3 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.3
1
+ 1.1.4
@@ -21,24 +21,23 @@ class HTTY::CLI::Commands::QuerySet < HTTY::CLI::Command
21
21
  'Navigation'
22
22
  end
23
23
 
24
- # Returns the arguments for the command-line usage of the _query-set_
25
- # command.
24
+ # Returns the arguments for the command-line usage of the _query-set_ command.
26
25
  def self.command_line_arguments
27
- 'name value'
26
+ 'name [value [name [value]] ...]'
28
27
  end
29
28
 
30
29
  # Returns the help text for the _query-set_ command.
31
30
  def self.help
32
- "Sets a query-string parameter in the request's address"
31
+ "Sets query-string parameters in the request's address"
33
32
  end
34
33
 
35
34
  # Returns the extended help text for the _query-set_ command.
36
35
  def self.help_extended
37
- 'Sets a query-string parameter used for the request. Does not ' +
36
+ 'Sets one or more query-string parameters used for the request. Does not ' +
38
37
  "communicate with the host.\n" +
39
38
  "\n" +
40
- 'The name and value of the query-string parameter will be URL-encoded if ' +
41
- "necessary.\n" +
39
+ 'The name(s) and value(s) of the query-string parameter will be URL-' +
40
+ "encoded if necessary.\n" +
42
41
  "\n" +
43
42
  'The console prompt shows the address for the current request.'
44
43
  end
@@ -54,9 +53,25 @@ class HTTY::CLI::Commands::QuerySet < HTTY::CLI::Command
54
53
  def perform
55
54
  add_request_if_has_response do |request|
56
55
  self.class.notify_if_cookies_cleared request do
57
- request.query_set(*escape_or_warn_of_escape_sequences(arguments))
56
+ escaped_arguments = escape_or_warn_of_escape_sequences(arguments)
57
+ in_groups_of(2, escaped_arguments).each do |key_value|
58
+ request.query_set(*key_value)
59
+ end
60
+ request
58
61
  end
59
62
  end
60
63
  end
61
64
 
65
+ private
66
+
67
+ def in_groups_of(how_many, source)
68
+ groups = []
69
+ source = source.dup
70
+ (source.length / how_many).times do
71
+ groups << source.slice!(0, how_many)
72
+ end
73
+ groups << source unless source.empty?
74
+ groups
75
+ end
76
+
62
77
  end
data/lib/htty/request.rb CHANGED
@@ -376,25 +376,22 @@ public
376
376
 
377
377
  # Establishes a new #uri, with the specified _value_ for the query-string
378
378
  # parameter specified by _name_.
379
- def query_set(name, value)
380
- query = uri.query ? "&#{uri.query}&" : ''
381
- parameter = Regexp.new("&#{Regexp.escape name}=.+?&")
382
- if query =~ parameter
383
- new_query = value.nil? ?
384
- query.gsub(parameter, '&') :
385
- query.gsub(parameter, "&#{name}=#{value}&")
386
- else
387
- new_query = value.nil? ? query : "#{query}#{name}=#{value}"
388
- end
389
- new_query = new_query.gsub(/^&/, '').gsub(/&$/, '')
390
- new_query = nil if (new_query == '')
379
+ def query_set(name, value=nil)
380
+ entries = current_query_entries
381
+ add_or_replace_field(entries, name, value)
382
+ new_query = entries.empty? ? nil : entries.join('&')
391
383
  rebuild_uri :query => new_query
392
384
  end
393
385
 
394
386
  # Establishes a new #uri, without the query-string parameter specified by
395
387
  # _name_.
396
388
  def query_unset(name)
397
- query_set name, nil
389
+ return unless uri.query
390
+ entries = current_query_entries
391
+ entries.delete_if do |entry|
392
+ entry =~ field_matcher(name)
393
+ end
394
+ rebuild_uri :query => entries.join('&')
398
395
  end
399
396
 
400
397
  # Establishes a new #uri without a query string.
@@ -462,16 +459,40 @@ protected
462
459
 
463
460
  private
464
461
 
462
+ def add_or_replace_field(chunks, name, value)
463
+ new_entry = name + (value.nil? ? '' : "=#{value}")
464
+ has_matched = false
465
+ chunks.each do |chunk|
466
+ if chunk =~ field_matcher(name)
467
+ if has_matched
468
+ chunks.delete chunk
469
+ else
470
+ chunks[chunks.index(chunk)] = new_entry
471
+ has_matched = true
472
+ end
473
+ end
474
+ end
475
+ chunks << new_entry unless has_matched
476
+ end
477
+
465
478
  def authority
466
479
  self.class.build_authority :userinfo => uri.userinfo,
467
480
  :host => uri.host,
468
481
  :port => uri.port
469
482
  end
470
483
 
484
+ def current_query_entries
485
+ uri.query ? uri.query.split('&') : []
486
+ end
487
+
471
488
  def establish_content_length
472
489
  header_set 'Content-Length', body.to_s.length
473
490
  end
474
491
 
492
+ def field_matcher(name)
493
+ Regexp.new "^#{Regexp.escape name}=?.*"
494
+ end
495
+
475
496
  def request!(method)
476
497
  request = response ? dup_without_response : self
477
498
  request.instance_variable_set '@request_method', method
File without changes
@@ -0,0 +1,2 @@
1
+ *** Welcome to  htty , the HTTP TTY. Heck To The Yeah!
2
+ *** Happy Trails To You!
@@ -0,0 +1,2 @@
1
+ *** Welcome to  htty , the HTTP TTY. Heck To The Yeah!
2
+ *** Happy Trails To You!
@@ -0,0 +1 @@
1
+ exit
File without changes
@@ -0,0 +1,2 @@
1
+ *** Welcome to  htty , the HTTP TTY. Heck To The Yeah!
2
+ *** Happy Trails To You!
@@ -0,0 +1,2 @@
1
+ *** Welcome to  htty , the HTTP TTY. Heck To The Yeah!
2
+ *** Happy Trails To You!
@@ -0,0 +1 @@
1
+ quit
@@ -0,0 +1,46 @@
1
+ require 'spec'
2
+ require 'pathname'
3
+
4
+ all_dir = Pathname.new("#{File.dirname __FILE__}/scenarios")
5
+ Dir.glob "#{all_dir}/**/*/" do |this_dir|
6
+ relative_path = Pathname.new(this_dir).relative_path_from(all_dir).to_s
7
+ context_name = relative_path.split('/').join(' ').gsub('_', ' ')
8
+ describe context_name do
9
+ define_method :content do |filename|
10
+ path = "#{this_dir}/#{filename}"
11
+ return nil unless File.file?(path)
12
+ content = File.read(path)
13
+ return nil if content.empty?
14
+ content
15
+ end
16
+
17
+ define_method :run_and_capture do |options={}|
18
+ stdin_filename = "#{this_dir}/stdin"
19
+ htty_filename = "#{File.dirname __FILE__}/../../bin/htty"
20
+ stderr_target = options[:combine_stdout_and_stderr] ?
21
+ '&1' :
22
+ "#{this_dir}/actual_stderr"
23
+ arguments = "#{content 'arguments'} 2>#{stderr_target}"
24
+ stdout_filename = "#{this_dir}/actual_stdout"
25
+ system "cat #{stdin_filename} | " +
26
+ "#{htty_filename} #{arguments} > " +
27
+ "#{this_dir}/actual_stdout"
28
+ end
29
+
30
+ it 'should produce the expected stdout' do
31
+ run_and_capture
32
+ content('actual_stdout').should == content('expected_stdout')
33
+ end
34
+
35
+ it 'should produce the expected stderr' do
36
+ run_and_capture
37
+ content('actual_stderr').should == content('expected_stderr')
38
+ end
39
+
40
+ it 'should produce the expected combined stdout and stderr' do
41
+ run_and_capture :combine_stdout_and_stderr => true
42
+ content('actual_stdout_and_stderr').should ==
43
+ content('expected_stdout_and_stderr')
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec'
2
+ require File.expand_path("#{File.dirname __FILE__}/../../../../../lib/htty/cli")
3
+
4
+ describe HTTY::CLI::Commands::QuerySet do
5
+ before :all do
6
+ @session = HTTY::Session.new('')
7
+ end
8
+
9
+ describe 'with one argument' do
10
+ it 'should assign a single key' do
11
+ query_set = create_query_set_and_perform('test')
12
+ query_set.session.requests.last.uri.query.should == 'test'
13
+ end
14
+ end
15
+
16
+ describe 'with two arguments' do
17
+ it 'should assign a key-value pair' do
18
+ query_set = create_query_set_and_perform('test', 'true')
19
+ query_set.session.requests.last.uri.query.should == 'test=true'
20
+ end
21
+ end
22
+
23
+ describe 'with three arguments' do
24
+ it 'should assign a key-value pair and a valueless key' do
25
+ query_set = create_query_set_and_perform('test', 'true', 'more')
26
+ query_set.session.requests.last.uri.query.should == 'test=true&more'
27
+ end
28
+ end
29
+
30
+ describe 'with four arguments' do
31
+ it 'should assign two key-value pairs' do
32
+ query_set = create_query_set_and_perform('test', 'true', 'more', 'false')
33
+ query_set.session.requests.last.uri.query.should == 'test=true&more=false'
34
+ end
35
+ end
36
+
37
+ describe 'with duplicate keys' do
38
+ it 'should replace existing key' do
39
+ @session.requests.last.uri.query = 'test=true'
40
+ query_set = create_query_set_and_perform('test', 'false')
41
+ query_set.session.requests.last.uri.query.should == 'test=false'
42
+ end
43
+
44
+ it 'should maintain field location' do
45
+ @session.requests.last.uri.query = 'test=true&more=true'
46
+ query_set = create_query_set_and_perform('test', 'false')
47
+ query_set.session.requests.last.uri.query.should == 'test=false&more=true'
48
+ end
49
+
50
+ it 'should replace multiple instances with one' do
51
+ @session.requests.last.uri.query = 'test=true&more=true&test=true'
52
+ query_set = create_query_set_and_perform('test', 'false')
53
+ query_set.session.requests.last.uri.query.should == 'test=false&more=true'
54
+ end
55
+
56
+ it 'should play nice with nested fields' do
57
+ @session.requests.last.uri.query = 'test[my][]=true'
58
+ query_set = create_query_set_and_perform('test[my][]', 'false')
59
+ query_set.session.requests.last.uri.query.should == 'test[my][]=false'
60
+ end
61
+ end
62
+
63
+ def create_query_set_and_perform(*arguments)
64
+ query_set = HTTY::CLI::Commands::QuerySet.new(:session => @session,
65
+ :arguments => arguments)
66
+ query_set.perform
67
+ query_set
68
+ end
69
+
70
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 1
8
- - 3
9
- version: 1.1.3
8
+ - 4
9
+ version: 1.1.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nils Jonsson
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-12 00:00:00 -05:00
17
+ date: 2010-10-16 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -169,6 +169,16 @@ files:
169
169
  - lib/htty/response.rb
170
170
  - lib/htty/session.rb
171
171
  - lib/htty.rb
172
+ - spec/system/scenarios/exit/actual_stderr
173
+ - spec/system/scenarios/exit/actual_stdout
174
+ - spec/system/scenarios/exit/expected_stdout
175
+ - spec/system/scenarios/exit/stdin
176
+ - spec/system/scenarios/quit/actual_stderr
177
+ - spec/system/scenarios/quit/actual_stdout
178
+ - spec/system/scenarios/quit/expected_stdout
179
+ - spec/system/scenarios/quit/stdin
180
+ - spec/system/scenarios_spec.rb
181
+ - spec/unit/htty/cli/commands/query_set_spec.rb
172
182
  - spec/unit/htty/cli_spec.rb
173
183
  - spec/unit/htty/ordered_hash_spec.rb
174
184
  - spec/unit/htty/request_spec.rb