mosquito 0.1.3 → 0.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/CHANGELOG CHANGED
@@ -1,3 +1,12 @@
1
+ == 0.1.4 - The glass slipper release
2
+
3
+ * Compatibility with Camping 2.0 (development version) and Camping 1.5
4
+ * Fix that newer Rails assert_select wants to know content_type
5
+ * Final solution to the fixture problems. We now setup and teardown properly.
6
+ * Fixed a bug that would occur when using an absolute URL (with scheme and host)
7
+ and query strings.
8
+ * Do not use eval for running the application because it fumbles the backtrace
9
+
1
10
  == 0.1.3 - The little fairy release
2
11
 
3
12
  * You can use an absolute URL with scheme and all instead of path-only abbreviation
@@ -15,7 +24,8 @@
15
24
  * for querystrings
16
25
  * for postvars
17
26
  * and yes, for uploads too
18
- * On that note, added a Mosquito::MockUpload to quickly simulate an uploaded file. The file will be filled with random text, so roll your own if you need concrete file content.
27
+ * On that note, added a Mosquito::MockUpload to quickly simulate an uploaded file. The file will be filled with random text,
28
+ so roll your own if you need concrete file content.
19
29
  * We are Camping 1.5 compatible
20
30
  * You can now do 'test "should do this"' and pass a block of assertions.
21
31
  * More tests for better coverage of mosquito.rb
@@ -18,3 +18,5 @@ test/test_blog.rb
18
18
  test/test_helpers.rb
19
19
  test/test_mock_request.rb
20
20
  test/test_mock_upload.rb
21
+ test/test_fixtures.rb
22
+
data/Rakefile CHANGED
@@ -1,4 +1,5 @@
1
1
  $: << 'lib'
2
+ $:.reject! { |e| e.include? 'TextMate' }
2
3
 
3
4
  require 'rubygems'
4
5
  require 'hoe'
@@ -16,6 +17,10 @@ Hoe.new('Mosquito', Mosquito::VERSION) do |p|
16
17
  p.summary = "A Camping test library."
17
18
  p.changes = p.paragraphs_of('CHANGELOG', 0..1).join("\n\n")
18
19
  p.url = "http://mosquito.rubyforge.org"
20
+ p.remote_rdoc_dir = '' # Release to root on rubyforge
21
+ # We need that so that Hoe does not grab Camping's own distro tests
22
+ p.test_globs = ['test/test_*.rb']
23
+ p.rsync_args << ' --exclude=statsvn/'
19
24
  p.rdoc_pattern = /README|CHANGELOG|mosquito/
20
25
  p.clean_globs = ['**.log', 'coverage', 'coverage.data', 'test/test.log', 'email.txt']
21
26
  p.extra_deps = ['activerecord', 'activesupport', 'camping']
@@ -38,3 +43,22 @@ begin
38
43
  end
39
44
  rescue LoadError
40
45
  end
46
+
47
+ require 'rake/testtask'
48
+
49
+
50
+ multicamp_tasks = Dir.glob(File.dirname(__FILE__) + '/test/camping-dist/camping-*/lib').sort.map do | path |
51
+ distname = File.basename(File.dirname(path))
52
+ libs = path[(File.dirname(__FILE__) + '/').length..-1]
53
+
54
+ desc "Run tests with #{distname}"
55
+ Rake::TestTask.new("test-with-#{distname}") do |t|
56
+ t.libs << "test" << libs
57
+ t.pattern = 'test/test_*.rb'
58
+ t.verbose = true
59
+ end
60
+ "test-with-#{distname}"
61
+ end
62
+
63
+ desc "Run with multiple Camping versions"
64
+ task :multicamp => multicamp_tasks
@@ -11,7 +11,7 @@ stringio
11
11
  ).each { |lib| require lib }
12
12
 
13
13
  module Mosquito
14
- VERSION = '0.1.3'
14
+ VERSION = '0.1.4'
15
15
 
16
16
  # For various methods that need to generate random text
17
17
  def self.garbage(amount) #:nodoc:
@@ -23,6 +23,9 @@ module Mosquito
23
23
  str.join
24
24
  end
25
25
 
26
+ # URL escape on both Camping versions
27
+ def self.esc(t); Camping.escape(t.to_s) rescue Rack::Utils.escape(t.to_s); end
28
+
26
29
  # Will be raised if you try to test for something Camping does not support.
27
30
  # Kind of a safeguard in the deep ocean of metaified Ruby goodness.
28
31
  class SageAdvice < RuntimeError; end
@@ -36,60 +39,44 @@ module Mosquito
36
39
  end
37
40
 
38
41
  def self.unstash #:nodoc:
39
- x, @stashed = @stashed, nil; x
42
+ (x, @stashed = @stashed, nil).shift
43
+ end
44
+
45
+ module Dusty
46
+ ##
47
+ # From Jay Fields.
48
+ #
49
+ # Allows tests to be specified as a block.
50
+ #
51
+ # test "should do this and that" do
52
+ # ...
53
+ # end
54
+
55
+ def test(name, &block)
56
+ test_name = :"test_#{name.gsub(' ','_')}"
57
+ raise ArgumentError, "#{test_name} is already defined" if self.instance_methods.include? test_name.to_s
58
+ define_method test_name, &block
59
+ end
40
60
  end
