Class: Camping::WebTest

Inherits:
Test
  • Object
show all
Defined in:
lib/mosquito.rb

Overview

Used to test the controllers and rendering. The test should be called <App>Test (BlogTest for the aplication called Blog). A number of helper instance variables will be created for you - @request, which will contain a Mosquito::MockRequest object, @response (contains the response with headers and body), @cookies (a hash) and @state (a hash). Request and response will be reset in each test.

Instance Method Summary collapse

Methods inherited from Test

#assert_difference, #assert_no_difference, #deny, use_transactional_fixtures

Methods included from Mosquito::Dusty

#test

Instance Method Details

Assert that a cookie of name matches a certain pattern



564
565
566
# File 'lib/mosquito.rb', line 564

def assert_cookie(name, pat, message=nil)
  assert_match pat, @cookies[name], message
end

#assert_match_body(regex, message = nil) ⇒ Object

Check that the text in the body matches a regexp



545
546
547
# File 'lib/mosquito.rb', line 545

def assert_match_body(regex, message=nil)
  assert_match regex, @response.body, message
end

#assert_no_match_body(regex, message = nil) ⇒ Object

Opposite of assert_match_body



550
551
552
# File 'lib/mosquito.rb', line 550

def assert_no_match_body(regex, message=nil)
  assert_no_match regex, @response.body, message
end

#assert_no_sessionObject

The reverse of assert_session_started



590
591
592
593
# File 'lib/mosquito.rb', line 590

def assert_no_session
  assert_nil @cookies["camping_sid"], 
    "A session cookie was sent although this should not happen"
end

#assert_redirected_to(url, message = nil) ⇒ Object

Make sure that we are redirected to a certain URL. It’s not needed to prepend the URL with a mount (instead of “/blog/latest-news” you can use “/latest-news”)

Checks both the response status and the url.



558
559
560
561
# File 'lib/mosquito.rb', line 558

def assert_redirected_to(url, message=nil)
  assert_response :redirect
  assert_equal url, extract_redirection_url, message
end

#assert_response(status_code) ⇒ Object

Assert a specific response (:success, :error or a freeform error code as integer)



530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/mosquito.rb', line 530

def assert_response(status_code)
  case status_code
  when :success
    assert_equal 200, @response.status
  when :redirect
    assert_equal 302, @response.status
  when :error
    assert @response.status >= 500, 
      "Response status should have been >= 500 but was #{@response.status}"
  else
    assert_equal status_code, @response.status
  end
end

#assert_session_startedObject

Checks that Camping sent us a cookie to attach a session



579
580
581
582
583
584
585
586
587
# File 'lib/mosquito.rb', line 579

def assert_session_started
  if Camping.respond_to?(:call) # Camping 2.0 prefers cookie sessions
    assert_not_nil @cookies["camping_hash"], 
      "The session hash cookie was empty although session should have started"
  else
    assert_not_nil @cookies["camping_sid"], 
      "The session ID cookie was empty although session should have started"
  end
end

#assigns(key = nil) ⇒ Object

Gives you access to the instance variables assigned by the controller



422
423
424
425
# File 'lib/mosquito.rb', line 422

def assigns(key = nil)
  @assigns ||= Camping::H.new
  key ? @assigns[key] : @assigns
end

#delete(url, vars = {}) ⇒ Object

Send a DELETE request to a URL. All requests except GET will allow setting verbatim request body as the third argument instead of a hash.



451
452
453
# File 'lib/mosquito.rb', line 451

def delete(url, vars={})
  send_request url, vars, 'DELETE'
end

#follow_redirectObject

Nothing is new under the sun



569
570
571
# File 'lib/mosquito.rb', line 569

def follow_redirect
  get extract_redirection_url
end

#get(url = '/', vars = {}) ⇒ Object

Send a GET request to a URL



437
438
439
# File 'lib/mosquito.rb', line 437

def get(url='/', vars={})
  send_request url, vars, 'GET'
