Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 1fa6fa27c346b7735ee19819535fbe3aec0514cc 0 parents
Adam Milligan authored
7 README
@@ -0,0 +1,7 @@
+Wapcaplet: Keep your functional tests functional
+
+HTTP doesn't send integers, Dates, or ActiveRecords as parameters, it sends strings. This little plugin will simply
+throw a friendly exception if you pass a parameter with an improper value to a functional test. It will accept strings,
+TestUploadedFile objects, and nil for all parameters. For route parameters (used to generate the path) it will also
+accept ActiveRecord objects.
+
1  init.rb
@@ -0,0 +1 @@
+require 'lib/wapcaplet' if RAILS_ENV == 'test'
51 lib/wapcaplet.rb
@@ -0,0 +1,51 @@
+require "action_controller/test_process"
+
+module Wapcaplet
+ module TestRequest
+ def self.included(klass)
+ klass.class_eval do
+ remove_method :assign_parameters
+ include InstanceMethods
+ end
+ end
+
+ module InstanceMethods
+ def assign_parameters(controller_path, action, parameters)
+ parameters = parameters.symbolize_keys.merge(:controller => controller_path, :action => action)
+ extra_keys = ActionController::Routing::Routes.extra_keys(parameters)
+ non_path_parameters = get? ? query_parameters : request_parameters
+ parameters.each do |key, value|
+ value = ActionController::Routing::PathSegment::Result.new(value) if value.is_a? Array
+
+ if extra_keys.include?(key.to_sym)
+ verify_proper_param(value)
+ non_path_parameters[key] = value
+ else
+ verify_proper_param(value, :route_param)
+ path_parameters[key.to_s] = value
+ end
+ end
+ raw_post # populate env['RAW_POST_DATA']
+ @parameters = nil # reset TestRequest#parameters to use the new path_parameters
+ end
+
+ private
+
+ def verify_proper_param(value, route_param = false)
+ case value
+ when Hash
+ value.values.each { |element| verify_proper_param(element) }
+ when Array
+ value.each { |element| verify_proper_param(element) }
+ when String, ActionController::TestUploadedFile, NilClass
+ when ActiveRecord::Base
+ raise ArgumentError.new("ActiveRecords are only valid as value for routing path parameters") unless route_param
+ else
+ raise ArgumentError.new("Test requests should have only string or file upload parameters")
+ end
+ end
+ end
+ end
+end
+
+ActionController::TestRequest.class_eval { include Wapcaplet::TestRequest }
BIN  spec/files/foo.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
119 spec/wapcaplet_spec.rb
@@ -0,0 +1,119 @@
+require "rubygems"
+
+require "activesupport"
+require "action_controller"
+require "activerecord"
+require "spec"
+require 'lib/wapcaplet'
+
+class AModel < ActiveRecord::Base
+ set_table_name nil
+ def self.columns; []; end
+
+ def to_param
+ "a-param"
+ end
+end
+
+class AController < ActionController::Base
+ def show
+ head :ok
+ end
+end
+
+class ANestedController < ActionController::Base
+ def show
+ head :ok
+ end
+end
+
+ActionController::Routing::Routes.load!
+ActionController::Routing::Routes.draw do |map|
+ map.resource :a_nested, :path_prefix => "parents/:parent_id/", :controller => 'a_nested', :action => 'show'
+end
+
+describe "functional test parameters" do
+ include ActionController::TestProcess
+
+ before(:each) do
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ @controller = AController.new
+ end
+
+ it "should raise an exception if any non-route parameter is not a string" do
+ non_strings.each do |not_a_string|
+ lambda { get :show, :wibble => not_a_string }.should raise_error(ArgumentError)
+ end
+ end
+
+ it "should raise an exception if any non-route parameter is not a string, even if it responds to #to_param" do
+ lambda { get :show, :wibble => fake_active_record }.should raise_error(ArgumentError)
+ end
+
+ it "should work with nested parameters" do
+ lambda { get :show, :wibble => { :thing => "a string" } }.should_not raise_error
+ end
+
+ it "should raise an exception if a nested HTTP request parameter is not a string" do
+ non_strings.each do |not_a_string|
+ lambda { get :show, :wibble => { :thing => not_a_string } }.should raise_error(ArgumentError)
+ end
+ end
+
+ it "should work with array parameters" do
+ lambda { get :show, :wibbles => ["one", "two"] }.should_not raise_error
+ end
+
+ it "should raise an exception if a member of an array parameter is not a string" do
+ non_strings.each do |not_a_string|
+ lambda { get :show, :wibbles => ["one", not_a_string] }.should raise_error(ArgumentError)
+ end
+ end
+
+ it "should not raise an exception for a file upload parameter" do
+ file_path = File.join(File.dirname(__FILE__), "files", "foo.jpg")
+ lambda { get :show, :wibble_asset => fixture_file_upload(file_path, 'image/jpg') }.should_not raise_error
+ end
+
+ it "should allow nils" do
+ lambda { get :show, :wibble => nil }.should_not raise_error
+ end
+
+ it "should not raise an exception if a session or flash parameter is not a string" do
+ non_strings.each do |not_a_string|
+ lambda { get :show, {}, { :foozle => not_a_string } }.should_not raise_error
+ lambda { get :show, {}, {}, { :foozle => not_a_string } }.should_not raise_error
+ end
+ end
+
+ context "for a nested route" do
+ before(:each) do
+ @controller = ANestedController.new
+ end
+
+ it "should allow string values for route parameters" do
+ lambda { get :show, :parent_id => "parent-param", :wibble => "a_string" }.should_not raise_error
+ end
+
+ it "should allow subclasses of ActiveRecord for route parameters" do
+ lambda { get :show, :parent_id => fake_active_record, :wibble => "a_string" }.should_not raise_error
+ end
+
+ it "should not allow non-ActiveRecord, non-string values for route parameters" do
+ non_strings.each do |not_a_string|
+ lambda { get :show, :parent_id => not_a_string, :wibble => "a_string" }.should raise_error(ArgumentError)
+ end
+ end
+ end
+
+ private
+
+ def non_strings
+ [1, Time.now, 1.0, /hello/, :symbol]
+ end
+
+ def fake_active_record
+ AModel.new
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.