41
61
  end
42
62
 
43
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ":memory:")
63
+ returning(:adapter => 'sqlite3', :database => ":memory:") do | config |
64
+ ActiveRecord::Base.establish_connection config
65
+ ActiveRecord::Base.configurations['test'] = config.stringify_keys
66
+ end
67
+
44
68
  ActiveRecord::Base.logger = Logger.new("test/test.log") rescue Logger.new("test.log")
45
69
 
46
70
  # This needs to be set relative to the file where the test comes from, NOT relative to the
47
71
  # mosquito itself
48
72
  Test::Unit::TestCase.fixture_path = "test/fixtures/"
49
73
 
50
- class Test::Unit::TestCase #:nodoc:
51
- def create_fixtures(*table_names)
52
- if block_given?
53
- self.class.fixtures(*table_names) { |*anything| yield(*anything) }
54
- else
55
- self.class.fixtures(*table_names)
56
- end
57
- end
58
-
59
- def self.fixtures(*table_names)
60
- if block_given?
61
- Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
62
- else
63
- Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
64
- end
65
- end
66
-
67
- ##
68
- # From Jay Fields.
69
- #
70
- # Allows tests to be specified as a block.
71
- #
72
- # test "should do this and that" do
73
- # ...
74
- # end
75
-
76
- def self.test(name, &block)
77
- test_name = :"test_#{name.gsub(' ','_')}"
78
- raise ArgumentError, "#{test_name} is already defined" if self.instance_methods.include? test_name.to_s
79
- define_method test_name, &block
80
- end
81
-
82
- # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
83
- self.use_transactional_fixtures = true
84
- # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
85
- self.use_instantiated_fixtures = false
86
- end
87
-
88
74
  # Mock request is used for composing the request body and headers
89
75
  class Mosquito::MockRequest
90
76
  # Should be a StringIO. However, you got some assignment methods that will
91
77
  # stuff it with encoded parameters for you
92
78
  attr_accessor :body
79
+ attr_reader :headers
93
80
 
94
81
  DEFAULT_HEADERS = {
95
82
  'SERVER_NAME' => 'test.host',
@@ -114,10 +101,11 @@ class Mosquito::MockRequest
114
101
  'HTTP_CONNECTION' => 'keep-alive',
115
102
  'REQUEST_METHOD' => 'GET',
116
103
  }
117
-
104
+
105
+
118
106
  def initialize
119
107
  @headers = DEFAULT_HEADERS.with_indifferent_access # :-)
120
- @body = StringIO.new('hello Camping')
108
+ @body = StringIO.new('')
121
109
  end
122
110
 
123
111
  # Returns the hash of headers
@@ -203,9 +191,27 @@ class Mosquito::MockRequest
203
191
  "msqto-" + Mosquito::garbage(16)
204
192
  end
205
193
 
194
+ # Return a hash that can be used as a Rack request
195
+ def to_rack_request
196
+ returning({}) do | inp |
197
+ inp.merge! @headers
198
+ inp.merge! 'rack.input' => @body,
199
+ 'rack.version' => [0,1],
200
+ 'rack.errors' => STDERR,
201
+ 'rack.url_scheme' => 'http',
202
+ 'CONTENT_LENGTH' => @body.string.length.to_s # Rack throws an error when it does not proper clen
203
+ end
204
+ end
205
+
206
+ # Return an array of Camping arguments
207
+ def to_camping_args
208
+ [@body, self]
209
+ end
210
+
206
211
  private
212
+
207
213
  # Quickly URL-escape something
208
- def esc(t); Camping.escape(t.to_s);end
214
+ def esc(t); Mosquito.esc(t); end
209
215
 
210
216
  # Extracts an array of values from a deeply-nested hash
211
217
  def extract_values(hash_or_a)
@@ -243,7 +249,7 @@ class Mosquito::MockRequest
243
249
  def uploaded_file_segment(key, upload_io, boundary)
244
250
  <<-EOF
245
251
  --#{boundary}\r
246
- Content-Disposition: form-data; name="#{key}"; filename="#{Camping.escape(upload_io.original_filename)}"\r
252
+ Content-Disposition: form-data; name="#{key}"; filename="#{Mosquito.esc(upload_io.original_filename)}"\r
247
253
  Content-Type: #{upload_io.content_type}\r
248
254
  Content-Length: #{upload_io.size}\r
249
255
  \r
@@ -365,11 +371,16 @@ module Mosquito::Proboscis #:nodoc:
365
371
  end
366
372
 
367
373
  module Camping
368
-
374
+
375
+ # The basic Mosquito-wielding test case with some infrastructure
369
376
  class Test < Test::Unit::TestCase
370
-
371
- def test_dummy; end #:nodoc
372
-
377
+ class << self; include Mosquito::Dusty; end
378
+
379
+ def test_default; end #:nodoc
380
+
381
+ # This is needed because Rails fixtures actually try to setup twice
382
+ def self.use_transactional_fixtures; false; end
383
+
373
384
  # The reverse of the reverse of the reverse of assert(condition)