end

#post(url, post_vars = {}) ⇒ Object

Send a POST request to a URL. All requests except GET will allow setting verbatim request body as the third argument instead of a hash.



444
445
446
# File 'lib/mosquito.rb', line 444

def post(url, post_vars={})
  send_request url, post_vars, 'POST'
end

#put(url, vars = {}) ⇒ Object

Send a PUT request to a URL. All requests except GET will allow setting verbatim request body as the third argument instead of a hash.



458
459
460
# File 'lib/mosquito.rb', line 458

def put(url, vars={})
  send_request url, vars, 'PUT'
end

#send_request(composite_url, post_vars, method) ⇒ Object

Send the request. We will try to guess what you meant - if there are uploads to be processed it’s not going to be a GET, that’s for sure.



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/mosquito.rb', line 464

def send_request(composite_url, post_vars, method)
  
  if method.to_s.downcase == "get"
    @request.query_string_params = post_vars
  else
    @request.post_params = post_vars
  end
  
  # If there is some stuff in the URL to be used as a query string, why ignore it?
  relative_url, qs_from_url = relativize_url(composite_url)
  
  @request.append_to_query_string(qs_from_url) if qs_from_url
  
  # We do allow the user to override that one
  @request['REQUEST_METHOD'] = method
  
  # Make the Camping app route our request
  @request['SCRIPT_NAME'] = '/' + @class_name_abbr.downcase
  
  # PATH_INFO is used for internal routing by Camping. We have to always pass
  # the leading slash
  @request['PATH_INFO'] = ensure_one_leading_slash(relative_url)
  
  # We need to munge this because the PATH_INFO has changed
  @request['REQUEST_URI'] = [@request.SCRIPT_NAME, @request.PATH_INFO].join('').squeeze('/')
  unless @request['QUERY_STRING'].blank?
    @request['REQUEST_URI'] += ('?' + @request['QUERY_STRING']) 
  end
  
  if @cookies
    @request['HTTP_COOKIE'] = @cookies.map {|k,v| "#{k}=#{Mosquito.esc(v)}" }.join('; ')
  end
  
  # Get the Camping app
  app_module = Kernel.const_get(@class_name_abbr)
  
  # Inject the proboscis if we haven't already done so
  app_module.send(:include, Mosquito::Proboscis) unless app_module.ancestors.include?(Mosquito::Proboscis)
  
  # Run the request. 
  @response = if app_module.respond_to?(:run) # Camping < 2.0
    app_module.run(*@request.to_camping_args)
  else # Camping 2.0
    app_module.call(@request.to_rack_request).pop # Serve a Rack::Response object
  end
    
  # Add content_type accessor for Rails assert_select
  eval("class << @response; def content_type; @headers['Content-Type']; end; end")
  
  # Downgrade the disguised Mab into a string
  @response.body = @response.body.to_s
  @assigns = Mosquito::unstash
  
  # We need to restore the cookies separately so that the app
  # restores our session on the next request. We retrieve cookies and
  # the session in their assigned form instead of parsing the headers and
  # doing a deserialization cycle 
  @cookies = @assigns.cookies || H[{}]
  @state = @assigns.state || H[{}]
  
  if @response.headers['X-Sendfile']
    @response.body = File.read(@response.headers['X-Sendfile'])
  end
end

#setupObject



429
430
431
432
433
434
# File 'lib/mosquito.rb', line 429

def setup
  super
  @class_name_abbr = self.class.name.gsub(/^Test/, '')
  @request = Mosquito::MockRequest.new
  @cookies, @response, @assigns = {}, {}, {}
end

#test_defaultObject

:nodoc



427
# File 'lib/mosquito.rb', line 427

def test_default; end

#upload(filename) ⇒ Object

Quickly gives you a handle to a file with random content



574
575
576
# File 'lib/mosquito.rb', line 574

def upload(filename)
  Mosquito::MockUpload.new(filename)
end