Class: Camping::WebTest
- 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 Attribute Summary collapse
-
#assigns ⇒ Object
readonly
Gives you access to the instance variables assigned by the controller.
Instance Method Summary collapse
-
#assert_cookie(name, pat, message = nil) ⇒ Object
Assert that a cookie of name matches a certain pattern.
-
#assert_match_body(regex, message = nil) ⇒ Object
Check that the text in the body matches a regexp.
-
#assert_no_match_body(regex, message = nil) ⇒ Object
Opposite of
assert_match_body
. -
#assert_no_session ⇒ Object
The reverse of
assert_session_started
. -
#assert_redirected_to(url, message = nil) ⇒ Object
Make sure that we are redirected to a certain URL.
-
#assert_response(status_code) ⇒ Object
Assert a specific response (:success, :error or a freeform error code as integer).
-
#assert_session_started ⇒ Object
Checks that Camping sent us a cookie to attach a session.
-
#delete(url, vars = {}) ⇒ Object
Send a DELETE request to a URL.
-
#follow_redirect ⇒ Object
Nothing is new under the sun.
-
#get(url = '/', vars = {}) ⇒ Object
Send a GET request to a URL.
-
#post(url, post_vars = {}) ⇒ Object
Send a POST request to a URL.
-
#put(url, vars = {}) ⇒ Object
Send a PUT request to a URL.
-
#send_request(url, post_vars, method) ⇒ Object
Send any request.
- #setup ⇒ Object
-
#test_dummy ⇒ Object
:nodoc.
-
#upload(filename) ⇒ Object
Quickly gives you a handle to a file with random content.
Methods inherited from Test
#assert_difference, #assert_no_difference, #deny
Instance Attribute Details
#assigns ⇒ Object (readonly)
Gives you access to the instance variables assigned by the controller
411 412 413 |
# File 'lib/mosquito.rb', line 411 def assigns @assigns end |
Instance Method Details
#assert_cookie(name, pat, message = nil) ⇒ Object
Assert that a cookie of name matches a certain pattern
534 535 536 |
# File 'lib/mosquito.rb', line 534 def (name, pat, =nil) assert_match pat, @cookies[name], end |
#assert_match_body(regex, message = nil) ⇒ Object
Check that the text in the body matches a regexp
515 516 517 |
# File 'lib/mosquito.rb', line 515 def assert_match_body(regex, =nil) assert_match regex, @response.body, end |
#assert_no_match_body(regex, message = nil) ⇒ Object
Opposite of assert_match_body
520 521 522 |
# File 'lib/mosquito.rb', line 520 def assert_no_match_body(regex, =nil) assert_no_match regex, @response.body, end |
#assert_no_session ⇒ Object
The reverse of assert_session_started
555 556 557 558 |
# File 'lib/mosquito.rb', line 555 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.
528 529 530 531 |
# File 'lib/mosquito.rb', line 528 def assert_redirected_to(url, =nil) assert_response :redirect assert_equal url, extract_redirection_url, end |
#assert_response(status_code) ⇒ Object
Assert a specific response (:success, :error or a freeform error code as integer)
500 501 502 503 504 505 506 507 508 509 510 511 512 |
# File 'lib/mosquito.rb', line 500 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_started ⇒ Object
Checks that Camping sent us a cookie to attach a session
549 550 551 552 |
# File 'lib/mosquito.rb', line 549 def assert_session_started assert_not_nil @cookies["camping_sid"], "The session ID cookie was empty although session should have started" end |
#delete(url, vars = {}) ⇒ Object
Send a DELETE request to a URL. All requests except GET will allow setting verbatim URL-encoded parameters as the third argument instead of a hash.
436 437 438 |
# File 'lib/mosquito.rb', line 436 def delete(url, vars={}) send_request url, vars, 'DELETE' end |
#follow_redirect ⇒ Object
Nothing is new under the sun
539 540 541 |
# File 'lib/mosquito.rb', line 539 def follow_redirect get extract_redirection_url end |
#get(url = '/', vars = {}) ⇒ Object
Send a GET request to a URL
422 423 424 |
# File 'lib/mosquito.rb', line 422 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 URL-encoded parameters as the third argument instead of a hash.
429 430 431 |
# File 'lib/mosquito.rb', line 429 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 URL-encoded parameters as the third argument instead of a hash.
443 444 445 |
# File 'lib/mosquito.rb', line 443 def put(url, vars={}) send_request url, vars, 'PUT' end |
#send_request(url, post_vars, method) ⇒ Object
Send any 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.
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 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 |
# File 'lib/mosquito.rb', line 449 def send_request(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? url, qs_from_url = url.split(/\?/) relativize_url!(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 @request['SCRIPT_NAME'] = '/' + @class_name_abbr.downcase @request['PATH_INFO'] = '/' + url @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}=#{Camping.escape(v)}" }.join('; ') end # Inject the proboscis if we haven't already done so pr = Mosquito::Proboscis eval("#{@class_name_abbr}.send(:include, pr) unless #{@class_name_abbr}.ancestors.include?(pr)") # Run the request @response = eval("#{@class_name_abbr}.run @request.body, @request") @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 |
#setup ⇒ Object
415 416 417 418 419 |
# File 'lib/mosquito.rb', line 415 def setup @class_name_abbr = self.class.name.gsub(/^Test/, '') @request = Mosquito::MockRequest.new @cookies, @response, @assigns = {}, {}, {} end |
#test_dummy ⇒ Object
:nodoc
413 |
# File 'lib/mosquito.rb', line 413 def test_dummy; end |
#upload(filename) ⇒ Object
Quickly gives you a handle to a file with random content
544 545 546 |
# File 'lib/mosquito.rb', line 544 def upload(filename) Mosquito::MockUpload.new(filename) end |