374
385
  def deny(condition, message='')
375
386
  assert !condition, message
@@ -408,11 +419,15 @@ module Camping
408
419
  class WebTest < Test
409
420
 
410
421
  # Gives you access to the instance variables assigned by the controller
411
- attr_reader :assigns
412
-
413
- def test_dummy; end #:nodoc
422
+ def assigns(key = nil)
423
+ @assigns ||= Camping::H.new
424
+ key ? @assigns[key] : @assigns
425
+ end
426
+
427
+ def test_default; end #:nodoc
414
428
 
415
429
  def setup
430
+ super
416
431
  @class_name_abbr = self.class.name.gsub(/^Test/, '')
417
432
  @request = Mosquito::MockRequest.new
418
433
  @cookies, @response, @assigns = {}, {}, {}
@@ -424,29 +439,29 @@ module Camping
424
439
  end
425
440
 
426
441
  # Send a POST request to a URL. All requests except GET will allow
427
- # setting verbatim URL-encoded parameters as the third argument instead
442
+ # setting verbatim request body as the third argument instead
428
443
  # of a hash.
429
444
  def post(url, post_vars={})
430
445
  send_request url, post_vars, 'POST'
431
446
  end
432
447
 
433
448
  # Send a DELETE request to a URL. All requests except GET will allow
434
- # setting verbatim URL-encoded parameters as the third argument instead
449
+ # setting verbatim request body as the third argument instead
435
450
  # of a hash.
436
451
  def delete(url, vars={})
437
452
  send_request url, vars, 'DELETE'
438
453
  end
439
454
 
440
455
  # Send a PUT request to a URL. All requests except GET will allow
441
- # setting verbatim URL-encoded parameters as the third argument instead
456
+ # setting verbatim request body as the third argument instead
442
457
  # of a hash.
443
458
  def put(url, vars={})
444
459
  send_request url, vars, 'PUT'
445
460
  end
446
461
 
447
- # Send any request. We will try to guess what you meant - if there are uploads to be
462
+ # Send the request. We will try to guess what you meant - if there are uploads to be
448
463
  # processed it's not going to be a GET, that's for sure.
449
- def send_request(url, post_vars, method)
464
+ def send_request(composite_url, post_vars, method)
450
465
 
451
466
  if method.to_s.downcase == "get"
452
467
  @request.query_string_params = post_vars
@@ -455,41 +470,56 @@ module Camping
455
470
  end
456
471
 
457
472
  # If there is some stuff in the URL to be used as a query string, why ignore it?
458
- url, qs_from_url = url.split(/\?/)
459
-
460
- relativize_url!(url)
473
+ relative_url, qs_from_url = relativize_url(composite_url)
461
474
 
462
475
  @request.append_to_query_string(qs_from_url) if qs_from_url
463
476
 
464
477
  # We do allow the user to override that one
465
478
  @request['REQUEST_METHOD'] = method
466
479
 
480
+ # Make the Camping app route our request
467
481
  @request['SCRIPT_NAME'] = '/' + @class_name_abbr.downcase
468
- @request['PATH_INFO'] = '/' + url
469
482
 
483
+ # PATH_INFO is used for internal routing by Camping. We have to always pass
484
+ # the leading slash
485
+ @request['PATH_INFO'] = ensure_one_leading_slash(relative_url)
486
+
487
+ # We need to munge this because the PATH_INFO has changed
470
488
  @request['REQUEST_URI'] = [@request.SCRIPT_NAME, @request.PATH_INFO].join('').squeeze('/')
471
489
  unless @request['QUERY_STRING'].blank?
472
490
  @request['REQUEST_URI'] += ('?' + @request['QUERY_STRING'])
473
491
  end
474
492
 
475
493
  if @cookies
476
- @request['HTTP_COOKIE'] = @cookies.map {|k,v| "#{k}=#{Camping.escape(v)}" }.join('; ')
494
+ @request['HTTP_COOKIE'] = @cookies.map {|k,v| "#{k}=#{Mosquito.esc(v)}" }.join('; ')
477
495
  end
478
496
 
497
+ # Get the Camping app
498
+ app_module = Kernel.const_get(@class_name_abbr)
499
+
479
500
  # Inject the proboscis if we haven't already done so
480
- pr = Mosquito::Proboscis
481
- eval("#{@class_name_abbr}.send(:include, pr) unless #{@class_name_abbr}.ancestors.include?(pr)")
501
+ app_module.send(:include, Mosquito::Proboscis) unless app_module.ancestors.include?(Mosquito::Proboscis)
502
+
503
+ # Run the request.
504
+ @response = if app_module.respond_to?(:run) # Camping < 2.0
505
+ app_module.run(*@request.to_camping_args)
506
+ else # Camping 2.0
507
+ app_module.call(@request.to_rack_request).pop # Serve a Rack::Response object
508
+ end
509
+
510
+ # Add content_type accessor for Rails assert_select
511
+ eval("class << @response; def content_type; @headers['Content-Type']; end; end")
482
512
 
483
- # Run the request
484
- @response = eval("#{@class_name_abbr}.run @request.body, @request")
513
+ # Downgrade the disguised Mab into a string
514
+ @response.body = @response.body.to_s
485
515
  @assigns = Mosquito::unstash
486
516
 
487
517
  # We need to restore the cookies separately so that the app
488
518
  # restores our session on the next request. We retrieve cookies and
489
519
  # the session in their assigned form instead of parsing the headers and
490
520
  # doing a deserialization cycle
491
- @cookies = @assigns[:cookies] || H[{}]
492
- @state = @assigns[:state] || H[{}]
521
+ @cookies = @assigns.cookies || H[{}]
522
+ @state = @assigns.state || H[{}]
493
523
 
494
524
  if @response.headers['X-Sendfile']
495
525
  @response.body = File.read(@response.headers['X-Sendfile'])
@@ -547,8 +577,13 @@ module Camping
547
577
 
548
578
  # Checks that Camping sent us a cookie to attach a session
549
579
  def assert_session_started
550
- assert_not_nil @cookies["camping_sid"],
551
- "The session ID cookie was empty although session should have started"
580
+ if Camping.respond_to?(:call) # Camping 2.0 prefers cookie sessions
581
+ assert_not_nil @cookies["camping_hash"],
582
+ "The session hash cookie was empty although session should have started"
583
+ else
584
+ assert_not_nil @cookies["camping_sid"],
585
+ "The session ID cookie was empty although session should have started"
586
+ end
552
587
  end
553
588
 
554
589
  # The reverse of +assert_session_started+
@@ -558,26 +593,33 @@ module Camping
558
593
  end
559
594
 
560
595
  private
596
+ # Ensure that we have a URL with one leading slash
597
+ def ensure_one_leading_slash(url)
598
+ ['/', url.to_s].join.gsub(/^(\/+)/, '/')
599
+ end
600
+
561
601
  def extract_redirection_url
562
- loc = @response.headers['Location']
563
- path_seg = @response.headers['Location'].path.gsub(%r!/#{@class_name_abbr.downcase}!, '')
602
+ # We parse once more because Camping 2 sends a String (Rack does not want a URI) and 1.5 sends URI
603
+ loc = URI.parse(@response.headers['Location'].to_s)
604
+ path_seg = loc.path.gsub(%r!/#{@class_name_abbr.downcase}!, '')
564
605
  loc.query ? (path_seg + "?" + loc.query).to_s : path_seg.to_s
565
606
  end
566
607
 
567
- def relativize_url!(url)
568
- return unless url =~ /^([a-z]+):\//
569
- p = URI.parse(url)
570
- unless p.host == @request.domain
608
+ def relativize_url(url)
609
+ parsed = URI.parse(url)
610
+ if !parsed.scheme.blank? && parsed.host != @request.domain
571
611
  raise ::Mosquito::NonLocalRequest,
572
- "You tried to callout to #{p} which is outside of the test domain"
612
+ "You tried to callout to '#{parsed}' which is outside of the test domain (#{@request.domain})"
573
613
  end
574
- url.replace(p.path + (p.query.blank ? '' : "?#{p.query}"))
614
+ # Now remove the path
615
+ parsed.path.gsub!(/^\/#{@class_name_abbr.downcase}\//, '/')
616
+ [parsed.path, parsed.query]
575
617
  end
576
618
  end
577
619
 
578
620
  # Used to test the models - no infrastructure will be created for running the request
579
621
  class ModelTest < Test
580
- def test_dummy; end #:nodoc
622
+ def test_default; end #:nodoc
581
623
  end
582
624
 
583
625
  # Deprecated but humane
@@ -1,5 +1,4 @@
1
1
  #!/usr/local/bin/ruby -rubygems
2
- require 'camping'
3
2
 
4
3
  Camping.goes :Bare
5
4
 
@@ -1,15 +1,19 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'rubygems'
4
- gem 'camping', '>=1.4'
5
- require 'camping'
6
- require 'camping/session'
3
+ begin
4
+ if Camping.respond_to?(:call) # Camping 2
5
+ require 'camping/ar/session'
6
+ else
7
+ require 'camping/session'
8
+ end
9
+ end
7
10
 
8
11
  Camping.goes :Blog
9
12
 
10
- require File.dirname(__FILE__) + '/blog/models'
11
- require File.dirname(__FILE__) + '/blog/views'
12
- require File.dirname(__FILE__) + '/blog/controllers'
13
+ $:.unshift File.dirname(__FILE__)
14
+ require 'blog/models'
15
+ require 'blog/views'
16
+ require 'blog/controllers'
13
17
 
14
18
  Blog::Models.schema do
15
19
  create_table :blog_posts, :force => true do |t|
@@ -152,6 +152,10 @@ module Blog::Controllers
152
152
  end
153
153
 
154
154
  class Restafarian < R('/rest')
155
+ def get
156
+ return 'Called get'
157
+ end
158
+
155
159
  def delete
156
160
  return "Called delete"
157
161
  end
@@ -17,6 +17,7 @@ module Blog::Views
17
17
  end
18
18
 
19
19
  def index
20
+ h1 "Welcome to the blog"
20
21
  if @posts.empty?
21
22
  p 'No posts found.'
22
23
  p { a 'Add', :href => R(Add) }
@@ -1,6 +1,3 @@
1
- require 'rubygems'
2
- require 'camping'
3
-
4
1
  Camping.goes :ParsingArrays
5
2
 
6
3
  class ParsingArrays::Controllers::Klonk < ParsingArrays::Controllers::R('/')
@@ -1,3 +1,4 @@
1
+ require File.dirname(__FILE__) + "/switcher"
1
2
  require File.dirname(__FILE__) + "/../lib/mosquito"
2
3
  require File.dirname(__FILE__) + "/../public/bare"
3
4
 
@@ -19,14 +20,14 @@ class TestBare < Camping::FunctionalTest
19
20
  assert_match_body %r!Charles!
20
21
  end
21
22
 
22
- test "should get page with success" do
23
+ test "should get sample page with success" do
23
24
  get '/sample'
24
25
  assert_response :success
25
26
  assert_no_session
26
27
  assert_match_body %r!<p>A sample page</p>!
27
28
  end
28
29
 
29
- def test_request_uri_preserves_query_vars
30
+ test "should place the passed query params in the querystring" do
30
31
  get '/sample', :somevar => 10
31
32
  assert_equal '/bare/sample?somevar=10', @request['REQUEST_URI']
32
33
  end
@@ -36,27 +37,47 @@ class TestBare < Camping::FunctionalTest
36
37
  assert_no_match_body /Rubber\s+Bubblegum\s+Burt Reynolds\s+Hippopotamus/
37
38
  end
38
39
 
40
+ test "should ensure single leading slash" do
41
+ assert_equal '/foo', self.send(:ensure_one_leading_slash, "foo")
42
+ assert_equal '/foo', self.send(:ensure_one_leading_slash, "//////foo")
43
+ assert_equal '/foo/bar', self.send(:ensure_one_leading_slash, "//////foo/bar")
44
+ assert_equal '/', self.send(:ensure_one_leading_slash, nil)
45
+ end
46
+
39
47
  test "should return error" do
40
- get '/error'
41
- assert_response :error
48
+ if Camping.respond_to?(:call)
49
+ # Camping on Rack reraises the errors which are caught by Rack's exception displayer
50
+ assert_raise(RuntimeError) { get '/error' }
51
+ else
52
+ assert_nothing_raised { get '/error' }
53
+ assert_response :error
54
+ end
42
55
  end
43
56
 
44
57
  test "should return 404 error" do
45
58
  get '/error404'
46
59
  assert_response 404
47
60
  end
48
-
49
- def test_assigning_verbatim_post_payload
61
+
62
+ test "should be able to assign verbatim POST payload" do
50
63
  post '/sample', 'foo=bar&plain=flat'
51
64
  @request.body.rewind
52
65
  assert_equal 'foo=bar&plain=flat', @request.body.read
66
+ assert_equal( {"foo"=>"bar", "plain"=>"flat"}, @assigns.input)
53
67
  end
54
-
68
+
55
69
  test "should redirect" do
56
70
  get '/redirect'
57
71
  assert_redirected_to '/faq'
58
72
  end
59
73
 
74
+ test "should coerce Mab in response to a String" do
75
+ assert_nothing_raised { get '/non-existing-something-something' }
76
+ assert_nothing_raised do
77
+ assert_kind_of String, @response.body
78
+ end
79
+ end
80
+
60
81
  # test "should send file" do
61
82
  # get '/file'
62
83
  # assert_response :success
@@ -1,3 +1,4 @@
1
+ require File.dirname(__FILE__) + "/switcher"
1
2
  require File.dirname(__FILE__) + "/../lib/mosquito"
2
3
  require File.dirname(__FILE__) + "/../public/blog"
3
4
  Blog.create
@@ -19,7 +20,7 @@ class TestBlog < Camping::FunctionalTest
19
20
  end
20
21
  end
21
22
 
22
- def test_cookies
23
+ def test_cookies_should_be_stored_and_loaded
23
24
  get '/cookies'
24
25
  assert_cookie 'awesome_cookie', 'camping for good'
25
26
  assert_equal @state.awesome_data, 'camping for good'
@@ -27,7 +28,7 @@ class TestBlog < Camping::FunctionalTest
27
28
  assert_equal @state.awesome_data, 'camping for good'
28
29
  end
29
30
 
30
- def test_cookies_persisted_across_requests_and_escaping_properly_handled
31
+ def test_cookies_should_be_persisted_across_requests_with_proper_escaping
31
32
  @cookies["asgård"] = 'Wøbble'
32
33
  get '/cookies'
33
34
  assert_equal 'asgård=W%C3%B8bble', @request['HTTP_COOKIE'], "The cookie val shouldbe escaped"
@@ -39,27 +40,25 @@ class TestBlog < Camping::FunctionalTest
39
40
  assert_equal 'Wøbble', @cookies["asgård"]
40
41
  end
41
42
 
42
- def test_index
43
+ def test_get_without_arguments_should_just_call_the_app_index
43
44
  get
44
45
  assert_response :success
45
- assert_match_body %r!>blog<!
46
+ assert_match_body /Welcome to the blog/
46
47
  assert_not_equal @state.awesome_data, 'camping for good'
47
- assert_kind_of Array, @assigns[:posts]
48
- assert_kind_of Post, assigns[:posts].first
49
48
  end
50
49
 
51
- def test_view
50
+ def test_calling_view_should_fetch_view
52
51
  get '/view/1'
53
52
  assert_response :success
54
53
  assert_match_body %r!The quick fox jumped over the lazy dog!
55
54
  end
56
55
 
57
- def test_styles
58
- get 'styles.css'
56
+ def test_calling_styles_should_give_us_the_CSS
57
+ get '/styles.css'
59
58
  assert_match_body %r!Utopia!
60
59
  end
61
60
 
62
- def test_edit_should_require_login
61
+ def test_calling_edit_should_render_login
63
62
  get '/edit/1'
64
63
  assert_response :success
65
64
  assert_match_body 'login'
@@ -139,7 +138,7 @@ class TestBlog < Camping::FunctionalTest
139
138
  assert_equal 'Called put', @response.body
140
139
  end
141
140
 
142
- def calling_with_an_absolute_url_should_relativize
141
+ def test_calling_with_an_absolute_url_should_relativize
143
142
  assert_equal 'test.host', @request.domain
144
143
  put 'http://test.host/blog/rest'
145
144
  assert_nothing_raised do
@@ -147,11 +146,21 @@ class TestBlog < Camping::FunctionalTest
147
146
  end
148
147
  end
149
148
 
149
+ def test_calling_with_an_absolute_url_and_querystring_should_relativize_and_pass_parameters
150
+ assert_equal 'test.host', @request.domain
151
+ get 'http://test.host/blog/rest?one=2&foo=bar'
152
+ assert_nothing_raised do
153
+ assert_equal 'Called get', @response.body
154
+ end
155
+ assert_equal({"one"=>'2', "foo"=>"bar"}, @assigns.input)
156
+ end
157
+
150
158
  def test_calling_with_an_absolute_url_outside_of_the_default_test_host_must_raise
151
159
  assert_equal 'test.host', @request.domain
152
- assert_raise(Mosquito::NonLocalRequest) do
153
- put 'http://yahoo.com/blog/rest'
160
+ non_local = assert_raise(Mosquito::NonLocalRequest) do
161
+ get 'http://yahoo.com/blog/rest'
154
162
  end
163
+ assert_equal "You tried to callout to 'http://yahoo.com/blog/rest' which is outside of the test domain (test.host)", non_local.message
155
164
  end
156
165
 
157
166
  def test_calling_with_an_absolute_url_outside_of_the_custom_test_host_must_raise
@@ -0,0 +1,72 @@
1
+ require File.dirname(__FILE__) + "/switcher"
2
+ require File.dirname(__FILE__) + '/test_helpers'
3
+ require File.dirname(__FILE__) + '/../public/blog'
4
+
5
+ Blog.create
6
+ include Blog::Models
7
+
8
+ class SomeTest < Camping::Test
9
+ fixtures :blog_posts
10
+ def setup
11
+ super
12
+ @parent_done = true
13
+ end
14
+
15
+ def test_setup_has_ran
16
+ assert_not_nil @parent_done, "This variable is set in setup"
17
+ end
18
+ end
19
+
20
+ module FixtureReloadingTests
21
+ def test_fixtures_reloaded_after_run_say_a
22
+ assert_equal 2, Post.count, "The test should start with 2 preloaded Posts"
23
+ Post.delete_all
24
+ end
25
+
26
+ def test_fixtures_reloaded_after_run_say_b
27
+ assert_equal 2, Post.count, "The test should start with 2 preloaded Posts"
28
+ Post.delete_all
29
+ end
30
+ end
31
+
32
+ class Sec < SomeTest; end
33
+ class Third < SomeTest; end
34
+ class Fourth < SomeTest
35
+ include FixtureReloadingTests
36
+ end
37
+
38
+ # This exploits something that Rails considers an edge case but what is essential for us
39
+ # - inheriting test cases with setup that also use fixtures
40
+ class FixturedTest < Camping::WebTest
41
+ def setup
42
+ super
43
+ @setup_in_parent = true
44
+ end
45
+ end
46
+
47
+ class InheritedFixturedTest < FixturedTest
48
+ fixtures :blog_posts
49
+ def setup
50
+ super
51
+ @setup_in_child = true
52
+ end
53
+ end
54
+
55
+ class ThirdInheritedFixturedTest < InheritedFixturedTest
56
+ def setup
57
+ super
58
+ @setup_in_third = true
59
+ end
60
+ include FixtureReloadingTests
61
+ end
62
+
63
+ class FourthInherited < ThirdInheritedFixturedTest
64
+ include FixtureReloadingTests
65
+
66
+ def test_case_setup_properly
67
+ assert_not_nil @request, "The setup for Mosquito web test should have been done"
68
+ assert_not_nil @setup_in_parent, "The first setup in the chain should have happened"
69
+ assert_not_nil @setup_in_child, "The second setup in the chain should have happsened"
70
+ assert_not_nil @setup_in_third, "The third setup in the chain should have happened"
71
+ end
72
+ end
@@ -1,9 +1,10 @@
1
+ require File.dirname(__FILE__) + "/switcher"
1
2
  require File.dirname(__FILE__) + "/../lib/mosquito"
2
3
 
3
4
  class TestHelpers < Camping::ModelTest
4
5
  # http://rubyforge.org/tracker/index.php?func=detail&aid=8921&group_id=351&atid=1416
5
6
  def test_supports_old_style_and_new_style_fixture_generation
6
- assert self.respond_to?(:create_fixtures), "Oldstyle method should work"
7
+ assert !self.respond_to?(:create_fixtures), "Oldstyle method is removed"
7
8
  assert self.class.respond_to?(:fixtures), "Newstyle method should work"
8
9
  end
9
10
 
@@ -1,3 +1,4 @@
1
+ require File.dirname(__FILE__) + "/switcher"
1
2
  require File.dirname(__FILE__) + "/../lib/mosquito"
2
3
 
3
4
  $KCODE = 'u'
@@ -39,6 +40,11 @@ class TestMockRequest < Test::Unit::TestCase
39
40
  assert_equal 'test.host', @req.to_hash['SERVER_NAME']
40
41
  assert_equal 'test.host', @req.to_hash['HTTP_HOST']
41
42
  end
43
+
44
+ def test_headers_mirrors_to_hash
45
+ assert_respond_to @req, :headers
46
+ assert_equal @req.headers, @req.to_hash
47
+ end
42
48
 
43
49
  def test_envars_translate_to_readers
44
50
  %w( server_name path_info accept_encoding user_agent
@@ -206,9 +212,9 @@ class TestMockRequest < Test::Unit::TestCase
206
212
  ]
207
213
 
208
214
  ref_segments.each_with_index do | ref, idx |
209
- if ref == String
215
+ if ref === String
210
216
  assert_equal ref, output[idx], "The segment #{idx} should be #{ref}"
211
- elsif ref == Regexp
217
+ elsif ref === Regexp
212
218
  assert_match ref, output[idx], "The segment #{idx} should match #{ref}"
213
219
  end
214
220
  end
@@ -240,10 +246,13 @@ class TestMockRequestWithRoundtrip < Test::Unit::TestCase
240
246
 
241
247
  assert_equal "john", @parsed_input["name"]
242
248
  assert_kind_of Hash, @parsed_input["somefile"]
243
- assert_equal "somefile", @parsed_input["somefile"]["name"]
244
- assert_equal "pic.jpg", @parsed_input["somefile"]["filename"]
245
- assert_equal "image/jpeg", @parsed_input["somefile"]["type"]
246
- assert_kind_of Tempfile, @parsed_input["somefile"]["tempfile"]
249
+
250
+ ef = @parsed_input["somefile"].with_indifferent_access
251
+
252
+ assert_equal "somefile", ef["name"]
253
+ assert_equal "pic.jpg", ef["filename"]
254
+ assert_equal "image/jpeg", ef["type"]
255
+ assert_kind_of Tempfile, ef["tempfile"]
247
256
 
248
257
 
249
258
  @req.post_params = {:hello => "welcome", :name => "john", :arrayed => [1, 2, 3], :somefile => "instead" }
@@ -280,11 +289,28 @@ class TestMockRequestWithRoundtrip < Test::Unit::TestCase
280
289
  ref = {"name"=>"john", "hello"=>"welæcome", "data"=>{"values"=>["1", "2", "3"]}}
281
290
  assert_equal ref, @parsed_input, "Camping should have parsed our input like so"
282
291
  end
292
+
293
+
294
+ def test_to_rack_request
295
+ @req.post_params = {:hello => "welæcome", :name => "john", :data => {:values => [1,2,3] } }
296
+ rack_r = @req.to_rack_request
297
+
298
+ assert_kind_of Hash, rack_r
299
+ assert_equal "http", rack_r['rack.url_scheme']
300
+ assert_kind_of StringIO, rack_r['rack.input']
301
+ assert_equal [0,1], rack_r['rack.version']
302
+ assert_equal '74', rack_r['CONTENT_LENGTH']
303
+ end
304
+
283
305
  private
284
306
  def run_request!
285
307
  @req.body.rewind
286
308
  begin
287
- Sniffer.run(@req.body, @req.to_hash)
309
+ if Sniffer.respond_to?(:call)
310
+ Sniffer.call(@req.to_rack_request)
311
+ else
312
+ Sniffer.run(@req.body, @req.to_hash)
313
+ end
288
314
  rescue Sniffer::Controllers::ParamPeeker::Messenger => e
289
315
  @parsed_input = e.packet
290
316
  end
@@ -1,3 +1,4 @@
1
+ require File.dirname(__FILE__) + "/switcher"
1
2
  require File.dirname(__FILE__) + "/../lib/mosquito"
2
3
 
3
4
  class TestMockUpload < Test::Unit::TestCase
metadata CHANGED
@@ -1,106 +1,119 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.0
3
- specification_version: 1
4
2
  name: mosquito
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.1.3
7
- date: 2007-09-24 00:00:00 -07:00
8
- summary: A Camping test library.
9
- require_paths:
10
- - lib
11
- email: boss@topfunky.com
12
- homepage: http://mosquito.rubyforge.org
13
- rubyforge_project: mosquito
14
- description: A library for writing tests for your Camping app.
15
- autorequire:
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ version: 0.1.4
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
7
  - Geoffrey Grosenbach
31
- files:
32
- - CHANGELOG
33
- - MIT-LICENSE
34
- - Manifest.txt
35
- - README.txt
36
- - Rakefile
37
- - lib/mosquito.rb
38
- - public/bare.rb
39
- - public/blog.rb
40
- - public/blog/controllers.rb
41
- - public/blog/models.rb
42
- - public/blog/views.rb
43
- - test/fixtures/blog_comments.yml
44
- - test/fixtures/blog_posts.yml
45
- - test/fixtures/blog_users.yml
46
- - test/sage_advice_cases/parsing_arrays.rb
47
- - test/test_bare.rb
48
- - test/test_blog.rb
49
- - test/test_helpers.rb
50
- - test/test_mock_request.rb
51
- - test/test_mock_upload.rb
52
- test_files:
53
- - test/test_bare.rb
54
- - test/test_blog.rb
55
- - test/test_helpers.rb
56
- - test/test_mock_request.rb
57
- - test/test_mock_upload.rb
58
- rdoc_options:
59
- - --main
60
- - README.txt
61
- extra_rdoc_files:
62
- - Manifest.txt
63
- - README.txt
64
- executables: []
65
-
66
- extensions: []
67
-
68
- requirements: []
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
69
11
 
12
+ date: 2008-11-17 00:00:00 -08:00
13
+ default_executable:
70
14
  dependencies:
71
15
  - !ruby/object:Gem::Dependency
72
16
  name: activerecord
17
+ type: :runtime
73
18
  version_requirement:
74
- version_requirements: !ruby/object:Gem::Version::Requirement
19
+ version_requirements: !ruby/object:Gem::Requirement
75
20
  requirements:
76
- - - ">"
21
+ - - ">="
77
22
  - !ruby/object:Gem::Version
78
- version: 0.0.0
23
+ version: "0"
79
24
  version:
80
25
  - !ruby/object:Gem::Dependency
81
26
  name: activesupport
27
+ type: :runtime
82
28
  version_requirement:
83
- version_requirements: !ruby/object:Gem::Version::Requirement
29
+ version_requirements: !ruby/object:Gem::Requirement
84
30
  requirements:
85
- - - ">"
31
+ - - ">="
86
32
  - !ruby/object:Gem::Version
87
- version: 0.0.0
33
+ version: "0"
88
34
  version:
89
35
  - !ruby/object:Gem::Dependency
90
36
  name: camping
37
+ type: :runtime
91
38
  version_requirement:
92
- version_requirements: !ruby/object:Gem::Version::Requirement
39
+ version_requirements: !ruby/object:Gem::Requirement
93
40
  requirements:
94
- - - ">"
41
+ - - ">="
95
42
  - !ruby/object:Gem::Version
96
- version: 0.0.0
43
+ version: "0"
97
44
  version:
98
45
  - !ruby/object:Gem::Dependency
99
46
  name: hoe
47
+ type: :development
100
48
  version_requirement:
101
- version_requirements: !ruby/object:Gem::Version::Requirement
49
+ version_requirements: !ruby/object:Gem::Requirement
102
50
  requirements:
103
51
  - - ">="
104
52
  - !ruby/object:Gem::Version
105
- version: 1.3.0
53
+ version: 1.8.2
106
54
  version:
55
+ description: A library for writing tests for your Camping app.
56
+ email: boss@topfunky.com
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - Manifest.txt
63
+ - README.txt
64
+ files:
65
+ - CHANGELOG
66
+ - MIT-LICENSE
67
+ - Manifest.txt
68
+ - README.txt
69
+ - Rakefile
70
+ - lib/mosquito.rb
71
+ - public/bare.rb
72
+ - public/blog.rb
73
+ - public/blog/controllers.rb
74
+ - public/blog/models.rb
75
+ - public/blog/views.rb
76
+ - test/fixtures/blog_comments.yml
77
+ - test/fixtures/blog_posts.yml
78
+ - test/fixtures/blog_users.yml
79
+ - test/sage_advice_cases/parsing_arrays.rb
80
+ - test/test_bare.rb
81
+ - test/test_blog.rb
82
+ - test/test_helpers.rb
83
+ - test/test_mock_request.rb
84
+ - test/test_mock_upload.rb
85
+ - test/test_fixtures.rb
86
+ has_rdoc: true
87
+ homepage: http://mosquito.rubyforge.org
88
+ post_install_message:
89
+ rdoc_options:
90
+ - --main
91
+ - README.txt
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: "0"
99
+ version:
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: "0"
105
+ version:
106
+ requirements: []
107
+
108
+ rubyforge_project: mosquito
109
+ rubygems_version: 1.3.1
110
+ signing_key:
111
+ specification_version: 2
112
+ summary: A Camping test library.
113
+ test_files:
114
+ - test/test_bare.rb
115
+ - test/test_blog.rb
116
+ - test/test_fixtures.rb
117
+ - test/test_helpers.rb
118
+ - test/test_mock_request.rb
119
+ - test/test_mock_upload.rb