Remove test cases from gems
git-svn-id: file:///home/svn/framework3/trunk@5938 4d416f70-5f16-0410-b530-b9f4589650daunstable
parent
0188938b46
commit
1acebca6c2
|
@ -1,56 +0,0 @@
|
|||
require 'test/unit'
|
||||
|
||||
$:.unshift "#{File.dirname(__FILE__)}/../lib"
|
||||
require 'action_mailer'
|
||||
require 'action_mailer/test_case'
|
||||
|
||||
# Show backtraces for deprecated behavior for quicker cleanup.
|
||||
ActiveSupport::Deprecation.debug = true
|
||||
|
||||
$:.unshift "#{File.dirname(__FILE__)}/fixtures/helpers"
|
||||
ActionMailer::Base.template_root = "#{File.dirname(__FILE__)}/fixtures"
|
||||
|
||||
class MockSMTP
|
||||
def self.deliveries
|
||||
@@deliveries
|
||||
end
|
||||
|
||||
def initialize
|
||||
@@deliveries = []
|
||||
end
|
||||
|
||||
def sendmail(mail, from, to)
|
||||
@@deliveries << [mail, from, to]
|
||||
end
|
||||
end
|
||||
|
||||
class Net::SMTP
|
||||
def self.start(*args)
|
||||
yield MockSMTP.new
|
||||
end
|
||||
end
|
||||
|
||||
def uses_gem(gem_name, test_name, version = '> 0')
|
||||
require 'rubygems'
|
||||
gem gem_name.to_s, version
|
||||
require gem_name.to_s
|
||||
yield
|
||||
rescue LoadError
|
||||
$stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again."
|
||||
end
|
||||
|
||||
# Wrap tests that use Mocha and skip if unavailable.
|
||||
unless defined? uses_mocha
|
||||
def uses_mocha(test_name, &block)
|
||||
uses_gem('mocha', test_name, '>= 0.5.5', &block)
|
||||
end
|
||||
end
|
||||
|
||||
def set_delivery_method(delivery_method)
|
||||
@old_delivery_method = ActionMailer::Base.delivery_method
|
||||
ActionMailer::Base.delivery_method = delivery_method
|
||||
end
|
||||
|
||||
def restore_delivery_method
|
||||
ActionMailer::Base.delivery_method = @old_delivery_method
|
||||
end
|
|
@ -1,51 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class DefaultDeliveryMethodMailer < ActionMailer::Base
|
||||
end
|
||||
|
||||
class NonDefaultDeliveryMethodMailer < ActionMailer::Base
|
||||
self.delivery_method = :sendmail
|
||||
end
|
||||
|
||||
class ActionMailerBase_delivery_method_Test < Test::Unit::TestCase
|
||||
def setup
|
||||
set_delivery_method :smtp
|
||||
end
|
||||
|
||||
def teardown
|
||||
restore_delivery_method
|
||||
end
|
||||
|
||||
def test_should_be_the_default_smtp
|
||||
assert_equal :smtp, ActionMailer::Base.delivery_method
|
||||
end
|
||||
end
|
||||
|
||||
class DefaultDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
|
||||
def setup
|
||||
set_delivery_method :smtp
|
||||
end
|
||||
|
||||
def teardown
|
||||
restore_delivery_method
|
||||
end
|
||||
|
||||
def test_should_be_the_default_smtp
|
||||
assert_equal :smtp, DefaultDeliveryMethodMailer.delivery_method
|
||||
end
|
||||
end
|
||||
|
||||
class NonDefaultDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
|
||||
def setup
|
||||
set_delivery_method :smtp
|
||||
end
|
||||
|
||||
def teardown
|
||||
restore_delivery_method
|
||||
end
|
||||
|
||||
def test_should_be_the_set_delivery_method
|
||||
assert_equal :sendmail, NonDefaultDeliveryMethodMailer.delivery_method
|
||||
end
|
||||
end
|
||||
|
|
@ -1 +0,0 @@
|
|||
first mail
|
|
@ -1 +0,0 @@
|
|||
So, <%= example_format(@text) %>
|
|
@ -1 +0,0 @@
|
|||
Hello, <%= person_name %>. Thanks for registering!
|
|
@ -1 +0,0 @@
|
|||
This message brought to you by <%= name_of_the_mailer_class %>.
|
|
@ -1,5 +0,0 @@
|
|||
From "Romeo and Juliet":
|
||||
|
||||
<%= block_format @text %>
|
||||
|
||||
Good ol' Shakespeare.
|
|
@ -1,5 +0,0 @@
|
|||
module ExampleHelper
|
||||
def example_format(text)
|
||||
"<em><strong><small>#{text}</small></strong></em>"
|
||||
end
|
||||
end
|
|
@ -1 +0,0 @@
|
|||
Have a lovely picture, from me. Enjoy!
|
|
@ -1,14 +0,0 @@
|
|||
From jamis_buck@byu.edu Mon May 2 16:07:05 2005
|
||||
Mime-Version: 1.0 (Apple Message framework v622)
|
||||
Content-Transfer-Encoding: base64
|
||||
Message-Id: <d3b8cf8e49f04480850c28713a1f473e@37signals.com>
|
||||
Content-Type: text/plain;
|
||||
charset=EUC-KR;
|
||||
format=flowed
|
||||
To: willard15georgina@jamis.backpackit.com
|
||||
From: Jamis Buck <jamis@37signals.com>
|
||||
Subject: =?EUC-KR?Q?NOTE:_=C7=D1=B1=B9=B8=BB=B7=CE_=C7=CF=B4=C2_=B0=CD?=
|
||||
Date: Mon, 2 May 2005 16:07:05 -0600
|
||||
|
||||
tOu6zrrQwMcguLbC+bChwfa3ziwgv+y4rrTCIMfPs6q01MC7ILnPvcC0z7TZLg0KDQrBpiDAzLin
|
||||
wLogSmFtaXPA1LTPtNku
|
|
@ -1,20 +0,0 @@
|
|||
Return-Path: <xxx@xxxx.xxx>
|
||||
Received: from xxx.xxxx.xxx by xxx.xxxx.xxx with ESMTP id C1B953B4CB6 for <xxxxx@Exxx.xxxx.xxx>; Tue, 10 May 2005 15:27:05 -0500
|
||||
Received: from SMS-GTYxxx.xxxx.xxx by xxx.xxxx.xxx with ESMTP id ca for <xxxxx@Exxx.xxxx.xxx>; Tue, 10 May 2005 15:27:04 -0500
|
||||
Received: from xxx.xxxx.xxx by SMS-GTYxxx.xxxx.xxx with ESMTP id j4AKR3r23323 for <xxxxx@Exxx.xxxx.xxx>; Tue, 10 May 2005 15:27:03 -0500
|
||||
Date: Tue, 10 May 2005 15:27:03 -0500
|
||||
From: xxx@xxxx.xxx
|
||||
Sender: xxx@xxxx.xxx
|
||||
To: xxxxxxxxxxx@xxxx.xxxx.xxx
|
||||
Message-Id: <xxx@xxxx.xxx>
|
||||
X-Original-To: xxxxxxxxxxx@xxxx.xxxx.xxx
|
||||
Delivered-To: xxx@xxxx.xxx
|
||||
Importance: normal
|
||||
Content-Type: text/plain; charset=X-UNKNOWN
|
||||
|
||||
Test test. Hi. Waving. m
|
||||
|
||||
----------------------------------------------------------------
|
||||
Sent via Bell Mobility's Text Messaging service.
|
||||
Envoyé par le service de messagerie texte de Bell Mobilité.
|
||||
----------------------------------------------------------------
|
|
@ -1,32 +0,0 @@
|
|||
Mime-Version: 1.0 (Apple Message framework v730)
|
||||
Content-Type: multipart/mixed; boundary=Apple-Mail-13-196941151
|
||||
Message-Id: <9169D984-4E0B-45EF-82D4-8F5E53AD7012@example.com>
|
||||
From: foo@example.com
|
||||
Subject: testing
|
||||
Date: Mon, 6 Jun 2005 22:21:22 +0200
|
||||
To: blah@example.com
|
||||
|
||||
|
||||
--Apple-Mail-13-196941151
|
||||
Content-Transfer-Encoding: quoted-printable
|
||||
Content-Type: text/plain;
|
||||
charset=ISO-8859-1;
|
||||
delsp=yes;
|
||||
format=flowed
|
||||
|
||||
This is the first part.
|
||||
|
||||
--Apple-Mail-13-196941151
|
||||
Content-Type: image/jpeg
|
||||
Content-Transfer-Encoding: base64
|
||||
Content-Location: Photo25.jpg
|
||||
Content-ID: <qbFGyPQAS8>
|
||||
Content-Disposition: inline
|
||||
|
||||
jamisSqGSIb3DQEHAqCAMIjamisxCzAJBgUrDgMCGgUAMIAGCSqGSjamisEHAQAAoIIFSjCCBUYw
|
||||
ggQujamisQICBD++ukQwDQYJKojamisNAQEFBQAwMTELMAkGA1UEBhMCRjamisAKBgNVBAoTA1RE
|
||||
QzEUMBIGjamisxMLVERDIE9DRVMgQ0jamisNMDQwMjI5MTE1OTAxWhcNMDYwMjamisIyOTAxWjCB
|
||||
gDELMAkGA1UEjamisEsxKTAnBgNVBAoTIEjamisuIG9yZ2FuaXNhdG9yaXNrIHRpbjamisRuaW5=
|
||||
|
||||
--Apple-Mail-13-196941151--
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
Mime-Version: 1.0 (Apple Message framework v730)
|
||||
Content-Type: multipart/mixed; boundary=Apple-Mail-13-196941151
|
||||
Message-Id: <9169D984-4E0B-45EF-82D4-8F5E53AD7012@example.com>
|
||||
From: foo@example.com
|
||||
Subject: testing
|
||||
Date: Mon, 6 Jun 2005 22:21:22 +0200
|
||||
To: blah@example.com
|
||||
|
||||
|
||||
--Apple-Mail-13-196941151
|
||||
Content-Transfer-Encoding: quoted-printable
|
||||
Content-Type: text/plain;
|
||||
charset=ISO-8859-1;
|
||||
delsp=yes;
|
||||
format=flowed
|
||||
|
||||
This is the first part.
|
||||
|
||||
--Apple-Mail-13-196941151
|
||||
Content-Type: text/x-ruby-script; name="hello.rb"
|
||||
Content-Transfer-Encoding: 7bit
|
||||
Content-Disposition: attachment;
|
||||
filename="api.rb"
|
||||
|
||||
puts "Hello, world!"
|
||||
gets
|
||||
|
||||
--Apple-Mail-13-196941151--
|
||||
|
|
@ -1,114 +0,0 @@
|
|||
From xxxxxxxxx.xxxxxxx@gmail.com Sun May 8 19:07:09 2005
|
||||
Return-Path: <xxxxxxxxx.xxxxxxx@gmail.com>
|
||||
X-Original-To: xxxxx@xxxxx.xxxxxxxxx.com
|
||||
Delivered-To: xxxxx@xxxxx.xxxxxxxxx.com
|
||||
Received: from localhost (localhost [127.0.0.1])
|
||||
by xxxxx.xxxxxxxxx.com (Postfix) with ESMTP id 06C9DA98D
|
||||
for <xxxxx@xxxxx.xxxxxxxxx.com>; Sun, 8 May 2005 19:09:13 +0000 (GMT)
|
||||
Received: from xxxxx.xxxxxxxxx.com ([127.0.0.1])
|
||||
by localhost (xxxxx.xxxxxxxxx.com [127.0.0.1]) (amavisd-new, port 10024)
|
||||
with LMTP id 88783-08 for <xxxxx@xxxxx.xxxxxxxxx.com>;
|
||||
Sun, 8 May 2005 19:09:12 +0000 (GMT)
|
||||
Received: from xxxxxxx.xxxxxxxxx.com (xxxxxxx.xxxxxxxxx.com [69.36.39.150])
|
||||
by xxxxx.xxxxxxxxx.com (Postfix) with ESMTP id 10D8BA960
|
||||
for <xxxxx@xxxxxxxxx.org>; Sun, 8 May 2005 19:09:12 +0000 (GMT)
|
||||
Received: from zproxy.gmail.com (zproxy.gmail.com [64.233.162.199])
|
||||
by xxxxxxx.xxxxxxxxx.com (Postfix) with ESMTP id 9EBC4148EAB
|
||||
for <xxxxx@xxxxxxxxx.com>; Sun, 8 May 2005 14:09:11 -0500 (CDT)
|
||||
Received: by zproxy.gmail.com with SMTP id 13so1233405nzp
|
||||
for <xxxxx@xxxxxxxxx.com>; Sun, 08 May 2005 12:09:11 -0700 (PDT)
|
||||
DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws;
|
||||
s=beta; d=gmail.com;
|
||||
h=received:message-id:date:from:reply-to:to:subject:in-reply-to:mime-version:content-type:references;
|
||||
b=cid1mzGEFa3gtRa06oSrrEYfKca2CTKu9sLMkWxjbvCsWMtp9RGEILjUz0L5RySdH5iO661LyNUoHRFQIa57bylAbXM3g2DTEIIKmuASDG3x3rIQ4sHAKpNxP7Pul+mgTaOKBv+spcH7af++QEJ36gHFXD2O/kx9RePs3JNf/K8=
|
||||
Received: by 10.36.10.16 with SMTP id 16mr1012493nzj;
|
||||
Sun, 08 May 2005 12:09:11 -0700 (PDT)
|
||||
Received: by 10.36.5.10 with HTTP; Sun, 8 May 2005 12:09:11 -0700 (PDT)
|
||||
Message-ID: <e85734b90505081209eaaa17b@mail.gmail.com>
|
||||
Date: Sun, 8 May 2005 14:09:11 -0500
|
||||
From: xxxxxxxxx xxxxxxx <xxxxxxxxx.xxxxxxx@gmail.com>
|
||||
Reply-To: xxxxxxxxx xxxxxxx <xxxxxxxxx.xxxxxxx@gmail.com>
|
||||
To: xxxxx xxxx <xxxxx@xxxxxxxxx.com>
|
||||
Subject: Fwd: Signed email causes file attachments
|
||||
In-Reply-To: <F6E2D0B4-CC35-4A91-BA4C-C7C712B10C13@mac.com>
|
||||
Mime-Version: 1.0
|
||||
Content-Type: multipart/mixed;
|
||||
boundary="----=_Part_5028_7368284.1115579351471"
|
||||
References: <F6E2D0B4-CC35-4A91-BA4C-C7C712B10C13@mac.com>
|
||||
|
||||
------=_Part_5028_7368284.1115579351471
|
||||
Content-Type: text/plain; charset=ISO-8859-1
|
||||
Content-Transfer-Encoding: quoted-printable
|
||||
Content-Disposition: inline
|
||||
|
||||
We should not include these files or vcards as attachments.
|
||||
|
||||
---------- Forwarded message ----------
|
||||
From: xxxxx xxxxxx <xxxxxxxx@xxx.com>
|
||||
Date: May 8, 2005 1:17 PM
|
||||
Subject: Signed email causes file attachments
|
||||
To: xxxxxxx@xxxxxxxxxx.com
|
||||
|
||||
|
||||
Hi,
|
||||
|
||||
Just started to use my xxxxxxxx account (to set-up a GTD system,
|
||||
natch) and noticed that when I send content via email the signature/
|
||||
certificate from my email account gets added as a file (e.g.
|
||||
"smime.p7s").
|
||||
|
||||
Obviously I can uncheck the signature option in the Mail compose
|
||||
window but how often will I remember to do that?
|
||||
|
||||
Is there any way these kind of files could be ignored, e.g. via some
|
||||
sort of exclusions list?
|
||||
|
||||
------=_Part_5028_7368284.1115579351471
|
||||
Content-Type: application/pkcs7-signature; name=smime.p7s
|
||||
Content-Transfer-Encoding: base64
|
||||
Content-Disposition: attachment; filename="smime.p7s"
|
||||
|
||||
MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIIGFDCCAs0w
|
||||
ggI2oAMCAQICAw5c+TANBgkqhkiG9w0BAQQFADBiMQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhh
|
||||
d3RlIENvbnN1bHRpbmcgKFB0eSkgTHRkLjEsMCoGA1UEAxMjVGhhd3RlIFBlcnNvbmFsIEZyZWVt
|
||||
YWlsIElzc3VpbmcgQ0EwHhcNMDUwMzI5MDkzOTEwWhcNMDYwMzI5MDkzOTEwWjBCMR8wHQYDVQQD
|
||||
ExZUaGF3dGUgRnJlZW1haWwgTWVtYmVyMR8wHQYJKoZIhvcNAQkBFhBzbWhhdW5jaEBtYWMuY29t
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn90dPsYS3LjfMY211OSYrDQLzwNYPlAL
|
||||
7+/0XA+kdy8/rRnyEHFGwhNCDmg0B6pxC7z3xxJD/8GfCd+IYUUNUQV5m9MkxfP9pTVXZVIYLaBw
|
||||
o8xS3A0a1LXealcmlEbJibmKkEaoXci3MhryLgpaa+Kk/sH02SNatDO1vS28bPsibZpcc6deFrla
|
||||
hSYnL+PW54mDTGHIcCN2fbx/Y6qspzqmtKaXrv75NBtuy9cB6KzU4j2xXbTkAwz3pRSghJJaAwdp
|
||||
+yIivAD3vr0kJE3p+Ez34HMh33EXEpFoWcN+MCEQZD9WnmFViMrvfvMXLGVFQfAAcC060eGFSRJ1
|
||||
ZQ9UVQIDAQABoy0wKzAbBgNVHREEFDASgRBzbWhhdW5jaEBtYWMuY29tMAwGA1UdEwEB/wQCMAAw
|
||||
DQYJKoZIhvcNAQEEBQADgYEAQMrg1n2pXVWteP7BBj+Pk3UfYtbuHb42uHcLJjfjnRlH7AxnSwrd
|
||||
L3HED205w3Cq8T7tzVxIjRRLO/ljq0GedSCFBky7eYo1PrXhztGHCTSBhsiWdiyLWxKlOxGAwJc/
|
||||
lMMnwqLOdrQcoF/YgbjeaUFOQbUh94w9VDNpWZYCZwcwggM/MIICqKADAgECAgENMA0GCSqGSIb3
|
||||
DQEBBQUAMIHRMQswCQYDVQQGEwJaQTEVMBMGA1UECBMMV2VzdGVybiBDYXBlMRIwEAYDVQQHEwlD
|
||||
YXBlIFRvd24xGjAYBgNVBAoTEVRoYXd0ZSBDb25zdWx0aW5nMSgwJgYDVQQLEx9DZXJ0aWZpY2F0
|
||||
aW9uIFNlcnZpY2VzIERpdmlzaW9uMSQwIgYDVQQDExtUaGF3dGUgUGVyc29uYWwgRnJlZW1haWwg
|
||||
Q0ExKzApBgkqhkiG9w0BCQEWHHBlcnNvbmFsLWZyZWVtYWlsQHRoYXd0ZS5jb20wHhcNMDMwNzE3
|
||||
MDAwMDAwWhcNMTMwNzE2MjM1OTU5WjBiMQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3RlIENv
|
||||
bnN1bHRpbmcgKFB0eSkgTHRkLjEsMCoGA1UEAxMjVGhhd3RlIFBlcnNvbmFsIEZyZWVtYWlsIElz
|
||||
c3VpbmcgQ0EwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMSmPFVzVftOucqZWh5owHUEcJ3f
|
||||
6f+jHuy9zfVb8hp2vX8MOmHyv1HOAdTlUAow1wJjWiyJFXCO3cnwK4Vaqj9xVsuvPAsH5/EfkTYk
|
||||
KhPPK9Xzgnc9A74r/rsYPge/QIACZNenprufZdHFKlSFD0gEf6e20TxhBEAeZBlyYLf7AgMBAAGj
|
||||
gZQwgZEwEgYDVR0TAQH/BAgwBgEB/wIBADBDBgNVHR8EPDA6MDigNqA0hjJodHRwOi8vY3JsLnRo
|
||||
YXd0ZS5jb20vVGhhd3RlUGVyc29uYWxGcmVlbWFpbENBLmNybDALBgNVHQ8EBAMCAQYwKQYDVR0R
|
||||
BCIwIKQeMBwxGjAYBgNVBAMTEVByaXZhdGVMYWJlbDItMTM4MA0GCSqGSIb3DQEBBQUAA4GBAEiM
|
||||
0VCD6gsuzA2jZqxnD3+vrL7CF6FDlpSdf0whuPg2H6otnzYvwPQcUCCTcDz9reFhYsPZOhl+hLGZ
|
||||
GwDFGguCdJ4lUJRix9sncVcljd2pnDmOjCBPZV+V2vf3h9bGCE6u9uo05RAaWzVNd+NWIXiC3CEZ
|
||||
Nd4ksdMdRv9dX2VPMYIC5zCCAuMCAQEwaTBiMQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3Rl
|
||||
IENvbnN1bHRpbmcgKFB0eSkgTHRkLjEsMCoGA1UEAxMjVGhhd3RlIFBlcnNvbmFsIEZyZWVtYWls
|
||||
IElzc3VpbmcgQ0ECAw5c+TAJBgUrDgMCGgUAoIIBUzAYBgkqhkiG9w0BCQMxCwYJKoZIhvcNAQcB
|
||||
MBwGCSqGSIb3DQEJBTEPFw0wNTA1MDgxODE3NDZaMCMGCSqGSIb3DQEJBDEWBBQSkG9j6+hB0pKp
|
||||
fV9tCi/iP59sNTB4BgkrBgEEAYI3EAQxazBpMGIxCzAJBgNVBAYTAlpBMSUwIwYDVQQKExxUaGF3
|
||||
dGUgQ29uc3VsdGluZyAoUHR5KSBMdGQuMSwwKgYDVQQDEyNUaGF3dGUgUGVyc29uYWwgRnJlZW1h
|
||||
aWwgSXNzdWluZyBDQQIDDlz5MHoGCyqGSIb3DQEJEAILMWugaTBiMQswCQYDVQQGEwJaQTElMCMG
|
||||
A1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkgTHRkLjEsMCoGA1UEAxMjVGhhd3RlIFBlcnNv
|
||||
bmFsIEZyZWVtYWlsIElzc3VpbmcgQ0ECAw5c+TANBgkqhkiG9w0BAQEFAASCAQAm1GeF7dWfMvrW
|
||||
8yMPjkhE+R8D1DsiCoWSCp+5gAQm7lcK7V3KrZh5howfpI3TmCZUbbaMxOH+7aKRKpFemxoBY5Q8
|
||||
rnCkbpg/++/+MI01T69hF/rgMmrGcrv2fIYy8EaARLG0xUVFSZHSP+NQSYz0TTmh4cAESHMzY3JA
|
||||
nHOoUkuPyl8RXrimY1zn0lceMXlweZRouiPGuPNl1hQKw8P+GhOC5oLlM71UtStnrlk3P9gqX5v7
|
||||
Tj7Hx057oVfY8FMevjxGwU3EK5TczHezHbWWgTyum9l2ZQbUQsDJxSniD3BM46C1VcbDLPaotAZ0
|
||||
fTYLZizQfm5hcWEbfYVzkSzLAAAAAAAA
|
||||
------=_Part_5028_7368284.1115579351471--
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
From xxxx@xxxx.com Tue May 10 11:28:07 2005
|
||||
Return-Path: <xxxx@xxxx.com>
|
||||
X-Original-To: xxxx@xxxx.com
|
||||
Delivered-To: xxxx@xxxx.com
|
||||
Received: from localhost (localhost [127.0.0.1])
|
||||
by xxx.xxxxx.com (Postfix) with ESMTP id 50FD3A96F
|
||||
for <xxxx@xxxx.com>; Tue, 10 May 2005 17:26:50 +0000 (GMT)
|
||||
Received: from xxx.xxxxx.com ([127.0.0.1])
|
||||
by localhost (xxx.xxxxx.com [127.0.0.1]) (amavisd-new, port 10024)
|
||||
with LMTP id 70060-03 for <xxxx@xxxx.com>;
|
||||
Tue, 10 May 2005 17:26:49 +0000 (GMT)
|
||||
Received: from xxx.xxxxx.com (xxx.xxxxx.com [69.36.39.150])
|
||||
by xxx.xxxxx.com (Postfix) with ESMTP id 8B957A94B
|
||||
for <xxxx@xxxx.com>; Tue, 10 May 2005 17:26:48 +0000 (GMT)
|
||||
Received: from xxx.xxxxx.com (xxx.xxxxx.com [64.233.184.203])
|
||||
by xxx.xxxxx.com (Postfix) with ESMTP id 9972514824C
|
||||
for <xxxx@xxxx.com>; Tue, 10 May 2005 12:26:40 -0500 (CDT)
|
||||
Received: by xxx.xxxxx.com with SMTP id 68so1694448wri
|
||||
for <xxxx@xxxx.com>; Tue, 10 May 2005 10:26:40 -0700 (PDT)
|
||||
DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws;
|
||||
s=beta; d=xxxxx.com;
|
||||
h=received:message-id:date:from:reply-to:to:subject:mime-version:content-type;
|
||||
b=g8ZO5ttS6GPEMAz9WxrRk9+9IXBUfQIYsZLL6T88+ECbsXqGIgfGtzJJFn6o9CE3/HMrrIGkN5AisxVFTGXWxWci5YA/7PTVWwPOhJff5BRYQDVNgRKqMl/SMttNrrRElsGJjnD1UyQ/5kQmcBxq2PuZI5Zc47u6CILcuoBcM+A=
|
||||
Received: by 10.54.96.19 with SMTP id t19mr621017wrb;
|
||||
Tue, 10 May 2005 10:26:39 -0700 (PDT)
|
||||
Received: by 10.54.110.5 with HTTP; Tue, 10 May 2005 10:26:39 -0700 (PDT)
|
||||
Message-ID: <xxxx@xxxx.com>
|
||||
Date: Tue, 10 May 2005 11:26:39 -0600
|
||||
From: Test Tester <xxxx@xxxx.com>
|
||||
Reply-To: Test Tester <xxxx@xxxx.com>
|
||||
To: xxxx@xxxx.com, xxxx@xxxx.com
|
||||
Subject: Another PDF
|
||||
Mime-Version: 1.0
|
||||
Content-Type: multipart/mixed;
|
||||
boundary="----=_Part_2192_32400445.1115745999735"
|
||||
X-Virus-Scanned: amavisd-new at textdrive.com
|
||||
|
||||
------=_Part_2192_32400445.1115745999735
|
||||
Content-Type: text/plain; charset=ISO-8859-1
|
||||
Content-Transfer-Encoding: quoted-printable
|
||||
Content-Disposition: inline
|
||||
|
||||
Just attaching another PDF, here, to see what the message looks like,
|
||||
and to see if I can figure out what is going wrong here.
|
||||
|
||||
------=_Part_2192_32400445.1115745999735
|
||||
Content-Type: application/pdf; name="broken.pdf"
|
||||
Content-Transfer-Encoding: base64
|
||||
Content-Disposition: attachment; filename="broken.pdf"
|
||||
|
||||
JVBERi0xLjQNCiXk9tzfDQoxIDAgb2JqDQo8PCAvTGVuZ3RoIDIgMCBSDQogICAvRmlsdGVyIC9G
|
||||
bGF0ZURlY29kZQ0KPj4NCnN0cmVhbQ0KeJy9Wt2KJbkNvm/od6jrhZxYln9hWEh2p+8HBvICySaE
|
||||
ycLuTV4/1ifJ9qnq09NpSBimu76yLUuy/qzqcPz7+em3Ixx/CDc6CsXxs3b5+fvfjr/8cPz6/BRu
|
||||
rbfAx/n3739/fuJylJ5u5fjX81OuDr4deK4Bz3z/aDP+8fz0yw8g0Ofq7ktr1Mn+u28rvhy/jVeD
|
||||
QSa+9YNKHP/pxjvDNfVAx/m3MFz54FhvTbaseaxiDoN2LeMVMw+yA7RbHSCDzxZuaYB2E1Yay7QU
|
||||
x89vz0+tyFDKMlAHK5yqLmnjF+c4RjEiQIUeKwblXMe+AsZjN1J5yGQL5DHpDHksurM81rF6PKab
|
||||
gK6zAarIDzIiUY23rJsN9iorAE816aIu6lsgAdQFsuhhkHOUFgVjp2GjMqSewITXNQ27jrMeamkg
|
||||
1rPI3iLWG2CIaSBB+V1245YVRICGbbpYKHc2USFDl6M09acQVQYhlwIrkBNLISvXhGlF1wi5FHCw
|
||||
wxZkoGNJlVeJCEsqKA+3YAV5AMb6KkeaqEJQmFKKQU8T1pRi2ihE1Y4CDrqoYFFXYjJJOatsyzuI
|
||||
8SIlykuxKTMibWK8H1PgEvqYgs4GmQSrEjJAalgGirIhik+p4ZQN9E3ETFPAHE1b8pp1l/0Rc1gl
|
||||
fQs0ABWvyoZZzU8VnPXwVVcO9BEsyjEJaO6eBoZRyKGlrKoYoOygA8BGIzgwN3RQ15ouigG5idZQ
|
||||
fx2U4Db2CqiLO0WHAZoylGiCAqhniNQjFjQPSkmjwfNTgQ6M1Ih+eWo36wFmjIxDJZiGUBiWsAyR
|
||||
xX3EekGOizkGI96Ol9zVZTAivikURhRsHh2E3JhWMpSTZCnnonrLhMCodgrNcgo4uyJUJc6qnVss
|
||||
nrGd1Ptr0YwisCOYyIbUwVjV4xBUNLbguSO2YHujonAMJkMdSI7bIw91Akq2AUlMUWGFTMAOamjU
|
||||
OvZQCxIkY2pCpMFo/IwLdVLHs6nddwTRrgoVbvLU9eB0G4EMndV0TNoxHbt3JBWwK6hhv3iHfDtF
|
||||
yokB302IpEBTnWICde4uYc/1khDbSIkQopO6lcqamGBu1OSE3N5IPSsZX00CkSHRiiyx6HQIShsS
|
||||
HSVNswdVsaOUSAWq9aYhDtGDaoG5a3lBGkYt/lFlBFt1UqrYnzVtUpUQnLiZeouKgf1KhRBViRRk
|
||||
ExepJCzTwEmFDalIRbLEGtw0gfpESOpIAF/NnpPzcVCG86s0g2DuSyd41uhNGbEgaSrWEXORErbw
|
||||
------=_Part_2192_32400445.1115745999735--
|
||||
|
|
@ -1,59 +0,0 @@
|
|||
Return-Path: <xxx@xxxx.xxx>
|
||||
Received: from xxx.xxxx.xxx by xxx.xxxx.xxx with ESMTP id 6AAEE3B4D23 for <xxx@xxxx.xxx>; Sun, 8 May 2005 12:30:23 -0500
|
||||
Received: from xxx.xxxx.xxx by xxx.xxxx.xxx with ESMTP id j48HUC213279 for <xxx@xxxx.xxx>; Sun, 8 May 2005 12:30:13 -0500
|
||||
Received: from conversion-xxx.xxxx.xxx.net by xxx.xxxx.xxx id <0IG600901LQ64I@xxx.xxxx.xxx> for <xxx@xxxx.xxx>; Sun, 8 May 2005 12:30:12 -0500
|
||||
Received: from agw1 by xxx.xxxx.xxx with ESMTP id <0IG600JFYLYCAxxx@xxxx.xxx> for <xxx@xxxx.xxx>; Sun, 8 May 2005 12:30:12 -0500
|
||||
Date: Sun, 8 May 2005 12:30:08 -0500
|
||||
From: xxx@xxxx.xxx
|
||||
To: xxx@xxxx.xxx
|
||||
Message-Id: <7864245.1115573412626.JavaMxxx@xxxx.xxx>
|
||||
Subject: Filth
|
||||
Mime-Version: 1.0
|
||||
Content-Type: multipart/mixed; boundary=mimepart_427e4cb4ca329_133ae40413c81ef
|
||||
X-Mms-Priority: 1
|
||||
X-Mms-Transaction-Id: 3198421808-0
|
||||
X-Mms-Message-Type: 0
|
||||
X-Mms-Sender-Visibility: 1
|
||||
X-Mms-Read-Reply: 1
|
||||
X-Original-To: xxx@xxxx.xxx
|
||||
X-Mms-Message-Class: 0
|
||||
X-Mms-Delivery-Report: 0
|
||||
X-Mms-Mms-Version: 16
|
||||
Delivered-To: xxx@xxxx.xxx
|
||||
X-Nokia-Ag-Version: 2.0
|
||||
|
||||
This is a multi-part message in MIME format.
|
||||
|
||||
--mimepart_427e4cb4ca329_133ae40413c81ef
|
||||
Content-Type: multipart/mixed; boundary=mimepart_427e4cb4cbd97_133ae40413c8217
|
||||
|
||||
|
||||
|
||||
--mimepart_427e4cb4cbd97_133ae40413c8217
|
||||
Content-Type: text/plain; charset=utf-8
|
||||
Content-Transfer-Encoding: 7bit
|
||||
Content-Disposition: inline
|
||||
Content-Location: text.txt
|
||||
|
||||
Some text
|
||||
|
||||
--mimepart_427e4cb4cbd97_133ae40413c8217--
|
||||
|
||||
--mimepart_427e4cb4ca329_133ae40413c81ef
|
||||
Content-Type: text/plain; charset=us-ascii
|
||||
Content-Transfer-Encoding: 7bit
|
||||
|
||||
|
||||
--
|
||||
This Orange Multi Media Message was sent wirefree from an Orange
|
||||
MMS phone. If you would like to reply, please text or phone the
|
||||
sender directly by using the phone number listed in the sender's
|
||||
address. To learn more about Orange's Multi Media Messaging
|
||||
Service, find us on the Web at xxx.xxxx.xxx.uk/mms
|
||||
|
||||
|
||||
--mimepart_427e4cb4ca329_133ae40413c81ef
|
||||
|
||||
|
||||
--mimepart_427e4cb4ca329_133ae40413c81ef-
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
Return-Path: <xxx@xxxx.xxx>
|
||||
Received: from xxx.xxxx.xxx by xxx.xxxx.xxx with ESMTP id C1B953B4CB6 for <xxxxx@Exxx.xxxx.xxx>; Tue, 10 May 2005 15:27:05 -0500
|
||||
Received: from SMS-GTYxxx.xxxx.xxx by xxx.xxxx.xxx with ESMTP id ca for <xxxxx@Exxx.xxxx.xxx>; Tue, 10 May 2005 15:27:04 -0500
|
||||
Received: from xxx.xxxx.xxx by SMS-GTYxxx.xxxx.xxx with ESMTP id j4AKR3r23323 for <xxxxx@Exxx.xxxx.xxx>; Tue, 10 May 2005 15:27:03 -0500
|
||||
Date: Tue, 10 May 2005 15:27:03 -0500
|
||||
From: xxx@xxxx.xxx
|
||||
Sender: xxx@xxxx.xxx
|
||||
To: xxxxxxxxxxx@xxxx.xxxx.xxx
|
||||
Message-Id: <xxx@xxxx.xxx>
|
||||
X-Original-To: xxxxxxxxxxx@xxxx.xxxx.xxx
|
||||
Delivered-To: xxx@xxxx.xxx
|
||||
Importance: normal
|
||||
|
||||
Test test. Hi. Waving. m
|
||||
|
||||
----------------------------------------------------------------
|
||||
Sent via Bell Mobility's Text Messaging service.
|
||||
Envoyé par le service de messagerie texte de Bell Mobilité.
|
||||
----------------------------------------------------------------
|
|
@ -1,20 +0,0 @@
|
|||
Return-Path: <xxx@xxxx.xxx>
|
||||
Received: from xxx.xxxx.xxx by xxx.xxxx.xxx with ESMTP id C1B953B4CB6 for <xxxxx@Exxx.xxxx.xxx>; Tue, 10 May 2005 15:27:05 -0500
|
||||
Received: from SMS-GTYxxx.xxxx.xxx by xxx.xxxx.xxx with ESMTP id ca for <xxxxx@Exxx.xxxx.xxx>; Tue, 10 May 2005 15:27:04 -0500
|
||||
Received: from xxx.xxxx.xxx by SMS-GTYxxx.xxxx.xxx with ESMTP id j4AKR3r23323 for <xxxxx@Exxx.xxxx.xxx>; Tue, 10 May 2005 15:27:03 -0500
|
||||
Date: Tue, 10 May 2005 15:27:03 -0500
|
||||
From: xxx@xxxx.xxx
|
||||
Sender: xxx@xxxx.xxx
|
||||
To: xxxxxxxxxxx@xxxx.xxxx.xxx
|
||||
Message-Id: <xxx@xxxx.xxx>
|
||||
X-Original-To: xxxxxxxxxxx@xxxx.xxxx.xxx
|
||||
Delivered-To: xxx@xxxx.xxx
|
||||
Importance: normal
|
||||
Content-Type: text/plain; charset=us-ascii
|
||||
|
||||
Test test. Hi. Waving. m
|
||||
|
||||
----------------------------------------------------------------
|
||||
Sent via Bell Mobility's Text Messaging service.
|
||||
Envoyé par le service de messagerie texte de Bell Mobilité.
|
||||
----------------------------------------------------------------
|
|
@ -1,66 +0,0 @@
|
|||
Mime-Version: 1.0 (Apple Message framework v730)
|
||||
Content-Type: multipart/mixed; boundary=Apple-Mail-13-196941151
|
||||
Message-Id: <9169D984-4E0B-45EF-82D4-8F5E53AD7012@example.com>
|
||||
From: foo@example.com
|
||||
Subject: testing
|
||||
Date: Mon, 6 Jun 2005 22:21:22 +0200
|
||||
To: blah@example.com
|
||||
|
||||
|
||||
--Apple-Mail-13-196941151
|
||||
Content-Type: multipart/mixed;
|
||||
boundary=Apple-Mail-12-196940926
|
||||
|
||||
|
||||
--Apple-Mail-12-196940926
|
||||
Content-Transfer-Encoding: quoted-printable
|
||||
Content-Type: text/plain;
|
||||
charset=ISO-8859-1;
|
||||
delsp=yes;
|
||||
format=flowed
|
||||
|
||||
This is the first part.
|
||||
|
||||
--Apple-Mail-12-196940926
|
||||
Content-Transfer-Encoding: 7bit
|
||||
Content-Type: text/x-ruby-script;
|
||||
x-unix-mode=0666;
|
||||
name="test.rb"
|
||||
Content-Disposition: attachment;
|
||||
filename=test.rb
|
||||
|
||||
puts "testing, testing"
|
||||
|
||||
--Apple-Mail-12-196940926
|
||||
Content-Transfer-Encoding: base64
|
||||
Content-Type: application/pdf;
|
||||
x-unix-mode=0666;
|
||||
name="test.pdf"
|
||||
Content-Disposition: inline;
|
||||
filename=test.pdf
|
||||
|
||||
YmxhaCBibGFoIGJsYWg=
|
||||
|
||||
--Apple-Mail-12-196940926
|
||||
Content-Transfer-Encoding: 7bit
|
||||
Content-Type: text/plain;
|
||||
charset=US-ASCII;
|
||||
format=flowed
|
||||
|
||||
|
||||
|
||||
--Apple-Mail-12-196940926--
|
||||
|
||||
--Apple-Mail-13-196941151
|
||||
Content-Transfer-Encoding: base64
|
||||
Content-Type: application/pkcs7-signature;
|
||||
name=smime.p7s
|
||||
Content-Disposition: attachment;
|
||||
filename=smime.p7s
|
||||
|
||||
jamisSqGSIb3DQEHAqCAMIjamisxCzAJBgUrDgMCGgUAMIAGCSqGSjamisEHAQAAoIIFSjCCBUYw
|
||||
ggQujamisQICBD++ukQwDQYJKojamisNAQEFBQAwMTELMAkGA1UEBhMCRjamisAKBgNVBAoTA1RE
|
||||
QzEUMBIGjamisxMLVERDIE9DRVMgQ0jamisNMDQwMjI5MTE1OTAxWhcNMDYwMjamisIyOTAxWjCB
|
||||
gDELMAkGA1UEjamisEsxKTAnBgNVBAoTIEjamisuIG9yZ2FuaXNhdG9yaXNrIHRpbjamisRuaW5=
|
||||
|
||||
--Apple-Mail-13-196941151--
|
|
@ -1,47 +0,0 @@
|
|||
From xxxxxxxxx.xxxxxxx@gmail.com Sun May 8 19:07:09 2005
|
||||
Return-Path: <xxxxxxxxx.xxxxxxx@gmail.com>
|
||||
Message-ID: <e85734b90505081209eaaa17b@mail.gmail.com>
|
||||
Date: Sun, 8 May 2005 14:09:11 -0500
|
||||
From: xxxxxxxxx xxxxxxx <xxxxxxxxx.xxxxxxx@gmail.com>
|
||||
Reply-To: xxxxxxxxx xxxxxxx <xxxxxxxxx.xxxxxxx@gmail.com>
|
||||
To: xxxxx xxxx <xxxxx@xxxxxxxxx.com>
|
||||
Subject: Fwd: Signed email causes file attachments
|
||||
In-Reply-To: <F6E2D0B4-CC35-4A91-BA4C-C7C712B10C13@mac.com>
|
||||
Mime-Version: 1.0
|
||||
Content-Type: multipart/mixed;
|
||||
boundary="----=_Part_5028_7368284.1115579351471"
|
||||
References: <F6E2D0B4-CC35-4A91-BA4C-C7C712B10C13@mac.com>
|
||||
|
||||
------=_Part_5028_7368284.1115579351471
|
||||
Content-Type: text/plain; charset=ISO-8859-1
|
||||
Content-Transfer-Encoding: quoted-printable
|
||||
Content-Disposition: inline
|
||||
|
||||
We should not include these files or vcards as attachments.
|
||||
|
||||
---------- Forwarded message ----------
|
||||
From: xxxxx xxxxxx <xxxxxxxx@xxx.com>
|
||||
Date: May 8, 2005 1:17 PM
|
||||
Subject: Signed email causes file attachments
|
||||
To: xxxxxxx@xxxxxxxxxx.com
|
||||
|
||||
|
||||
Hi,
|
||||
|
||||
Test attachments oddly encoded with japanese charset.
|
||||
|
||||
|
||||
------=_Part_5028_7368284.1115579351471
|
||||
Content-Type: application/octet-stream; name*=iso-2022-jp'ja'01%20Quien%20Te%20Dij%8aat.%20Pitbull.mp3
|
||||
Content-Transfer-Encoding: base64
|
||||
Content-Disposition: attachment
|
||||
|
||||
MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIIGFDCCAs0w
|
||||
ggI2oAMCAQICAw5c+TANBgkqhkiG9w0BAQQFADBiMQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhh
|
||||
d3RlIENvbnN1bHRpbmcgKFB0eSkgTHRkLjEsMCoGA1UEAxMjVGhhd3RlIFBlcnNvbmFsIEZyZWVt
|
||||
YWlsIElzc3VpbmcgQ0EwHhcNMDUwMzI5MDkzOTEwWhcNMDYwMzI5MDkzOTEwWjBCMR8wHQYDVQQD
|
||||
ExZUaGF3dGUgRnJlZW1haWwgTWVtYmVyMR8wHQYJKoZIhvcNAQkBFhBzbWhhdW5jaEBtYWMuY29t
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn90dPsYS3LjfMY211OSYrDQLzwNYPlAL
|
||||
7+/0XA+kdy8/rRnyEHFGwhNCDmg0B6pxC7z3xxJD/8GfCd+IYUUNUQV5m9MkxfP9pTVXZVIYLaBw
|
||||
------=_Part_5028_7368284.1115579351471--
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
Received: from xxx.xxx.xxx ([xxx.xxx.xxx.xxx] verified)
|
||||
by xxx.com (CommuniGate Pro SMTP 4.2.8)
|
||||
with SMTP id 2532598 for xxx@xxx.com; Wed, 23 Feb 2005 17:51:49 -0500
|
||||
Received-SPF: softfail
|
||||
receiver=xxx.com; client-ip=xxx.xxx.xxx.xxx; envelope-from=xxx@xxx.xxx
|
||||
quite Delivered-To: xxx@xxx.xxx
|
||||
Received: by xxx.xxx.xxx (Wostfix, from userid xxx)
|
||||
id 0F87F333; Wed, 23 Feb 2005 16:16:17 -0600
|
||||
Date: Wed, 23 Feb 2005 18:20:17 -0400
|
||||
From: "xxx xxx" <xxx@xxx.xxx>
|
||||
Message-ID: <4D6AA7EB.6490534@xxx.xxx>
|
||||
To: xxx@xxx.com
|
||||
Subject: Stop adware/spyware once and for all.
|
||||
X-Scanned-By: MIMEDefang 2.11 (www dot roaringpenguin dot com slash mimedefang)
|
||||
|
||||
You are infected with:
|
||||
Ad Ware and Spy Ware
|
||||
|
||||
Get your free scan and removal download now,
|
||||
before it gets any worse.
|
||||
|
||||
http://xxx.xxx.info?aid=3D13&?stat=3D4327kdzt
|
||||
|
||||
|
||||
|
||||
|
||||
no more? (you will still be infected)
|
||||
http://xxx.xxx.info/discon/?xxx@xxx.com
|
|
@ -1,14 +0,0 @@
|
|||
Mime-Version: 1.0 (Apple Message framework v730)
|
||||
Message-Id: <9169D984-4E0B-45EF-82D4-8F5E53AD7012@example.com>
|
||||
From: foo@example.com
|
||||
Subject: testing
|
||||
Date: Mon, 6 Jun 2005 22:21:22 +0200
|
||||
To: blah@example.com
|
||||
Content-Transfer-Encoding: quoted-printable
|
||||
Content-Type: text/plain
|
||||
|
||||
A fax has arrived from remote ID ''.=0D=0A-----------------------=
|
||||
-------------------------------------=0D=0ATime: 3/9/2006 3:50:52=
|
||||
PM=0D=0AReceived from remote ID: =0D=0AInbound user ID XXXXXXXXXX, r=
|
||||
outing code XXXXXXXXX=0D=0AResult: (0/352;0/0) Successful Send=0D=0AP=
|
||||
age record: 1 - 1=0D=0AElapsed time: 00:58 on channel 11=0D=0A
|
|
@ -1,104 +0,0 @@
|
|||
Return-Path: <mikel.other@baci>
|
||||
Received: from some.isp.com by baci with ESMTP id 632BD5758 for <mikel.lindsaar@baci>; Sun, 21 Oct 2007 19:38:21 +1000
|
||||
Date: Sun, 21 Oct 2007 19:38:13 +1000
|
||||
From: Mikel Lindsaar <mikel.other@baci>
|
||||
Reply-To: Mikel Lindsaar <mikel.other@baci>
|
||||
To: mikel.lindsaar@baci
|
||||
Message-Id: <009601c813c6$19df3510$0437d30a@mikel091a>
|
||||
Subject: Testing outlook
|
||||
Mime-Version: 1.0
|
||||
Content-Type: multipart/alternative; boundary=----=_NextPart_000_0093_01C81419.EB75E850
|
||||
Delivered-To: mikel.lindsaar@baci
|
||||
X-Mimeole: Produced By Microsoft MimeOLE V6.00.2900.3138
|
||||
X-Msmail-Priority: Normal
|
||||
|
||||
This is a multi-part message in MIME format.
|
||||
|
||||
|
||||
------=_NextPart_000_0093_01C81419.EB75E850
|
||||
Content-Type: text/plain; charset=iso-8859-1
|
||||
Content-Transfer-Encoding: Quoted-printable
|
||||
|
||||
Hello
|
||||
This is an outlook test
|
||||
|
||||
So there.
|
||||
|
||||
Me.
|
||||
|
||||
------=_NextPart_000_0093_01C81419.EB75E850
|
||||
Content-Type: text/html; charset=iso-8859-1
|
||||
Content-Transfer-Encoding: Quoted-printable
|
||||
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
||||
<HTML><HEAD>
|
||||
<META http-equiv=3DContent-Type content=3D"text/html; =
|
||||
charset=3Diso-8859-1">
|
||||
<META content=3D"MSHTML 6.00.6000.16525" name=3DGENERATOR>
|
||||
<STYLE></STYLE>
|
||||
</HEAD>
|
||||
<BODY bgColor=3D#ffffff>
|
||||
<DIV><FONT face=3DArial size=3D2>Hello</FONT></DIV>
|
||||
<DIV><FONT face=3DArial size=3D2><STRONG>This is an outlook=20
|
||||
test</STRONG></FONT></DIV>
|
||||
<DIV><FONT face=3DArial size=3D2><STRONG></STRONG></FONT> </DIV>
|
||||
<DIV><FONT face=3DArial size=3D2><STRONG>So there.</STRONG></FONT></DIV>
|
||||
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
|
||||
<DIV><FONT face=3DArial size=3D2>Me.</FONT></DIV></BODY></HTML>
|
||||
|
||||
|
||||
------=_NextPart_000_0093_01C81419.EB75E850--
|
||||
|
||||
|
||||
Return-Path: <mikel.other@baci>
|
||||
Received: from some.isp.com by baci with ESMTP id 632BD5758 for <mikel.lindsaar@baci>; Sun, 21 Oct 2007 19:38:21 +1000
|
||||
Date: Sun, 21 Oct 2007 19:38:13 +1000
|
||||
From: Mikel Lindsaar <mikel.other@baci>
|
||||
Reply-To: Mikel Lindsaar <mikel.other@baci>
|
||||
To: mikel.lindsaar@baci
|
||||
Message-Id: <009601c813c6$19df3510$0437d30a@mikel091a>
|
||||
Subject: Testing outlook
|
||||
Mime-Version: 1.0
|
||||
Content-Type: multipart/alternative; boundary=----=_NextPart_000_0093_01C81419.EB75E850
|
||||
Delivered-To: mikel.lindsaar@baci
|
||||
X-Mimeole: Produced By Microsoft MimeOLE V6.00.2900.3138
|
||||
X-Msmail-Priority: Normal
|
||||
|
||||
This is a multi-part message in MIME format.
|
||||
|
||||
|
||||
------=_NextPart_000_0093_01C81419.EB75E850
|
||||
Content-Type: text/plain; charset=iso-8859-1
|
||||
Content-Transfer-Encoding: Quoted-printable
|
||||
|
||||
Hello
|
||||
This is an outlook test
|
||||
|
||||
So there.
|
||||
|
||||
Me.
|
||||
|
||||
------=_NextPart_000_0093_01C81419.EB75E850
|
||||
Content-Type: text/html; charset=iso-8859-1
|
||||
Content-Transfer-Encoding: Quoted-printable
|
||||
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
||||
<HTML><HEAD>
|
||||
<META http-equiv=3DContent-Type content=3D"text/html; =
|
||||
charset=3Diso-8859-1">
|
||||
<META content=3D"MSHTML 6.00.6000.16525" name=3DGENERATOR>
|
||||
<STYLE></STYLE>
|
||||
</HEAD>
|
||||
<BODY bgColor=3D#ffffff>
|
||||
<DIV><FONT face=3DArial size=3D2>Hello</FONT></DIV>
|
||||
<DIV><FONT face=3DArial size=3D2><STRONG>This is an outlook=20
|
||||
test</STRONG></FONT></DIV>
|
||||
<DIV><FONT face=3DArial size=3D2><STRONG></STRONG></FONT> </DIV>
|
||||
<DIV><FONT face=3DArial size=3D2><STRONG>So there.</STRONG></FONT></DIV>
|
||||
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
|
||||
<DIV><FONT face=3DArial size=3D2>Me.</FONT></DIV></BODY></HTML>
|
||||
|
||||
|
||||
------=_NextPart_000_0093_01C81419.EB75E850--
|
||||
|
||||
|
|
@ -1,100 +0,0 @@
|
|||
From jamis@37signals.com Thu Feb 22 11:20:31 2007
|
||||
Mime-Version: 1.0 (Apple Message framework v752.3)
|
||||
Message-Id: <2CCE0408-10C7-4045-9B16-A1C11C31469B@37signals.com>
|
||||
Content-Type: multipart/signed;
|
||||
micalg=sha1;
|
||||
boundary=Apple-Mail-42-587703407;
|
||||
protocol="application/pkcs7-signature"
|
||||
To: Jamis Buck <jamis@jamisbuck.org>
|
||||
Subject: Testing attachments
|
||||
From: Jamis Buck <jamis@37signals.com>
|
||||
Date: Thu, 22 Feb 2007 11:20:31 -0700
|
||||
|
||||
|
||||
--Apple-Mail-42-587703407
|
||||
Content-Type: multipart/mixed;
|
||||
boundary=Apple-Mail-41-587703287
|
||||
|
||||
|
||||
--Apple-Mail-41-587703287
|
||||
Content-Transfer-Encoding: 7bit
|
||||
Content-Type: text/plain;
|
||||
charset=US-ASCII;
|
||||
format=flowed
|
||||
|
||||
Here is a test of an attachment via email.
|
||||
|
||||
- Jamis
|
||||
|
||||
|
||||
--Apple-Mail-41-587703287
|
||||
Content-Transfer-Encoding: base64
|
||||
Content-Type: image/png;
|
||||
x-unix-mode=0644;
|
||||
name=byo-ror-cover.png
|
||||
Content-Disposition: inline;
|
||||
filename=truncated.png
|
||||
|
||||
iVBORw0KGgoAAAANSUhEUgAAAKUAAADXCAYAAAB7wZEQAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz
|
||||
AAALEgAACxIB0t1+/AAAABd0RVh0Q3JlYXRpb24gVGltZQAxLzI1LzIwMDeD9CJVAAAAGHRFWHRT
|
||||
b2Z0d2FyZQBBZG9iZSBGaXJld29ya3NPsx9OAAAyBWlUWHRYTUw6Y29tLmFkb2JlLnhtcDw/eHBh
|
||||
Y2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+Cjx4OnhtcG1l
|
||||
dGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDQuMS1j
|
||||
MDIwIDEuMjU1NzE2LCBUdWUgT2N0IDEwIDIwMDYgMjM6MTY6MzQiPgogICA8cmRmOlJERiB4bWxu
|
||||
czpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPgogICAg
|
||||
ICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAgICAgICAgICB4bWxuczp4YXA9Imh0
|
||||
dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iPgogICAgICAgICA8eGFwOkNyZWF0b3JUb29sPkFk
|
||||
b2JlIEZpcmV3b3JrcyBDUzM8L3hhcDpDcmVhdG9yVG9vbD4KICAgICAgICAgPHhhcDpDcmVhdGVE
|
||||
YXRlPjIwMDctMDEtMjVUMDU6Mjg6MjFaPC94YXA6Q3JlYXRlRGF0ZT4KICAgICAgICAgPHhhcDpN
|
||||
b2RpZnlEYXRlPjIwMDctMDEtMjVUMDU6Mjg6MjFaPC94YXA6TW9kaWZ5RGF0ZT4KICAgICAgPC9y
|
||||
ZGY6RGVzY3JpcHRpb24+CiAgICAgIDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiCiAgICAg
|
||||
ICAgICAgIHhtbG5zOmRjPSJodHRwOi8vcHVybC5vcmcvZGMvZWxlbWVudHMvMS4xLyI+CiAgICAg
|
||||
ICAgIDxkYzpmb3JtYXQ+aW1hZ2UvcG5nPC9kYzpmb3JtYXQ+CiAgICAgIDwvcmRmOkRlc2NyaXB0
|
||||
hhojpmnJMfaYFmSkXWg5PGCmHXVj/c9At0hSK2xGdd8F3muk0VFjb4f5Ue0ksQ8qAcq0delaXhdb
|
||||
DjKNnF+3B3t9kObZYmk7AZgWYqO9anpR3wpM9sQ5XslB9a+kWyTtNb0fOmudzGHfPFBQDKesyycm
|
||||
DBL7Cw5bXjIEuci+SSOm/LYnXDZu6iuPEj8lYBb+OU8xx1f9m+e5rhJiYKqjo5vHfiZp+VUkW9xc
|
||||
Ufd6JHNWc47PkQqb9ie3SLEZB/ZqyAssiqURY+G35iOMZUrHbasHnb80QAPv9FHtAbJIyro7bi5b
|
||||
ai2TEAKen5+LJNWrglZjm3UbZvt7KryA2J5b5J1jZF8kL6GzvG1Zqx54Y1y7J7n20wMOt9frG2sW
|
||||
uwGP07kNz3732vf6bfvAvLldfS+9fts2euXY37D+R29FGZdlnhzV4TTFmPJduBP2RbNNua4rTqcT
|
||||
Qt7Xy1KUB0AHSdP5AZQYvHZg7WD1XvYeMO1A9HhZPqMX5KXbMBrn2efxns/ee21674efxz4Tp/fq
|
||||
2HZ648dgYaC1i3Vq1IbNPq3PvDTPezY9FaRISjvnzWqdgcWN8EJgjnNq+Z7ktOm9l2Nfth28EZi4
|
||||
bG/we5JwxM+Tql47/D/X6b38I8/RyxvxPJrX6zvQbo3h9jyJx+C0ALX327QETHl5eYlaYCT5rPTb
|
||||
+5/rAq26t3lKIxV/p88hq6ptngdgCzoPjJqndiLfc/6y5A14WeDFGNPct4iUsJBV2bYzLEV7m83s
|
||||
6Rp63VPhHKC/g/LzaU9qexJRr56043JWinqAtfZqsSm1sjoznthl54dtCqv+uL4nIY+oYWuc3+nH
|
||||
kGfn8b0HQpvOYLQAZUDanbJs3jQhITZEgdarZK+cO6ySlL13rut5nFaN23s7u3Snz6eRPTkCoc2/
|
||||
Vp1zHfZVFpZ87FiMVLV1iqyK5rlzfji2GzjfDsodlD+Weo5UD4h6PwKqzQMqID0tq2VjjFVSMpis
|
||||
ZLRAs7sePZBZAHI+gIanB8I7MD+femAceeUe2Kxa5jS950kZ1p5eNEdeX1+jFmSpZ+1EdWCsDcne
|
||||
NPNgUHNw3aYpnzv9PGTX0uo94EtN9qq1rOdxe3kc79T8ukeHJJ8Fnxej6qlylbLLsjQLOy6Xy2a1
|
||||
kefs/N+nM7+S7IG5/E5Yc7F003pWErLjbH0O5cGadiMptSB/DZ5U5DI9yeg5MFYyMj8lC/Y7/Xjq
|
||||
OZlWcnpg9aQfXz2HRq+Wn5xOp6gN8tWq8R44e2pfyzLYemEgprst+XXk2Zj2nXlbsG05BprndTMv
|
||||
C3QRaXczshhVsHnMgfYn80Y2g5JureA6wBasPeP7LkE/jvZMJAaf/g/U2RelHsisvan5FqweIAHg
|
||||
Pwc7L68GxvVDAAAAAElFTkSuQmCC
|
||||
|
||||
--Apple-Mail-41-587703287--
|
||||
|
||||
--Apple-Mail-42-587703407
|
||||
Content-Transfer-Encoding: base64
|
||||
Content-Type: application/pkcs7-signature;
|
||||
name=smime.p7s
|
||||
Content-Disposition: attachment;
|
||||
filename=smime.p7s
|
||||
|
||||
MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIIGJzCCAuAw
|
||||
ggJJoAMCAQICEFjnFNYXwDEZRWY5EkfzopUwDQYJKoZIhvcNAQEFBQAwYjELMAkGA1UEBhMCWkEx
|
||||
JTAjBgNVBAoTHFRoYXd0ZSBDb25zdWx0aW5nIChQdHkpIEx0ZC4xLDAqBgNVBAMTI1RoYXd0ZSBQ
|
||||
ZXJzb25hbCBGcmVlbWFpbCBJc3N1aW5nIENBMB4XDTA2MDkxMjE3MDExMloXDTA3MDkxMjE3MDEx
|
||||
MlowRTEfMB0GA1UEAxMWVGhhd3RlIEZyZWVtYWlsIE1lbWJlcjEiMCAGCSqGSIb3DQEJARYTamFt
|
||||
aXNAMzdzaWduYWxzLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAO2A9JeOFIFJ
|
||||
G6z8pTcAldrZ2nMe+Xb1tNrbHgoVzN/QhHXM4qst2Ml93cmFLjMmwG7P9RJeU4oNx+jTqVoBB7NV
|
||||
Ne1/o56Do0KhfMZ9iUDQdPLbkZMq4EEpFMdm6PyM3muRKwPhj66iAWe/osCb8DowUK2f66vaRx0Z
|
||||
Y0MQHIIrXE02Ta4IfAhIfPqBLkZ4WgTYBHN9vMdYea1jF0GO4gqGk1wqwb3yxv2QMYMbwJ6SI+k/
|
||||
ZjkSR/OilTCBhwYLKoZIhvcNAQkQAgsxeKB2MGIxCzAJBgNVBAYTAlpBMSUwIwYDVQQKExxUaGF3
|
||||
dGUgQ29uc3VsdGluZyAoUHR5KSBMdGQuMSwwKgYDVQQDEyNUaGF3dGUgUGVyc29uYWwgRnJlZW1h
|
||||
aWwgSXNzdWluZyBDQQIQWOcU1hfAMRlFZjkSR/OilTANBgkqhkiG9w0BAQEFAASCAQCfwQiC3v6/
|
||||
yleRDGv3bJ4nQYQ+c3mz3+mn3Xi6uU35n3piwxZZaWRdmLyiXPvU+QReHpSf3l2qsEZM3sdE0XF9
|
||||
eRul/+QTFJcDNXOEAxG1zC2Gpz+6c6RrX4Ou12Pwkp+pNrZWTSY/mZgdqcArupOBcZi7qBjoWcy5
|
||||
wb54dfvSSjrjmqLbkH/E8ww/6gGQuU/xXpAUZgUrTmQHrNKeIdSh5oDkOxFaFWvnmb8Z/2ixKqW/
|
||||
Ux6WqamyvBtTs/5YBEtnpZOk+uVoscYEUBhU+DVJ2OSvTdXSivMtBdXmGTsG22k+P1NGUHi/A7ev
|
||||
xPaO0uk4V8xyjNlN4HPuGpkrlXwPAAAAAAAA
|
||||
|
||||
--Apple-Mail-42-587703407--
|
|
@ -1,14 +0,0 @@
|
|||
From jamis@37signals.com Mon May 2 16:07:05 2005
|
||||
Mime-Version: 1.0 (Apple Message framework v622)
|
||||
Content-Transfer-Encoding: base64
|
||||
Message-Id: <d3b8cf8e49f04480850c28713a1f473e@37signals.com>
|
||||
Content-Type: text/plain;
|
||||
charset=EUC-KR;
|
||||
format=flowed
|
||||
To: jamis@37signals.com
|
||||
From: Jamis Buck <jamis@37signals.com>
|
||||
Subject: Re: Test: =?UTF-8?B?Iua8ouWtlyI=?= mid =?UTF-8?B?Iua8ouWtlyI=?= tail
|
||||
Date: Mon, 2 May 2005 16:07:05 -0600
|
||||
|
||||
tOu6zrrQwMcguLbC+bChwfa3ziwgv+y4rrTCIMfPs6q01MC7ILnPvcC0z7TZLg0KDQrBpiDAzLin
|
||||
wLogSmFtaXPA1LTPtNku
|
|
@ -1 +0,0 @@
|
|||
second mail
|
|
@ -1,3 +0,0 @@
|
|||
Hello there,
|
||||
|
||||
Mr. <%= @recipient %>
|
|
@ -1 +0,0 @@
|
|||
let's go!
|
|
@ -1,6 +0,0 @@
|
|||
%p Hello there,
|
||||
|
||||
%p
|
||||
Mr.
|
||||
= @recipient
|
||||
from haml
|
|
@ -1,6 +0,0 @@
|
|||
%p Hello there,
|
||||
|
||||
%p
|
||||
Mr.
|
||||
= @recipient
|
||||
from haml
|
|
@ -1 +0,0 @@
|
|||
Ignored when searching for implicitly multipart parts.
|
|
@ -1 +0,0 @@
|
|||
Ignored when searching for implicitly multipart parts.
|
|
@ -1,10 +0,0 @@
|
|||
<html>
|
||||
<body>
|
||||
HTML formatted message to <strong><%= @recipient %></strong>.
|
||||
</body>
|
||||
</html>
|
||||
<html>
|
||||
<body>
|
||||
HTML formatted message to <strong><%= @recipient %></strong>.
|
||||
</body>
|
||||
</html>
|
|
@ -1,2 +0,0 @@
|
|||
Plain text to <%= @recipient %>.
|
||||
Plain text to <%= @recipient %>.
|
|
@ -1 +0,0 @@
|
|||
yaml to: <%= @recipient %>
|
|
@ -1 +0,0 @@
|
|||
Hey Ho, <%= render :partial => "subtemplate" %>
|
|
@ -1,2 +0,0 @@
|
|||
xml.instruct!
|
||||
xml.test
|
|
@ -1,2 +0,0 @@
|
|||
xml.instruct!
|
||||
xml.test
|
|
@ -1,3 +0,0 @@
|
|||
Hello there,
|
||||
|
||||
Mr. <%= @recipient %>
|
|
@ -1,5 +0,0 @@
|
|||
Hello there,
|
||||
|
||||
Mr. <%= @recipient %>. Please see our greeting at <%= @welcome_url %> <%= welcome_url %>
|
||||
|
||||
<%= image_tag "somelogo.png" %>
|
|
@ -1,95 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
module MailerHelper
|
||||
def person_name
|
||||
"Mr. Joe Person"
|
||||
end
|
||||
end
|
||||
|
||||
class HelperMailer < ActionMailer::Base
|
||||
helper MailerHelper
|
||||
helper :example
|
||||
|
||||
def use_helper(recipient)
|
||||
recipients recipient
|
||||
subject "using helpers"
|
||||
from "tester@example.com"
|
||||
end
|
||||
|
||||
def use_example_helper(recipient)
|
||||
recipients recipient
|
||||
subject "using helpers"
|
||||
from "tester@example.com"
|
||||
self.body = { :text => "emphasize me!" }
|
||||
end
|
||||
|
||||
def use_mail_helper(recipient)
|
||||
recipients recipient
|
||||
subject "using mailing helpers"
|
||||
from "tester@example.com"
|
||||
self.body = { :text =>
|
||||
"But soft! What light through yonder window breaks? It is the east, " +
|
||||
"and Juliet is the sun. Arise, fair sun, and kill the envious moon, " +
|
||||
"which is sick and pale with grief that thou, her maid, art far more " +
|
||||
"fair than she. Be not her maid, for she is envious! Her vestal " +
|
||||
"livery is but sick and green, and none but fools do wear it. Cast " +
|
||||
"it off!"
|
||||
}
|
||||
end
|
||||
|
||||
def use_helper_method(recipient)
|
||||
recipients recipient
|
||||
subject "using helpers"
|
||||
from "tester@example.com"
|
||||
self.body = { :text => "emphasize me!" }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def name_of_the_mailer_class
|
||||
self.class.name
|
||||
end
|
||||
helper_method :name_of_the_mailer_class
|
||||
end
|
||||
|
||||
class MailerHelperTest < Test::Unit::TestCase
|
||||
def new_mail( charset="utf-8" )
|
||||
mail = TMail::Mail.new
|
||||
mail.set_content_type "text", "plain", { "charset" => charset } if charset
|
||||
mail
|
||||
end
|
||||
|
||||
def setup
|
||||
set_delivery_method :test
|
||||
ActionMailer::Base.perform_deliveries = true
|
||||
ActionMailer::Base.deliveries = []
|
||||
|
||||
@recipient = 'test@localhost'
|
||||
end
|
||||
|
||||
def teardown
|
||||
restore_delivery_method
|
||||
end
|
||||
|
||||
def test_use_helper
|
||||
mail = HelperMailer.create_use_helper(@recipient)
|
||||
assert_match %r{Mr. Joe Person}, mail.encoded
|
||||
end
|
||||
|
||||
def test_use_example_helper
|
||||
mail = HelperMailer.create_use_example_helper(@recipient)
|
||||
assert_match %r{<em><strong><small>emphasize me!}, mail.encoded
|
||||
end
|
||||
|
||||
def test_use_helper_method
|
||||
mail = HelperMailer.create_use_helper_method(@recipient)
|
||||
assert_match %r{HelperMailer}, mail.encoded
|
||||
end
|
||||
|
||||
def test_use_mail_helper
|
||||
mail = HelperMailer.create_use_mail_helper(@recipient)
|
||||
assert_match %r{ But soft!}, mail.encoded
|
||||
assert_match %r{east, and\n Juliet}, mail.encoded
|
||||
end
|
||||
end
|
||||
|
|
@ -1,122 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class RenderMailer < ActionMailer::Base
|
||||
def inline_template(recipient)
|
||||
recipients recipient
|
||||
subject "using helpers"
|
||||
from "tester@example.com"
|
||||
body render(:inline => "Hello, <%= @world %>", :body => { :world => "Earth" })
|
||||
end
|
||||
|
||||
def file_template(recipient)
|
||||
recipients recipient
|
||||
subject "using helpers"
|
||||
from "tester@example.com"
|
||||
body render(:file => "signed_up", :body => { :recipient => recipient })
|
||||
end
|
||||
|
||||
def rxml_template(recipient)
|
||||
recipients recipient
|
||||
subject "rendering rxml template"
|
||||
from "tester@example.com"
|
||||
end
|
||||
|
||||
def included_subtemplate(recipient)
|
||||
recipients recipient
|
||||
subject "Including another template in the one being rendered"
|
||||
from "tester@example.com"
|
||||
end
|
||||
|
||||
def included_old_subtemplate(recipient)
|
||||
recipients recipient
|
||||
subject "Including another template in the one being rendered"
|
||||
from "tester@example.com"
|
||||
body render(:inline => "Hello, <%= render \"subtemplate\" %>", :body => { :world => "Earth" })
|
||||
end
|
||||
|
||||
def initialize_defaults(method_name)
|
||||
super
|
||||
mailer_name "test_mailer"
|
||||
end
|
||||
end
|
||||
|
||||
class FirstMailer < ActionMailer::Base
|
||||
def share(recipient)
|
||||
recipients recipient
|
||||
subject "using helpers"
|
||||
from "tester@example.com"
|
||||
end
|
||||
end
|
||||
|
||||
class SecondMailer < ActionMailer::Base
|
||||
def share(recipient)
|
||||
recipients recipient
|
||||
subject "using helpers"
|
||||
from "tester@example.com"
|
||||
end
|
||||
end
|
||||
|
||||
class RenderHelperTest < Test::Unit::TestCase
|
||||
def setup
|
||||
set_delivery_method :test
|
||||
ActionMailer::Base.perform_deliveries = true
|
||||
ActionMailer::Base.deliveries = []
|
||||
|
||||
@recipient = 'test@localhost'
|
||||
end
|
||||
|
||||
def teardown
|
||||
restore_delivery_method
|
||||
end
|
||||
|
||||
def test_inline_template
|
||||
mail = RenderMailer.create_inline_template(@recipient)
|
||||
assert_equal "Hello, Earth", mail.body.strip
|
||||
end
|
||||
|
||||
def test_file_template
|
||||
mail = RenderMailer.create_file_template(@recipient)
|
||||
assert_equal "Hello there, \n\nMr. test@localhost", mail.body.strip
|
||||
end
|
||||
|
||||
def test_rxml_template
|
||||
mail = RenderMailer.deliver_rxml_template(@recipient)
|
||||
assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test/>", mail.body.strip
|
||||
end
|
||||
|
||||
def test_included_subtemplate
|
||||
mail = RenderMailer.deliver_included_subtemplate(@recipient)
|
||||
assert_equal "Hey Ho, let's go!", mail.body.strip
|
||||
end
|
||||
|
||||
def test_deprecated_old_subtemplate
|
||||
assert_raises ActionView::ActionViewError do
|
||||
RenderMailer.deliver_included_old_subtemplate(@recipient)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class FirstSecondHelperTest < Test::Unit::TestCase
|
||||
def setup
|
||||
set_delivery_method :test
|
||||
ActionMailer::Base.perform_deliveries = true
|
||||
ActionMailer::Base.deliveries = []
|
||||
|
||||
@recipient = 'test@localhost'
|
||||
end
|
||||
|
||||
def teardown
|
||||
restore_delivery_method
|
||||
end
|
||||
|
||||
def test_ordering
|
||||
mail = FirstMailer.create_share(@recipient)
|
||||
assert_equal "first mail", mail.body.strip
|
||||
mail = SecondMailer.create_share(@recipient)
|
||||
assert_equal "second mail", mail.body.strip
|
||||
mail = FirstMailer.create_share(@recipient)
|
||||
assert_equal "first mail", mail.body.strip
|
||||
mail = SecondMailer.create_share(@recipient)
|
||||
assert_equal "second mail", mail.body.strip
|
||||
end
|
||||
end
|
|
@ -1,979 +0,0 @@
|
|||
# encoding: utf-8
|
||||
require 'abstract_unit'
|
||||
|
||||
class FunkyPathMailer < ActionMailer::Base
|
||||
self.template_root = "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
|
||||
|
||||
def multipart_with_template_path_with_dots(recipient)
|
||||
recipients recipient
|
||||
subject "Have a lovely picture"
|
||||
from "Chad Fowler <chad@chadfowler.com>"
|
||||
attachment :content_type => "image/jpeg",
|
||||
:body => "not really a jpeg, we're only testing, after all"
|
||||
end
|
||||
end
|
||||
|
||||
class TestMailer < ActionMailer::Base
|
||||
def signed_up(recipient)
|
||||
@recipients = recipient
|
||||
@subject = "[Signed up] Welcome #{recipient}"
|
||||
@from = "system@loudthinking.com"
|
||||
@sent_on = Time.local(2004, 12, 12)
|
||||
@body["recipient"] = recipient
|
||||
end
|
||||
|
||||
def cancelled_account(recipient)
|
||||
self.recipients = recipient
|
||||
self.subject = "[Cancelled] Goodbye #{recipient}"
|
||||
self.from = "system@loudthinking.com"
|
||||
self.sent_on = Time.local(2004, 12, 12)
|
||||
self.body = "Goodbye, Mr. #{recipient}"
|
||||
end
|
||||
|
||||
def cc_bcc(recipient)
|
||||
recipients recipient
|
||||
subject "testing bcc/cc"
|
||||
from "system@loudthinking.com"
|
||||
sent_on Time.local(2004, 12, 12)
|
||||
cc "nobody@loudthinking.com"
|
||||
bcc "root@loudthinking.com"
|
||||
body "Nothing to see here."
|
||||
end
|
||||
|
||||
def different_reply_to(recipient)
|
||||
recipients recipient
|
||||
subject "testing reply_to"
|
||||
from "system@loudthinking.com"
|
||||
sent_on Time.local(2008, 5, 23)
|
||||
reply_to "atraver@gmail.com"
|
||||
body "Nothing to see here."
|
||||
end
|
||||
|
||||
def iso_charset(recipient)
|
||||
@recipients = recipient
|
||||
@subject = "testing isø charsets"
|
||||
@from = "system@loudthinking.com"
|
||||
@sent_on = Time.local 2004, 12, 12
|
||||
@cc = "nobody@loudthinking.com"
|
||||
@bcc = "root@loudthinking.com"
|
||||
@body = "Nothing to see here."
|
||||
@charset = "iso-8859-1"
|
||||
end
|
||||
|
||||
def unencoded_subject(recipient)
|
||||
@recipients = recipient
|
||||
@subject = "testing unencoded subject"
|
||||
@from = "system@loudthinking.com"
|
||||
@sent_on = Time.local 2004, 12, 12
|
||||
@cc = "nobody@loudthinking.com"
|
||||
@bcc = "root@loudthinking.com"
|
||||
@body = "Nothing to see here."
|
||||
end
|
||||
|
||||
def extended_headers(recipient)
|
||||
@recipients = recipient
|
||||
@subject = "testing extended headers"
|
||||
@from = "Grytøyr <stian1@example.net>"
|
||||
@sent_on = Time.local 2004, 12, 12
|
||||
@cc = "Grytøyr <stian2@example.net>"
|
||||
@bcc = "Grytøyr <stian3@example.net>"
|
||||
@body = "Nothing to see here."
|
||||
@charset = "iso-8859-1"
|
||||
end
|
||||
|
||||
def utf8_body(recipient)
|
||||
@recipients = recipient
|
||||
@subject = "testing utf-8 body"
|
||||
@from = "Foo áëô îü <extended@example.net>"
|
||||
@sent_on = Time.local 2004, 12, 12
|
||||
@cc = "Foo áëô îü <extended@example.net>"
|
||||
@bcc = "Foo áëô îü <extended@example.net>"
|
||||
@body = "åœö blah"
|
||||
@charset = "utf-8"
|
||||
end
|
||||
|
||||
def multipart_with_mime_version(recipient)
|
||||
recipients recipient
|
||||
subject "multipart with mime_version"
|
||||
from "test@example.com"
|
||||
sent_on Time.local(2004, 12, 12)
|
||||
mime_version "1.1"
|
||||
content_type "multipart/alternative"
|
||||
|
||||
part "text/plain" do |p|
|
||||
p.body = "blah"
|
||||
end
|
||||
|
||||
part "text/html" do |p|
|
||||
p.body = "<b>blah</b>"
|
||||
end
|
||||
end
|
||||
|
||||
def multipart_with_utf8_subject(recipient)
|
||||
recipients recipient
|
||||
subject "Foo áëô îü"
|
||||
from "test@example.com"
|
||||
charset "utf-8"
|
||||
|
||||
part "text/plain" do |p|
|
||||
p.body = "blah"
|
||||
end
|
||||
|
||||
part "text/html" do |p|
|
||||
p.body = "<b>blah</b>"
|
||||
end
|
||||
end
|
||||
|
||||
def explicitly_multipart_example(recipient, ct=nil)
|
||||
recipients recipient
|
||||
subject "multipart example"
|
||||
from "test@example.com"
|
||||
sent_on Time.local(2004, 12, 12)
|
||||
body "plain text default"
|
||||
content_type ct if ct
|
||||
|
||||
part "text/html" do |p|
|
||||
p.charset = "iso-8859-1"
|
||||
p.body = "blah"
|
||||
end
|
||||
|
||||
attachment :content_type => "image/jpeg", :filename => "foo.jpg",
|
||||
:body => "123456789"
|
||||
end
|
||||
|
||||
def implicitly_multipart_example(recipient, cs = nil, order = nil)
|
||||
@recipients = recipient
|
||||
@subject = "multipart example"
|
||||
@from = "test@example.com"
|
||||
@sent_on = Time.local 2004, 12, 12
|
||||
@body = { "recipient" => recipient }
|
||||
@charset = cs if cs
|
||||
@implicit_parts_order = order if order
|
||||
end
|
||||
|
||||
def implicitly_multipart_with_utf8
|
||||
recipients "no.one@nowhere.test"
|
||||
subject "Foo áëô îü"
|
||||
from "some.one@somewhere.test"
|
||||
template "implicitly_multipart_example"
|
||||
body ({ "recipient" => "no.one@nowhere.test" })
|
||||
end
|
||||
|
||||
def html_mail(recipient)
|
||||
recipients recipient
|
||||
subject "html mail"
|
||||
from "test@example.com"
|
||||
body "<em>Emphasize</em> <strong>this</strong>"
|
||||
content_type "text/html"
|
||||
end
|
||||
|
||||
def html_mail_with_underscores(recipient)
|
||||
subject "html mail with underscores"
|
||||
body %{<a href="http://google.com" target="_blank">_Google</a>}
|
||||
end
|
||||
|
||||
def custom_template(recipient)
|
||||
recipients recipient
|
||||
subject "[Signed up] Welcome #{recipient}"
|
||||
from "system@loudthinking.com"
|
||||
sent_on Time.local(2004, 12, 12)
|
||||
template "signed_up"
|
||||
|
||||
body["recipient"] = recipient
|
||||
end
|
||||
|
||||
def custom_templating_extension(recipient)
|
||||
recipients recipient
|
||||
subject "[Signed up] Welcome #{recipient}"
|
||||
from "system@loudthinking.com"
|
||||
sent_on Time.local(2004, 12, 12)
|
||||
|
||||
body["recipient"] = recipient
|
||||
end
|
||||
|
||||
def various_newlines(recipient)
|
||||
recipients recipient
|
||||
subject "various newlines"
|
||||
from "test@example.com"
|
||||
body "line #1\nline #2\rline #3\r\nline #4\r\r" +
|
||||
"line #5\n\nline#6\r\n\r\nline #7"
|
||||
end
|
||||
|
||||
def various_newlines_multipart(recipient)
|
||||
recipients recipient
|
||||
subject "various newlines multipart"
|
||||
from "test@example.com"
|
||||
content_type "multipart/alternative"
|
||||
part :content_type => "text/plain", :body => "line #1\nline #2\rline #3\r\nline #4\r\r"
|
||||
part :content_type => "text/html", :body => "<p>line #1</p>\n<p>line #2</p>\r<p>line #3</p>\r\n<p>line #4</p>\r\r"
|
||||
end
|
||||
|
||||
def nested_multipart(recipient)
|
||||
recipients recipient
|
||||
subject "nested multipart"
|
||||
from "test@example.com"
|
||||
content_type "multipart/mixed"
|
||||
part :content_type => "multipart/alternative", :content_disposition => "inline", :headers => { "foo" => "bar" } do |p|
|
||||
p.part :content_type => "text/plain", :body => "test text\nline #2"
|
||||
p.part :content_type => "text/html", :body => "<b>test</b> HTML<br/>\nline #2"
|
||||
end
|
||||
attachment :content_type => "application/octet-stream",:filename => "test.txt", :body => "test abcdefghijklmnopqstuvwxyz"
|
||||
end
|
||||
|
||||
def nested_multipart_with_body(recipient)
|
||||
recipients recipient
|
||||
subject "nested multipart with body"
|
||||
from "test@example.com"
|
||||
content_type "multipart/mixed"
|
||||
part :content_type => "multipart/alternative", :content_disposition => "inline", :body => "Nothing to see here." do |p|
|
||||
p.part :content_type => "text/html", :body => "<b>test</b> HTML<br/>"
|
||||
end
|
||||
end
|
||||
|
||||
def attachment_with_custom_header(recipient)
|
||||
recipients recipient
|
||||
subject "custom header in attachment"
|
||||
from "test@example.com"
|
||||
content_type "multipart/related"
|
||||
part :content_type => "text/html", :body => 'yo'
|
||||
attachment :content_type => "image/jpeg",:filename => "test.jpeg", :body => "i am not a real picture", :headers => { 'Content-ID' => '<test@test.com>' }
|
||||
end
|
||||
|
||||
def unnamed_attachment(recipient)
|
||||
recipients recipient
|
||||
subject "nested multipart"
|
||||
from "test@example.com"
|
||||
content_type "multipart/mixed"
|
||||
part :content_type => "text/plain", :body => "hullo"
|
||||
attachment :content_type => "application/octet-stream", :body => "test abcdefghijklmnopqstuvwxyz"
|
||||
end
|
||||
|
||||
def headers_with_nonalpha_chars(recipient)
|
||||
recipients recipient
|
||||
subject "nonalpha chars"
|
||||
from "One: Two <test@example.com>"
|
||||
cc "Three: Four <test@example.com>"
|
||||
bcc "Five: Six <test@example.com>"
|
||||
body "testing"
|
||||
end
|
||||
|
||||
def custom_content_type_attributes
|
||||
recipients "no.one@nowhere.test"
|
||||
subject "custom content types"
|
||||
from "some.one@somewhere.test"
|
||||
content_type "text/plain; format=flowed"
|
||||
body "testing"
|
||||
end
|
||||
|
||||
def return_path
|
||||
recipients "no.one@nowhere.test"
|
||||
subject "return path test"
|
||||
from "some.one@somewhere.test"
|
||||
body "testing"
|
||||
headers "return-path" => "another@somewhere.test"
|
||||
end
|
||||
|
||||
class <<self
|
||||
attr_accessor :received_body
|
||||
end
|
||||
|
||||
def receive(mail)
|
||||
self.class.received_body = mail.body
|
||||
end
|
||||
end
|
||||
|
||||
uses_mocha 'ActionMailerTest' do
|
||||
|
||||
class ActionMailerTest < Test::Unit::TestCase
|
||||
include ActionMailer::Quoting
|
||||
|
||||
def encode( text, charset="utf-8" )
|
||||
quoted_printable( text, charset )
|
||||
end
|
||||
|
||||
def new_mail( charset="utf-8" )
|
||||
mail = TMail::Mail.new
|
||||
mail.mime_version = "1.0"
|
||||
if charset
|
||||
mail.set_content_type "text", "plain", { "charset" => charset }
|
||||
end
|
||||
mail
|
||||
end
|
||||
|
||||
# Replacing logger work around for mocha bug. Should be fixed in mocha 0.3.3
|
||||
def setup
|
||||
set_delivery_method :test
|
||||
ActionMailer::Base.perform_deliveries = true
|
||||
ActionMailer::Base.raise_delivery_errors = true
|
||||
ActionMailer::Base.deliveries = []
|
||||
|
||||
@original_logger = TestMailer.logger
|
||||
@recipient = 'test@localhost'
|
||||
end
|
||||
|
||||
def teardown
|
||||
TestMailer.logger = @original_logger
|
||||
restore_delivery_method
|
||||
end
|
||||
|
||||
def test_nested_parts
|
||||
created = nil
|
||||
assert_nothing_raised { created = TestMailer.create_nested_multipart(@recipient)}
|
||||
assert_equal 2,created.parts.size
|
||||
assert_equal 2,created.parts.first.parts.size
|
||||
|
||||
assert_equal "multipart/mixed", created.content_type
|
||||
assert_equal "multipart/alternative", created.parts.first.content_type
|
||||
assert_equal "bar", created.parts.first.header['foo'].to_s
|
||||
assert_equal "text/plain", created.parts.first.parts.first.content_type
|
||||
assert_equal "text/html", created.parts.first.parts[1].content_type
|
||||
assert_equal "application/octet-stream", created.parts[1].content_type
|
||||
end
|
||||
|
||||
def test_nested_parts_with_body
|
||||
created = nil
|
||||
assert_nothing_raised { created = TestMailer.create_nested_multipart_with_body(@recipient)}
|
||||
assert_equal 1,created.parts.size
|
||||
assert_equal 2,created.parts.first.parts.size
|
||||
|
||||
assert_equal "multipart/mixed", created.content_type
|
||||
assert_equal "multipart/alternative", created.parts.first.content_type
|
||||
assert_equal "Nothing to see here.", created.parts.first.parts.first.body
|
||||
assert_equal "text/plain", created.parts.first.parts.first.content_type
|
||||
assert_equal "text/html", created.parts.first.parts[1].content_type
|
||||
end
|
||||
|
||||
def test_attachment_with_custom_header
|
||||
created = nil
|
||||
assert_nothing_raised { created = TestMailer.create_attachment_with_custom_header(@recipient)}
|
||||
assert_equal "<test@test.com>", created.parts[1].header['content-id'].to_s
|
||||
end
|
||||
|
||||
def test_signed_up
|
||||
expected = new_mail
|
||||
expected.to = @recipient
|
||||
expected.subject = "[Signed up] Welcome #{@recipient}"
|
||||
expected.body = "Hello there, \n\nMr. #{@recipient}"
|
||||
expected.from = "system@loudthinking.com"
|
||||
expected.date = Time.local(2004, 12, 12)
|
||||
|
||||
created = nil
|
||||
assert_nothing_raised { created = TestMailer.create_signed_up(@recipient) }
|
||||
assert_not_nil created
|
||||
assert_equal expected.encoded, created.encoded
|
||||
|
||||
assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) }
|
||||
assert_not_nil ActionMailer::Base.deliveries.first
|
||||
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
|
||||
end
|
||||
|
||||
def test_custom_template
|
||||
expected = new_mail
|
||||
expected.to = @recipient
|
||||
expected.subject = "[Signed up] Welcome #{@recipient}"
|
||||
expected.body = "Hello there, \n\nMr. #{@recipient}"
|
||||
expected.from = "system@loudthinking.com"
|
||||
expected.date = Time.local(2004, 12, 12)
|
||||
|
||||
created = nil
|
||||
assert_nothing_raised { created = TestMailer.create_custom_template(@recipient) }
|
||||
assert_not_nil created
|
||||
assert_equal expected.encoded, created.encoded
|
||||
end
|
||||
|
||||
def test_custom_templating_extension
|
||||
#
|
||||
# N.b., custom_templating_extension.text.plain.haml is expected to be in fixtures/test_mailer directory
|
||||
expected = new_mail
|
||||
expected.to = @recipient
|
||||
expected.subject = "[Signed up] Welcome #{@recipient}"
|
||||
expected.body = "Hello there, \n\nMr. #{@recipient}"
|
||||
expected.from = "system@loudthinking.com"
|
||||
expected.date = Time.local(2004, 12, 12)
|
||||
|
||||
# Stub the render method so no alternative renderers need be present.
|
||||
ActionView::Base.any_instance.stubs(:render).returns("Hello there, \n\nMr. #{@recipient}")
|
||||
|
||||
# If the template is not registered, there should be no parts.
|
||||
created = nil
|
||||
assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
|
||||
assert_not_nil created
|
||||
assert_equal 0, created.parts.length
|
||||
|
||||
ActionMailer::Base.register_template_extension('haml')
|
||||
|
||||
# Now that the template is registered, there should be one part. The text/plain part.
|
||||
created = nil
|
||||
assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
|
||||
assert_not_nil created
|
||||
assert_equal 2, created.parts.length
|
||||
assert_equal 'text/plain', created.parts[0].content_type
|
||||
assert_equal 'text/html', created.parts[1].content_type
|
||||
end
|
||||
|
||||
def test_cancelled_account
|
||||
expected = new_mail
|
||||
expected.to = @recipient
|
||||
expected.subject = "[Cancelled] Goodbye #{@recipient}"
|
||||
expected.body = "Goodbye, Mr. #{@recipient}"
|
||||
expected.from = "system@loudthinking.com"
|
||||
expected.date = Time.local(2004, 12, 12)
|
||||
|
||||
created = nil
|
||||
assert_nothing_raised { created = TestMailer.create_cancelled_account(@recipient) }
|
||||
assert_not_nil created
|
||||
assert_equal expected.encoded, created.encoded
|
||||
|
||||
assert_nothing_raised { TestMailer.deliver_cancelled_account(@recipient) }
|
||||
assert_not_nil ActionMailer::Base.deliveries.first
|
||||
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
|
||||
end
|
||||
|
||||
def test_cc_bcc
|
||||
expected = new_mail
|
||||
expected.to = @recipient
|
||||
expected.subject = "testing bcc/cc"
|
||||
expected.body = "Nothing to see here."
|
||||
expected.from = "system@loudthinking.com"
|
||||
expected.cc = "nobody@loudthinking.com"
|
||||
expected.bcc = "root@loudthinking.com"
|
||||
expected.date = Time.local 2004, 12, 12
|
||||
|
||||
created = nil
|
||||
assert_nothing_raised do
|
||||
created = TestMailer.create_cc_bcc @recipient
|
||||
end
|
||||
assert_not_nil created
|
||||
assert_equal expected.encoded, created.encoded
|
||||
|
||||
assert_nothing_raised do
|
||||
TestMailer.deliver_cc_bcc @recipient
|
||||
end
|
||||
|
||||
assert_not_nil ActionMailer::Base.deliveries.first
|
||||
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
|
||||
end
|
||||
|
||||
def test_reply_to
|
||||
expected = new_mail
|
||||
|
||||
expected.to = @recipient
|
||||
expected.subject = "testing reply_to"
|
||||
expected.body = "Nothing to see here."
|
||||
expected.from = "system@loudthinking.com"
|
||||
expected.reply_to = "atraver@gmail.com"
|
||||
expected.date = Time.local 2008, 5, 23
|
||||
|
||||
created = nil
|
||||
assert_nothing_raised do
|
||||
created = TestMailer.create_different_reply_to @recipient
|
||||
end
|
||||
assert_not_nil created
|
||||
assert_equal expected.encoded, created.encoded
|
||||
|
||||
assert_nothing_raised do
|
||||
TestMailer.deliver_different_reply_to @recipient
|
||||
end
|
||||
|
||||
assert_not_nil ActionMailer::Base.deliveries.first
|
||||
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
|
||||
end
|
||||
|
||||
def test_iso_charset
|
||||
expected = new_mail( "iso-8859-1" )
|
||||
expected.to = @recipient
|
||||
expected.subject = encode "testing isø charsets", "iso-8859-1"
|
||||
expected.body = "Nothing to see here."
|
||||
expected.from = "system@loudthinking.com"
|
||||
expected.cc = "nobody@loudthinking.com"
|
||||
expected.bcc = "root@loudthinking.com"
|
||||
expected.date = Time.local 2004, 12, 12
|
||||
|
||||
created = nil
|
||||
assert_nothing_raised do
|
||||
created = TestMailer.create_iso_charset @recipient
|
||||
end
|
||||
assert_not_nil created
|
||||
assert_equal expected.encoded, created.encoded
|
||||
|
||||
assert_nothing_raised do
|
||||
TestMailer.deliver_iso_charset @recipient
|
||||
end
|
||||
|
||||
assert_not_nil ActionMailer::Base.deliveries.first
|
||||
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
|
||||
end
|
||||
|
||||
def test_unencoded_subject
|
||||
expected = new_mail
|
||||
expected.to = @recipient
|
||||
expected.subject = "testing unencoded subject"
|
||||
expected.body = "Nothing to see here."
|
||||
expected.from = "system@loudthinking.com"
|
||||
expected.cc = "nobody@loudthinking.com"
|
||||
expected.bcc = "root@loudthinking.com"
|
||||
expected.date = Time.local 2004, 12, 12
|
||||
|
||||
created = nil
|
||||
assert_nothing_raised do
|
||||
created = TestMailer.create_unencoded_subject @recipient
|
||||
end
|
||||
assert_not_nil created
|
||||
assert_equal expected.encoded, created.encoded
|
||||
|
||||
assert_nothing_raised do
|
||||
TestMailer.deliver_unencoded_subject @recipient
|
||||
end
|
||||
|
||||
assert_not_nil ActionMailer::Base.deliveries.first
|
||||
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
|
||||
end
|
||||
|
||||
def test_instances_are_nil
|
||||
assert_nil ActionMailer::Base.new
|
||||
assert_nil TestMailer.new
|
||||
end
|
||||
|
||||
def test_deliveries_array
|
||||
assert_not_nil ActionMailer::Base.deliveries
|
||||
assert_equal 0, ActionMailer::Base.deliveries.size
|
||||
TestMailer.deliver_signed_up(@recipient)
|
||||
assert_equal 1, ActionMailer::Base.deliveries.size
|
||||
assert_not_nil ActionMailer::Base.deliveries.first
|
||||
end
|
||||
|
||||
def test_perform_deliveries_flag
|
||||
ActionMailer::Base.perform_deliveries = false
|
||||
TestMailer.deliver_signed_up(@recipient)
|
||||
assert_equal 0, ActionMailer::Base.deliveries.size
|
||||
ActionMailer::Base.perform_deliveries = true
|
||||
TestMailer.deliver_signed_up(@recipient)
|
||||
assert_equal 1, ActionMailer::Base.deliveries.size
|
||||
end
|
||||
|
||||
def test_doesnt_raise_errors_when_raise_delivery_errors_is_false
|
||||
ActionMailer::Base.raise_delivery_errors = false
|
||||
TestMailer.any_instance.expects(:perform_delivery_test).raises(Exception)
|
||||
assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) }
|
||||
end
|
||||
|
||||
def test_performs_delivery_via_sendmail
|
||||
sm = mock()
|
||||
sm.expects(:print).with(anything)
|
||||
sm.expects(:flush)
|
||||
IO.expects(:popen).once.with('/usr/sbin/sendmail -i -t', 'w+').yields(sm)
|
||||
ActionMailer::Base.delivery_method = :sendmail
|
||||
TestMailer.deliver_signed_up(@recipient)
|
||||
end
|
||||
|
||||
def test_delivery_logs_sent_mail
|
||||
mail = TestMailer.create_signed_up(@recipient)
|
||||
logger = mock()
|
||||
logger.expects(:info).with("Sent mail to #{@recipient}")
|
||||
logger.expects(:debug).with("\n#{mail.encoded}")
|
||||
TestMailer.logger = logger
|
||||
TestMailer.deliver_signed_up(@recipient)
|
||||
end
|
||||
|
||||
def test_unquote_quoted_printable_subject
|
||||
msg = <<EOF
|
||||
From: me@example.com
|
||||
Subject: =?utf-8?Q?testing_testing_=D6=A4?=
|
||||
Content-Type: text/plain; charset=iso-8859-1
|
||||
|
||||
The body
|
||||
EOF
|
||||
mail = TMail::Mail.parse(msg)
|
||||
assert_equal "testing testing \326\244", mail.subject
|
||||
assert_equal "=?utf-8?Q?testing_testing_=D6=A4?=", mail.quoted_subject
|
||||
end
|
||||
|
||||
def test_unquote_7bit_subject
|
||||
msg = <<EOF
|
||||
From: me@example.com
|
||||
Subject: this == working?
|
||||
Content-Type: text/plain; charset=iso-8859-1
|
||||
|
||||
The body
|
||||
EOF
|
||||
mail = TMail::Mail.parse(msg)
|
||||
assert_equal "this == working?", mail.subject
|
||||
assert_equal "this == working?", mail.quoted_subject
|
||||
end
|
||||
|
||||
def test_unquote_7bit_body
|
||||
msg = <<EOF
|
||||
From: me@example.com
|
||||
Subject: subject
|
||||
Content-Type: text/plain; charset=iso-8859-1
|
||||
Content-Transfer-Encoding: 7bit
|
||||
|
||||
The=3Dbody
|
||||
EOF
|
||||
mail = TMail::Mail.parse(msg)
|
||||
assert_equal "The=3Dbody", mail.body.strip
|
||||
assert_equal "The=3Dbody", mail.quoted_body.strip
|
||||
end
|
||||
|
||||
def test_unquote_quoted_printable_body
|
||||
msg = <<EOF
|
||||
From: me@example.com
|
||||
Subject: subject
|
||||
Content-Type: text/plain; charset=iso-8859-1
|
||||
Content-Transfer-Encoding: quoted-printable
|
||||
|
||||
The=3Dbody
|
||||
EOF
|
||||
mail = TMail::Mail.parse(msg)
|
||||
assert_equal "The=body", mail.body.strip
|
||||
assert_equal "The=3Dbody", mail.quoted_body.strip
|
||||
end
|
||||
|
||||
def test_unquote_base64_body
|
||||
msg = <<EOF
|
||||
From: me@example.com
|
||||
Subject: subject
|
||||
Content-Type: text/plain; charset=iso-8859-1
|
||||
Content-Transfer-Encoding: base64
|
||||
|
||||
VGhlIGJvZHk=
|
||||
EOF
|
||||
mail = TMail::Mail.parse(msg)
|
||||
assert_equal "The body", mail.body.strip
|
||||
assert_equal "VGhlIGJvZHk=", mail.quoted_body.strip
|
||||
end
|
||||
|
||||
def test_extended_headers
|
||||
@recipient = "Grytøyr <test@localhost>"
|
||||
|
||||
expected = new_mail "iso-8859-1"
|
||||
expected.to = quote_address_if_necessary @recipient, "iso-8859-1"
|
||||
expected.subject = "testing extended headers"
|
||||
expected.body = "Nothing to see here."
|
||||
expected.from = quote_address_if_necessary "Grytøyr <stian1@example.net>", "iso-8859-1"
|
||||
expected.cc = quote_address_if_necessary "Grytøyr <stian2@example.net>", "iso-8859-1"
|
||||
expected.bcc = quote_address_if_necessary "Grytøyr <stian3@example.net>", "iso-8859-1"
|
||||
expected.date = Time.local 2004, 12, 12
|
||||
|
||||
created = nil
|
||||
assert_nothing_raised do
|
||||
created = TestMailer.create_extended_headers @recipient
|
||||
end
|
||||
|
||||
assert_not_nil created
|
||||
assert_equal expected.encoded, created.encoded
|
||||
|
||||
assert_nothing_raised do
|
||||
TestMailer.deliver_extended_headers @recipient
|
||||
end
|
||||
|
||||
assert_not_nil ActionMailer::Base.deliveries.first
|
||||
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
|
||||
end
|
||||
|
||||
def test_utf8_body_is_not_quoted
|
||||
@recipient = "Foo áëô îü <extended@example.net>"
|
||||
expected = new_mail "utf-8"
|
||||
expected.to = quote_address_if_necessary @recipient, "utf-8"
|
||||
expected.subject = "testing utf-8 body"
|
||||
expected.body = "åœö blah"
|
||||
expected.from = quote_address_if_necessary @recipient, "utf-8"
|
||||
expected.cc = quote_address_if_necessary @recipient, "utf-8"
|
||||
expected.bcc = quote_address_if_necessary @recipient, "utf-8"
|
||||
expected.date = Time.local 2004, 12, 12
|
||||
|
||||
created = TestMailer.create_utf8_body @recipient
|
||||
assert_match(/åœö blah/, created.encoded)
|
||||
end
|
||||
|
||||
def test_multiple_utf8_recipients
|
||||
@recipient = ["\"Foo áëô îü\" <extended@example.net>", "\"Example Recipient\" <me@example.com>"]
|
||||
expected = new_mail "utf-8"
|
||||
expected.to = quote_address_if_necessary @recipient, "utf-8"
|
||||
expected.subject = "testing utf-8 body"
|
||||
expected.body = "åœö blah"
|
||||
expected.from = quote_address_if_necessary @recipient.first, "utf-8"
|
||||
expected.cc = quote_address_if_necessary @recipient, "utf-8"
|
||||
expected.bcc = quote_address_if_necessary @recipient, "utf-8"
|
||||
expected.date = Time.local 2004, 12, 12
|
||||
|
||||
created = TestMailer.create_utf8_body @recipient
|
||||
assert_match(/\nFrom: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>\r/, created.encoded)
|
||||
assert_match(/\nTo: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>, Example Recipient <me/, created.encoded)
|
||||
end
|
||||
|
||||
def test_receive_decodes_base64_encoded_mail
|
||||
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email")
|
||||
TestMailer.receive(fixture)
|
||||
assert_match(/Jamis/, TestMailer.received_body)
|
||||
end
|
||||
|
||||
def test_receive_attachments
|
||||
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email2")
|
||||
mail = TMail::Mail.parse(fixture)
|
||||
attachment = mail.attachments.last
|
||||
assert_equal "smime.p7s", attachment.original_filename
|
||||
assert_equal "application/pkcs7-signature", attachment.content_type
|
||||
end
|
||||
|
||||
def test_decode_attachment_without_charset
|
||||
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email3")
|
||||
mail = TMail::Mail.parse(fixture)
|
||||
attachment = mail.attachments.last
|
||||
assert_equal 1026, attachment.read.length
|
||||
end
|
||||
|
||||
def test_attachment_using_content_location
|
||||
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email12")
|
||||
mail = TMail::Mail.parse(fixture)
|
||||
assert_equal 1, mail.attachments.length
|
||||
assert_equal "Photo25.jpg", mail.attachments.first.original_filename
|
||||
end
|
||||
|
||||
def test_attachment_with_text_type
|
||||
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email13")
|
||||
mail = TMail::Mail.parse(fixture)
|
||||
assert mail.has_attachments?
|
||||
assert_equal 1, mail.attachments.length
|
||||
assert_equal "hello.rb", mail.attachments.first.original_filename
|
||||
end
|
||||
|
||||
def test_decode_part_without_content_type
|
||||
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email4")
|
||||
mail = TMail::Mail.parse(fixture)
|
||||
assert_nothing_raised { mail.body }
|
||||
end
|
||||
|
||||
def test_decode_message_without_content_type
|
||||
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email5")
|
||||
mail = TMail::Mail.parse(fixture)
|
||||
assert_nothing_raised { mail.body }
|
||||
end
|
||||
|
||||
def test_decode_message_with_incorrect_charset
|
||||
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email6")
|
||||
mail = TMail::Mail.parse(fixture)
|
||||
assert_nothing_raised { mail.body }
|
||||
end
|
||||
|
||||
def test_multipart_with_mime_version
|
||||
mail = TestMailer.create_multipart_with_mime_version(@recipient)
|
||||
assert_equal "1.1", mail.mime_version
|
||||
end
|
||||
|
||||
def test_multipart_with_utf8_subject
|
||||
mail = TestMailer.create_multipart_with_utf8_subject(@recipient)
|
||||
assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail.encoded)
|
||||
end
|
||||
|
||||
def test_implicitly_multipart_with_utf8
|
||||
mail = TestMailer.create_implicitly_multipart_with_utf8
|
||||
assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail.encoded)
|
||||
end
|
||||
|
||||
def test_explicitly_multipart_messages
|
||||
mail = TestMailer.create_explicitly_multipart_example(@recipient)
|
||||
assert_equal 3, mail.parts.length
|
||||
assert_nil mail.content_type
|
||||
assert_equal "text/plain", mail.parts[0].content_type
|
||||
|
||||
assert_equal "text/html", mail.parts[1].content_type
|
||||
assert_equal "iso-8859-1", mail.parts[1].sub_header("content-type", "charset")
|
||||
assert_equal "inline", mail.parts[1].content_disposition
|
||||
|
||||
assert_equal "image/jpeg", mail.parts[2].content_type
|
||||
assert_equal "attachment", mail.parts[2].content_disposition
|
||||
assert_equal "foo.jpg", mail.parts[2].sub_header("content-disposition", "filename")
|
||||
assert_equal "foo.jpg", mail.parts[2].sub_header("content-type", "name")
|
||||
assert_nil mail.parts[2].sub_header("content-type", "charset")
|
||||
end
|
||||
|
||||
def test_explicitly_multipart_with_content_type
|
||||
mail = TestMailer.create_explicitly_multipart_example(@recipient, "multipart/alternative")
|
||||
assert_equal 3, mail.parts.length
|
||||
assert_equal "multipart/alternative", mail.content_type
|
||||
end
|
||||
|
||||
def test_explicitly_multipart_with_invalid_content_type
|
||||
mail = TestMailer.create_explicitly_multipart_example(@recipient, "text/xml")
|
||||
assert_equal 3, mail.parts.length
|
||||
assert_nil mail.content_type
|
||||
end
|
||||
|
||||
def test_implicitly_multipart_messages
|
||||
mail = TestMailer.create_implicitly_multipart_example(@recipient)
|
||||
assert_equal 3, mail.parts.length
|
||||
assert_equal "1.0", mail.mime_version
|
||||
assert_equal "multipart/alternative", mail.content_type
|
||||
assert_equal "text/yaml", mail.parts[0].content_type
|
||||
assert_equal "utf-8", mail.parts[0].sub_header("content-type", "charset")
|
||||
assert_equal "text/plain", mail.parts[1].content_type
|
||||
assert_equal "utf-8", mail.parts[1].sub_header("content-type", "charset")
|
||||
assert_equal "text/html", mail.parts[2].content_type
|
||||
assert_equal "utf-8", mail.parts[2].sub_header("content-type", "charset")
|
||||
end
|
||||
|
||||
def test_implicitly_multipart_messages_with_custom_order
|
||||
mail = TestMailer.create_implicitly_multipart_example(@recipient, nil, ["text/yaml", "text/plain"])
|
||||
assert_equal 3, mail.parts.length
|
||||
assert_equal "text/html", mail.parts[0].content_type
|
||||
assert_equal "text/plain", mail.parts[1].content_type
|
||||
assert_equal "text/yaml", mail.parts[2].content_type
|
||||
end
|
||||
|
||||
def test_implicitly_multipart_messages_with_charset
|
||||
mail = TestMailer.create_implicitly_multipart_example(@recipient, 'iso-8859-1')
|
||||
|
||||
assert_equal "multipart/alternative", mail.header['content-type'].body
|
||||
|
||||
assert_equal 'iso-8859-1', mail.parts[0].sub_header("content-type", "charset")
|
||||
assert_equal 'iso-8859-1', mail.parts[1].sub_header("content-type", "charset")
|
||||
assert_equal 'iso-8859-1', mail.parts[2].sub_header("content-type", "charset")
|
||||
end
|
||||
|
||||
def test_html_mail
|
||||
mail = TestMailer.create_html_mail(@recipient)
|
||||
assert_equal "text/html", mail.content_type
|
||||
end
|
||||
|
||||
def test_html_mail_with_underscores
|
||||
mail = TestMailer.create_html_mail_with_underscores(@recipient)
|
||||
assert_equal %{<a href="http://google.com" target="_blank">_Google</a>}, mail.body
|
||||
end
|
||||
|
||||
def test_various_newlines
|
||||
mail = TestMailer.create_various_newlines(@recipient)
|
||||
assert_equal("line #1\nline #2\nline #3\nline #4\n\n" +
|
||||
"line #5\n\nline#6\n\nline #7", mail.body)
|
||||
end
|
||||
|
||||
def test_various_newlines_multipart
|
||||
mail = TestMailer.create_various_newlines_multipart(@recipient)
|
||||
assert_equal "line #1\nline #2\nline #3\nline #4\n\n", mail.parts[0].body
|
||||
assert_equal "<p>line #1</p>\n<p>line #2</p>\n<p>line #3</p>\n<p>line #4</p>\n\n", mail.parts[1].body
|
||||
end
|
||||
|
||||
def test_headers_removed_on_smtp_delivery
|
||||
ActionMailer::Base.delivery_method = :smtp
|
||||
TestMailer.deliver_cc_bcc(@recipient)
|
||||
assert MockSMTP.deliveries[0][2].include?("root@loudthinking.com")
|
||||
assert MockSMTP.deliveries[0][2].include?("nobody@loudthinking.com")
|
||||
assert MockSMTP.deliveries[0][2].include?(@recipient)
|
||||
assert_match %r{^Cc: nobody@loudthinking.com}, MockSMTP.deliveries[0][0]
|
||||
assert_match %r{^To: #{@recipient}}, MockSMTP.deliveries[0][0]
|
||||
assert_no_match %r{^Bcc: root@loudthinking.com}, MockSMTP.deliveries[0][0]
|
||||
end
|
||||
|
||||
def test_recursive_multipart_processing
|
||||
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email7")
|
||||
mail = TMail::Mail.parse(fixture)
|
||||
assert_equal "This is the first part.\n\nAttachment: test.rb\nAttachment: test.pdf\n\n\nAttachment: smime.p7s\n", mail.body
|
||||
end
|
||||
|
||||
def test_decode_encoded_attachment_filename
|
||||
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email8")
|
||||
mail = TMail::Mail.parse(fixture)
|
||||
attachment = mail.attachments.last
|
||||
|
||||
expected = "01 Quien Te Dij\212at. Pitbull.mp3"
|
||||
expected.force_encoding(Encoding::ASCII_8BIT) if expected.respond_to?(:force_encoding)
|
||||
|
||||
assert_equal expected, attachment.original_filename
|
||||
end
|
||||
|
||||
def test_wrong_mail_header
|
||||
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email9")
|
||||
assert_raise(TMail::SyntaxError) { TMail::Mail.parse(fixture) }
|
||||
end
|
||||
|
||||
def test_decode_message_with_unknown_charset
|
||||
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email10")
|
||||
mail = TMail::Mail.parse(fixture)
|
||||
assert_nothing_raised { mail.body }
|
||||
end
|
||||
|
||||
def test_empty_header_values_omitted
|
||||
result = TestMailer.create_unnamed_attachment(@recipient).encoded
|
||||
assert_match %r{Content-Type: application/octet-stream[^;]}, result
|
||||
assert_match %r{Content-Disposition: attachment[^;]}, result
|
||||
end
|
||||
|
||||
def test_headers_with_nonalpha_chars
|
||||
mail = TestMailer.create_headers_with_nonalpha_chars(@recipient)
|
||||
assert !mail.from_addrs.empty?
|
||||
assert !mail.cc_addrs.empty?
|
||||
assert !mail.bcc_addrs.empty?
|
||||
assert_match(/:/, mail.from_addrs.to_s)
|
||||
assert_match(/:/, mail.cc_addrs.to_s)
|
||||
assert_match(/:/, mail.bcc_addrs.to_s)
|
||||
end
|
||||
|
||||
def test_deliver_with_mail_object
|
||||
mail = TestMailer.create_headers_with_nonalpha_chars(@recipient)
|
||||
assert_nothing_raised { TestMailer.deliver(mail) }
|
||||
assert_equal 1, TestMailer.deliveries.length
|
||||
end
|
||||
|
||||
def test_multipart_with_template_path_with_dots
|
||||
mail = FunkyPathMailer.create_multipart_with_template_path_with_dots(@recipient)
|
||||
assert_equal 2, mail.parts.length
|
||||
end
|
||||
|
||||
def test_custom_content_type_attributes
|
||||
mail = TestMailer.create_custom_content_type_attributes
|
||||
assert_match %r{format=flowed}, mail['content-type'].to_s
|
||||
assert_match %r{charset=utf-8}, mail['content-type'].to_s
|
||||
end
|
||||
|
||||
def test_return_path_with_create
|
||||
mail = TestMailer.create_return_path
|
||||
assert_equal "<another@somewhere.test>", mail['return-path'].to_s
|
||||
end
|
||||
|
||||
def test_return_path_with_deliver
|
||||
ActionMailer::Base.delivery_method = :smtp
|
||||
TestMailer.deliver_return_path
|
||||
assert_match %r{^Return-Path: <another@somewhere.test>}, MockSMTP.deliveries[0][0]
|
||||
end
|
||||
end
|
||||
|
||||
end # uses_mocha
|
||||
|
||||
class InheritableTemplateRootTest < Test::Unit::TestCase
|
||||
def test_attr
|
||||
expected = "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
|
||||
assert_equal expected, FunkyPathMailer.template_root
|
||||
|
||||
sub = Class.new(FunkyPathMailer)
|
||||
sub.template_root = 'test/path'
|
||||
|
||||
assert_equal 'test/path', sub.template_root
|
||||
assert_equal expected, FunkyPathMailer.template_root
|
||||
end
|
||||
end
|
||||
|
||||
class MethodNamingTest < Test::Unit::TestCase
|
||||
class TestMailer < ActionMailer::Base
|
||||
def send
|
||||
body 'foo'
|
||||
end
|
||||
end
|
||||
|
||||
def setup
|
||||
set_delivery_method :test
|
||||
ActionMailer::Base.perform_deliveries = true
|
||||
ActionMailer::Base.deliveries = []
|
||||
end
|
||||
|
||||
def teardown
|
||||
restore_delivery_method
|
||||
end
|
||||
|
||||
def test_send_method
|
||||
assert_nothing_raised do
|
||||
assert_emails 1 do
|
||||
TestMailer.deliver_send
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,98 +0,0 @@
|
|||
# encoding: utf-8
|
||||
require 'abstract_unit'
|
||||
require 'tmail'
|
||||
require 'tempfile'
|
||||
|
||||
class QuotingTest < Test::Unit::TestCase
|
||||
# Move some tests from TMAIL here
|
||||
def test_unquote_quoted_printable
|
||||
a ="=?ISO-8859-1?Q?[166417]_Bekr=E6ftelse_fra_Rejsefeber?="
|
||||
b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
|
||||
assert_equal "[166417] Bekr\303\246ftelse fra Rejsefeber", b
|
||||
end
|
||||
|
||||
def test_unquote_base64
|
||||
a ="=?ISO-8859-1?B?WzE2NjQxN10gQmVrcuZmdGVsc2UgZnJhIFJlanNlZmViZXI=?="
|
||||
b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
|
||||
assert_equal "[166417] Bekr\303\246ftelse fra Rejsefeber", b
|
||||
end
|
||||
|
||||
def test_unquote_without_charset
|
||||
a ="[166417]_Bekr=E6ftelse_fra_Rejsefeber"
|
||||
b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
|
||||
assert_equal "[166417]_Bekr=E6ftelse_fra_Rejsefeber", b
|
||||
end
|
||||
|
||||
def test_unqoute_multiple
|
||||
a ="=?utf-8?q?Re=3A_=5B12=5D_=23137=3A_Inkonsistente_verwendung_von_=22Hin?==?utf-8?b?enVmw7xnZW4i?="
|
||||
b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
|
||||
assert_equal "Re: [12] #137: Inkonsistente verwendung von \"Hinzuf\303\274gen\"", b
|
||||
end
|
||||
|
||||
def test_unqoute_in_the_middle
|
||||
a ="Re: Photos =?ISO-8859-1?Q?Brosch=FCre_Rand?="
|
||||
b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
|
||||
assert_equal "Re: Photos Brosch\303\274re Rand", b
|
||||
end
|
||||
|
||||
def test_unqoute_iso
|
||||
a ="=?ISO-8859-1?Q?Brosch=FCre_Rand?="
|
||||
b = TMail::Unquoter.unquote_and_convert_to(a, 'iso-8859-1')
|
||||
expected = "Brosch\374re Rand"
|
||||
expected.force_encoding 'iso-8859-1' if expected.respond_to?(:force_encoding)
|
||||
assert_equal expected, b
|
||||
end
|
||||
|
||||
def test_quote_multibyte_chars
|
||||
original = "\303\246 \303\270 and \303\245"
|
||||
original.force_encoding('ASCII-8BIT') if original.respond_to?(:force_encoding)
|
||||
|
||||
result = execute_in_sandbox(<<-CODE)
|
||||
$:.unshift(File.dirname(__FILE__) + "/../lib/")
|
||||
$KCODE = 'u'
|
||||
require 'jcode'
|
||||
require 'action_mailer/quoting'
|
||||
include ActionMailer::Quoting
|
||||
quoted_printable(#{original.inspect}, "UTF-8")
|
||||
CODE
|
||||
|
||||
unquoted = TMail::Unquoter.unquote_and_convert_to(result, nil)
|
||||
assert_equal unquoted, original
|
||||
end
|
||||
|
||||
|
||||
# test an email that has been created using \r\n newlines, instead of
|
||||
# \n newlines.
|
||||
def test_email_quoted_with_0d0a
|
||||
mail = TMail::Mail.parse(IO.read("#{File.dirname(__FILE__)}/fixtures/raw_email_quoted_with_0d0a"))
|
||||
assert_match %r{Elapsed time}, mail.body
|
||||
end
|
||||
|
||||
def test_email_with_partially_quoted_subject
|
||||
mail = TMail::Mail.parse(IO.read("#{File.dirname(__FILE__)}/fixtures/raw_email_with_partially_quoted_subject"))
|
||||
assert_equal "Re: Test: \"\346\274\242\345\255\227\" mid \"\346\274\242\345\255\227\" tail", mail.subject
|
||||
end
|
||||
|
||||
private
|
||||
# This whole thing *could* be much simpler, but I don't think Tempfile,
|
||||
# popen and others exist on all platforms (like Windows).
|
||||
def execute_in_sandbox(code)
|
||||
test_name = "#{File.dirname(__FILE__)}/am-quoting-test.#{$$}.rb"
|
||||
res_name = "#{File.dirname(__FILE__)}/am-quoting-test.#{$$}.out"
|
||||
|
||||
File.open(test_name, "w+") do |file|
|
||||
file.write(<<-CODE)
|
||||
block = Proc.new do
|
||||
#{code}
|
||||
end
|
||||
puts block.call
|
||||
CODE
|
||||
end
|
||||
|
||||
system("ruby #{test_name} > #{res_name}") or raise "could not run test in sandbox"
|
||||
File.read(res_name).chomp
|
||||
ensure
|
||||
File.delete(test_name) rescue nil
|
||||
File.delete(res_name) rescue nil
|
||||
end
|
||||
end
|
|
@ -1,129 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class TestHelperMailer < ActionMailer::Base
|
||||
def test
|
||||
recipients "test@example.com"
|
||||
from "tester@example.com"
|
||||
body render(:inline => "Hello, <%= @world %>", :body => { :world => "Earth" })
|
||||
end
|
||||
end
|
||||
|
||||
class TestHelperMailerTest < ActionMailer::TestCase
|
||||
def test_setup_sets_right_action_mailer_options
|
||||
assert_equal :test, ActionMailer::Base.delivery_method
|
||||
assert ActionMailer::Base.perform_deliveries
|
||||
assert_equal [], ActionMailer::Base.deliveries
|
||||
end
|
||||
|
||||
def test_setup_creates_the_expected_mailer
|
||||
assert @expected.is_a?(TMail::Mail)
|
||||
assert_equal "1.0", @expected.mime_version
|
||||
assert_equal "text/plain", @expected.content_type
|
||||
end
|
||||
|
||||
def test_mailer_class_is_correctly_inferred
|
||||
assert_equal TestHelperMailer, self.class.mailer_class
|
||||
end
|
||||
|
||||
def test_determine_default_mailer_raises_correct_error
|
||||
assert_raises(ActionMailer::NonInferrableMailerError) do
|
||||
self.class.determine_default_mailer("NotAMailerTest")
|
||||
end
|
||||
end
|
||||
|
||||
def test_charset_is_utf_8
|
||||
assert_equal "utf-8", charset
|
||||
end
|
||||
|
||||
def test_encode
|
||||
assert_equal "=?utf-8?Q?=0aasdf=0a?=", encode("\nasdf\n")
|
||||
end
|
||||
|
||||
def test_assert_emails
|
||||
assert_nothing_raised do
|
||||
assert_emails 1 do
|
||||
TestHelperMailer.deliver_test
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_repeated_assert_emails_calls
|
||||
assert_nothing_raised do
|
||||
assert_emails 1 do
|
||||
TestHelperMailer.deliver_test
|
||||
end
|
||||
end
|
||||
|
||||
assert_nothing_raised do
|
||||
assert_emails 2 do
|
||||
TestHelperMailer.deliver_test
|
||||
TestHelperMailer.deliver_test
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_assert_emails_with_no_block
|
||||
assert_nothing_raised do
|
||||
TestHelperMailer.deliver_test
|
||||
assert_emails 1
|
||||
end
|
||||
|
||||
assert_nothing_raised do
|
||||
TestHelperMailer.deliver_test
|
||||
TestHelperMailer.deliver_test
|
||||
assert_emails 3
|
||||
end
|
||||
end
|
||||
|
||||
def test_assert_no_emails
|
||||
assert_nothing_raised do
|
||||
assert_no_emails do
|
||||
TestHelperMailer.create_test
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_assert_emails_too_few_sent
|
||||
error = assert_raises Test::Unit::AssertionFailedError do
|
||||
assert_emails 2 do
|
||||
TestHelperMailer.deliver_test
|
||||
end
|
||||
end
|
||||
|
||||
assert_match /2 .* but 1/, error.message
|
||||
end
|
||||
|
||||
def test_assert_emails_too_many_sent
|
||||
error = assert_raises Test::Unit::AssertionFailedError do
|
||||
assert_emails 1 do
|
||||
TestHelperMailer.deliver_test
|
||||
TestHelperMailer.deliver_test
|
||||
end
|
||||
end
|
||||
|
||||
assert_match /1 .* but 2/, error.message
|
||||
end
|
||||
|
||||
def test_assert_no_emails_failure
|
||||
error = assert_raises Test::Unit::AssertionFailedError do
|
||||
assert_no_emails do
|
||||
TestHelperMailer.deliver_test
|
||||
end
|
||||
end
|
||||
|
||||
assert_match /0 .* but 1/, error.message
|
||||
end
|
||||
end
|
||||
|
||||
class AnotherTestHelperMailerTest < ActionMailer::TestCase
|
||||
tests TestHelperMailer
|
||||
|
||||
def setup
|
||||
@test_var = "a value"
|
||||
end
|
||||
|
||||
def test_setup_shouldnt_conflict_with_mailer_setup
|
||||
assert @expected.is_a?(TMail::Mail)
|
||||
assert_equal 'a value', @test_var
|
||||
end
|
||||
end
|
|
@ -1,22 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class TMailMailTest < Test::Unit::TestCase
|
||||
def test_body
|
||||
m = TMail::Mail.new
|
||||
expected = 'something_with_underscores'
|
||||
m.encoding = 'quoted-printable'
|
||||
quoted_body = [expected].pack('*M')
|
||||
m.body = quoted_body
|
||||
assert_equal "something_with_underscores=\n", m.quoted_body
|
||||
assert_equal expected, m.body
|
||||
end
|
||||
|
||||
def test_nested_attachments_are_recognized_correctly
|
||||
fixture = File.read("#{File.dirname(__FILE__)}/fixtures/raw_email_with_nested_attachment")
|
||||
mail = TMail::Mail.parse(fixture)
|
||||
assert_equal 2, mail.attachments.length
|
||||
assert_equal "image/png", mail.attachments.first.content_type
|
||||
assert_equal 1902, mail.attachments.first.length
|
||||
assert_equal "application/pkcs7-signature", mail.attachments.last.content_type
|
||||
end
|
||||
end
|
|
@ -1,76 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class TestMailer < ActionMailer::Base
|
||||
|
||||
default_url_options[:host] = 'www.basecamphq.com'
|
||||
|
||||
def signed_up_with_url(recipient)
|
||||
@recipients = recipient
|
||||
@subject = "[Signed up] Welcome #{recipient}"
|
||||
@from = "system@loudthinking.com"
|
||||
@sent_on = Time.local(2004, 12, 12)
|
||||
|
||||
@body["recipient"] = recipient
|
||||
@body["welcome_url"] = url_for :host => "example.com", :controller => "welcome", :action => "greeting"
|
||||
end
|
||||
|
||||
class <<self
|
||||
attr_accessor :received_body
|
||||
end
|
||||
|
||||
def receive(mail)
|
||||
self.class.received_body = mail.body
|
||||
end
|
||||
end
|
||||
|
||||
class ActionMailerUrlTest < Test::Unit::TestCase
|
||||
include ActionMailer::Quoting
|
||||
|
||||
def encode( text, charset="utf-8" )
|
||||
quoted_printable( text, charset )
|
||||
end
|
||||
|
||||
def new_mail( charset="utf-8" )
|
||||
mail = TMail::Mail.new
|
||||
mail.mime_version = "1.0"
|
||||
if charset
|
||||
mail.set_content_type "text", "plain", { "charset" => charset }
|
||||
end
|
||||
mail
|
||||
end
|
||||
|
||||
def setup
|
||||
set_delivery_method :test
|
||||
ActionMailer::Base.perform_deliveries = true
|
||||
ActionMailer::Base.deliveries = []
|
||||
|
||||
@recipient = 'test@localhost'
|
||||
end
|
||||
|
||||
def teardown
|
||||
restore_delivery_method
|
||||
end
|
||||
|
||||
def test_signed_up_with_url
|
||||
ActionController::Routing::Routes.draw do |map|
|
||||
map.connect ':controller/:action/:id'
|
||||
map.welcome 'welcome', :controller=>"foo", :action=>"bar"
|
||||
end
|
||||
|
||||
expected = new_mail
|
||||
expected.to = @recipient
|
||||
expected.subject = "[Signed up] Welcome #{@recipient}"
|
||||
expected.body = "Hello there, \n\nMr. #{@recipient}. Please see our greeting at http://example.com/welcome/greeting http://www.basecamphq.com/welcome\n\n<img alt=\"Somelogo\" src=\"/images/somelogo.png\" />"
|
||||
expected.from = "system@loudthinking.com"
|
||||
expected.date = Time.local(2004, 12, 12)
|
||||
|
||||
created = nil
|
||||
assert_nothing_raised { created = TestMailer.create_signed_up_with_url(@recipient) }
|
||||
assert_not_nil created
|
||||
assert_equal expected.encoded, created.encoded
|
||||
|
||||
assert_nothing_raised { TestMailer.deliver_signed_up_with_url(@recipient) }
|
||||
assert_not_nil ActionMailer::Base.deliveries.first
|
||||
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
|
||||
end
|
||||
end
|
|
@ -1,36 +0,0 @@
|
|||
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
||||
$:.unshift(File.dirname(__FILE__) + '/../../activesupport/lib/active_support')
|
||||
$:.unshift(File.dirname(__FILE__) + '/fixtures/helpers')
|
||||
|
||||
require 'yaml'
|
||||
require 'stringio'
|
||||
require 'test/unit'
|
||||
require 'action_controller'
|
||||
require 'action_controller/cgi_ext'
|
||||
require 'action_controller/test_process'
|
||||
require 'action_view/test_case'
|
||||
|
||||
begin
|
||||
require 'ruby-debug'
|
||||
rescue LoadError
|
||||
# Debugging disabled. `gem install ruby-debug` to enable.
|
||||
end
|
||||
|
||||
# Show backtraces for deprecated behavior for quicker cleanup.
|
||||
ActiveSupport::Deprecation.debug = true
|
||||
|
||||
ActionController::Base.logger = nil
|
||||
ActionController::Routing::Routes.reload rescue nil
|
||||
|
||||
|
||||
# Wrap tests that use Mocha and skip if unavailable.
|
||||
def uses_mocha(test_name)
|
||||
unless Object.const_defined?(:Mocha)
|
||||
require 'mocha'
|
||||
require 'stubba'
|
||||
end
|
||||
yield
|
||||
rescue LoadError => load_error
|
||||
raise unless load_error.message =~ /mocha/i
|
||||
$stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again."
|
||||
end
|
|
@ -1,105 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
# Define the essentials
|
||||
class ActiveRecordTestConnector
|
||||
cattr_accessor :able_to_connect
|
||||
cattr_accessor :connected
|
||||
|
||||
# Set our defaults
|
||||
self.connected = false
|
||||
self.able_to_connect = true
|
||||
end
|
||||
|
||||
# Try to grab AR
|
||||
if defined?(ActiveRecord) && defined?(Fixtures)
|
||||
$stderr.puts 'Active Record is already loaded, running tests'
|
||||
else
|
||||
$stderr.print 'Attempting to load Active Record... '
|
||||
begin
|
||||
PATH_TO_AR = "#{File.dirname(__FILE__)}/../../activerecord/lib"
|
||||
raise LoadError, "#{PATH_TO_AR} doesn't exist" unless File.directory?(PATH_TO_AR)
|
||||
$LOAD_PATH.unshift PATH_TO_AR
|
||||
require 'active_record'
|
||||
require 'active_record/fixtures'
|
||||
$stderr.puts 'success'
|
||||
rescue LoadError => e
|
||||
$stderr.print "failed. Skipping Active Record assertion tests: #{e}"
|
||||
ActiveRecordTestConnector.able_to_connect = false
|
||||
end
|
||||
end
|
||||
$stderr.flush
|
||||
|
||||
|
||||
|
||||
# Define the rest of the connector
|
||||
class ActiveRecordTestConnector
|
||||
class << self
|
||||
def setup
|
||||
unless self.connected || !self.able_to_connect
|
||||
setup_connection
|
||||
load_schema
|
||||
require_fixture_models
|
||||
self.connected = true
|
||||
end
|
||||
rescue Exception => e # errors from ActiveRecord setup
|
||||
$stderr.puts "\nSkipping ActiveRecord assertion tests: #{e}"
|
||||
#$stderr.puts " #{e.backtrace.join("\n ")}\n"
|
||||
self.able_to_connect = false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def setup_connection
|
||||
if Object.const_defined?(:ActiveRecord)
|
||||
defaults = { :database => ':memory:' }
|
||||
begin
|
||||
options = defaults.merge :adapter => 'sqlite3', :timeout => 500
|
||||
ActiveRecord::Base.establish_connection(options)
|
||||
ActiveRecord::Base.configurations = { 'sqlite3_ar_integration' => options }
|
||||
ActiveRecord::Base.connection
|
||||
rescue Exception # errors from establishing a connection
|
||||
$stderr.puts 'SQLite 3 unavailable; trying SQLite 2.'
|
||||
options = defaults.merge :adapter => 'sqlite'
|
||||
ActiveRecord::Base.establish_connection(options)
|
||||
ActiveRecord::Base.configurations = { 'sqlite2_ar_integration' => options }
|
||||
ActiveRecord::Base.connection
|
||||
end
|
||||
|
||||
Object.send(:const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')) unless Object.const_defined?(:QUOTED_TYPE)
|
||||
else
|
||||
raise "Can't setup connection since ActiveRecord isn't loaded."
|
||||
end
|
||||
end
|
||||
|
||||
# Load actionpack sqlite tables
|
||||
def load_schema
|
||||
File.read(File.dirname(__FILE__) + "/fixtures/db_definitions/sqlite.sql").split(';').each do |sql|
|
||||
ActiveRecord::Base.connection.execute(sql) unless sql.blank?
|
||||
end
|
||||
end
|
||||
|
||||
def require_fixture_models
|
||||
Dir.glob(File.dirname(__FILE__) + "/fixtures/*.rb").each {|f| require f}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class ActiveRecordTestCase < ActiveSupport::TestCase
|
||||
# Set our fixture path
|
||||
if ActiveRecordTestConnector.able_to_connect
|
||||
self.fixture_path = "#{File.dirname(__FILE__)}/fixtures/"
|
||||
self.use_transactional_fixtures = false
|
||||
end
|
||||
|
||||
def self.fixtures(*args)
|
||||
super if ActiveRecordTestConnector.connected
|
||||
end
|
||||
|
||||
def run(*args)
|
||||
super if ActiveRecordTestConnector.connected
|
||||
end
|
||||
|
||||
def default_test; end
|
||||
end
|
||||
|
||||
ActiveRecordTestConnector.setup
|
|
@ -1,141 +0,0 @@
|
|||
# These tests exercise CGI::Session::ActiveRecordStore, so you're going to
|
||||
# need AR in a sibling directory to AP and have SQLite installed.
|
||||
require 'active_record_unit'
|
||||
require 'action_controller/session/active_record_store'
|
||||
|
||||
module CommonActiveRecordStoreTests
|
||||
def test_basics
|
||||
s = session_class.new(:session_id => '1234', :data => { 'foo' => 'bar' })
|
||||
assert_equal 'bar', s.data['foo']
|
||||
assert s.save
|
||||
assert_equal 'bar', s.data['foo']
|
||||
|
||||
assert_not_nil t = session_class.find_by_session_id('1234')
|
||||
assert_not_nil t.data
|
||||
assert_equal 'bar', t.data['foo']
|
||||
end
|
||||
|
||||
def test_reload_same_session
|
||||
@new_session.update
|
||||
reloaded = CGI::Session.new(CGI.new, 'session_id' => @new_session.session_id, 'database_manager' => CGI::Session::ActiveRecordStore)
|
||||
assert_equal 'bar', reloaded['foo']
|
||||
end
|
||||
|
||||
def test_tolerates_close_close
|
||||
assert_nothing_raised do
|
||||
@new_session.close
|
||||
@new_session.close
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class ActiveRecordStoreTest < ActiveRecordTestCase
|
||||
include CommonActiveRecordStoreTests
|
||||
|
||||
def session_class
|
||||
CGI::Session::ActiveRecordStore::Session
|
||||
end
|
||||
|
||||
def session_id_column
|
||||
"session_id"
|
||||
end
|
||||
|
||||
def setup
|
||||
session_class.create_table!
|
||||
|
||||
ENV['REQUEST_METHOD'] = 'GET'
|
||||
ENV['REQUEST_URI'] = '/'
|
||||
CGI::Session::ActiveRecordStore.session_class = session_class
|
||||
|
||||
@cgi = CGI.new
|
||||
@new_session = CGI::Session.new(@cgi, 'database_manager' => CGI::Session::ActiveRecordStore, 'new_session' => true)
|
||||
@new_session['foo'] = 'bar'
|
||||
end
|
||||
|
||||
# this test only applies for eager session saving
|
||||
# def test_another_instance
|
||||
# @another = CGI::Session.new(@cgi, 'session_id' => @new_session.session_id, 'database_manager' => CGI::Session::ActiveRecordStore)
|
||||
# assert_equal @new_session.session_id, @another.session_id
|
||||
# end
|
||||
|
||||
def test_model_attribute
|
||||
assert_kind_of CGI::Session::ActiveRecordStore::Session, @new_session.model
|
||||
assert_equal({ 'foo' => 'bar' }, @new_session.model.data)
|
||||
end
|
||||
|
||||
def test_save_unloaded_session
|
||||
c = session_class.connection
|
||||
bogus_class = c.quote(ActiveSupport::Base64.encode64("\004\010o:\vBlammo\000"))
|
||||
c.insert("INSERT INTO #{session_class.table_name} ('#{session_id_column}', 'data') VALUES ('abcdefghijklmnop', #{bogus_class})")
|
||||
|
||||
sess = session_class.find_by_session_id('abcdefghijklmnop')
|
||||
assert_not_nil sess
|
||||
assert !sess.loaded?
|
||||
|
||||
# because the session is not loaded, the save should be a no-op. If it
|
||||
# isn't, this'll try and unmarshall the bogus class, and should get an error.
|
||||
assert_nothing_raised { sess.save }
|
||||
end
|
||||
|
||||
def teardown
|
||||
session_class.drop_table!
|
||||
end
|
||||
end
|
||||
|
||||
class ColumnLimitTest < ActiveRecordTestCase
|
||||
def setup
|
||||
@session_class = CGI::Session::ActiveRecordStore::Session
|
||||
@session_class.create_table!
|
||||
end
|
||||
|
||||
def teardown
|
||||
@session_class.drop_table!
|
||||
end
|
||||
|
||||
def test_protection_from_data_larger_than_column
|
||||
# Can't test this unless there is a limit
|
||||
return unless limit = @session_class.data_column_size_limit
|
||||
too_big = ':(' * limit
|
||||
s = @session_class.new(:session_id => '666', :data => {'foo' => too_big})
|
||||
s.data
|
||||
assert_raise(ActionController::SessionOverflowError) { s.save }
|
||||
end
|
||||
end
|
||||
|
||||
class DeprecatedActiveRecordStoreTest < ActiveRecordStoreTest
|
||||
def session_id_column
|
||||
"sessid"
|
||||
end
|
||||
|
||||
def setup
|
||||
session_class.connection.execute 'create table old_sessions (id integer primary key, sessid text unique, data text)'
|
||||
session_class.table_name = 'old_sessions'
|
||||
session_class.send :setup_sessid_compatibility!
|
||||
|
||||
ENV['REQUEST_METHOD'] = 'GET'
|
||||
CGI::Session::ActiveRecordStore.session_class = session_class
|
||||
|
||||
@new_session = CGI::Session.new(CGI.new, 'database_manager' => CGI::Session::ActiveRecordStore, 'new_session' => true)
|
||||
@new_session['foo'] = 'bar'
|
||||
end
|
||||
|
||||
def teardown
|
||||
session_class.connection.execute 'drop table old_sessions'
|
||||
session_class.table_name = 'sessions'
|
||||
end
|
||||
end
|
||||
|
||||
class SqlBypassActiveRecordStoreTest < ActiveRecordStoreTest
|
||||
def session_class
|
||||
unless defined? @session_class
|
||||
@session_class = CGI::Session::ActiveRecordStore::SqlBypass
|
||||
@session_class.connection = CGI::Session::ActiveRecordStore::Session.connection
|
||||
end
|
||||
@session_class
|
||||
end
|
||||
|
||||
def test_model_attribute
|
||||
assert_kind_of CGI::Session::ActiveRecordStore::SqlBypass, @new_session.model
|
||||
assert_equal({ 'foo' => 'bar' }, @new_session.model.data)
|
||||
end
|
||||
end
|
|
@ -1,191 +0,0 @@
|
|||
require 'active_record_unit'
|
||||
|
||||
class RenderPartialWithRecordIdentificationController < ActionController::Base
|
||||
def render_with_has_many_and_belongs_to_association
|
||||
@developer = Developer.find(1)
|
||||
render :partial => @developer.projects
|
||||
end
|
||||
|
||||
def render_with_has_many_association
|
||||
@topic = Topic.find(1)
|
||||
render :partial => @topic.replies
|
||||
end
|
||||
|
||||
def render_with_named_scope
|
||||
render :partial => Reply.base
|
||||
end
|
||||
|
||||
def render_with_has_many_through_association
|
||||
@developer = Developer.find(:first)
|
||||
render :partial => @developer.topics
|
||||
end
|
||||
|
||||
def render_with_has_one_association
|
||||
@company = Company.find(1)
|
||||
render :partial => @company.mascot
|
||||
end
|
||||
|
||||
def render_with_belongs_to_association
|
||||
@reply = Reply.find(1)
|
||||
render :partial => @reply.topic
|
||||
end
|
||||
|
||||
def render_with_record
|
||||
@developer = Developer.find(:first)
|
||||
render :partial => @developer
|
||||
end
|
||||
|
||||
def render_with_record_collection
|
||||
@developers = Developer.find(:all)
|
||||
render :partial => @developers
|
||||
end
|
||||
end
|
||||
RenderPartialWithRecordIdentificationController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
|
||||
class RenderPartialWithRecordIdentificationTest < ActiveRecordTestCase
|
||||
fixtures :developers, :projects, :developers_projects, :topics, :replies, :companies, :mascots
|
||||
|
||||
def setup
|
||||
@controller = RenderPartialWithRecordIdentificationController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
super
|
||||
end
|
||||
|
||||
def test_rendering_partial_with_has_many_and_belongs_to_association
|
||||
get :render_with_has_many_and_belongs_to_association
|
||||
assert_template 'projects/_project'
|
||||
end
|
||||
|
||||
def test_rendering_partial_with_has_many_association
|
||||
get :render_with_has_many_association
|
||||
assert_template 'replies/_reply'
|
||||
end
|
||||
|
||||
def test_rendering_partial_with_named_scope
|
||||
get :render_with_named_scope
|
||||
assert_template 'replies/_reply'
|
||||
end
|
||||
|
||||
def test_render_with_record
|
||||
get :render_with_record
|
||||
assert_template 'developers/_developer'
|
||||
end
|
||||
|
||||
def test_render_with_record_collection
|
||||
get :render_with_record_collection
|
||||
assert_template 'developers/_developer'
|
||||
end
|
||||
|
||||
def test_rendering_partial_with_has_one_association
|
||||
mascot = Company.find(1).mascot
|
||||
get :render_with_has_one_association
|
||||
assert_template 'mascots/_mascot'
|
||||
assert_equal mascot.name, @response.body
|
||||
end
|
||||
end
|
||||
|
||||
class RenderPartialWithRecordIdentificationController < ActionController::Base
|
||||
def render_with_has_many_and_belongs_to_association
|
||||
@developer = Developer.find(1)
|
||||
render :partial => @developer.projects
|
||||
end
|
||||
|
||||
def render_with_has_many_association
|
||||
@topic = Topic.find(1)
|
||||
render :partial => @topic.replies
|
||||
end
|
||||
|
||||
def render_with_has_many_through_association
|
||||
@developer = Developer.find(:first)
|
||||
render :partial => @developer.topics
|
||||
end
|
||||
|
||||
def render_with_belongs_to_association
|
||||
@reply = Reply.find(1)
|
||||
render :partial => @reply.topic
|
||||
end
|
||||
|
||||
def render_with_record
|
||||
@developer = Developer.find(:first)
|
||||
render :partial => @developer
|
||||
end
|
||||
|
||||
def render_with_record_collection
|
||||
@developers = Developer.find(:all)
|
||||
render :partial => @developers
|
||||
end
|
||||
end
|
||||
RenderPartialWithRecordIdentificationController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
|
||||
class Game < Struct.new(:name, :id)
|
||||
def to_param
|
||||
id.to_s
|
||||
end
|
||||
end
|
||||
|
||||
module Fun
|
||||
class NestedController < ActionController::Base
|
||||
def render_with_record_in_nested_controller
|
||||
render :partial => Game.new("Pong")
|
||||
end
|
||||
|
||||
def render_with_record_collection_in_nested_controller
|
||||
render :partial => [ Game.new("Pong"), Game.new("Tank") ]
|
||||
end
|
||||
end
|
||||
NestedController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
|
||||
module Serious
|
||||
class NestedDeeperController < ActionController::Base
|
||||
def render_with_record_in_deeper_nested_controller
|
||||
render :partial => Game.new("Chess")
|
||||
end
|
||||
|
||||
def render_with_record_collection_in_deeper_nested_controller
|
||||
render :partial => [ Game.new("Chess"), Game.new("Sudoku"), Game.new("Solitaire") ]
|
||||
end
|
||||
end
|
||||
NestedDeeperController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
end
|
||||
end
|
||||
|
||||
class RenderPartialWithRecordIdentificationAndNestedControllersTest < ActiveRecordTestCase
|
||||
def setup
|
||||
@controller = Fun::NestedController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
super
|
||||
end
|
||||
|
||||
def test_render_with_record_in_nested_controller
|
||||
get :render_with_record_in_nested_controller
|
||||
assert_template 'fun/games/_game'
|
||||
end
|
||||
|
||||
def test_render_with_record_collection_in_nested_controller
|
||||
get :render_with_record_collection_in_nested_controller
|
||||
assert_template 'fun/games/_game'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class RenderPartialWithRecordIdentificationAndNestedDeeperControllersTest < ActiveRecordTestCase
|
||||
def setup
|
||||
@controller = Fun::Serious::NestedDeeperController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
super
|
||||
end
|
||||
|
||||
def test_render_with_record_in_deeper_nested_controller
|
||||
get :render_with_record_in_deeper_nested_controller
|
||||
assert_template 'fun/serious/games/_game'
|
||||
end
|
||||
|
||||
def test_render_with_record_collection_in_deeper_nested_controller
|
||||
get :render_with_record_collection_in_deeper_nested_controller
|
||||
assert_template 'fun/serious/games/_game'
|
||||
end
|
||||
|
||||
end
|
|
@ -1,20 +0,0 @@
|
|||
require File.dirname(__FILE__) + '/abstract_unit'
|
||||
require 'action_mailer/adv_attr_accessor'
|
||||
|
||||
class AdvAttrTest < Test::Unit::TestCase
|
||||
class Person
|
||||
include ActionMailer::AdvAttrAccessor
|
||||
adv_attr_accessor :name
|
||||
end
|
||||
|
||||
def test_adv_attr
|
||||
bob = Person.new
|
||||
assert_nil bob.name
|
||||
bob.name 'Bob'
|
||||
assert_equal 'Bob', bob.name
|
||||
|
||||
assert_raise(ArgumentError) {bob.name 'x', 'y'}
|
||||
end
|
||||
|
||||
|
||||
end
|
|
@ -1,543 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
# a controller class to facilitate the tests
|
||||
class ActionPackAssertionsController < ActionController::Base
|
||||
|
||||
# this does absolutely nothing
|
||||
def nothing() head :ok end
|
||||
|
||||
# a standard template
|
||||
def hello_world() render :template => "test/hello_world"; end
|
||||
|
||||
# a standard template
|
||||
def hello_xml_world() render :template => "test/hello_xml_world"; end
|
||||
|
||||
# a redirect to an internal location
|
||||
def redirect_internal() redirect_to "/nothing"; end
|
||||
|
||||
def redirect_to_action() redirect_to :action => "flash_me", :id => 1, :params => { "panda" => "fun" }; end
|
||||
|
||||
def redirect_to_controller() redirect_to :controller => "elsewhere", :action => "flash_me"; end
|
||||
|
||||
def redirect_to_controller_with_symbol() redirect_to :controller => :elsewhere, :action => :flash_me; end
|
||||
|
||||
def redirect_to_path() redirect_to '/some/path' end
|
||||
|
||||
def redirect_to_named_route() redirect_to route_one_url end
|
||||
|
||||
# a redirect to an external location
|
||||
def redirect_external() redirect_to "http://www.rubyonrails.org"; end
|
||||
|
||||
# a 404
|
||||
def response404() head '404 AWOL' end
|
||||
|
||||
# a 500
|
||||
def response500() head '500 Sorry' end
|
||||
|
||||
# a fictional 599
|
||||
def response599() head '599 Whoah!' end
|
||||
|
||||
# putting stuff in the flash
|
||||
def flash_me
|
||||
flash['hello'] = 'my name is inigo montoya...'
|
||||
render :text => "Inconceivable!"
|
||||
end
|
||||
|
||||
# we have a flash, but nothing is in it
|
||||
def flash_me_naked
|
||||
flash.clear
|
||||
render :text => "wow!"
|
||||
end
|
||||
|
||||
# assign some template instance variables
|
||||
def assign_this
|
||||
@howdy = "ho"
|
||||
render :inline => "Mr. Henke"
|
||||
end
|
||||
|
||||
def render_based_on_parameters
|
||||
render :text => "Mr. #{params[:name]}"
|
||||
end
|
||||
|
||||
def render_url
|
||||
render :text => "<div>#{url_for(:action => 'flash_me', :only_path => true)}</div>"
|
||||
end
|
||||
|
||||
def render_text_with_custom_content_type
|
||||
render :text => "Hello!", :content_type => Mime::RSS
|
||||
end
|
||||
|
||||
# puts something in the session
|
||||
def session_stuffing
|
||||
session['xmas'] = 'turkey'
|
||||
render :text => "ho ho ho"
|
||||
end
|
||||
|
||||
# raises exception on get requests
|
||||
def raise_on_get
|
||||
raise "get" if request.get?
|
||||
render :text => "request method: #{request.env['REQUEST_METHOD']}"
|
||||
end
|
||||
|
||||
# raises exception on post requests
|
||||
def raise_on_post
|
||||
raise "post" if request.post?
|
||||
render :text => "request method: #{request.env['REQUEST_METHOD']}"
|
||||
end
|
||||
|
||||
def get_valid_record
|
||||
@record = Class.new do
|
||||
def valid?
|
||||
true
|
||||
end
|
||||
|
||||
def errors
|
||||
Class.new do
|
||||
def full_messages; []; end
|
||||
end.new
|
||||
end
|
||||
|
||||
end.new
|
||||
|
||||
render :nothing => true
|
||||
end
|
||||
|
||||
|
||||
def get_invalid_record
|
||||
@record = Class.new do
|
||||
|
||||
def valid?
|
||||
false
|
||||
end
|
||||
|
||||
def errors
|
||||
Class.new do
|
||||
def full_messages; ['...stuff...']; end
|
||||
end.new
|
||||
end
|
||||
end.new
|
||||
|
||||
render :nothing => true
|
||||
end
|
||||
|
||||
# 911
|
||||
def rescue_action(e) raise; end
|
||||
end
|
||||
|
||||
# Used to test that assert_response includes the exception message
|
||||
# in the failure message when an action raises and assert_response
|
||||
# is expecting something other than an error.
|
||||
class AssertResponseWithUnexpectedErrorController < ActionController::Base
|
||||
def index
|
||||
raise 'FAIL'
|
||||
end
|
||||
|
||||
def show
|
||||
render :text => "Boom", :status => 500
|
||||
end
|
||||
end
|
||||
|
||||
class UserController < ActionController::Base
|
||||
end
|
||||
|
||||
module Admin
|
||||
class InnerModuleController < ActionController::Base
|
||||
def index
|
||||
render :nothing => true
|
||||
end
|
||||
|
||||
def redirect_to_index
|
||||
redirect_to admin_inner_module_path
|
||||
end
|
||||
|
||||
def redirect_to_absolute_controller
|
||||
redirect_to :controller => '/content'
|
||||
end
|
||||
|
||||
def redirect_to_fellow_controller
|
||||
redirect_to :controller => 'user'
|
||||
end
|
||||
|
||||
def redirect_to_top_level_named_route
|
||||
redirect_to top_level_url(:id => "foo")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
# tell the controller where to find its templates but start from parent
|
||||
# directory of test_request_response to simulate the behaviour of a
|
||||
# production environment
|
||||
ActionPackAssertionsController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
|
||||
# a test case to exercise the new capabilities TestRequest & TestResponse
|
||||
class ActionPackAssertionsControllerTest < Test::Unit::TestCase
|
||||
# let's get this party started
|
||||
def setup
|
||||
ActionController::Routing::Routes.reload
|
||||
ActionController::Routing.use_controllers!(%w(action_pack_assertions admin/inner_module user content admin/user))
|
||||
@controller = ActionPackAssertionsController.new
|
||||
@request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
def teardown
|
||||
ActionController::Routing::Routes.reload
|
||||
end
|
||||
|
||||
# -- assertion-based testing ------------------------------------------------
|
||||
|
||||
def test_assert_tag_and_url_for
|
||||
get :render_url
|
||||
assert_tag :content => "/action_pack_assertions/flash_me"
|
||||
end
|
||||
|
||||
# test the get method, make sure the request really was a get
|
||||
def test_get
|
||||
assert_raise(RuntimeError) { get :raise_on_get }
|
||||
get :raise_on_post
|
||||
assert_equal @response.body, 'request method: GET'
|
||||
end
|
||||
|
||||
# test the get method, make sure the request really was a get
|
||||
def test_post
|
||||
assert_raise(RuntimeError) { post :raise_on_post }
|
||||
post :raise_on_get
|
||||
assert_equal @response.body, 'request method: POST'
|
||||
end
|
||||
|
||||
# the following test fails because the request_method is now cached on the request instance
|
||||
# test the get/post switch within one test action
|
||||
# def test_get_post_switch
|
||||
# post :raise_on_get
|
||||
# assert_equal @response.body, 'request method: POST'
|
||||
# get :raise_on_post
|
||||
# assert_equal @response.body, 'request method: GET'
|
||||
# post :raise_on_get
|
||||
# assert_equal @response.body, 'request method: POST'
|
||||
# get :raise_on_post
|
||||
# assert_equal @response.body, 'request method: GET'
|
||||
# end
|
||||
|
||||
# test the redirection to a named route
|
||||
def test_assert_redirect_to_named_route
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing'
|
||||
map.connect ':controller/:action/:id'
|
||||
end
|
||||
set.install_helpers
|
||||
|
||||
process :redirect_to_named_route
|
||||
assert_redirected_to 'http://test.host/route_one'
|
||||
assert_redirected_to route_one_url
|
||||
assert_redirected_to :route_one_url
|
||||
end
|
||||
end
|
||||
|
||||
def test_assert_redirect_to_named_route_failure
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'one'
|
||||
map.route_two 'route_two', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
|
||||
map.connect ':controller/:action/:id'
|
||||
end
|
||||
process :redirect_to_named_route
|
||||
assert_raise(Test::Unit::AssertionFailedError) do
|
||||
assert_redirected_to 'http://test.host/route_two'
|
||||
end
|
||||
assert_raise(Test::Unit::AssertionFailedError) do
|
||||
assert_redirected_to :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
|
||||
end
|
||||
assert_raise(Test::Unit::AssertionFailedError) do
|
||||
assert_redirected_to route_two_url
|
||||
end
|
||||
assert_raise(Test::Unit::AssertionFailedError) do
|
||||
assert_redirected_to :route_two_url
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_assert_redirect_to_nested_named_route
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.admin_inner_module 'admin/inner_module', :controller => 'admin/inner_module', :action => 'index'
|
||||
map.connect ':controller/:action/:id'
|
||||
end
|
||||
@controller = Admin::InnerModuleController.new
|
||||
process :redirect_to_index
|
||||
# redirection is <{"action"=>"index", "controller"=>"admin/admin/inner_module"}>
|
||||
assert_redirected_to admin_inner_module_path
|
||||
end
|
||||
end
|
||||
|
||||
def test_assert_redirected_to_top_level_named_route_from_nested_controller
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.top_level '/action_pack_assertions/:id', :controller => 'action_pack_assertions', :action => 'index'
|
||||
map.connect ':controller/:action/:id'
|
||||
end
|
||||
@controller = Admin::InnerModuleController.new
|
||||
process :redirect_to_top_level_named_route
|
||||
# assert_redirected_to "http://test.host/action_pack_assertions/foo" would pass because of exact match early return
|
||||
assert_redirected_to "/action_pack_assertions/foo"
|
||||
end
|
||||
end
|
||||
|
||||
def test_assert_redirected_to_top_level_named_route_with_same_controller_name_in_both_namespaces
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
# this controller exists in the admin namespace as well which is the only difference from previous test
|
||||
map.top_level '/user/:id', :controller => 'user', :action => 'index'
|
||||
map.connect ':controller/:action/:id'
|
||||
end
|
||||
@controller = Admin::InnerModuleController.new
|
||||
process :redirect_to_top_level_named_route
|
||||
# assert_redirected_to top_level_url('foo') would pass because of exact match early return
|
||||
assert_redirected_to top_level_path('foo')
|
||||
end
|
||||
end
|
||||
|
||||
# -- standard request/response object testing --------------------------------
|
||||
|
||||
# make sure that the template objects exist
|
||||
def test_template_objects_alive
|
||||
process :assign_this
|
||||
assert !@response.has_template_object?('hi')
|
||||
assert @response.has_template_object?('howdy')
|
||||
end
|
||||
|
||||
# make sure we don't have template objects when we shouldn't
|
||||
def test_template_object_missing
|
||||
process :nothing
|
||||
assert_nil @response.template_objects['howdy']
|
||||
end
|
||||
|
||||
# check the empty flashing
|
||||
def test_flash_me_naked
|
||||
process :flash_me_naked
|
||||
assert !@response.has_flash?
|
||||
assert !@response.has_flash_with_contents?
|
||||
end
|
||||
|
||||
# check if we have flash objects
|
||||
def test_flash_haves
|
||||
process :flash_me
|
||||
assert @response.has_flash?
|
||||
assert @response.has_flash_with_contents?
|
||||
assert @response.has_flash_object?('hello')
|
||||
end
|
||||
|
||||
# ensure we don't have flash objects
|
||||
def test_flash_have_nots
|
||||
process :nothing
|
||||
assert !@response.has_flash?
|
||||
assert !@response.has_flash_with_contents?
|
||||
assert_nil @response.flash['hello']
|
||||
end
|
||||
|
||||
# check if we were rendered by a file-based template?
|
||||
def test_rendered_action
|
||||
process :nothing
|
||||
assert !@response.rendered_with_file?
|
||||
|
||||
process :hello_world
|
||||
assert @response.rendered_with_file?
|
||||
assert 'hello_world', @response.rendered_file
|
||||
end
|
||||
|
||||
# check the redirection location
|
||||
def test_redirection_location
|
||||
process :redirect_internal
|
||||
assert_equal 'http://test.host/nothing', @response.redirect_url
|
||||
|
||||
process :redirect_external
|
||||
assert_equal 'http://www.rubyonrails.org', @response.redirect_url
|
||||
end
|
||||
|
||||
def test_no_redirect_url
|
||||
process :nothing
|
||||
assert_nil @response.redirect_url
|
||||
end
|
||||
|
||||
|
||||
# check server errors
|
||||
def test_server_error_response_code
|
||||
process :response500
|
||||
assert @response.server_error?
|
||||
|
||||
process :response599
|
||||
assert @response.server_error?
|
||||
|
||||
process :response404
|
||||
assert !@response.server_error?
|
||||
end
|
||||
|
||||
# check a 404 response code
|
||||
def test_missing_response_code
|
||||
process :response404
|
||||
assert @response.missing?
|
||||
end
|
||||
|
||||
# check to see if our redirection matches a pattern
|
||||
def test_redirect_url_match
|
||||
process :redirect_external
|
||||
assert @response.redirect?
|
||||
assert @response.redirect_url_match?("rubyonrails")
|
||||
assert @response.redirect_url_match?(/rubyonrails/)
|
||||
assert !@response.redirect_url_match?("phpoffrails")
|
||||
assert !@response.redirect_url_match?(/perloffrails/)
|
||||
end
|
||||
|
||||
# check for a redirection
|
||||
def test_redirection
|
||||
process :redirect_internal
|
||||
assert @response.redirect?
|
||||
|
||||
process :redirect_external
|
||||
assert @response.redirect?
|
||||
|
||||
process :nothing
|
||||
assert !@response.redirect?
|
||||
end
|
||||
|
||||
# check a successful response code
|
||||
def test_successful_response_code
|
||||
process :nothing
|
||||
assert @response.success?
|
||||
end
|
||||
|
||||
# a basic check to make sure we have a TestResponse object
|
||||
def test_has_response
|
||||
process :nothing
|
||||
assert_kind_of ActionController::TestResponse, @response
|
||||
end
|
||||
|
||||
def test_render_based_on_parameters
|
||||
process :render_based_on_parameters, "name" => "David"
|
||||
assert_equal "Mr. David", @response.body
|
||||
end
|
||||
|
||||
def test_follow_redirect
|
||||
process :redirect_to_action
|
||||
assert_redirected_to :action => "flash_me"
|
||||
|
||||
assert_deprecated { follow_redirect }
|
||||
assert_equal 1, @request.parameters["id"].to_i
|
||||
|
||||
assert "Inconceivable!", @response.body
|
||||
end
|
||||
|
||||
def test_follow_redirect_outside_current_action
|
||||
process :redirect_to_controller
|
||||
assert_redirected_to :controller => "elsewhere", :action => "flash_me"
|
||||
|
||||
assert_raises(RuntimeError, "Can't follow redirects outside of current controller (elsewhere)") do
|
||||
assert_deprecated { follow_redirect }
|
||||
end
|
||||
end
|
||||
|
||||
def test_assert_redirection_fails_with_incorrect_controller
|
||||
process :redirect_to_controller
|
||||
assert_raise(Test::Unit::AssertionFailedError) do
|
||||
assert_redirected_to :controller => "action_pack_assertions", :action => "flash_me"
|
||||
end
|
||||
end
|
||||
|
||||
def test_assert_redirection_with_extra_controller_option
|
||||
get :redirect_to_action
|
||||
assert_redirected_to :controller => 'action_pack_assertions', :action => "flash_me", :id => 1, :params => { :panda => 'fun' }
|
||||
end
|
||||
|
||||
def test_redirected_to_url_leadling_slash
|
||||
process :redirect_to_path
|
||||
assert_redirected_to '/some/path'
|
||||
end
|
||||
def test_redirected_to_url_no_leadling_slash
|
||||
process :redirect_to_path
|
||||
assert_redirected_to 'some/path'
|
||||
end
|
||||
def test_redirected_to_url_full_url
|
||||
process :redirect_to_path
|
||||
assert_redirected_to 'http://test.host/some/path'
|
||||
end
|
||||
|
||||
def test_assert_redirection_with_symbol
|
||||
process :redirect_to_controller_with_symbol
|
||||
assert_nothing_raised {
|
||||
assert_redirected_to :controller => "elsewhere", :action => "flash_me"
|
||||
}
|
||||
process :redirect_to_controller_with_symbol
|
||||
assert_nothing_raised {
|
||||
assert_redirected_to :controller => :elsewhere, :action => :flash_me
|
||||
}
|
||||
end
|
||||
|
||||
def test_redirected_to_with_nested_controller
|
||||
@controller = Admin::InnerModuleController.new
|
||||
get :redirect_to_absolute_controller
|
||||
assert_redirected_to :controller => 'content'
|
||||
|
||||
get :redirect_to_fellow_controller
|
||||
assert_redirected_to :controller => 'admin/user'
|
||||
end
|
||||
|
||||
def test_assert_valid
|
||||
get :get_valid_record
|
||||
assert_valid assigns('record')
|
||||
end
|
||||
|
||||
def test_assert_valid_failing
|
||||
get :get_invalid_record
|
||||
|
||||
begin
|
||||
assert_valid assigns('record')
|
||||
assert false
|
||||
rescue Test::Unit::AssertionFailedError => e
|
||||
end
|
||||
end
|
||||
|
||||
def test_assert_response_uses_exception_message
|
||||
@controller = AssertResponseWithUnexpectedErrorController.new
|
||||
get :index
|
||||
assert_response :success
|
||||
flunk 'Expected non-success response'
|
||||
rescue Test::Unit::AssertionFailedError => e
|
||||
assert e.message.include?('FAIL')
|
||||
end
|
||||
|
||||
def test_assert_response_failure_response_with_no_exception
|
||||
@controller = AssertResponseWithUnexpectedErrorController.new
|
||||
get :show
|
||||
assert_response :success
|
||||
flunk 'Expected non-success response'
|
||||
rescue Test::Unit::AssertionFailedError
|
||||
rescue
|
||||
flunk "assert_response failed to handle failure response with missing, but optional, exception."
|
||||
end
|
||||
end
|
||||
|
||||
class ActionPackHeaderTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = ActionPackAssertionsController.new
|
||||
@request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
def test_rendering_xml_sets_content_type
|
||||
process :hello_xml_world
|
||||
assert_equal('application/xml; charset=utf-8', @response.headers['type'])
|
||||
end
|
||||
|
||||
def test_rendering_xml_respects_content_type
|
||||
@response.headers['type'] = 'application/pdf'
|
||||
process :hello_xml_world
|
||||
assert_equal('application/pdf; charset=utf-8', @response.headers['type'])
|
||||
end
|
||||
|
||||
|
||||
def test_render_text_with_custom_content_type
|
||||
get :render_text_with_custom_content_type
|
||||
assert_equal 'application/rss+xml; charset=utf-8', @response.headers['type']
|
||||
end
|
||||
end
|
|
@ -1,43 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class Address
|
||||
|
||||
def Address.count(conditions = nil, join = nil)
|
||||
nil
|
||||
end
|
||||
|
||||
def Address.find_all(arg1, arg2, arg3, arg4)
|
||||
[]
|
||||
end
|
||||
|
||||
def self.find(*args)
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
class AddressesTestController < ActionController::Base
|
||||
def self.controller_name; "addresses"; end
|
||||
def self.controller_path; "addresses"; end
|
||||
end
|
||||
|
||||
AddressesTestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
|
||||
class AddressesTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = AddressesTestController.new
|
||||
|
||||
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
|
||||
# a more accurate simulation of what happens in "real life".
|
||||
@controller.logger = Logger.new(nil)
|
||||
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
|
||||
@request.host = "www.nextangle.com"
|
||||
end
|
||||
|
||||
def test_list
|
||||
get :list
|
||||
assert_equal "We only need to get this far!", @response.body.chomp
|
||||
end
|
||||
end
|
|
@ -1,694 +0,0 @@
|
|||
#--
|
||||
# Copyright (c) 2006 Assaf Arkin (http://labnotes.org)
|
||||
# Under MIT and/or CC By license.
|
||||
#++
|
||||
|
||||
require 'abstract_unit'
|
||||
require 'controller/fake_controllers'
|
||||
|
||||
|
||||
unless defined?(ActionMailer)
|
||||
begin
|
||||
$:.unshift(File.dirname(__FILE__) + "/../../../actionmailer/lib")
|
||||
require 'action_mailer'
|
||||
rescue LoadError
|
||||
require 'rubygems'
|
||||
gem 'actionmailer'
|
||||
end
|
||||
end
|
||||
|
||||
class AssertSelectTest < Test::Unit::TestCase
|
||||
class AssertSelectController < ActionController::Base
|
||||
def response_with=(content)
|
||||
@content = content
|
||||
end
|
||||
|
||||
def response_with(&block)
|
||||
@update = block
|
||||
end
|
||||
|
||||
def html()
|
||||
render :text=>@content, :layout=>false, :content_type=>Mime::HTML
|
||||
@content = nil
|
||||
end
|
||||
|
||||
def rjs()
|
||||
render :update do |page|
|
||||
@update.call page
|
||||
end
|
||||
@update = nil
|
||||
end
|
||||
|
||||
def xml()
|
||||
render :text=>@content, :layout=>false, :content_type=>Mime::XML
|
||||
@content = nil
|
||||
end
|
||||
|
||||
def rescue_action(e)
|
||||
raise e
|
||||
end
|
||||
end
|
||||
|
||||
class AssertSelectMailer < ActionMailer::Base
|
||||
def test(html)
|
||||
recipients "test <test@test.host>"
|
||||
from "test@test.host"
|
||||
subject "Test e-mail"
|
||||
part :content_type=>"text/html", :body=>html
|
||||
end
|
||||
end
|
||||
|
||||
AssertionFailedError = Test::Unit::AssertionFailedError
|
||||
|
||||
def setup
|
||||
@controller = AssertSelectController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
ActionMailer::Base.delivery_method = :test
|
||||
ActionMailer::Base.perform_deliveries = true
|
||||
ActionMailer::Base.deliveries = []
|
||||
end
|
||||
|
||||
|
||||
def teardown
|
||||
ActionMailer::Base.deliveries.clear
|
||||
end
|
||||
|
||||
def assert_failure(message, &block)
|
||||
e = assert_raises(AssertionFailedError, &block)
|
||||
assert_match(message, e.message) if Regexp === message
|
||||
assert_equal(message, e.message) if String === message
|
||||
end
|
||||
|
||||
#
|
||||
# Test assert select.
|
||||
#
|
||||
|
||||
def test_assert_select
|
||||
render_html %Q{<div id="1"></div><div id="2"></div>}
|
||||
assert_select "div", 2
|
||||
assert_failure(/Expected at least 3 elements matching \"div\", found 2/) { assert_select "div", 3 }
|
||||
assert_failure(/Expected at least 1 element matching \"p\", found 0/) { assert_select "p" }
|
||||
end
|
||||
|
||||
|
||||
def test_equality_true_false
|
||||
render_html %Q{<div id="1"></div><div id="2"></div>}
|
||||
assert_nothing_raised { assert_select "div" }
|
||||
assert_raises(AssertionFailedError) { assert_select "p" }
|
||||
assert_nothing_raised { assert_select "div", true }
|
||||
assert_raises(AssertionFailedError) { assert_select "p", true }
|
||||
assert_raises(AssertionFailedError) { assert_select "div", false }
|
||||
assert_nothing_raised { assert_select "p", false }
|
||||
end
|
||||
|
||||
|
||||
def test_equality_string_and_regexp
|
||||
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
|
||||
assert_nothing_raised { assert_select "div", "foo" }
|
||||
assert_raises(AssertionFailedError) { assert_select "div", "bar" }
|
||||
assert_nothing_raised { assert_select "div", :text=>"foo" }
|
||||
assert_raises(AssertionFailedError) { assert_select "div", :text=>"bar" }
|
||||
assert_nothing_raised { assert_select "div", /(foo|bar)/ }
|
||||
assert_raises(AssertionFailedError) { assert_select "div", /foobar/ }
|
||||
assert_nothing_raised { assert_select "div", :text=>/(foo|bar)/ }
|
||||
assert_raises(AssertionFailedError) { assert_select "div", :text=>/foobar/ }
|
||||
assert_raises(AssertionFailedError) { assert_select "p", :text=>/foobar/ }
|
||||
end
|
||||
|
||||
|
||||
def test_equality_of_html
|
||||
render_html %Q{<p>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</p>}
|
||||
text = "\"This is not a big problem,\" he said."
|
||||
html = "<em>\"This is <strong>not</strong> a big problem,\"</em> he said."
|
||||
assert_nothing_raised { assert_select "p", text }
|
||||
assert_raises(AssertionFailedError) { assert_select "p", html }
|
||||
assert_nothing_raised { assert_select "p", :html=>html }
|
||||
assert_raises(AssertionFailedError) { assert_select "p", :html=>text }
|
||||
# No stripping for pre.
|
||||
render_html %Q{<pre>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</pre>}
|
||||
text = "\n\"This is not a big problem,\" he said.\n"
|
||||
html = "\n<em>\"This is <strong>not</strong> a big problem,\"</em> he said.\n"
|
||||
assert_nothing_raised { assert_select "pre", text }
|
||||
assert_raises(AssertionFailedError) { assert_select "pre", html }
|
||||
assert_nothing_raised { assert_select "pre", :html=>html }
|
||||
assert_raises(AssertionFailedError) { assert_select "pre", :html=>text }
|
||||
end
|
||||
|
||||
|
||||
def test_counts
|
||||
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
|
||||
assert_nothing_raised { assert_select "div", 2 }
|
||||
assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
|
||||
assert_select "div", 3
|
||||
end
|
||||
assert_nothing_raised { assert_select "div", 1..2 }
|
||||
assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
|
||||
assert_select "div", 3..4
|
||||
end
|
||||
assert_nothing_raised { assert_select "div", :count=>2 }
|
||||
assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
|
||||
assert_select "div", :count=>3
|
||||
end
|
||||
assert_nothing_raised { assert_select "div", :minimum=>1 }
|
||||
assert_nothing_raised { assert_select "div", :minimum=>2 }
|
||||
assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
|
||||
assert_select "div", :minimum=>3
|
||||
end
|
||||
assert_nothing_raised { assert_select "div", :maximum=>2 }
|
||||
assert_nothing_raised { assert_select "div", :maximum=>3 }
|
||||
assert_failure(/Expected at most 1 element matching \"div\", found 2/) do
|
||||
assert_select "div", :maximum=>1
|
||||
end
|
||||
assert_nothing_raised { assert_select "div", :minimum=>1, :maximum=>2 }
|
||||
assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
|
||||
assert_select "div", :minimum=>3, :maximum=>4
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def test_substitution_values
|
||||
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
|
||||
assert_select "div#?", /\d+/ do |elements|
|
||||
assert_equal 2, elements.size
|
||||
end
|
||||
assert_select "div" do
|
||||
assert_select "div#?", /\d+/ do |elements|
|
||||
assert_equal 2, elements.size
|
||||
assert_select "#1"
|
||||
assert_select "#2"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def test_nested_assert_select
|
||||
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
|
||||
assert_select "div" do |elements|
|
||||
assert_equal 2, elements.size
|
||||
assert_select elements[0], "#1"
|
||||
assert_select elements[1], "#2"
|
||||
end
|
||||
assert_select "div" do
|
||||
assert_select "div" do |elements|
|
||||
assert_equal 2, elements.size
|
||||
# Testing in a group is one thing
|
||||
assert_select "#1,#2"
|
||||
# Testing individually is another.
|
||||
assert_select "#1"
|
||||
assert_select "#2"
|
||||
assert_select "#3", false
|
||||
end
|
||||
end
|
||||
|
||||
assert_failure(/Expected at least 1 element matching \"#4\", found 0\./) do
|
||||
assert_select "div" do
|
||||
assert_select "#4"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def test_assert_select_text_match
|
||||
render_html %Q{<div id="1"><span>foo</span></div><div id="2"><span>bar</span></div>}
|
||||
assert_select "div" do
|
||||
assert_nothing_raised { assert_select "div", "foo" }
|
||||
assert_nothing_raised { assert_select "div", "bar" }
|
||||
assert_nothing_raised { assert_select "div", /\w*/ }
|
||||
assert_nothing_raised { assert_select "div", /\w*/, :count=>2 }
|
||||
assert_raises(AssertionFailedError) { assert_select "div", :text=>"foo", :count=>2 }
|
||||
assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" }
|
||||
assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" }
|
||||
assert_nothing_raised { assert_select "div", :html=>/\w*/ }
|
||||
assert_nothing_raised { assert_select "div", :html=>/\w*/, :count=>2 }
|
||||
assert_raises(AssertionFailedError) { assert_select "div", :html=>"<span>foo</span>", :count=>2 }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# With single result.
|
||||
def test_assert_select_from_rjs_with_single_result
|
||||
render_rjs do |page|
|
||||
page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
|
||||
end
|
||||
assert_select "div" do |elements|
|
||||
assert elements.size == 2
|
||||
assert_select "#1"
|
||||
assert_select "#2"
|
||||
end
|
||||
assert_select "div#?", /\d+/ do |elements|
|
||||
assert_select "#1"
|
||||
assert_select "#2"
|
||||
end
|
||||
end
|
||||
|
||||
# With multiple results.
|
||||
def test_assert_select_from_rjs_with_multiple_results
|
||||
render_rjs do |page|
|
||||
page.replace_html "test", "<div id=\"1\">foo</div>"
|
||||
page.replace_html "test2", "<div id=\"2\">foo</div>"
|
||||
end
|
||||
assert_select "div" do |elements|
|
||||
assert elements.size == 2
|
||||
assert_select "#1"
|
||||
assert_select "#2"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
#
|
||||
# Test css_select.
|
||||
#
|
||||
|
||||
|
||||
def test_css_select
|
||||
render_html %Q{<div id="1"></div><div id="2"></div>}
|
||||
assert 2, css_select("div").size
|
||||
assert 0, css_select("p").size
|
||||
end
|
||||
|
||||
|
||||
def test_nested_css_select
|
||||
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
|
||||
assert_select "div#?", /\d+/ do |elements|
|
||||
assert_equal 1, css_select(elements[0], "div").size
|
||||
assert_equal 1, css_select(elements[1], "div").size
|
||||
end
|
||||
assert_select "div" do
|
||||
assert_equal 2, css_select("div").size
|
||||
css_select("div").each do |element|
|
||||
# Testing as a group is one thing
|
||||
assert !css_select("#1,#2").empty?
|
||||
# Testing individually is another
|
||||
assert !css_select("#1").empty?
|
||||
assert !css_select("#2").empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# With one result.
|
||||
def test_css_select_from_rjs_with_single_result
|
||||
render_rjs do |page|
|
||||
page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
|
||||
end
|
||||
assert_equal 2, css_select("div").size
|
||||
assert_equal 1, css_select("#1").size
|
||||
assert_equal 1, css_select("#2").size
|
||||
end
|
||||
|
||||
# With multiple results.
|
||||
def test_css_select_from_rjs_with_multiple_results
|
||||
render_rjs do |page|
|
||||
page.replace_html "test", "<div id=\"1\">foo</div>"
|
||||
page.replace_html "test2", "<div id=\"2\">foo</div>"
|
||||
end
|
||||
|
||||
assert_equal 2, css_select("div").size
|
||||
assert_equal 1, css_select("#1").size
|
||||
assert_equal 1, css_select("#2").size
|
||||
end
|
||||
|
||||
|
||||
#
|
||||
# Test assert_select_rjs.
|
||||
#
|
||||
|
||||
|
||||
# Test that we can pick up all statements in the result.
|
||||
def test_assert_select_rjs_picks_up_all_statements
|
||||
render_rjs do |page|
|
||||
page.replace "test", "<div id=\"1\">foo</div>"
|
||||
page.replace_html "test2", "<div id=\"2\">foo</div>"
|
||||
page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
|
||||
end
|
||||
|
||||
found = false
|
||||
assert_select_rjs do
|
||||
assert_select "#1"
|
||||
assert_select "#2"
|
||||
assert_select "#3"
|
||||
found = true
|
||||
end
|
||||
assert found
|
||||
end
|
||||
|
||||
# Test that we fail if there is nothing to pick.
|
||||
def test_assert_select_rjs_fails_if_nothing_to_pick
|
||||
render_rjs { }
|
||||
assert_raises(AssertionFailedError) { assert_select_rjs }
|
||||
end
|
||||
|
||||
def test_assert_select_rjs_with_unicode
|
||||
# Test that non-ascii characters (which are converted into \uXXXX in RJS) are decoded correctly.
|
||||
render_rjs do |page|
|
||||
page.replace "test", "<div id=\"1\">\343\203\201\343\202\261\343\203\203\343\203\210</div>"
|
||||
end
|
||||
assert_select_rjs do
|
||||
str = "#1"
|
||||
assert_select str, :text => "\343\203\201\343\202\261\343\203\203\343\203\210"
|
||||
assert_select str, "\343\203\201\343\202\261\343\203\203\343\203\210"
|
||||
if str.respond_to?(:force_encoding)
|
||||
str.force_encoding(Encoding::UTF_8)
|
||||
assert_select str, /\343\203\201..\343\203\210/u
|
||||
assert_raises(AssertionFailedError) { assert_select str, /\343\203\201.\343\203\210/u }
|
||||
else
|
||||
assert_select str, Regexp.new("\343\203\201..\343\203\210",0,'U')
|
||||
assert_raises(AssertionFailedError) { assert_select str, Regexp.new("\343\203\201.\343\203\210",0,'U') }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_assert_select_rjs_with_id
|
||||
# Test that we can pick up all statements in the result.
|
||||
render_rjs do |page|
|
||||
page.replace "test1", "<div id=\"1\">foo</div>"
|
||||
page.replace_html "test2", "<div id=\"2\">foo</div>"
|
||||
page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
|
||||
end
|
||||
assert_select_rjs "test1" do
|
||||
assert_select "div", 1
|
||||
assert_select "#1"
|
||||
end
|
||||
assert_select_rjs "test2" do
|
||||
assert_select "div", 1
|
||||
assert_select "#2"
|
||||
end
|
||||
assert_select_rjs "test3" do
|
||||
assert_select "div", 1
|
||||
assert_select "#3"
|
||||
end
|
||||
assert_raises(AssertionFailedError) { assert_select_rjs "test4" }
|
||||
end
|
||||
|
||||
|
||||
def test_assert_select_rjs_for_replace
|
||||
render_rjs do |page|
|
||||
page.replace "test1", "<div id=\"1\">foo</div>"
|
||||
page.replace_html "test2", "<div id=\"2\">foo</div>"
|
||||
page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
|
||||
end
|
||||
# Replace.
|
||||
assert_select_rjs :replace do
|
||||
assert_select "div", 1
|
||||
assert_select "#1"
|
||||
end
|
||||
assert_select_rjs :replace, "test1" do
|
||||
assert_select "div", 1
|
||||
assert_select "#1"
|
||||
end
|
||||
assert_raises(AssertionFailedError) { assert_select_rjs :replace, "test2" }
|
||||
# Replace HTML.
|
||||
assert_select_rjs :replace_html do
|
||||
assert_select "div", 1
|
||||
assert_select "#2"
|
||||
end
|
||||
assert_select_rjs :replace_html, "test2" do
|
||||
assert_select "div", 1
|
||||
assert_select "#2"
|
||||
end
|
||||
assert_raises(AssertionFailedError) { assert_select_rjs :replace_html, "test1" }
|
||||
end
|
||||
|
||||
def test_assert_select_rjs_for_chained_replace
|
||||
render_rjs do |page|
|
||||
page['test1'].replace "<div id=\"1\">foo</div>"
|
||||
page['test2'].replace_html "<div id=\"2\">foo</div>"
|
||||
page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
|
||||
end
|
||||
# Replace.
|
||||
assert_select_rjs :chained_replace do
|
||||
assert_select "div", 1
|
||||
assert_select "#1"
|
||||
end
|
||||
assert_select_rjs :chained_replace, "test1" do
|
||||
assert_select "div", 1
|
||||
assert_select "#1"
|
||||
end
|
||||
assert_raises(AssertionFailedError) { assert_select_rjs :chained_replace, "test2" }
|
||||
# Replace HTML.
|
||||
assert_select_rjs :chained_replace_html do
|
||||
assert_select "div", 1
|
||||
assert_select "#2"
|
||||
end
|
||||
assert_select_rjs :chained_replace_html, "test2" do
|
||||
assert_select "div", 1
|
||||
assert_select "#2"
|
||||
end
|
||||
assert_raises(AssertionFailedError) { assert_select_rjs :replace_html, "test1" }
|
||||
end
|
||||
|
||||
# Simple remove
|
||||
def test_assert_select_rjs_for_remove
|
||||
render_rjs do |page|
|
||||
page.remove "test1"
|
||||
end
|
||||
|
||||
assert_select_rjs :remove, "test1"
|
||||
end
|
||||
|
||||
def test_assert_select_rjs_for_remove_ignores_block
|
||||
render_rjs do |page|
|
||||
page.remove "test1"
|
||||
end
|
||||
|
||||
assert_nothing_raised do
|
||||
assert_select_rjs :remove, "test1" do
|
||||
assert_select "p"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Simple show
|
||||
def test_assert_select_rjs_for_show
|
||||
render_rjs do |page|
|
||||
page.show "test1"
|
||||
end
|
||||
|
||||
assert_select_rjs :show, "test1"
|
||||
end
|
||||
|
||||
def test_assert_select_rjs_for_show_ignores_block
|
||||
render_rjs do |page|
|
||||
page.show "test1"
|
||||
end
|
||||
|
||||
assert_nothing_raised do
|
||||
assert_select_rjs :show, "test1" do
|
||||
assert_select "p"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Simple hide
|
||||
def test_assert_select_rjs_for_hide
|
||||
render_rjs do |page|
|
||||
page.hide "test1"
|
||||
end
|
||||
|
||||
assert_select_rjs :hide, "test1"
|
||||
end
|
||||
|
||||
def test_assert_select_rjs_for_hide_ignores_block
|
||||
render_rjs do |page|
|
||||
page.hide "test1"
|
||||
end
|
||||
|
||||
assert_nothing_raised do
|
||||
assert_select_rjs :hide, "test1" do
|
||||
assert_select "p"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Simple toggle
|
||||
def test_assert_select_rjs_for_toggle
|
||||
render_rjs do |page|
|
||||
page.toggle "test1"
|
||||
end
|
||||
|
||||
assert_select_rjs :toggle, "test1"
|
||||
end
|
||||
|
||||
def test_assert_select_rjs_for_toggle_ignores_block
|
||||
render_rjs do |page|
|
||||
page.toggle "test1"
|
||||
end
|
||||
|
||||
assert_nothing_raised do
|
||||
assert_select_rjs :toggle, "test1" do
|
||||
assert_select "p"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Non-positioned insert.
|
||||
def test_assert_select_rjs_for_nonpositioned_insert
|
||||
render_rjs do |page|
|
||||
page.replace "test1", "<div id=\"1\">foo</div>"
|
||||
page.replace_html "test2", "<div id=\"2\">foo</div>"
|
||||
page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
|
||||
end
|
||||
assert_select_rjs :insert_html do
|
||||
assert_select "div", 1
|
||||
assert_select "#3"
|
||||
end
|
||||
assert_select_rjs :insert_html, "test3" do
|
||||
assert_select "div", 1
|
||||
assert_select "#3"
|
||||
end
|
||||
assert_raises(AssertionFailedError) { assert_select_rjs :insert_html, "test1" }
|
||||
end
|
||||
|
||||
# Positioned insert.
|
||||
def test_assert_select_rjs_for_positioned_insert
|
||||
render_rjs do |page|
|
||||
page.insert_html :top, "test1", "<div id=\"1\">foo</div>"
|
||||
page.insert_html :bottom, "test2", "<div id=\"2\">foo</div>"
|
||||
page.insert_html :before, "test3", "<div id=\"3\">foo</div>"
|
||||
page.insert_html :after, "test4", "<div id=\"4\">foo</div>"
|
||||
end
|
||||
assert_select_rjs :insert, :top do
|
||||
assert_select "div", 1
|
||||
assert_select "#1"
|
||||
end
|
||||
assert_select_rjs :insert, :bottom do
|
||||
assert_select "div", 1
|
||||
assert_select "#2"
|
||||
end
|
||||
assert_select_rjs :insert, :before do
|
||||
assert_select "div", 1
|
||||
assert_select "#3"
|
||||
end
|
||||
assert_select_rjs :insert, :after do
|
||||
assert_select "div", 1
|
||||
assert_select "#4"
|
||||
end
|
||||
assert_select_rjs :insert_html do
|
||||
assert_select "div", 4
|
||||
end
|
||||
end
|
||||
|
||||
def test_assert_select_rjs_raise_errors
|
||||
assert_raises(ArgumentError) { assert_select_rjs(:destroy) }
|
||||
assert_raises(ArgumentError) { assert_select_rjs(:insert, :left) }
|
||||
end
|
||||
|
||||
# Simple selection from a single result.
|
||||
def test_nested_assert_select_rjs_with_single_result
|
||||
render_rjs do |page|
|
||||
page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
|
||||
end
|
||||
|
||||
assert_select_rjs "test" do |elements|
|
||||
assert_equal 2, elements.size
|
||||
assert_select "#1"
|
||||
assert_select "#2"
|
||||
end
|
||||
end
|
||||
|
||||
# Deal with two results.
|
||||
def test_nested_assert_select_rjs_with_two_results
|
||||
render_rjs do |page|
|
||||
page.replace_html "test", "<div id=\"1\">foo</div>"
|
||||
page.replace_html "test2", "<div id=\"2\">foo</div>"
|
||||
end
|
||||
|
||||
assert_select_rjs "test" do |elements|
|
||||
assert_equal 1, elements.size
|
||||
assert_select "#1"
|
||||
end
|
||||
|
||||
assert_select_rjs "test2" do |elements|
|
||||
assert_equal 1, elements.size
|
||||
assert_select "#2"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def test_feed_item_encoded
|
||||
render_xml <<-EOF
|
||||
<rss version="2.0">
|
||||
<channel>
|
||||
<item>
|
||||
<description>
|
||||
<![CDATA[
|
||||
<p>Test 1</p>
|
||||
]]>
|
||||
</description>
|
||||
</item>
|
||||
<item>
|
||||
<description>
|
||||
<![CDATA[
|
||||
<p>Test 2</p>
|
||||
]]>
|
||||
</description>
|
||||
</item>
|
||||
</channel>
|
||||
</rss>
|
||||
EOF
|
||||
assert_select "channel item description" do
|
||||
# Test element regardless of wrapper.
|
||||
assert_select_encoded do
|
||||
assert_select "p", :count=>2, :text=>/Test/
|
||||
end
|
||||
# Test through encoded wrapper.
|
||||
assert_select_encoded do
|
||||
assert_select "encoded p", :count=>2, :text=>/Test/
|
||||
end
|
||||
# Use :root instead (recommended)
|
||||
assert_select_encoded do
|
||||
assert_select ":root p", :count=>2, :text=>/Test/
|
||||
end
|
||||
# Test individually.
|
||||
assert_select "description" do |elements|
|
||||
assert_select_encoded elements[0] do
|
||||
assert_select "p", "Test 1"
|
||||
end
|
||||
assert_select_encoded elements[1] do
|
||||
assert_select "p", "Test 2"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Test that we only un-encode element itself.
|
||||
assert_select "channel item" do
|
||||
assert_select_encoded do
|
||||
assert_select "p", 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
#
|
||||
# Test assert_select_email
|
||||
#
|
||||
|
||||
def test_assert_select_email
|
||||
assert_raises(AssertionFailedError) { assert_select_email {} }
|
||||
AssertSelectMailer.deliver_test "<div><p>foo</p><p>bar</p></div>"
|
||||
assert_select_email do
|
||||
assert_select "div:root" do
|
||||
assert_select "p:first-child", "foo"
|
||||
assert_select "p:last-child", "bar"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
protected
|
||||
def render_html(html)
|
||||
@controller.response_with = html
|
||||
get :html
|
||||
end
|
||||
|
||||
def render_rjs(&block)
|
||||
@controller.response_with &block
|
||||
get :rjs
|
||||
end
|
||||
|
||||
def render_xml(xml)
|
||||
@controller.response_with = xml
|
||||
get :xml
|
||||
end
|
||||
end
|
|
@ -1,219 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late
|
||||
|
||||
# Provide some controller to run the tests on.
|
||||
module Submodule
|
||||
class ContainedEmptyController < ActionController::Base
|
||||
end
|
||||
class ContainedNonEmptyController < ActionController::Base
|
||||
def public_action
|
||||
render :nothing => true
|
||||
end
|
||||
|
||||
hide_action :hidden_action
|
||||
def hidden_action
|
||||
raise "Noooo!"
|
||||
end
|
||||
|
||||
def another_hidden_action
|
||||
end
|
||||
hide_action :another_hidden_action
|
||||
end
|
||||
class SubclassedController < ContainedNonEmptyController
|
||||
hide_action :public_action # Hiding it here should not affect the superclass.
|
||||
end
|
||||
end
|
||||
class EmptyController < ActionController::Base
|
||||
end
|
||||
class NonEmptyController < ActionController::Base
|
||||
def public_action
|
||||
end
|
||||
|
||||
hide_action :hidden_action
|
||||
def hidden_action
|
||||
end
|
||||
end
|
||||
|
||||
class MethodMissingController < ActionController::Base
|
||||
|
||||
hide_action :shouldnt_be_called
|
||||
def shouldnt_be_called
|
||||
raise "NO WAY!"
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def method_missing(selector)
|
||||
render :text => selector.to_s
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class DefaultUrlOptionsController < ActionController::Base
|
||||
def default_url_options_action
|
||||
end
|
||||
|
||||
def default_url_options(options = nil)
|
||||
{ :host => 'www.override.com', :action => 'new', :bacon => 'chunky' }
|
||||
end
|
||||
end
|
||||
|
||||
class ControllerClassTests < Test::Unit::TestCase
|
||||
def test_controller_path
|
||||
assert_equal 'empty', EmptyController.controller_path
|
||||
assert_equal EmptyController.controller_path, EmptyController.new.controller_path
|
||||
assert_equal 'submodule/contained_empty', Submodule::ContainedEmptyController.controller_path
|
||||
assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path
|
||||
end
|
||||
def test_controller_name
|
||||
assert_equal 'empty', EmptyController.controller_name
|
||||
assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name
|
||||
end
|
||||
end
|
||||
|
||||
class ControllerInstanceTests < Test::Unit::TestCase
|
||||
def setup
|
||||
@empty = EmptyController.new
|
||||
@contained = Submodule::ContainedEmptyController.new
|
||||
@empty_controllers = [@empty, @contained, Submodule::SubclassedController.new]
|
||||
|
||||
@non_empty_controllers = [NonEmptyController.new,
|
||||
Submodule::ContainedNonEmptyController.new]
|
||||
end
|
||||
|
||||
def test_action_methods
|
||||
@empty_controllers.each do |c|
|
||||
hide_mocha_methods_from_controller(c)
|
||||
assert_equal Set.new, c.send!(:action_methods), "#{c.controller_path} should be empty!"
|
||||
end
|
||||
@non_empty_controllers.each do |c|
|
||||
hide_mocha_methods_from_controller(c)
|
||||
assert_equal Set.new(%w(public_action)), c.send!(:action_methods), "#{c.controller_path} should not be empty!"
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
# Mocha adds some public instance methods to Object that would be
|
||||
# considered actions, so explicitly hide_action them.
|
||||
def hide_mocha_methods_from_controller(controller)
|
||||
mocha_methods = [
|
||||
:expects, :mocha, :mocha_inspect, :reset_mocha, :stubba_object,
|
||||
:stubba_method, :stubs, :verify, :__metaclass__, :__is_a__, :to_matcher,
|
||||
]
|
||||
controller.class.send!(:hide_action, *mocha_methods)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class PerformActionTest < Test::Unit::TestCase
|
||||
class MockLogger
|
||||
attr_reader :logged
|
||||
|
||||
def initialize
|
||||
@logged = []
|
||||
end
|
||||
|
||||
def method_missing(method, *args)
|
||||
@logged << args.first
|
||||
end
|
||||
end
|
||||
|
||||
def use_controller(controller_class)
|
||||
@controller = controller_class.new
|
||||
|
||||
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
|
||||
# a more accurate simulation of what happens in "real life".
|
||||
@controller.logger = Logger.new(nil)
|
||||
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
|
||||
@request.host = "www.nextangle.com"
|
||||
end
|
||||
|
||||
def test_get_on_priv_should_show_selector
|
||||
use_controller MethodMissingController
|
||||
get :shouldnt_be_called
|
||||
assert_response :success
|
||||
assert_equal 'shouldnt_be_called', @response.body
|
||||
end
|
||||
|
||||
def test_method_missing_is_not_an_action_name
|
||||
use_controller MethodMissingController
|
||||
assert ! @controller.send!(:action_methods).include?('method_missing')
|
||||
|
||||
get :method_missing
|
||||
assert_response :success
|
||||
assert_equal 'method_missing', @response.body
|
||||
end
|
||||
|
||||
def test_get_on_hidden_should_fail
|
||||
use_controller NonEmptyController
|
||||
get :hidden_action
|
||||
assert_response 404
|
||||
|
||||
get :another_hidden_action
|
||||
assert_response 404
|
||||
end
|
||||
|
||||
def test_namespaced_action_should_log_module_name
|
||||
use_controller Submodule::ContainedNonEmptyController
|
||||
@controller.logger = MockLogger.new
|
||||
get :public_action
|
||||
assert_match /Processing\sSubmodule::ContainedNonEmptyController#public_action/, @controller.logger.logged[1]
|
||||
end
|
||||
end
|
||||
|
||||
class DefaultUrlOptionsTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = DefaultUrlOptionsController.new
|
||||
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
|
||||
@request.host = 'www.example.com'
|
||||
end
|
||||
|
||||
def test_default_url_options_are_used_if_set
|
||||
ActionController::Routing::Routes.draw do |map|
|
||||
map.default_url_options 'default_url_options', :controller => 'default_url_options'
|
||||
map.connect ':controller/:action/:id'
|
||||
end
|
||||
|
||||
get :default_url_options_action # Make a dummy request so that the controller is initialized properly.
|
||||
|
||||
assert_equal 'http://www.override.com/default_url_options/new?bacon=chunky', @controller.url_for(:controller => 'default_url_options')
|
||||
assert_equal 'http://www.override.com/default_url_options?bacon=chunky', @controller.send(:default_url_options_url)
|
||||
ensure
|
||||
ActionController::Routing::Routes.load!
|
||||
end
|
||||
end
|
||||
|
||||
class EmptyUrlOptionsTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = NonEmptyController.new
|
||||
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
|
||||
@request.host = 'www.example.com'
|
||||
end
|
||||
|
||||
def test_ensure_url_for_works_as_expected_when_called_with_no_options_if_default_url_options_is_not_set
|
||||
get :public_action
|
||||
assert_equal "http://www.example.com/non_empty/public_action", @controller.url_for
|
||||
end
|
||||
end
|
||||
|
||||
class EnsureNamedRoutesWorksTicket22BugTest < Test::Unit::TestCase
|
||||
def test_named_routes_still_work
|
||||
ActionController::Routing::Routes.draw do |map|
|
||||
map.resources :things
|
||||
end
|
||||
EmptyController.send :include, ActionController::UrlWriter
|
||||
|
||||
assert_equal '/things', EmptyController.new.send(:things_path)
|
||||
ensure
|
||||
ActionController::Routing::Routes.load!
|
||||
end
|
||||
end
|
|
@ -1,32 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
# Provide some static controllers.
|
||||
class BenchmarkedController < ActionController::Base
|
||||
def public_action
|
||||
render :nothing => true
|
||||
end
|
||||
|
||||
def rescue_action(e)
|
||||
raise e
|
||||
end
|
||||
end
|
||||
|
||||
class BenchmarkTest < Test::Unit::TestCase
|
||||
class MockLogger
|
||||
def method_missing(*args)
|
||||
end
|
||||
end
|
||||
|
||||
def setup
|
||||
@controller = BenchmarkedController.new
|
||||
# benchmark doesn't do anything unless a logger is set
|
||||
@controller.logger = MockLogger.new
|
||||
@request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
|
||||
@request.host = "test.actioncontroller.i"
|
||||
end
|
||||
|
||||
def test_with_http_1_0_request
|
||||
@request.host = nil
|
||||
assert_nothing_raised { get :public_action }
|
||||
end
|
||||
end
|
|
@ -1,610 +0,0 @@
|
|||
require 'fileutils'
|
||||
require 'abstract_unit'
|
||||
|
||||
CACHE_DIR = 'test_cache'
|
||||
# Don't change '/../temp/' cavalierly or you might hose something you don't want hosed
|
||||
FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR)
|
||||
ActionController::Base.page_cache_directory = FILE_STORE_PATH
|
||||
ActionController::Base.cache_store = :file_store, FILE_STORE_PATH
|
||||
ActionController::Base.view_paths = [ File.dirname(__FILE__) + '/../fixtures/' ]
|
||||
|
||||
class PageCachingTestController < ActionController::Base
|
||||
caches_page :ok, :no_content, :if => Proc.new { |c| !c.request.format.json? }
|
||||
caches_page :found, :not_found
|
||||
|
||||
def ok
|
||||
head :ok
|
||||
end
|
||||
|
||||
def no_content
|
||||
head :no_content
|
||||
end
|
||||
|
||||
def found
|
||||
redirect_to :action => 'ok'
|
||||
end
|
||||
|
||||
def not_found
|
||||
head :not_found
|
||||
end
|
||||
|
||||
def custom_path
|
||||
render :text => "Super soaker"
|
||||
cache_page("Super soaker", "/index.html")
|
||||
end
|
||||
|
||||
def expire_custom_path
|
||||
expire_page("/index.html")
|
||||
head :ok
|
||||
end
|
||||
|
||||
def trailing_slash
|
||||
render :text => "Sneak attack"
|
||||
end
|
||||
end
|
||||
|
||||
class PageCachingTest < Test::Unit::TestCase
|
||||
def setup
|
||||
ActionController::Base.perform_caching = true
|
||||
|
||||
ActionController::Routing::Routes.draw do |map|
|
||||
map.main '', :controller => 'posts'
|
||||
map.resources :posts
|
||||
map.connect ':controller/:action/:id'
|
||||
end
|
||||
|
||||
@request = ActionController::TestRequest.new
|
||||
@request.host = 'hostname.com'
|
||||
|
||||
@response = ActionController::TestResponse.new
|
||||
@controller = PageCachingTestController.new
|
||||
|
||||
@params = {:controller => 'posts', :action => 'index', :only_path => true, :skip_relative_url_root => true}
|
||||
@rewriter = ActionController::UrlRewriter.new(@request, @params)
|
||||
|
||||
FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
|
||||
FileUtils.mkdir_p(FILE_STORE_PATH)
|
||||
end
|
||||
|
||||
def teardown
|
||||
FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
|
||||
|
||||
ActionController::Base.perform_caching = false
|
||||
end
|
||||
|
||||
def test_page_caching_resources_saves_to_correct_path_with_extension_even_if_default_route
|
||||
@params[:format] = 'rss'
|
||||
assert_equal '/posts.rss', @rewriter.rewrite(@params)
|
||||
@params[:format] = nil
|
||||
assert_equal '/', @rewriter.rewrite(@params)
|
||||
end
|
||||
|
||||
def test_should_cache_get_with_ok_status
|
||||
get :ok
|
||||
assert_response :ok
|
||||
assert_page_cached :ok, "get with ok status should have been cached"
|
||||
end
|
||||
|
||||
def test_should_cache_with_custom_path
|
||||
get :custom_path
|
||||
assert File.exist?("#{FILE_STORE_PATH}/index.html")
|
||||
end
|
||||
|
||||
def test_should_expire_cache_with_custom_path
|
||||
get :custom_path
|
||||
assert File.exist?("#{FILE_STORE_PATH}/index.html")
|
||||
|
||||
get :expire_custom_path
|
||||
assert !File.exist?("#{FILE_STORE_PATH}/index.html")
|
||||
end
|
||||
|
||||
def test_should_cache_without_trailing_slash_on_url
|
||||
@controller.class.cache_page 'cached content', '/page_caching_test/trailing_slash'
|
||||
assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/trailing_slash.html")
|
||||
end
|
||||
|
||||
def test_should_cache_with_trailing_slash_on_url
|
||||
@controller.class.cache_page 'cached content', '/page_caching_test/trailing_slash/'
|
||||
assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/trailing_slash.html")
|
||||
end
|
||||
|
||||
uses_mocha("should_cache_ok_at_custom_path") do
|
||||
def test_should_cache_ok_at_custom_path
|
||||
@request.expects(:path).returns("/index.html")
|
||||
get :ok
|
||||
assert_response :ok
|
||||
assert File.exist?("#{FILE_STORE_PATH}/index.html")
|
||||
end
|
||||
end
|
||||
|
||||
[:ok, :no_content, :found, :not_found].each do |status|
|
||||
[:get, :post, :put, :delete].each do |method|
|
||||
unless method == :get and status == :ok
|
||||
define_method "test_shouldnt_cache_#{method}_with_#{status}_status" do
|
||||
@request.env['REQUEST_METHOD'] = method.to_s.upcase
|
||||
process status
|
||||
assert_response status
|
||||
assert_page_not_cached status, "#{method} with #{status} status shouldn't have been cached"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_page_caching_conditional_options
|
||||
@request.env['HTTP_ACCEPT'] = 'application/json'
|
||||
get :ok
|
||||
assert_page_not_cached :ok
|
||||
end
|
||||
|
||||
private
|
||||
def assert_page_cached(action, message = "#{action} should have been cached")
|
||||
assert page_cached?(action), message
|
||||
end
|
||||
|
||||
def assert_page_not_cached(action, message = "#{action} shouldn't have been cached")
|
||||
assert !page_cached?(action), message
|
||||
end
|
||||
|
||||
def page_cached?(action)
|
||||
File.exist? "#{FILE_STORE_PATH}/page_caching_test/#{action}.html"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class ActionCachingTestController < ActionController::Base
|
||||
caches_action :index, :redirected, :forbidden, :if => Proc.new { |c| !c.request.format.json? }
|
||||
caches_action :show, :cache_path => 'http://test.host/custom/show'
|
||||
caches_action :edit, :cache_path => Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" }
|
||||
caches_action :with_layout
|
||||
|
||||
layout 'talk_from_action.erb'
|
||||
|
||||
def index
|
||||
@cache_this = MockTime.now.to_f.to_s
|
||||
render :text => @cache_this
|
||||
end
|
||||
|
||||
def redirected
|
||||
redirect_to :action => 'index'
|
||||
end
|
||||
|
||||
def forbidden
|
||||
render :text => "Forbidden"
|
||||
headers["Status"] = "403 Forbidden"
|
||||
end
|
||||
|
||||
def with_layout
|
||||
@cache_this = MockTime.now.to_f.to_s
|
||||
render :text => @cache_this, :layout => true
|
||||
end
|
||||
|
||||
alias_method :show, :index
|
||||
alias_method :edit, :index
|
||||
alias_method :destroy, :index
|
||||
|
||||
def expire
|
||||
expire_action :controller => 'action_caching_test', :action => 'index'
|
||||
render :nothing => true
|
||||
end
|
||||
end
|
||||
|
||||
class MockTime < Time
|
||||
# Let Time spicy to assure that Time.now != Time.now
|
||||
def to_f
|
||||
super+rand
|
||||
end
|
||||
end
|
||||
|
||||
class ActionCachingMockController
|
||||
attr_accessor :mock_url_for
|
||||
attr_accessor :mock_path
|
||||
|
||||
def initialize
|
||||
yield self if block_given?
|
||||
end
|
||||
|
||||
def url_for(*args)
|
||||
@mock_url_for
|
||||
end
|
||||
|
||||
def request
|
||||
mocked_path = @mock_path
|
||||
Object.new.instance_eval(<<-EVAL)
|
||||
def path; '#{@mock_path}' end
|
||||
self
|
||||
EVAL
|
||||
end
|
||||
end
|
||||
|
||||
class ActionCacheTest < Test::Unit::TestCase
|
||||
def setup
|
||||
reset!
|
||||
FileUtils.mkdir_p(FILE_STORE_PATH)
|
||||
@path_class = ActionController::Caching::Actions::ActionCachePath
|
||||
@mock_controller = ActionCachingMockController.new
|
||||
end
|
||||
|
||||
def teardown
|
||||
FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
|
||||
end
|
||||
|
||||
def test_simple_action_cache
|
||||
get :index
|
||||
cached_time = content_to_cache
|
||||
assert_equal cached_time, @response.body
|
||||
assert fragment_exist?('hostname.com/action_caching_test')
|
||||
reset!
|
||||
|
||||
get :index
|
||||
assert_equal cached_time, @response.body
|
||||
end
|
||||
|
||||
def test_simple_action_not_cached
|
||||
get :destroy
|
||||
cached_time = content_to_cache
|
||||
assert_equal cached_time, @response.body
|
||||
assert !fragment_exist?('hostname.com/action_caching_test/destroy')
|
||||
reset!
|
||||
|
||||
get :destroy
|
||||
assert_not_equal cached_time, @response.body
|
||||
end
|
||||
|
||||
def test_action_cache_with_layout
|
||||
get :with_layout
|
||||
cached_time = content_to_cache
|
||||
assert_not_equal cached_time, @response.body
|
||||
assert fragment_exist?('hostname.com/action_caching_test/with_layout')
|
||||
reset!
|
||||
|
||||
get :with_layout
|
||||
assert_not_equal cached_time, @response.body
|
||||
|
||||
assert_equal @response.body, read_fragment('hostname.com/action_caching_test/with_layout')
|
||||
end
|
||||
|
||||
def test_action_cache_conditional_options
|
||||
@request.env['HTTP_ACCEPT'] = 'application/json'
|
||||
get :index
|
||||
assert !fragment_exist?('hostname.com/action_caching_test')
|
||||
end
|
||||
|
||||
def test_action_cache_with_custom_cache_path
|
||||
get :show
|
||||
cached_time = content_to_cache
|
||||
assert_equal cached_time, @response.body
|
||||
assert fragment_exist?('test.host/custom/show')
|
||||
reset!
|
||||
|
||||
get :show
|
||||
assert_equal cached_time, @response.body
|
||||
end
|
||||
|
||||
def test_action_cache_with_custom_cache_path_in_block
|
||||
get :edit
|
||||
assert fragment_exist?('test.host/edit')
|
||||
reset!
|
||||
|
||||
get :edit, :id => 1
|
||||
assert fragment_exist?('test.host/1;edit')
|
||||
end
|
||||
|
||||
def test_cache_expiration
|
||||
get :index
|
||||
cached_time = content_to_cache
|
||||
reset!
|
||||
|
||||
get :index
|
||||
assert_equal cached_time, @response.body
|
||||
reset!
|
||||
|
||||
get :expire
|
||||
reset!
|
||||
|
||||
get :index
|
||||
new_cached_time = content_to_cache
|
||||
assert_not_equal cached_time, @response.body
|
||||
reset!
|
||||
|
||||
get :index
|
||||
assert_response :success
|
||||
assert_equal new_cached_time, @response.body
|
||||
end
|
||||
|
||||
def test_cache_is_scoped_by_subdomain
|
||||
@request.host = 'jamis.hostname.com'
|
||||
get :index
|
||||
jamis_cache = content_to_cache
|
||||
|
||||
reset!
|
||||
|
||||
@request.host = 'david.hostname.com'
|
||||
get :index
|
||||
david_cache = content_to_cache
|
||||
assert_not_equal jamis_cache, @response.body
|
||||
|
||||
reset!
|
||||
|
||||
@request.host = 'jamis.hostname.com'
|
||||
get :index
|
||||
assert_equal jamis_cache, @response.body
|
||||
|
||||
reset!
|
||||
|
||||
@request.host = 'david.hostname.com'
|
||||
get :index
|
||||
assert_equal david_cache, @response.body
|
||||
end
|
||||
|
||||
def test_redirect_is_not_cached
|
||||
get :redirected
|
||||
assert_response :redirect
|
||||
reset!
|
||||
|
||||
get :redirected
|
||||
assert_response :redirect
|
||||
end
|
||||
|
||||
def test_forbidden_is_not_cached
|
||||
get :forbidden
|
||||
assert_response :forbidden
|
||||
reset!
|
||||
|
||||
get :forbidden
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
def test_xml_version_of_resource_is_treated_as_different_cache
|
||||
@mock_controller.mock_url_for = 'http://example.org/posts/'
|
||||
@mock_controller.mock_path = '/posts/index.xml'
|
||||
path_object = @path_class.new(@mock_controller, {})
|
||||
assert_equal 'xml', path_object.extension
|
||||
assert_equal 'example.org/posts/index.xml', path_object.path
|
||||
end
|
||||
|
||||
def test_correct_content_type_is_returned_for_cache_hit
|
||||
# run it twice to cache it the first time
|
||||
get :index, :id => 'content-type.xml'
|
||||
get :index, :id => 'content-type.xml'
|
||||
assert_equal 'application/xml', @response.content_type
|
||||
end
|
||||
|
||||
def test_empty_path_is_normalized
|
||||
@mock_controller.mock_url_for = 'http://example.org/'
|
||||
@mock_controller.mock_path = '/'
|
||||
|
||||
assert_equal 'example.org/index', @path_class.path_for(@mock_controller, {})
|
||||
end
|
||||
|
||||
def test_file_extensions
|
||||
get :index, :id => 'kitten.jpg'
|
||||
get :index, :id => 'kitten.jpg'
|
||||
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
private
|
||||
def content_to_cache
|
||||
assigns(:cache_this)
|
||||
end
|
||||
|
||||
def reset!
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
@controller = ActionCachingTestController.new
|
||||
@request.host = 'hostname.com'
|
||||
end
|
||||
|
||||
def fragment_exist?(path)
|
||||
@controller.fragment_exist?(path)
|
||||
end
|
||||
|
||||
def read_fragment(path)
|
||||
@controller.read_fragment(path)
|
||||
end
|
||||
end
|
||||
|
||||
class FragmentCachingTestController < ActionController::Base
|
||||
def some_action; end;
|
||||
end
|
||||
|
||||
class FragmentCachingTest < Test::Unit::TestCase
|
||||
def setup
|
||||
ActionController::Base.perform_caching = true
|
||||
@store = ActiveSupport::Cache::MemoryStore.new
|
||||
ActionController::Base.cache_store = @store
|
||||
@controller = FragmentCachingTestController.new
|
||||
@params = {:controller => 'posts', :action => 'index'}
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
@controller.params = @params
|
||||
@controller.request = @request
|
||||
@controller.response = @response
|
||||
@controller.send(:initialize_current_url)
|
||||
end
|
||||
|
||||
def test_fragment_cache_key
|
||||
assert_equal 'views/what a key', @controller.fragment_cache_key('what a key')
|
||||
assert_equal( "views/test.host/fragment_caching_test/some_action",
|
||||
@controller.fragment_cache_key(:controller => 'fragment_caching_test',:action => 'some_action'))
|
||||
end
|
||||
|
||||
def test_read_fragment__with_caching_enabled
|
||||
@store.write('views/name', 'value')
|
||||
assert_equal 'value', @controller.read_fragment('name')
|
||||
end
|
||||
|
||||
def test_read_fragment__with_caching_disabled
|
||||
ActionController::Base.perform_caching = false
|
||||
@store.write('views/name', 'value')
|
||||
assert_nil @controller.read_fragment('name')
|
||||
end
|
||||
|
||||
def test_fragment_exist__with_caching_enabled
|
||||
@store.write('views/name', 'value')
|
||||
assert @controller.fragment_exist?('name')
|
||||
assert !@controller.fragment_exist?('other_name')
|
||||
end
|
||||
|
||||
def test_fragment_exist__with_caching_disabled
|
||||
ActionController::Base.perform_caching = false
|
||||
@store.write('views/name', 'value')
|
||||
assert !@controller.fragment_exist?('name')
|
||||
assert !@controller.fragment_exist?('other_name')
|
||||
end
|
||||
|
||||
def test_write_fragment__with_caching_enabled
|
||||
assert_nil @store.read('views/name')
|
||||
assert_equal 'value', @controller.write_fragment('name', 'value')
|
||||
assert_equal 'value', @store.read('views/name')
|
||||
end
|
||||
|
||||
def test_write_fragment__with_caching_disabled
|
||||
assert_nil @store.read('views/name')
|
||||
ActionController::Base.perform_caching = false
|
||||
assert_equal nil, @controller.write_fragment('name', 'value')
|
||||
assert_nil @store.read('views/name')
|
||||
end
|
||||
|
||||
def test_expire_fragment__with_simple_key
|
||||
@store.write('views/name', 'value')
|
||||
@controller.expire_fragment 'name'
|
||||
assert_nil @store.read('views/name')
|
||||
end
|
||||
|
||||
def test_expire_fragment__with__regexp
|
||||
@store.write('views/name', 'value')
|
||||
@store.write('views/another_name', 'another_value')
|
||||
@store.write('views/primalgrasp', 'will not expire ;-)')
|
||||
|
||||
@controller.expire_fragment /name/
|
||||
|
||||
assert_nil @store.read('views/name')
|
||||
assert_nil @store.read('views/another_name')
|
||||
assert_equal 'will not expire ;-)', @store.read('views/primalgrasp')
|
||||
end
|
||||
|
||||
def test_fragment_for__with_disabled_caching
|
||||
ActionController::Base.perform_caching = false
|
||||
|
||||
@store.write('views/expensive', 'fragment content')
|
||||
fragment_computed = false
|
||||
|
||||
buffer = 'generated till now -> '
|
||||
@controller.fragment_for(Proc.new { fragment_computed = true }, 'expensive') { buffer }
|
||||
|
||||
assert fragment_computed
|
||||
assert_equal 'generated till now -> ', buffer
|
||||
end
|
||||
|
||||
def test_fragment_for
|
||||
@store.write('views/expensive', 'fragment content')
|
||||
fragment_computed = false
|
||||
|
||||
buffer = 'generated till now -> '
|
||||
@controller.fragment_for(Proc.new { fragment_computed = true }, 'expensive') { buffer}
|
||||
|
||||
assert !fragment_computed
|
||||
assert_equal 'generated till now -> fragment content', buffer
|
||||
end
|
||||
|
||||
def test_cache_erb_fragment
|
||||
@store.write('views/expensive', 'fragment content')
|
||||
_erbout = 'generated till now -> '
|
||||
|
||||
assert_equal( 'generated till now -> fragment content',
|
||||
ActionView::TemplateHandlers::ERB.new(@controller).cache_fragment(Proc.new{ }, 'expensive'))
|
||||
end
|
||||
|
||||
def test_cache_rxml_fragment
|
||||
@store.write('views/expensive', 'fragment content')
|
||||
xml = 'generated till now -> '
|
||||
class << xml; def target!; to_s; end; end
|
||||
|
||||
assert_equal( 'generated till now -> fragment content',
|
||||
ActionView::TemplateHandlers::Builder.new(@controller).cache_fragment(Proc.new{ }, 'expensive'))
|
||||
end
|
||||
|
||||
def test_cache_rjs_fragment
|
||||
@store.write('views/expensive', 'fragment content')
|
||||
page = 'generated till now -> '
|
||||
|
||||
assert_equal( 'generated till now -> fragment content',
|
||||
ActionView::TemplateHandlers::RJS.new(@controller).cache_fragment(Proc.new{ }, 'expensive'))
|
||||
end
|
||||
|
||||
def test_cache_rjs_fragment_debug_mode_does_not_interfere
|
||||
@store.write('views/expensive', 'fragment content')
|
||||
page = 'generated till now -> '
|
||||
|
||||
begin
|
||||
debug_mode, ActionView::Base.debug_rjs = ActionView::Base.debug_rjs, true
|
||||
assert_equal( 'generated till now -> fragment content',
|
||||
ActionView::TemplateHandlers::RJS.new(@controller).cache_fragment(Proc.new{ }, 'expensive'))
|
||||
assert ActionView::Base.debug_rjs
|
||||
ensure
|
||||
ActionView::Base.debug_rjs = debug_mode
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class FunctionalCachingController < ActionController::Base
|
||||
def fragment_cached
|
||||
end
|
||||
|
||||
def html_fragment_cached_with_partial
|
||||
respond_to do |format|
|
||||
format.html
|
||||
end
|
||||
end
|
||||
|
||||
def js_fragment_cached_with_partial
|
||||
respond_to do |format|
|
||||
format.js
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def rescue_action(e)
|
||||
raise e
|
||||
end
|
||||
end
|
||||
|
||||
FunctionalCachingController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
|
||||
class FunctionalFragmentCachingTest < Test::Unit::TestCase
|
||||
def setup
|
||||
ActionController::Base.perform_caching = true
|
||||
@store = ActiveSupport::Cache::MemoryStore.new
|
||||
ActionController::Base.cache_store = @store
|
||||
@controller = FunctionalCachingController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
end
|
||||
def test_fragment_caching
|
||||
get :fragment_cached
|
||||
assert_response :success
|
||||
expected_body = <<-CACHED
|
||||
Hello
|
||||
This bit's fragment cached
|
||||
CACHED
|
||||
assert_equal expected_body, @response.body
|
||||
|
||||
assert_equal "This bit's fragment cached", @store.read('views/test.host/functional_caching/fragment_cached')
|
||||
end
|
||||
|
||||
def test_fragment_caching_in_partials
|
||||
get :html_fragment_cached_with_partial
|
||||
assert_response :success
|
||||
assert_match /Fragment caching in a partial/, @response.body
|
||||
assert_match "Fragment caching in a partial", @store.read('views/test.host/functional_caching/html_fragment_cached_with_partial')
|
||||
end
|
||||
|
||||
def test_fragment_caching_in_rjs_partials
|
||||
xhr :get, :js_fragment_cached_with_partial
|
||||
assert_response :success
|
||||
assert_match /Fragment caching in a partial/, @response.body
|
||||
assert_match "Fragment caching in a partial", @store.read('views/test.host/functional_caching/js_fragment_cached_with_partial')
|
||||
end
|
||||
end
|
|
@ -1,89 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class CaptureController < ActionController::Base
|
||||
def self.controller_name; "test"; end
|
||||
def self.controller_path; "test"; end
|
||||
|
||||
def content_for
|
||||
render :layout => "talk_from_action"
|
||||
end
|
||||
|
||||
def content_for_with_parameter
|
||||
render :layout => "talk_from_action"
|
||||
end
|
||||
|
||||
def content_for_concatenated
|
||||
render :layout => "talk_from_action"
|
||||
end
|
||||
|
||||
def erb_content_for
|
||||
render :layout => "talk_from_action"
|
||||
end
|
||||
|
||||
def block_content_for
|
||||
render :layout => "talk_from_action"
|
||||
end
|
||||
|
||||
def non_erb_block_content_for
|
||||
render :layout => "talk_from_action"
|
||||
end
|
||||
|
||||
def rescue_action(e) raise end
|
||||
end
|
||||
|
||||
CaptureController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
|
||||
class CaptureTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = CaptureController.new
|
||||
|
||||
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
|
||||
# a more accurate simulation of what happens in "real life".
|
||||
@controller.logger = Logger.new(nil)
|
||||
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
|
||||
@request.host = "www.nextangle.com"
|
||||
end
|
||||
|
||||
def test_simple_capture
|
||||
get :capturing
|
||||
assert_equal "Dreamy days", @response.body.strip
|
||||
end
|
||||
|
||||
def test_content_for
|
||||
get :content_for
|
||||
assert_equal expected_content_for_output, @response.body
|
||||
end
|
||||
|
||||
def test_should_concatentate_content_for
|
||||
get :content_for_concatenated
|
||||
assert_equal expected_content_for_output, @response.body
|
||||
end
|
||||
|
||||
def test_erb_content_for
|
||||
get :erb_content_for
|
||||
assert_equal expected_content_for_output, @response.body
|
||||
end
|
||||
|
||||
def test_should_set_content_for_with_parameter
|
||||
get :content_for_with_parameter
|
||||
assert_equal expected_content_for_output, @response.body
|
||||
end
|
||||
|
||||
def test_block_content_for
|
||||
get :block_content_for
|
||||
assert_equal expected_content_for_output, @response.body
|
||||
end
|
||||
|
||||
def test_non_erb_block_content_for
|
||||
get :non_erb_block_content_for
|
||||
assert_equal expected_content_for_output, @response.body
|
||||
end
|
||||
|
||||
private
|
||||
def expected_content_for_output
|
||||
"<title>Putting stuff in the title!</title>\n\nGreat stuff!"
|
||||
end
|
||||
end
|
|
@ -1,116 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
require 'action_controller/cgi_process'
|
||||
|
||||
class BaseCgiTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@request_hash = {"HTTP_MAX_FORWARDS"=>"10", "SERVER_NAME"=>"glu.ttono.us:8007", "FCGI_ROLE"=>"RESPONDER", "HTTP_X_FORWARDED_HOST"=>"glu.ttono.us", "HTTP_ACCEPT_ENCODING"=>"gzip, deflate", "HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/312.5.1 (KHTML, like Gecko) Safari/312.3.1", "PATH_INFO"=>"", "HTTP_ACCEPT_LANGUAGE"=>"en", "HTTP_HOST"=>"glu.ttono.us:8007", "SERVER_PROTOCOL"=>"HTTP/1.1", "REDIRECT_URI"=>"/dispatch.fcgi", "SCRIPT_NAME"=>"/dispatch.fcgi", "SERVER_ADDR"=>"207.7.108.53", "REMOTE_ADDR"=>"207.7.108.53", "SERVER_SOFTWARE"=>"lighttpd/1.4.5", "HTTP_COOKIE"=>"_session_id=c84ace84796670c052c6ceb2451fb0f2; is_admin=yes", "HTTP_X_FORWARDED_SERVER"=>"glu.ttono.us", "REQUEST_URI"=>"/admin", "DOCUMENT_ROOT"=>"/home/kevinc/sites/typo/public", "SERVER_PORT"=>"8007", "QUERY_STRING"=>"", "REMOTE_PORT"=>"63137", "GATEWAY_INTERFACE"=>"CGI/1.1", "HTTP_X_FORWARDED_FOR"=>"65.88.180.234", "HTTP_ACCEPT"=>"*/*", "SCRIPT_FILENAME"=>"/home/kevinc/sites/typo/public/dispatch.fcgi", "REDIRECT_STATUS"=>"200", "REQUEST_METHOD"=>"GET"}
|
||||
# some Nokia phone browsers omit the space after the semicolon separator.
|
||||
# some developers have grown accustomed to using comma in cookie values.
|
||||
@alt_cookie_fmt_request_hash = {"HTTP_COOKIE"=>"_session_id=c84ace847,96670c052c6ceb2451fb0f2;is_admin=yes"}
|
||||
@fake_cgi = Struct.new(:env_table).new(@request_hash)
|
||||
@request = ActionController::CgiRequest.new(@fake_cgi)
|
||||
end
|
||||
|
||||
def default_test; end
|
||||
end
|
||||
|
||||
|
||||
class CgiRequestTest < BaseCgiTest
|
||||
def test_proxy_request
|
||||
assert_equal 'glu.ttono.us', @request.host_with_port
|
||||
end
|
||||
|
||||
def test_http_host
|
||||
@request_hash.delete "HTTP_X_FORWARDED_HOST"
|
||||
@request_hash['HTTP_HOST'] = "rubyonrails.org:8080"
|
||||
assert_equal "rubyonrails.org:8080", @request.host_with_port
|
||||
|
||||
@request_hash['HTTP_X_FORWARDED_HOST'] = "www.firsthost.org, www.secondhost.org"
|
||||
assert_equal "www.secondhost.org", @request.host
|
||||
end
|
||||
|
||||
def test_http_host_with_default_port_overrides_server_port
|
||||
@request_hash.delete "HTTP_X_FORWARDED_HOST"
|
||||
@request_hash['HTTP_HOST'] = "rubyonrails.org"
|
||||
assert_equal "rubyonrails.org", @request.host_with_port
|
||||
end
|
||||
|
||||
def test_host_with_port_defaults_to_server_name_if_no_host_headers
|
||||
@request_hash.delete "HTTP_X_FORWARDED_HOST"
|
||||
@request_hash.delete "HTTP_HOST"
|
||||
assert_equal "glu.ttono.us:8007", @request.host_with_port
|
||||
end
|
||||
|
||||
def test_host_with_port_falls_back_to_server_addr_if_necessary
|
||||
@request_hash.delete "HTTP_X_FORWARDED_HOST"
|
||||
@request_hash.delete "HTTP_HOST"
|
||||
@request_hash.delete "SERVER_NAME"
|
||||
assert_equal "207.7.108.53:8007", @request.host_with_port
|
||||
end
|
||||
|
||||
def test_host_with_port_if_http_standard_port_is_specified
|
||||
@request_hash['HTTP_X_FORWARDED_HOST'] = "glu.ttono.us:80"
|
||||
assert_equal "glu.ttono.us", @request.host_with_port
|
||||
end
|
||||
|
||||
def test_host_with_port_if_https_standard_port_is_specified
|
||||
@request_hash['HTTP_X_FORWARDED_PROTO'] = "https"
|
||||
@request_hash['HTTP_X_FORWARDED_HOST'] = "glu.ttono.us:443"
|
||||
assert_equal "glu.ttono.us", @request.host_with_port
|
||||
end
|
||||
|
||||
def test_host_if_ipv6_reference
|
||||
@request_hash.delete "HTTP_X_FORWARDED_HOST"
|
||||
@request_hash['HTTP_HOST'] = "[2001:1234:5678:9abc:def0::dead:beef]"
|
||||
assert_equal "[2001:1234:5678:9abc:def0::dead:beef]", @request.host
|
||||
end
|
||||
|
||||
def test_host_if_ipv6_reference_with_port
|
||||
@request_hash.delete "HTTP_X_FORWARDED_HOST"
|
||||
@request_hash['HTTP_HOST'] = "[2001:1234:5678:9abc:def0::dead:beef]:8008"
|
||||
assert_equal "[2001:1234:5678:9abc:def0::dead:beef]", @request.host
|
||||
end
|
||||
|
||||
def test_cookie_syntax_resilience
|
||||
cookies = CGI::Cookie::parse(@request_hash["HTTP_COOKIE"]);
|
||||
assert_equal ["c84ace84796670c052c6ceb2451fb0f2"], cookies["_session_id"], cookies.inspect
|
||||
assert_equal ["yes"], cookies["is_admin"], cookies.inspect
|
||||
|
||||
alt_cookies = CGI::Cookie::parse(@alt_cookie_fmt_request_hash["HTTP_COOKIE"]);
|
||||
assert_equal ["c84ace847,96670c052c6ceb2451fb0f2"], alt_cookies["_session_id"], alt_cookies.inspect
|
||||
assert_equal ["yes"], alt_cookies["is_admin"], alt_cookies.inspect
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class CgiRequestParamsParsingTest < BaseCgiTest
|
||||
def test_doesnt_break_when_content_type_has_charset
|
||||
data = 'flamenco=love'
|
||||
@request.env['CONTENT_LENGTH'] = data.length
|
||||
@request.env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded; charset=utf-8'
|
||||
@request.env['RAW_POST_DATA'] = data
|
||||
assert_equal({"flamenco"=> "love"}, @request.request_parameters)
|
||||
end
|
||||
|
||||
def test_doesnt_interpret_request_uri_as_query_string_when_missing
|
||||
@request.env['REQUEST_URI'] = 'foo'
|
||||
assert_equal({}, @request.query_parameters)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class CgiRequestNeedsRewoundTest < BaseCgiTest
|
||||
def test_body_should_be_rewound
|
||||
data = 'foo'
|
||||
fake_cgi = Struct.new(:env_table, :query_string, :stdinput).new(@request_hash, '', StringIO.new(data))
|
||||
fake_cgi.env_table['CONTENT_LENGTH'] = data.length
|
||||
fake_cgi.env_table['CONTENT_TYPE'] = 'application/x-www-form-urlencoded; charset=utf-8'
|
||||
|
||||
# Read the request body by parsing params.
|
||||
request = ActionController::CgiRequest.new(fake_cgi)
|
||||
request.request_parameters
|
||||
|
||||
# Should have rewound the body.
|
||||
assert_equal 0, request.body.pos
|
||||
end
|
||||
end
|
|
@ -1,140 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class CallerController < ActionController::Base
|
||||
def calling_from_controller
|
||||
render_component(:controller => "callee", :action => "being_called")
|
||||
end
|
||||
|
||||
def calling_from_controller_with_params
|
||||
render_component(:controller => "callee", :action => "being_called", :params => { "name" => "David" })
|
||||
end
|
||||
|
||||
def calling_from_controller_with_different_status_code
|
||||
render_component(:controller => "callee", :action => "blowing_up")
|
||||
end
|
||||
|
||||
def calling_from_template
|
||||
render :inline => "Ring, ring: <%= render_component(:controller => 'callee', :action => 'being_called') %>"
|
||||
end
|
||||
|
||||
def internal_caller
|
||||
render :inline => "Are you there? <%= render_component(:action => 'internal_callee') %>"
|
||||
end
|
||||
|
||||
def internal_callee
|
||||
render :text => "Yes, ma'am"
|
||||
end
|
||||
|
||||
def set_flash
|
||||
render_component(:controller => "callee", :action => "set_flash")
|
||||
end
|
||||
|
||||
def use_flash
|
||||
render_component(:controller => "callee", :action => "use_flash")
|
||||
end
|
||||
|
||||
def calling_redirected
|
||||
render_component(:controller => "callee", :action => "redirected")
|
||||
end
|
||||
|
||||
def calling_redirected_as_string
|
||||
render :inline => "<%= render_component(:controller => 'callee', :action => 'redirected') %>"
|
||||
end
|
||||
|
||||
def rescue_action(e) raise end
|
||||
end
|
||||
|
||||
class CalleeController < ActionController::Base
|
||||
def being_called
|
||||
render :text => "#{params[:name] || "Lady"} of the House, speaking"
|
||||
end
|
||||
|
||||
def blowing_up
|
||||
render :text => "It's game over, man, just game over, man!", :status => 500
|
||||
end
|
||||
|
||||
def set_flash
|
||||
flash[:notice] = 'My stoney baby'
|
||||
render :text => 'flash is set'
|
||||
end
|
||||
|
||||
def use_flash
|
||||
render :text => flash[:notice] || 'no flash'
|
||||
end
|
||||
|
||||
def redirected
|
||||
redirect_to :controller => "callee", :action => "being_called"
|
||||
end
|
||||
|
||||
def rescue_action(e) raise end
|
||||
end
|
||||
|
||||
class ComponentsTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = CallerController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
def test_calling_from_controller
|
||||
get :calling_from_controller
|
||||
assert_equal "Lady of the House, speaking", @response.body
|
||||
end
|
||||
|
||||
def test_calling_from_controller_with_params
|
||||
get :calling_from_controller_with_params
|
||||
assert_equal "David of the House, speaking", @response.body
|
||||
end
|
||||
|
||||
def test_calling_from_controller_with_different_status_code
|
||||
get :calling_from_controller_with_different_status_code
|
||||
assert_equal 500, @response.response_code
|
||||
end
|
||||
|
||||
def test_calling_from_template
|
||||
get :calling_from_template
|
||||
assert_equal "Ring, ring: Lady of the House, speaking", @response.body
|
||||
end
|
||||
|
||||
def test_etag_is_set_for_parent_template_when_calling_from_template
|
||||
get :calling_from_template
|
||||
expected_etag = etag_for("Ring, ring: Lady of the House, speaking")
|
||||
assert_equal expected_etag, @response.headers['ETag']
|
||||
end
|
||||
|
||||
def test_internal_calling
|
||||
get :internal_caller
|
||||
assert_equal "Are you there? Yes, ma'am", @response.body
|
||||
end
|
||||
|
||||
def test_flash
|
||||
get :set_flash
|
||||
assert_equal 'My stoney baby', flash[:notice]
|
||||
get :use_flash
|
||||
assert_equal 'My stoney baby', @response.body
|
||||
get :use_flash
|
||||
assert_equal 'no flash', @response.body
|
||||
end
|
||||
|
||||
def test_component_redirect_redirects
|
||||
get :calling_redirected
|
||||
|
||||
assert_redirected_to :action => "being_called"
|
||||
end
|
||||
|
||||
def test_component_multiple_redirect_redirects
|
||||
test_component_redirect_redirects
|
||||
test_internal_calling
|
||||
end
|
||||
|
||||
def test_component_as_string_redirect_renders_redirected_action
|
||||
get :calling_redirected_as_string
|
||||
|
||||
assert_equal "Lady of the House, speaking", @response.body
|
||||
end
|
||||
|
||||
protected
|
||||
def etag_for(text)
|
||||
%("#{Digest::MD5.hexdigest(text)}")
|
||||
end
|
||||
end
|
|
@ -1,139 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class ContentTypeController < ActionController::Base
|
||||
def render_content_type_from_body
|
||||
response.content_type = Mime::RSS
|
||||
render :text => "hello world!"
|
||||
end
|
||||
|
||||
def render_defaults
|
||||
render :text => "hello world!"
|
||||
end
|
||||
|
||||
def render_content_type_from_render
|
||||
render :text => "hello world!", :content_type => Mime::RSS
|
||||
end
|
||||
|
||||
def render_charset_from_body
|
||||
response.charset = "utf-16"
|
||||
render :text => "hello world!"
|
||||
end
|
||||
|
||||
def render_default_for_rhtml
|
||||
end
|
||||
|
||||
def render_default_for_rxml
|
||||
end
|
||||
|
||||
def render_default_for_rjs
|
||||
end
|
||||
|
||||
def render_change_for_rxml
|
||||
response.content_type = Mime::HTML
|
||||
render :action => "render_default_for_rxml"
|
||||
end
|
||||
|
||||
def render_default_content_types_for_respond_to
|
||||
respond_to do |format|
|
||||
format.html { render :text => "hello world!" }
|
||||
format.xml { render :action => "render_default_content_types_for_respond_to.rhtml" }
|
||||
format.js { render :text => "hello world!" }
|
||||
format.rss { render :text => "hello world!", :content_type => Mime::XML }
|
||||
end
|
||||
end
|
||||
|
||||
def rescue_action(e) raise end
|
||||
end
|
||||
|
||||
ContentTypeController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
|
||||
class ContentTypeTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = ContentTypeController.new
|
||||
|
||||
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
|
||||
# a more accurate simulation of what happens in "real life".
|
||||
@controller.logger = Logger.new(nil)
|
||||
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
def test_render_defaults
|
||||
get :render_defaults
|
||||
assert_equal "utf-8", @response.charset
|
||||
assert_equal Mime::HTML, @response.content_type
|
||||
end
|
||||
|
||||
def test_render_changed_charset_default
|
||||
ContentTypeController.default_charset = "utf-16"
|
||||
get :render_defaults
|
||||
assert_equal "utf-16", @response.charset
|
||||
assert_equal Mime::HTML, @response.content_type
|
||||
ContentTypeController.default_charset = "utf-8"
|
||||
end
|
||||
|
||||
def test_content_type_from_body
|
||||
get :render_content_type_from_body
|
||||
assert_equal "application/rss+xml", @response.content_type
|
||||
assert_equal "utf-8", @response.charset
|
||||
end
|
||||
|
||||
def test_content_type_from_render
|
||||
get :render_content_type_from_render
|
||||
assert_equal "application/rss+xml", @response.content_type
|
||||
assert_equal "utf-8", @response.charset
|
||||
end
|
||||
|
||||
def test_charset_from_body
|
||||
get :render_charset_from_body
|
||||
assert_equal "utf-16", @response.charset
|
||||
assert_equal Mime::HTML, @response.content_type
|
||||
end
|
||||
|
||||
def test_default_for_rhtml
|
||||
get :render_default_for_rhtml
|
||||
assert_equal Mime::HTML, @response.content_type
|
||||
assert_equal "utf-8", @response.charset
|
||||
end
|
||||
|
||||
def test_default_for_rxml
|
||||
get :render_default_for_rxml
|
||||
assert_equal Mime::XML, @response.content_type
|
||||
assert_equal "utf-8", @response.charset
|
||||
end
|
||||
|
||||
def test_default_for_rjs
|
||||
xhr :post, :render_default_for_rjs
|
||||
assert_equal Mime::JS, @response.content_type
|
||||
assert_equal "utf-8", @response.charset
|
||||
end
|
||||
|
||||
def test_change_for_rxml
|
||||
get :render_change_for_rxml
|
||||
assert_equal Mime::HTML, @response.content_type
|
||||
assert_equal "utf-8", @response.charset
|
||||
end
|
||||
|
||||
def test_render_default_content_types_for_respond_to
|
||||
@request.env["HTTP_ACCEPT"] = Mime::HTML.to_s
|
||||
get :render_default_content_types_for_respond_to
|
||||
assert_equal Mime::HTML, @response.content_type
|
||||
|
||||
@request.env["HTTP_ACCEPT"] = Mime::JS.to_s
|
||||
get :render_default_content_types_for_respond_to
|
||||
assert_equal Mime::JS, @response.content_type
|
||||
end
|
||||
|
||||
def test_render_default_content_types_for_respond_to_with_template
|
||||
@request.env["HTTP_ACCEPT"] = Mime::XML.to_s
|
||||
get :render_default_content_types_for_respond_to
|
||||
assert_equal Mime::XML, @response.content_type
|
||||
end
|
||||
|
||||
def test_render_default_content_types_for_respond_to_with_overwrite
|
||||
@request.env["HTTP_ACCEPT"] = Mime::RSS.to_s
|
||||
get :render_default_content_types_for_respond_to
|
||||
assert_equal Mime::XML, @response.content_type
|
||||
end
|
||||
end
|
|
@ -1,146 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class CookieTest < Test::Unit::TestCase
|
||||
class TestController < ActionController::Base
|
||||
def authenticate
|
||||
cookies["user_name"] = "david"
|
||||
end
|
||||
|
||||
def authenticate_for_fourteen_days
|
||||
cookies["user_name"] = { "value" => "david", "expires" => Time.local(2005, 10, 10) }
|
||||
end
|
||||
|
||||
def authenticate_for_fourteen_days_with_symbols
|
||||
cookies[:user_name] = { :value => "david", :expires => Time.local(2005, 10, 10) }
|
||||
end
|
||||
|
||||
def set_multiple_cookies
|
||||
cookies["user_name"] = { "value" => "david", "expires" => Time.local(2005, 10, 10) }
|
||||
cookies["login"] = "XJ-122"
|
||||
end
|
||||
|
||||
def access_frozen_cookies
|
||||
cookies["will"] = "work"
|
||||
end
|
||||
|
||||
def logout
|
||||
cookies.delete("user_name")
|
||||
end
|
||||
|
||||
def delete_cookie_with_path
|
||||
cookies.delete("user_name", :path => '/beaten')
|
||||
render :text => "hello world"
|
||||
end
|
||||
|
||||
def authenticate_with_http_only
|
||||
cookies["user_name"] = { :value => "david", :http_only => true }
|
||||
end
|
||||
|
||||
def rescue_action(e)
|
||||
raise unless ActionView::MissingTemplate # No templates here, and we don't care about the output
|
||||
end
|
||||
end
|
||||
|
||||
def setup
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
|
||||
@controller = TestController.new
|
||||
@request.host = "www.nextangle.com"
|
||||
end
|
||||
|
||||
def test_setting_cookie
|
||||
get :authenticate
|
||||
assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david") ], @response.headers["cookie"]
|
||||
end
|
||||
|
||||
def test_setting_cookie_for_fourteen_days
|
||||
get :authenticate_for_fourteen_days
|
||||
assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david", "expires" => Time.local(2005, 10, 10)) ], @response.headers["cookie"]
|
||||
end
|
||||
|
||||
def test_setting_cookie_for_fourteen_days_with_symbols
|
||||
get :authenticate_for_fourteen_days
|
||||
assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david", "expires" => Time.local(2005, 10, 10)) ], @response.headers["cookie"]
|
||||
end
|
||||
|
||||
def test_setting_cookie_with_http_only
|
||||
get :authenticate_with_http_only
|
||||
assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david", "http_only" => true) ], @response.headers["cookie"]
|
||||
assert_equal CGI::Cookie::new("name" => "user_name", "value" => "david", "path" => "/", "http_only" => true).to_s, @response.headers["cookie"][0].to_s
|
||||
end
|
||||
|
||||
def test_multiple_cookies
|
||||
get :set_multiple_cookies
|
||||
assert_equal 2, @response.cookies.size
|
||||
end
|
||||
|
||||
def test_setting_test_cookie
|
||||
assert_nothing_raised { get :access_frozen_cookies }
|
||||
end
|
||||
|
||||
def test_expiring_cookie
|
||||
get :logout
|
||||
assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "", "expires" => Time.at(0)) ], @response.headers["cookie"]
|
||||
assert_equal CGI::Cookie::new("name" => "user_name", "value" => "", "expires" => Time.at(0)).value, []
|
||||
end
|
||||
|
||||
def test_cookiejar_accessor
|
||||
@request.cookies["user_name"] = CGI::Cookie.new("name" => "user_name", "value" => "david", "expires" => Time.local(2025, 10, 10))
|
||||
@controller.request = @request
|
||||
jar = ActionController::CookieJar.new(@controller)
|
||||
assert_equal "david", jar["user_name"]
|
||||
assert_equal nil, jar["something_else"]
|
||||
end
|
||||
|
||||
def test_cookiejar_accessor_with_array_value
|
||||
a = %w{1 2 3}
|
||||
@request.cookies["pages"] = CGI::Cookie.new("name" => "pages", "value" => a, "expires" => Time.local(2025, 10, 10))
|
||||
@controller.request = @request
|
||||
jar = ActionController::CookieJar.new(@controller)
|
||||
assert_equal a, jar["pages"]
|
||||
end
|
||||
|
||||
def test_delete_cookie_with_path
|
||||
get :delete_cookie_with_path
|
||||
assert_equal "/beaten", @response.headers["cookie"].first.path
|
||||
assert_not_equal "/", @response.headers["cookie"].first.path
|
||||
end
|
||||
|
||||
def test_cookie_to_s_simple_values
|
||||
assert_equal 'myname=myvalue; path=', CGI::Cookie.new('myname', 'myvalue').to_s
|
||||
end
|
||||
|
||||
def test_cookie_to_s_hash
|
||||
cookie_str = CGI::Cookie.new(
|
||||
'name' => 'myname',
|
||||
'value' => 'myvalue',
|
||||
'domain' => 'mydomain',
|
||||
'path' => 'mypath',
|
||||
'expires' => Time.utc(2007, 10, 20),
|
||||
'secure' => true,
|
||||
'http_only' => true).to_s
|
||||
assert_equal 'myname=myvalue; domain=mydomain; path=mypath; expires=Sat, 20 Oct 2007 00:00:00 GMT; secure; HttpOnly', cookie_str
|
||||
end
|
||||
|
||||
def test_cookie_to_s_hash_default_not_secure_not_http_only
|
||||
cookie_str = CGI::Cookie.new(
|
||||
'name' => 'myname',
|
||||
'value' => 'myvalue',
|
||||
'domain' => 'mydomain',
|
||||
'path' => 'mypath',
|
||||
'expires' => Time.utc(2007, 10, 20))
|
||||
assert cookie_str !~ /secure/
|
||||
assert cookie_str !~ /HttpOnly/
|
||||
end
|
||||
|
||||
def test_cookies_should_not_be_split_on_ampersand_values
|
||||
cookies = CGI::Cookie.parse('return_to=http://rubyonrails.org/search?term=api&scope=all&global=true')
|
||||
assert_equal({"return_to" => ["http://rubyonrails.org/search?term=api&scope=all&global=true"]}, cookies)
|
||||
end
|
||||
|
||||
def test_cookies_should_not_be_split_on_values_with_newlines
|
||||
cookies = CGI::Cookie.new("name" => "val", "value" => "this\nis\na\ntest")
|
||||
assert cookies.size == 1
|
||||
end
|
||||
end
|
|
@ -1,45 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class CustomHandler < ActionView::TemplateHandler
|
||||
def initialize( view )
|
||||
@view = view
|
||||
end
|
||||
|
||||
def render( template )
|
||||
[ template.source,
|
||||
template.locals,
|
||||
@view ]
|
||||
end
|
||||
end
|
||||
|
||||
class CustomHandlerTest < Test::Unit::TestCase
|
||||
def setup
|
||||
ActionView::Template.register_template_handler "foo", CustomHandler
|
||||
ActionView::Template.register_template_handler :foo2, CustomHandler
|
||||
@view = ActionView::Base.new
|
||||
end
|
||||
|
||||
def test_custom_render
|
||||
template = ActionView::InlineTemplate.new(@view, "hello <%= one %>", { :one => "two" }, "foo")
|
||||
|
||||
result = @view.render_template(template)
|
||||
assert_equal(
|
||||
[ "hello <%= one %>", { :one => "two" }, @view ],
|
||||
result )
|
||||
end
|
||||
|
||||
def test_custom_render2
|
||||
template = ActionView::InlineTemplate.new(@view, "hello <%= one %>", { :one => "two" }, "foo2")
|
||||
result = @view.render_template(template)
|
||||
assert_equal(
|
||||
[ "hello <%= one %>", { :one => "two" }, @view ],
|
||||
result )
|
||||
end
|
||||
|
||||
def test_unhandled_extension
|
||||
# uses the ERb handler by default if the extension isn't recognized
|
||||
template = ActionView::InlineTemplate.new(@view, "hello <%= one %>", { :one => "two" }, "bar")
|
||||
result = @view.render_template(template)
|
||||
assert_equal "hello two", result
|
||||
end
|
||||
end
|
|
@ -1,37 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class DeprecatedBaseMethodsTest < Test::Unit::TestCase
|
||||
class Target < ActionController::Base
|
||||
|
||||
def home_url(greeting)
|
||||
"http://example.com/#{greeting}"
|
||||
end
|
||||
|
||||
def raises_name_error
|
||||
this_method_doesnt_exist
|
||||
end
|
||||
|
||||
def rescue_action(e) raise e end
|
||||
end
|
||||
|
||||
Target.view_paths = [ File.dirname(__FILE__) + "/../../fixtures" ]
|
||||
|
||||
def setup
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
@controller = Target.new
|
||||
end
|
||||
|
||||
def test_log_error_silences_deprecation_warnings
|
||||
get :raises_name_error
|
||||
rescue => e
|
||||
assert_not_deprecated { @controller.send :log_error, e }
|
||||
end
|
||||
|
||||
def test_assertion_failed_error_silences_deprecation_warnings
|
||||
get :raises_name_error
|
||||
rescue => e
|
||||
error = Test::Unit::Error.new('testing ur doodz', e)
|
||||
assert_not_deprecated { error.message }
|
||||
end
|
||||
end
|
|
@ -1,105 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
uses_mocha 'dispatcher tests' do
|
||||
|
||||
require 'action_controller/dispatcher'
|
||||
|
||||
class DispatcherTest < Test::Unit::TestCase
|
||||
Dispatcher = ActionController::Dispatcher
|
||||
|
||||
def setup
|
||||
@output = StringIO.new
|
||||
ENV['REQUEST_METHOD'] = 'GET'
|
||||
|
||||
# Clear callbacks as they are redefined by Dispatcher#define_dispatcher_callbacks
|
||||
Dispatcher.instance_variable_set("@prepare_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
|
||||
Dispatcher.instance_variable_set("@before_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
|
||||
Dispatcher.instance_variable_set("@after_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
|
||||
|
||||
Dispatcher.stubs(:require_dependency)
|
||||
|
||||
@dispatcher = Dispatcher.new(@output)
|
||||
end
|
||||
|
||||
def teardown
|
||||
ENV.delete 'REQUEST_METHOD'
|
||||
end
|
||||
|
||||
def test_clears_dependencies_after_dispatch_if_in_loading_mode
|
||||
ActionController::Routing::Routes.expects(:reload).once
|
||||
ActiveSupport::Dependencies.expects(:clear).once
|
||||
|
||||
dispatch(@output, false)
|
||||
end
|
||||
|
||||
def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode
|
||||
ActionController::Routing::Routes.expects(:reload).never
|
||||
ActiveSupport::Dependencies.expects(:clear).never
|
||||
|
||||
dispatch
|
||||
end
|
||||
|
||||
# Stub out dispatch error logger
|
||||
class << Dispatcher
|
||||
def log_failsafe_exception(status, exception); end
|
||||
end
|
||||
|
||||
def test_failsafe_response
|
||||
CGI.expects(:new).raises('some multipart parsing failure')
|
||||
Dispatcher.expects(:log_failsafe_exception)
|
||||
|
||||
assert_nothing_raised { dispatch }
|
||||
|
||||
assert_equal "Status: 400 Bad Request\r\nContent-Type: text/html\r\n\r\n<html><body><h1>400 Bad Request</h1></body></html>", @output.string
|
||||
end
|
||||
|
||||
def test_prepare_callbacks
|
||||
a = b = c = nil
|
||||
Dispatcher.to_prepare { |*args| a = b = c = 1 }
|
||||
Dispatcher.to_prepare { |*args| b = c = 2 }
|
||||
Dispatcher.to_prepare { |*args| c = 3 }
|
||||
|
||||
# Ensure to_prepare callbacks are not run when defined
|
||||
assert_nil a || b || c
|
||||
|
||||
# Run callbacks
|
||||
@dispatcher.send :run_callbacks, :prepare_dispatch
|
||||
|
||||
assert_equal 1, a
|
||||
assert_equal 2, b
|
||||
assert_equal 3, c
|
||||
|
||||
# Make sure they are only run once
|
||||
a = b = c = nil
|
||||
@dispatcher.send :dispatch
|
||||
assert_nil a || b || c
|
||||
end
|
||||
|
||||
def test_to_prepare_with_identifier_replaces
|
||||
a = b = nil
|
||||
Dispatcher.to_prepare(:unique_id) { |*args| a = b = 1 }
|
||||
Dispatcher.to_prepare(:unique_id) { |*args| a = 2 }
|
||||
|
||||
@dispatcher.send :run_callbacks, :prepare_dispatch
|
||||
assert_equal 2, a
|
||||
assert_equal nil, b
|
||||
end
|
||||
|
||||
private
|
||||
def dispatch(output = @output, cache_classes = true)
|
||||
controller = mock
|
||||
controller.stubs(:process).returns(controller)
|
||||
controller.stubs(:out).with(output).returns('response')
|
||||
|
||||
ActionController::Routing::Routes.stubs(:recognize).returns(controller)
|
||||
|
||||
Dispatcher.define_dispatcher_callbacks(cache_classes)
|
||||
Dispatcher.dispatch(nil, {}, output)
|
||||
end
|
||||
|
||||
def assert_subclasses(howmany, klass, message = klass.subclasses.inspect)
|
||||
assert_equal howmany, klass.subclasses.size, message
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -1,33 +0,0 @@
|
|||
class << Object; alias_method :const_available?, :const_defined?; end
|
||||
|
||||
class ContentController < Class.new(ActionController::Base)
|
||||
end
|
||||
class NotAController
|
||||
end
|
||||
module Admin
|
||||
class << self; alias_method :const_available?, :const_defined?; end
|
||||
class UserController < Class.new(ActionController::Base); end
|
||||
class NewsFeedController < Class.new(ActionController::Base); end
|
||||
end
|
||||
|
||||
# For speed test
|
||||
class SpeedController < ActionController::Base; end
|
||||
class SearchController < SpeedController; end
|
||||
class VideosController < SpeedController; end
|
||||
class VideoFileController < SpeedController; end
|
||||
class VideoSharesController < SpeedController; end
|
||||
class VideoAbusesController < SpeedController; end
|
||||
class VideoUploadsController < SpeedController; end
|
||||
class VideoVisitsController < SpeedController; end
|
||||
class UsersController < SpeedController; end
|
||||
class SettingsController < SpeedController; end
|
||||
class ChannelsController < SpeedController; end
|
||||
class ChannelVideosController < SpeedController; end
|
||||
class SessionsController < SpeedController; end
|
||||
class LostPasswordsController < SpeedController; end
|
||||
class PagesController < SpeedController; end
|
||||
|
||||
ActionController::Routing::Routes.draw do |map|
|
||||
map.route_one 'route_one', :controller => 'elsewhere', :action => 'flash_me'
|
||||
map.connect ':controller/:action/:id'
|
||||
end
|
|
@ -1,11 +0,0 @@
|
|||
class Customer < Struct.new(:name, :id)
|
||||
def to_param
|
||||
id.to_s
|
||||
end
|
||||
end
|
||||
|
||||
class BadCustomer < Customer
|
||||
end
|
||||
|
||||
class GoodCustomer < Customer
|
||||
end
|
|
@ -1,49 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class FilterParamController < ActionController::Base
|
||||
end
|
||||
|
||||
class FilterParamTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = FilterParamController.new
|
||||
end
|
||||
|
||||
def test_filter_parameters
|
||||
assert FilterParamController.respond_to?(:filter_parameter_logging)
|
||||
assert !@controller.respond_to?(:filter_parameters)
|
||||
|
||||
FilterParamController.filter_parameter_logging
|
||||
assert @controller.respond_to?(:filter_parameters)
|
||||
|
||||
test_hashes = [[{},{},[]],
|
||||
[{'foo'=>nil},{'foo'=>nil},[]],
|
||||
[{'foo'=>'bar'},{'foo'=>'bar'},[]],
|
||||
[{'foo'=>'bar'},{'foo'=>'bar'},%w'food'],
|
||||
[{'foo'=>'bar'},{'foo'=>'[FILTERED]'},%w'foo'],
|
||||
[{'foo'=>'bar', 'bar'=>'foo'},{'foo'=>'[FILTERED]', 'bar'=>'foo'},%w'foo baz'],
|
||||
[{'foo'=>'bar', 'baz'=>'foo'},{'foo'=>'[FILTERED]', 'baz'=>'[FILTERED]'},%w'foo baz'],
|
||||
[{'bar'=>{'foo'=>'bar','bar'=>'foo'}},{'bar'=>{'foo'=>'[FILTERED]','bar'=>'foo'}},%w'fo'],
|
||||
[{'foo'=>{'foo'=>'bar','bar'=>'foo'}},{'foo'=>'[FILTERED]'},%w'f banana']]
|
||||
|
||||
test_hashes.each do |before_filter, after_filter, filter_words|
|
||||
FilterParamController.filter_parameter_logging(*filter_words)
|
||||
assert_equal after_filter, @controller.send!(:filter_parameters, before_filter)
|
||||
|
||||
filter_words.push('blah')
|
||||
FilterParamController.filter_parameter_logging(*filter_words) do |key, value|
|
||||
value.reverse! if key =~ /bargain/
|
||||
end
|
||||
|
||||
before_filter['barg'] = {'bargain'=>'gain', 'blah'=>'bar', 'bar'=>{'bargain'=>{'blah'=>'foo'}}}
|
||||
after_filter['barg'] = {'bargain'=>'niag', 'blah'=>'[FILTERED]', 'bar'=>{'bargain'=>{'blah'=>'[FILTERED]'}}}
|
||||
|
||||
assert_equal after_filter, @controller.send!(:filter_parameters, before_filter)
|
||||
end
|
||||
end
|
||||
|
||||
def test_filter_parameters_is_protected
|
||||
FilterParamController.filter_parameter_logging(:foo)
|
||||
assert !FilterParamController.action_methods.include?('filter_parameters')
|
||||
assert_raise(NoMethodError) { @controller.filter_parameters([{'password' => '[FILTERED]'}]) }
|
||||
end
|
||||
end
|
|
@ -1,881 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
# FIXME: crashes Ruby 1.9
|
||||
class FilterTest < Test::Unit::TestCase
|
||||
class TestController < ActionController::Base
|
||||
before_filter :ensure_login
|
||||
after_filter :clean_up
|
||||
|
||||
def show
|
||||
render :inline => "ran action"
|
||||
end
|
||||
|
||||
private
|
||||
def ensure_login
|
||||
@ran_filter ||= []
|
||||
@ran_filter << "ensure_login"
|
||||
end
|
||||
|
||||
def clean_up
|
||||
@ran_after_filter ||= []
|
||||
@ran_after_filter << "clean_up"
|
||||
end
|
||||
end
|
||||
|
||||
class ChangingTheRequirementsController < TestController
|
||||
before_filter :ensure_login, :except => [:go_wild]
|
||||
|
||||
def go_wild
|
||||
render :text => "gobble"
|
||||
end
|
||||
end
|
||||
|
||||
class TestMultipleFiltersController < ActionController::Base
|
||||
before_filter :try_1
|
||||
before_filter :try_2
|
||||
before_filter :try_3
|
||||
|
||||
(1..3).each do |i|
|
||||
define_method "fail_#{i}" do
|
||||
render :text => i.to_s
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
(1..3).each do |i|
|
||||
define_method "try_#{i}" do
|
||||
instance_variable_set :@try, i
|
||||
if action_name == "fail_#{i}"
|
||||
head(404)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class RenderingController < ActionController::Base
|
||||
before_filter :render_something_else
|
||||
|
||||
def show
|
||||
@ran_action = true
|
||||
render :inline => "ran action"
|
||||
end
|
||||
|
||||
private
|
||||
def render_something_else
|
||||
render :inline => "something else"
|
||||
end
|
||||
end
|
||||
|
||||
class ConditionalFilterController < ActionController::Base
|
||||
def show
|
||||
render :inline => "ran action"
|
||||
end
|
||||
|
||||
def another_action
|
||||
render :inline => "ran action"
|
||||
end
|
||||
|
||||
def show_without_filter
|
||||
render :inline => "ran action without filter"
|
||||
end
|
||||
|
||||
private
|
||||
def ensure_login
|
||||
@ran_filter ||= []
|
||||
@ran_filter << "ensure_login"
|
||||
end
|
||||
|
||||
def clean_up_tmp
|
||||
@ran_filter ||= []
|
||||
@ran_filter << "clean_up_tmp"
|
||||
end
|
||||
|
||||
def rescue_action(e) raise(e) end
|
||||
end
|
||||
|
||||
class ConditionalCollectionFilterController < ConditionalFilterController
|
||||
before_filter :ensure_login, :except => [ :show_without_filter, :another_action ]
|
||||
end
|
||||
|
||||
class OnlyConditionSymController < ConditionalFilterController
|
||||
before_filter :ensure_login, :only => :show
|
||||
end
|
||||
|
||||
class ExceptConditionSymController < ConditionalFilterController
|
||||
before_filter :ensure_login, :except => :show_without_filter
|
||||
end
|
||||
|
||||
class BeforeAndAfterConditionController < ConditionalFilterController
|
||||
before_filter :ensure_login, :only => :show
|
||||
after_filter :clean_up_tmp, :only => :show
|
||||
end
|
||||
|
||||
class OnlyConditionProcController < ConditionalFilterController
|
||||
before_filter(:only => :show) {|c| c.assigns["ran_proc_filter"] = true }
|
||||
end
|
||||
|
||||
class ExceptConditionProcController < ConditionalFilterController
|
||||
before_filter(:except => :show_without_filter) {|c| c.assigns["ran_proc_filter"] = true }
|
||||
end
|
||||
|
||||
class ConditionalClassFilter
|
||||
def self.filter(controller) controller.assigns["ran_class_filter"] = true end
|
||||
end
|
||||
|
||||
class OnlyConditionClassController < ConditionalFilterController
|
||||
before_filter ConditionalClassFilter, :only => :show
|
||||
end
|
||||
|
||||
class ExceptConditionClassController < ConditionalFilterController
|
||||
before_filter ConditionalClassFilter, :except => :show_without_filter
|
||||
end
|
||||
|
||||
class AnomolousYetValidConditionController < ConditionalFilterController
|
||||
before_filter(ConditionalClassFilter, :ensure_login, Proc.new {|c| c.assigns["ran_proc_filter1"] = true }, :except => :show_without_filter) { |c| c.assigns["ran_proc_filter2"] = true}
|
||||
end
|
||||
|
||||
class ConditionalOptionsFilter < ConditionalFilterController
|
||||
before_filter :ensure_login, :if => Proc.new { |c| true }
|
||||
before_filter :clean_up_tmp, :if => Proc.new { |c| false }
|
||||
end
|
||||
|
||||
class EmptyFilterChainController < TestController
|
||||
self.filter_chain.clear
|
||||
def show
|
||||
@action_executed = true
|
||||
render :text => "yawp!"
|
||||
end
|
||||
end
|
||||
|
||||
class PrependingController < TestController
|
||||
prepend_before_filter :wonderful_life
|
||||
# skip_before_filter :fire_flash
|
||||
|
||||
private
|
||||
def wonderful_life
|
||||
@ran_filter ||= []
|
||||
@ran_filter << "wonderful_life"
|
||||
end
|
||||
end
|
||||
|
||||
class SkippingAndLimitedController < TestController
|
||||
skip_before_filter :ensure_login
|
||||
before_filter :ensure_login, :only => :index
|
||||
|
||||
def index
|
||||
render :text => 'ok'
|
||||
end
|
||||
|
||||
def public
|
||||
end
|
||||
end
|
||||
|
||||
class SkippingAndReorderingController < TestController
|
||||
skip_before_filter :ensure_login
|
||||
before_filter :find_record
|
||||
before_filter :ensure_login
|
||||
|
||||
private
|
||||
def find_record
|
||||
@ran_filter ||= []
|
||||
@ran_filter << "find_record"
|
||||
end
|
||||
end
|
||||
|
||||
class ConditionalSkippingController < TestController
|
||||
skip_before_filter :ensure_login, :only => [ :login ]
|
||||
skip_after_filter :clean_up, :only => [ :login ]
|
||||
|
||||
before_filter :find_user, :only => [ :change_password ]
|
||||
|
||||
def login
|
||||
render :inline => "ran action"
|
||||
end
|
||||
|
||||
def change_password
|
||||
render :inline => "ran action"
|
||||
end
|
||||
|
||||
protected
|
||||
def find_user
|
||||
@ran_filter ||= []
|
||||
@ran_filter << "find_user"
|
||||
end
|
||||
end
|
||||
|
||||
class ConditionalParentOfConditionalSkippingController < ConditionalFilterController
|
||||
before_filter :conditional_in_parent, :only => [:show, :another_action]
|
||||
after_filter :conditional_in_parent, :only => [:show, :another_action]
|
||||
|
||||
private
|
||||
|
||||
def conditional_in_parent
|
||||
@ran_filter ||= []
|
||||
@ran_filter << 'conditional_in_parent'
|
||||
end
|
||||
end
|
||||
|
||||
class ChildOfConditionalParentController < ConditionalParentOfConditionalSkippingController
|
||||
skip_before_filter :conditional_in_parent, :only => :another_action
|
||||
skip_after_filter :conditional_in_parent, :only => :another_action
|
||||
end
|
||||
|
||||
class AnotherChildOfConditionalParentController < ConditionalParentOfConditionalSkippingController
|
||||
skip_before_filter :conditional_in_parent, :only => :show
|
||||
end
|
||||
|
||||
class ProcController < PrependingController
|
||||
before_filter(proc { |c| c.assigns["ran_proc_filter"] = true })
|
||||
end
|
||||
|
||||
class ImplicitProcController < PrependingController
|
||||
before_filter { |c| c.assigns["ran_proc_filter"] = true }
|
||||
end
|
||||
|
||||
class AuditFilter
|
||||
def self.filter(controller)
|
||||
controller.assigns["was_audited"] = true
|
||||
end
|
||||
end
|
||||
|
||||
class AroundFilter
|
||||
def before(controller)
|
||||
@execution_log = "before"
|
||||
controller.class.execution_log << " before aroundfilter " if controller.respond_to? :execution_log
|
||||
controller.assigns["before_ran"] = true
|
||||
end
|
||||
|
||||
def after(controller)
|
||||
controller.assigns["execution_log"] = @execution_log + " and after"
|
||||
controller.assigns["after_ran"] = true
|
||||
controller.class.execution_log << " after aroundfilter " if controller.respond_to? :execution_log
|
||||
end
|
||||
end
|
||||
|
||||
class AppendedAroundFilter
|
||||
def before(controller)
|
||||
controller.class.execution_log << " before appended aroundfilter "
|
||||
end
|
||||
|
||||
def after(controller)
|
||||
controller.class.execution_log << " after appended aroundfilter "
|
||||
end
|
||||
end
|
||||
|
||||
class AuditController < ActionController::Base
|
||||
before_filter(AuditFilter)
|
||||
|
||||
def show
|
||||
render :text => "hello"
|
||||
end
|
||||
end
|
||||
|
||||
class AroundFilterController < PrependingController
|
||||
around_filter AroundFilter.new
|
||||
end
|
||||
|
||||
class BeforeAfterClassFilterController < PrependingController
|
||||
begin
|
||||
filter = AroundFilter.new
|
||||
before_filter filter
|
||||
after_filter filter
|
||||
end
|
||||
end
|
||||
|
||||
class MixedFilterController < PrependingController
|
||||
cattr_accessor :execution_log
|
||||
|
||||
def initialize
|
||||
@@execution_log = ""
|
||||
end
|
||||
|
||||
before_filter { |c| c.class.execution_log << " before procfilter " }
|
||||
prepend_around_filter AroundFilter.new
|
||||
|
||||
after_filter { |c| c.class.execution_log << " after procfilter " }
|
||||
append_around_filter AppendedAroundFilter.new
|
||||
end
|
||||
|
||||
class MixedSpecializationController < ActionController::Base
|
||||
class OutOfOrder < StandardError; end
|
||||
|
||||
before_filter :first
|
||||
before_filter :second, :only => :foo
|
||||
|
||||
def foo
|
||||
render :text => 'foo'
|
||||
end
|
||||
|
||||
def bar
|
||||
render :text => 'bar'
|
||||
end
|
||||
|
||||
protected
|
||||
def first
|
||||
@first = true
|
||||
end
|
||||
|
||||
def second
|
||||
raise OutOfOrder unless @first
|
||||
end
|
||||
end
|
||||
|
||||
class DynamicDispatchController < ActionController::Base
|
||||
before_filter :choose
|
||||
|
||||
%w(foo bar baz).each do |action|
|
||||
define_method(action) { render :text => action }
|
||||
end
|
||||
|
||||
private
|
||||
def choose
|
||||
self.action_name = params[:choose]
|
||||
end
|
||||
end
|
||||
|
||||
class PrependingBeforeAndAfterController < ActionController::Base
|
||||
prepend_before_filter :before_all
|
||||
prepend_after_filter :after_all
|
||||
before_filter :between_before_all_and_after_all
|
||||
|
||||
def before_all
|
||||
@ran_filter ||= []
|
||||
@ran_filter << 'before_all'
|
||||
end
|
||||
|
||||
def after_all
|
||||
@ran_filter ||= []
|
||||
@ran_filter << 'after_all'
|
||||
end
|
||||
|
||||
def between_before_all_and_after_all
|
||||
@ran_filter ||= []
|
||||
@ran_filter << 'between_before_all_and_after_all'
|
||||
end
|
||||
def show
|
||||
render :text => 'hello'
|
||||
end
|
||||
end
|
||||
|
||||
class ErrorToRescue < Exception; end
|
||||
|
||||
class RescuingAroundFilterWithBlock
|
||||
def filter(controller)
|
||||
begin
|
||||
yield
|
||||
rescue ErrorToRescue => ex
|
||||
controller.send! :render, :text => "I rescued this: #{ex.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class RescuedController < ActionController::Base
|
||||
around_filter RescuingAroundFilterWithBlock.new
|
||||
|
||||
def show
|
||||
raise ErrorToRescue.new("Something made the bad noise.")
|
||||
end
|
||||
|
||||
private
|
||||
def rescue_action(exception)
|
||||
raise exception
|
||||
end
|
||||
end
|
||||
|
||||
class NonYieldingAroundFilterController < ActionController::Base
|
||||
|
||||
before_filter :filter_one
|
||||
around_filter :non_yielding_filter
|
||||
before_filter :filter_two
|
||||
after_filter :filter_three
|
||||
|
||||
def index
|
||||
render :inline => "index"
|
||||
end
|
||||
|
||||
#make sure the controller complains
|
||||
def rescue_action(e); raise e; end
|
||||
|
||||
private
|
||||
|
||||
def filter_one
|
||||
@filters ||= []
|
||||
@filters << "filter_one"
|
||||
end
|
||||
|
||||
def filter_two
|
||||
@filters << "filter_two"
|
||||
end
|
||||
|
||||
def non_yielding_filter
|
||||
@filters << "zomg it didn't yield"
|
||||
@filter_return_value
|
||||
end
|
||||
|
||||
def filter_three
|
||||
@filters << "filter_three"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def test_non_yielding_around_filters_not_returning_false_do_not_raise
|
||||
controller = NonYieldingAroundFilterController.new
|
||||
controller.instance_variable_set "@filter_return_value", true
|
||||
assert_nothing_raised do
|
||||
test_process(controller, "index")
|
||||
end
|
||||
end
|
||||
|
||||
def test_non_yielding_around_filters_returning_false_do_not_raise
|
||||
controller = NonYieldingAroundFilterController.new
|
||||
controller.instance_variable_set "@filter_return_value", false
|
||||
assert_nothing_raised do
|
||||
test_process(controller, "index")
|
||||
end
|
||||
end
|
||||
|
||||
def test_after_filters_are_not_run_if_around_filter_returns_false
|
||||
controller = NonYieldingAroundFilterController.new
|
||||
controller.instance_variable_set "@filter_return_value", false
|
||||
test_process(controller, "index")
|
||||
assert_equal ["filter_one", "zomg it didn't yield"], controller.assigns['filters']
|
||||
end
|
||||
|
||||
def test_after_filters_are_not_run_if_around_filter_does_not_yield
|
||||
controller = NonYieldingAroundFilterController.new
|
||||
controller.instance_variable_set "@filter_return_value", true
|
||||
test_process(controller, "index")
|
||||
assert_equal ["filter_one", "zomg it didn't yield"], controller.assigns['filters']
|
||||
end
|
||||
|
||||
def test_empty_filter_chain
|
||||
assert_equal 0, EmptyFilterChainController.filter_chain.size
|
||||
assert test_process(EmptyFilterChainController).template.assigns['action_executed']
|
||||
end
|
||||
|
||||
def test_added_filter_to_inheritance_graph
|
||||
assert_equal [ :ensure_login ], TestController.before_filters
|
||||
end
|
||||
|
||||
def test_base_class_in_isolation
|
||||
assert_equal [ ], ActionController::Base.before_filters
|
||||
end
|
||||
|
||||
def test_prepending_filter
|
||||
assert_equal [ :wonderful_life, :ensure_login ], PrependingController.before_filters
|
||||
end
|
||||
|
||||
def test_running_filters
|
||||
assert_equal %w( wonderful_life ensure_login ), test_process(PrependingController).template.assigns["ran_filter"]
|
||||
end
|
||||
|
||||
def test_running_filters_with_proc
|
||||
assert test_process(ProcController).template.assigns["ran_proc_filter"]
|
||||
end
|
||||
|
||||
def test_running_filters_with_implicit_proc
|
||||
assert test_process(ImplicitProcController).template.assigns["ran_proc_filter"]
|
||||
end
|
||||
|
||||
def test_running_filters_with_class
|
||||
assert test_process(AuditController).template.assigns["was_audited"]
|
||||
end
|
||||
|
||||
def test_running_anomolous_yet_valid_condition_filters
|
||||
response = test_process(AnomolousYetValidConditionController)
|
||||
assert_equal %w( ensure_login ), response.template.assigns["ran_filter"]
|
||||
assert response.template.assigns["ran_class_filter"]
|
||||
assert response.template.assigns["ran_proc_filter1"]
|
||||
assert response.template.assigns["ran_proc_filter2"]
|
||||
|
||||
response = test_process(AnomolousYetValidConditionController, "show_without_filter")
|
||||
assert_equal nil, response.template.assigns["ran_filter"]
|
||||
assert !response.template.assigns["ran_class_filter"]
|
||||
assert !response.template.assigns["ran_proc_filter1"]
|
||||
assert !response.template.assigns["ran_proc_filter2"]
|
||||
end
|
||||
|
||||
def test_running_conditional_options
|
||||
response = test_process(ConditionalOptionsFilter)
|
||||
assert_equal %w( ensure_login ), response.template.assigns["ran_filter"]
|
||||
end
|
||||
|
||||
def test_running_collection_condition_filters
|
||||
assert_equal %w( ensure_login ), test_process(ConditionalCollectionFilterController).template.assigns["ran_filter"]
|
||||
assert_equal nil, test_process(ConditionalCollectionFilterController, "show_without_filter").template.assigns["ran_filter"]
|
||||
assert_equal nil, test_process(ConditionalCollectionFilterController, "another_action").template.assigns["ran_filter"]
|
||||
end
|
||||
|
||||
def test_running_only_condition_filters
|
||||
assert_equal %w( ensure_login ), test_process(OnlyConditionSymController).template.assigns["ran_filter"]
|
||||
assert_equal nil, test_process(OnlyConditionSymController, "show_without_filter").template.assigns["ran_filter"]
|
||||
|
||||
assert test_process(OnlyConditionProcController).template.assigns["ran_proc_filter"]
|
||||
assert !test_process(OnlyConditionProcController, "show_without_filter").template.assigns["ran_proc_filter"]
|
||||
|
||||
assert test_process(OnlyConditionClassController).template.assigns["ran_class_filter"]
|
||||
assert !test_process(OnlyConditionClassController, "show_without_filter").template.assigns["ran_class_filter"]
|
||||
end
|
||||
|
||||
def test_running_except_condition_filters
|
||||
assert_equal %w( ensure_login ), test_process(ExceptConditionSymController).template.assigns["ran_filter"]
|
||||
assert_equal nil, test_process(ExceptConditionSymController, "show_without_filter").template.assigns["ran_filter"]
|
||||
|
||||
assert test_process(ExceptConditionProcController).template.assigns["ran_proc_filter"]
|
||||
assert !test_process(ExceptConditionProcController, "show_without_filter").template.assigns["ran_proc_filter"]
|
||||
|
||||
assert test_process(ExceptConditionClassController).template.assigns["ran_class_filter"]
|
||||
assert !test_process(ExceptConditionClassController, "show_without_filter").template.assigns["ran_class_filter"]
|
||||
end
|
||||
|
||||
def test_running_before_and_after_condition_filters
|
||||
assert_equal %w( ensure_login clean_up_tmp), test_process(BeforeAndAfterConditionController).template.assigns["ran_filter"]
|
||||
assert_equal nil, test_process(BeforeAndAfterConditionController, "show_without_filter").template.assigns["ran_filter"]
|
||||
end
|
||||
|
||||
def test_around_filter
|
||||
controller = test_process(AroundFilterController)
|
||||
assert controller.template.assigns["before_ran"]
|
||||
assert controller.template.assigns["after_ran"]
|
||||
end
|
||||
|
||||
def test_before_after_class_filter
|
||||
controller = test_process(BeforeAfterClassFilterController)
|
||||
assert controller.template.assigns["before_ran"]
|
||||
assert controller.template.assigns["after_ran"]
|
||||
end
|
||||
|
||||
def test_having_properties_in_around_filter
|
||||
controller = test_process(AroundFilterController)
|
||||
assert_equal "before and after", controller.template.assigns["execution_log"]
|
||||
end
|
||||
|
||||
def test_prepending_and_appending_around_filter
|
||||
controller = test_process(MixedFilterController)
|
||||
assert_equal " before aroundfilter before procfilter before appended aroundfilter " +
|
||||
" after appended aroundfilter after aroundfilter after procfilter ",
|
||||
MixedFilterController.execution_log
|
||||
end
|
||||
|
||||
def test_rendering_breaks_filtering_chain
|
||||
response = test_process(RenderingController)
|
||||
assert_equal "something else", response.body
|
||||
assert !response.template.assigns["ran_action"]
|
||||
end
|
||||
|
||||
def test_filters_with_mixed_specialization_run_in_order
|
||||
assert_nothing_raised do
|
||||
response = test_process(MixedSpecializationController, 'bar')
|
||||
assert_equal 'bar', response.body
|
||||
end
|
||||
|
||||
assert_nothing_raised do
|
||||
response = test_process(MixedSpecializationController, 'foo')
|
||||
assert_equal 'foo', response.body
|
||||
end
|
||||
end
|
||||
|
||||
def test_dynamic_dispatch
|
||||
%w(foo bar baz).each do |action|
|
||||
request = ActionController::TestRequest.new
|
||||
request.query_parameters[:choose] = action
|
||||
response = DynamicDispatchController.process(request, ActionController::TestResponse.new)
|
||||
assert_equal action, response.body
|
||||
end
|
||||
end
|
||||
|
||||
def test_running_prepended_before_and_after_filter
|
||||
assert_equal 3, PrependingBeforeAndAfterController.filter_chain.length
|
||||
response = test_process(PrependingBeforeAndAfterController)
|
||||
assert_equal %w( before_all between_before_all_and_after_all after_all ), response.template.assigns["ran_filter"]
|
||||
end
|
||||
|
||||
def test_skipping_and_limiting_controller
|
||||
assert_equal %w( ensure_login ), test_process(SkippingAndLimitedController, "index").template.assigns["ran_filter"]
|
||||
assert_nil test_process(SkippingAndLimitedController, "public").template.assigns["ran_filter"]
|
||||
end
|
||||
|
||||
def test_skipping_and_reordering_controller
|
||||
assert_equal %w( find_record ensure_login ), test_process(SkippingAndReorderingController, "index").template.assigns["ran_filter"]
|
||||
end
|
||||
|
||||
def test_conditional_skipping_of_filters
|
||||
assert_nil test_process(ConditionalSkippingController, "login").template.assigns["ran_filter"]
|
||||
assert_equal %w( ensure_login find_user ), test_process(ConditionalSkippingController, "change_password").template.assigns["ran_filter"]
|
||||
|
||||
assert_nil test_process(ConditionalSkippingController, "login").template.controller.instance_variable_get("@ran_after_filter")
|
||||
assert_equal %w( clean_up ), test_process(ConditionalSkippingController, "change_password").template.controller.instance_variable_get("@ran_after_filter")
|
||||
end
|
||||
|
||||
def test_conditional_skipping_of_filters_when_parent_filter_is_also_conditional
|
||||
assert_equal %w( conditional_in_parent conditional_in_parent ), test_process(ChildOfConditionalParentController).template.assigns['ran_filter']
|
||||
assert_nil test_process(ChildOfConditionalParentController, 'another_action').template.assigns['ran_filter']
|
||||
end
|
||||
|
||||
def test_condition_skipping_of_filters_when_siblings_also_have_conditions
|
||||
assert_equal %w( conditional_in_parent conditional_in_parent ), test_process(ChildOfConditionalParentController).template.assigns['ran_filter'], "1"
|
||||
assert_equal nil, test_process(AnotherChildOfConditionalParentController).template.assigns['ran_filter']
|
||||
assert_equal %w( conditional_in_parent conditional_in_parent ), test_process(ChildOfConditionalParentController).template.assigns['ran_filter']
|
||||
end
|
||||
|
||||
def test_changing_the_requirements
|
||||
assert_equal nil, test_process(ChangingTheRequirementsController, "go_wild").template.assigns['ran_filter']
|
||||
end
|
||||
|
||||
def test_a_rescuing_around_filter
|
||||
response = nil
|
||||
assert_nothing_raised do
|
||||
response = test_process(RescuedController)
|
||||
end
|
||||
|
||||
assert response.success?
|
||||
assert_equal("I rescued this: #<FilterTest::ErrorToRescue: Something made the bad noise.>", response.body)
|
||||
end
|
||||
|
||||
private
|
||||
def test_process(controller, action = "show")
|
||||
request = ActionController::TestRequest.new
|
||||
request.action = action
|
||||
controller.process(request, ActionController::TestResponse.new)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
class PostsController < ActionController::Base
|
||||
def rescue_action(e); raise e; end
|
||||
|
||||
module AroundExceptions
|
||||
class Error < StandardError ; end
|
||||
class Before < Error ; end
|
||||
class After < Error ; end
|
||||
end
|
||||
include AroundExceptions
|
||||
|
||||
class DefaultFilter
|
||||
include AroundExceptions
|
||||
end
|
||||
|
||||
module_eval %w(raises_before raises_after raises_both no_raise no_filter).map { |action| "def #{action}; default_action end" }.join("\n")
|
||||
|
||||
private
|
||||
def default_action
|
||||
render :inline => "#{action_name} called"
|
||||
end
|
||||
end
|
||||
|
||||
class ControllerWithSymbolAsFilter < PostsController
|
||||
around_filter :raise_before, :only => :raises_before
|
||||
around_filter :raise_after, :only => :raises_after
|
||||
around_filter :without_exception, :only => :no_raise
|
||||
|
||||
private
|
||||
def raise_before
|
||||
raise Before
|
||||
yield
|
||||
end
|
||||
|
||||
def raise_after
|
||||
yield
|
||||
raise After
|
||||
end
|
||||
|
||||
def without_exception
|
||||
# Do stuff...
|
||||
1 + 1
|
||||
|
||||
yield
|
||||
|
||||
# Do stuff...
|
||||
1 + 1
|
||||
end
|
||||
end
|
||||
|
||||
class ControllerWithFilterClass < PostsController
|
||||
class YieldingFilter < DefaultFilter
|
||||
def self.filter(controller)
|
||||
yield
|
||||
raise After
|
||||
end
|
||||
end
|
||||
|
||||
around_filter YieldingFilter, :only => :raises_after
|
||||
end
|
||||
|
||||
class ControllerWithFilterInstance < PostsController
|
||||
class YieldingFilter < DefaultFilter
|
||||
def filter(controller)
|
||||
yield
|
||||
raise After
|
||||
end
|
||||
end
|
||||
|
||||
around_filter YieldingFilter.new, :only => :raises_after
|
||||
end
|
||||
|
||||
class ControllerWithFilterMethod < PostsController
|
||||
class YieldingFilter < DefaultFilter
|
||||
def filter(controller)
|
||||
yield
|
||||
raise After
|
||||
end
|
||||
end
|
||||
|
||||
around_filter YieldingFilter.new.method(:filter), :only => :raises_after
|
||||
end
|
||||
|
||||
class ControllerWithProcFilter < PostsController
|
||||
around_filter(:only => :no_raise) do |c,b|
|
||||
c.assigns['before'] = true
|
||||
b.call
|
||||
c.assigns['after'] = true
|
||||
end
|
||||
end
|
||||
|
||||
class ControllerWithNestedFilters < ControllerWithSymbolAsFilter
|
||||
around_filter :raise_before, :raise_after, :without_exception, :only => :raises_both
|
||||
end
|
||||
|
||||
class ControllerWithAllTypesOfFilters < PostsController
|
||||
before_filter :before
|
||||
around_filter :around
|
||||
after_filter :after
|
||||
around_filter :around_again
|
||||
|
||||
private
|
||||
def before
|
||||
@ran_filter ||= []
|
||||
@ran_filter << 'before'
|
||||
end
|
||||
|
||||
def around
|
||||
@ran_filter << 'around (before yield)'
|
||||
yield
|
||||
@ran_filter << 'around (after yield)'
|
||||
end
|
||||
|
||||
def after
|
||||
@ran_filter << 'after'
|
||||
end
|
||||
|
||||
def around_again
|
||||
@ran_filter << 'around_again (before yield)'
|
||||
yield
|
||||
@ran_filter << 'around_again (after yield)'
|
||||
end
|
||||
end
|
||||
|
||||
class ControllerWithTwoLessFilters < ControllerWithAllTypesOfFilters
|
||||
skip_filter :around_again
|
||||
skip_filter :after
|
||||
end
|
||||
|
||||
class YieldingAroundFiltersTest < Test::Unit::TestCase
|
||||
include PostsController::AroundExceptions
|
||||
|
||||
def test_filters_registering
|
||||
assert_equal 1, ControllerWithFilterMethod.filter_chain.size
|
||||
assert_equal 1, ControllerWithFilterClass.filter_chain.size
|
||||
assert_equal 1, ControllerWithFilterInstance.filter_chain.size
|
||||
assert_equal 3, ControllerWithSymbolAsFilter.filter_chain.size
|
||||
assert_equal 6, ControllerWithNestedFilters.filter_chain.size
|
||||
assert_equal 4, ControllerWithAllTypesOfFilters.filter_chain.size
|
||||
end
|
||||
|
||||
def test_base
|
||||
controller = PostsController
|
||||
assert_nothing_raised { test_process(controller,'no_raise') }
|
||||
assert_nothing_raised { test_process(controller,'raises_before') }
|
||||
assert_nothing_raised { test_process(controller,'raises_after') }
|
||||
assert_nothing_raised { test_process(controller,'no_filter') }
|
||||
end
|
||||
|
||||
def test_with_symbol
|
||||
controller = ControllerWithSymbolAsFilter
|
||||
assert_nothing_raised { test_process(controller,'no_raise') }
|
||||
assert_raise(Before) { test_process(controller,'raises_before') }
|
||||
assert_raise(After) { test_process(controller,'raises_after') }
|
||||
assert_nothing_raised { test_process(controller,'no_raise') }
|
||||
end
|
||||
|
||||
def test_with_class
|
||||
controller = ControllerWithFilterClass
|
||||
assert_nothing_raised { test_process(controller,'no_raise') }
|
||||
assert_raise(After) { test_process(controller,'raises_after') }
|
||||
end
|
||||
|
||||
def test_with_instance
|
||||
controller = ControllerWithFilterInstance
|
||||
assert_nothing_raised { test_process(controller,'no_raise') }
|
||||
assert_raise(After) { test_process(controller,'raises_after') }
|
||||
end
|
||||
|
||||
def test_with_method
|
||||
controller = ControllerWithFilterMethod
|
||||
assert_nothing_raised { test_process(controller,'no_raise') }
|
||||
assert_raise(After) { test_process(controller,'raises_after') }
|
||||
end
|
||||
|
||||
def test_with_proc
|
||||
controller = test_process(ControllerWithProcFilter,'no_raise')
|
||||
assert controller.template.assigns['before']
|
||||
assert controller.template.assigns['after']
|
||||
end
|
||||
|
||||
def test_nested_filters
|
||||
controller = ControllerWithNestedFilters
|
||||
assert_nothing_raised do
|
||||
begin
|
||||
test_process(controller,'raises_both')
|
||||
rescue Before, After
|
||||
end
|
||||
end
|
||||
assert_raise Before do
|
||||
begin
|
||||
test_process(controller,'raises_both')
|
||||
rescue After
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_filter_order_with_all_filter_types
|
||||
controller = test_process(ControllerWithAllTypesOfFilters,'no_raise')
|
||||
assert_equal 'before around (before yield) around_again (before yield) around_again (after yield) around (after yield) after',controller.template.assigns['ran_filter'].join(' ')
|
||||
end
|
||||
|
||||
def test_filter_order_with_skip_filter_method
|
||||
controller = test_process(ControllerWithTwoLessFilters,'no_raise')
|
||||
assert_equal 'before around (before yield) around (after yield)',controller.template.assigns['ran_filter'].join(' ')
|
||||
end
|
||||
|
||||
def test_first_filter_in_multiple_before_filter_chain_halts
|
||||
controller = ::FilterTest::TestMultipleFiltersController.new
|
||||
response = test_process(controller, 'fail_1')
|
||||
assert_equal ' ', response.body
|
||||
assert_equal 1, controller.instance_variable_get(:@try)
|
||||
assert controller.instance_variable_get(:@before_filter_chain_aborted)
|
||||
end
|
||||
|
||||
def test_second_filter_in_multiple_before_filter_chain_halts
|
||||
controller = ::FilterTest::TestMultipleFiltersController.new
|
||||
response = test_process(controller, 'fail_2')
|
||||
assert_equal ' ', response.body
|
||||
assert_equal 2, controller.instance_variable_get(:@try)
|
||||
assert controller.instance_variable_get(:@before_filter_chain_aborted)
|
||||
end
|
||||
|
||||
def test_last_filter_in_multiple_before_filter_chain_halts
|
||||
controller = ::FilterTest::TestMultipleFiltersController.new
|
||||
response = test_process(controller, 'fail_3')
|
||||
assert_equal ' ', response.body
|
||||
assert_equal 3, controller.instance_variable_get(:@try)
|
||||
assert controller.instance_variable_get(:@before_filter_chain_aborted)
|
||||
end
|
||||
|
||||
protected
|
||||
def test_process(controller, action = "show")
|
||||
request = ActionController::TestRequest.new
|
||||
request.action = action
|
||||
controller.process(request, ActionController::TestResponse.new)
|
||||
end
|
||||
end
|
|
@ -1,146 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class FlashTest < Test::Unit::TestCase
|
||||
class TestController < ActionController::Base
|
||||
def set_flash
|
||||
flash["that"] = "hello"
|
||||
render :inline => "hello"
|
||||
end
|
||||
|
||||
def set_flash_now
|
||||
flash.now["that"] = "hello"
|
||||
flash.now["foo"] ||= "bar"
|
||||
flash.now["foo"] ||= "err"
|
||||
@flashy = flash.now["that"]
|
||||
@flash_copy = {}.update flash
|
||||
render :inline => "hello"
|
||||
end
|
||||
|
||||
def attempt_to_use_flash_now
|
||||
@flash_copy = {}.update flash
|
||||
@flashy = flash["that"]
|
||||
render :inline => "hello"
|
||||
end
|
||||
|
||||
def use_flash
|
||||
@flash_copy = {}.update flash
|
||||
@flashy = flash["that"]
|
||||
render :inline => "hello"
|
||||
end
|
||||
|
||||
def use_flash_and_keep_it
|
||||
@flash_copy = {}.update flash
|
||||
@flashy = flash["that"]
|
||||
flash.keep
|
||||
render :inline => "hello"
|
||||
end
|
||||
|
||||
def use_flash_and_update_it
|
||||
flash.update("this" => "hello again")
|
||||
@flash_copy = {}.update flash
|
||||
render :inline => "hello"
|
||||
end
|
||||
|
||||
def use_flash_after_reset_session
|
||||
flash["that"] = "hello"
|
||||
@flashy_that = flash["that"]
|
||||
reset_session
|
||||
@flashy_that_reset = flash["that"]
|
||||
flash["this"] = "good-bye"
|
||||
@flashy_this = flash["this"]
|
||||
render :inline => "hello"
|
||||
end
|
||||
|
||||
def rescue_action(e)
|
||||
raise unless ActionView::MissingTemplate === e
|
||||
end
|
||||
|
||||
# methods for test_sweep_after_halted_filter_chain
|
||||
before_filter :halt_and_redir, :only => "filter_halting_action"
|
||||
|
||||
def std_action
|
||||
@flash_copy = {}.update(flash)
|
||||
end
|
||||
|
||||
def filter_halting_action
|
||||
@flash_copy = {}.update(flash)
|
||||
end
|
||||
|
||||
def halt_and_redir
|
||||
flash["foo"] = "bar"
|
||||
redirect_to :action => "std_action"
|
||||
@flash_copy = {}.update(flash)
|
||||
end
|
||||
end
|
||||
|
||||
def setup
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
@controller = TestController.new
|
||||
end
|
||||
|
||||
def test_flash
|
||||
get :set_flash
|
||||
|
||||
get :use_flash
|
||||
assert_equal "hello", @response.template.assigns["flash_copy"]["that"]
|
||||
assert_equal "hello", @response.template.assigns["flashy"]
|
||||
|
||||
get :use_flash
|
||||
assert_nil @response.template.assigns["flash_copy"]["that"], "On second flash"
|
||||
end
|
||||
|
||||
def test_keep_flash
|
||||
get :set_flash
|
||||
|
||||
get :use_flash_and_keep_it
|
||||
assert_equal "hello", @response.template.assigns["flash_copy"]["that"]
|
||||
assert_equal "hello", @response.template.assigns["flashy"]
|
||||
|
||||
get :use_flash
|
||||
assert_equal "hello", @response.template.assigns["flash_copy"]["that"], "On second flash"
|
||||
|
||||
get :use_flash
|
||||
assert_nil @response.template.assigns["flash_copy"]["that"], "On third flash"
|
||||
end
|
||||
|
||||
def test_flash_now
|
||||
get :set_flash_now
|
||||
assert_equal "hello", @response.template.assigns["flash_copy"]["that"]
|
||||
assert_equal "bar" , @response.template.assigns["flash_copy"]["foo"]
|
||||
assert_equal "hello", @response.template.assigns["flashy"]
|
||||
|
||||
get :attempt_to_use_flash_now
|
||||
assert_nil @response.template.assigns["flash_copy"]["that"]
|
||||
assert_nil @response.template.assigns["flash_copy"]["foo"]
|
||||
assert_nil @response.template.assigns["flashy"]
|
||||
end
|
||||
|
||||
def test_update_flash
|
||||
get :set_flash
|
||||
get :use_flash_and_update_it
|
||||
assert_equal "hello", @response.template.assigns["flash_copy"]["that"]
|
||||
assert_equal "hello again", @response.template.assigns["flash_copy"]["this"]
|
||||
get :use_flash
|
||||
assert_nil @response.template.assigns["flash_copy"]["that"], "On second flash"
|
||||
assert_equal "hello again", @response.template.assigns["flash_copy"]["this"], "On second flash"
|
||||
end
|
||||
|
||||
def test_flash_after_reset_session
|
||||
get :use_flash_after_reset_session
|
||||
assert_equal "hello", @response.template.assigns["flashy_that"]
|
||||
assert_equal "good-bye", @response.template.assigns["flashy_this"]
|
||||
assert_nil @response.template.assigns["flashy_that_reset"]
|
||||
end
|
||||
|
||||
def test_sweep_after_halted_filter_chain
|
||||
get :std_action
|
||||
assert_nil @response.template.assigns["flash_copy"]["foo"]
|
||||
get :filter_halting_action
|
||||
assert_equal "bar", @response.template.assigns["flash_copy"]["foo"]
|
||||
get :std_action # follow redirection
|
||||
assert_equal "bar", @response.template.assigns["flash_copy"]["foo"]
|
||||
get :std_action
|
||||
assert_nil @response.template.assigns["flash_copy"]["foo"]
|
||||
end
|
||||
end
|
|
@ -1,14 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class HeaderTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@headers = ActionController::Http::Headers.new("HTTP_CONTENT_TYPE"=>"text/plain")
|
||||
end
|
||||
|
||||
def test_content_type_works
|
||||
assert_equal "text/plain", @headers["Content-Type"]
|
||||
assert_equal "text/plain", @headers["content-type"]
|
||||
assert_equal "text/plain", @headers["CONTENT_TYPE"]
|
||||
assert_equal "text/plain", @headers["HTTP_CONTENT_TYPE"]
|
||||
end
|
||||
end
|
|
@ -1,210 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
ActionController::Helpers::HELPERS_DIR.replace File.dirname(__FILE__) + '/../fixtures/helpers'
|
||||
|
||||
class TestController < ActionController::Base
|
||||
attr_accessor :delegate_attr
|
||||
def delegate_method() end
|
||||
def rescue_action(e) raise end
|
||||
end
|
||||
|
||||
module Fun
|
||||
class GamesController < ActionController::Base
|
||||
def render_hello_world
|
||||
render :inline => "hello: <%= stratego %>"
|
||||
end
|
||||
|
||||
def rescue_action(e) raise end
|
||||
end
|
||||
|
||||
class PdfController < ActionController::Base
|
||||
def test
|
||||
render :inline => "test: <%= foobar %>"
|
||||
end
|
||||
|
||||
def rescue_action(e) raise end
|
||||
end
|
||||
end
|
||||
|
||||
class ApplicationController < ActionController::Base
|
||||
helper :all
|
||||
end
|
||||
|
||||
module LocalAbcHelper
|
||||
def a() end
|
||||
def b() end
|
||||
def c() end
|
||||
end
|
||||
|
||||
class HelperTest < Test::Unit::TestCase
|
||||
def setup
|
||||
# Increment symbol counter.
|
||||
@symbol = (@@counter ||= 'A0').succ!.dup
|
||||
|
||||
# Generate new controller class.
|
||||
controller_class_name = "Helper#{@symbol}Controller"
|
||||
eval("class #{controller_class_name} < TestController; end")
|
||||
@controller_class = self.class.const_get(controller_class_name)
|
||||
|
||||
# Set default test helper.
|
||||
self.test_helper = LocalAbcHelper
|
||||
end
|
||||
|
||||
def test_deprecated_helper
|
||||
assert_equal expected_helper_methods, missing_methods
|
||||
assert_nothing_raised { @controller_class.helper TestHelper }
|
||||
assert_equal [], missing_methods
|
||||
end
|
||||
|
||||
def test_declare_helper
|
||||
require 'abc_helper'
|
||||
self.test_helper = AbcHelper
|
||||
assert_equal expected_helper_methods, missing_methods
|
||||
assert_nothing_raised { @controller_class.helper :abc }
|
||||
assert_equal [], missing_methods
|
||||
end
|
||||
|
||||
def test_declare_missing_helper
|
||||
assert_equal expected_helper_methods, missing_methods
|
||||
assert_raise(MissingSourceFile) { @controller_class.helper :missing }
|
||||
end
|
||||
|
||||
def test_declare_missing_file_from_helper
|
||||
require 'broken_helper'
|
||||
rescue LoadError => e
|
||||
assert_nil(/\bbroken_helper\b/.match(e.to_s)[1])
|
||||
end
|
||||
|
||||
def test_helper_block
|
||||
assert_nothing_raised {
|
||||
@controller_class.helper { def block_helper_method; end }
|
||||
}
|
||||
assert master_helper_methods.include?('block_helper_method')
|
||||
end
|
||||
|
||||
def test_helper_block_include
|
||||
assert_equal expected_helper_methods, missing_methods
|
||||
assert_nothing_raised {
|
||||
@controller_class.helper { include HelperTest::TestHelper }
|
||||
}
|
||||
assert [], missing_methods
|
||||
end
|
||||
|
||||
def test_helper_method
|
||||
assert_nothing_raised { @controller_class.helper_method :delegate_method }
|
||||
assert master_helper_methods.include?('delegate_method')
|
||||
end
|
||||
|
||||
def test_helper_attr
|
||||
assert_nothing_raised { @controller_class.helper_attr :delegate_attr }
|
||||
assert master_helper_methods.include?('delegate_attr')
|
||||
assert master_helper_methods.include?('delegate_attr=')
|
||||
end
|
||||
|
||||
def test_helper_for_nested_controller
|
||||
request = ActionController::TestRequest.new
|
||||
response = ActionController::TestResponse.new
|
||||
request.action = 'render_hello_world'
|
||||
|
||||
assert_equal 'hello: Iz guuut!', Fun::GamesController.process(request, response).body
|
||||
end
|
||||
|
||||
def test_helper_for_acronym_controller
|
||||
request = ActionController::TestRequest.new
|
||||
response = ActionController::TestResponse.new
|
||||
request.action = 'test'
|
||||
|
||||
assert_equal 'test: baz', Fun::PdfController.process(request, response).body
|
||||
end
|
||||
|
||||
def test_all_helpers
|
||||
methods = ApplicationController.master_helper_module.instance_methods.map(&:to_s)
|
||||
|
||||
# abc_helper.rb
|
||||
assert methods.include?('bare_a')
|
||||
|
||||
# fun/games_helper.rb
|
||||
assert methods.include?('stratego')
|
||||
|
||||
# fun/pdf_helper.rb
|
||||
assert methods.include?('foobar')
|
||||
end
|
||||
|
||||
def test_helper_proxy
|
||||
methods = ApplicationController.helpers.methods.map(&:to_s)
|
||||
|
||||
# ActionView
|
||||
assert methods.include?('pluralize')
|
||||
|
||||
# abc_helper.rb
|
||||
assert methods.include?('bare_a')
|
||||
|
||||
# fun/games_helper.rb
|
||||
assert methods.include?('stratego')
|
||||
|
||||
# fun/pdf_helper.rb
|
||||
assert methods.include?('foobar')
|
||||
end
|
||||
|
||||
private
|
||||
def expected_helper_methods
|
||||
TestHelper.instance_methods.map(&:to_s)
|
||||
end
|
||||
|
||||
def master_helper_methods
|
||||
@controller_class.master_helper_module.instance_methods.map(&:to_s)
|
||||
end
|
||||
|
||||
def missing_methods
|
||||
expected_helper_methods - master_helper_methods
|
||||
end
|
||||
|
||||
def test_helper=(helper_module)
|
||||
silence_warnings { self.class.const_set('TestHelper', helper_module) }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class IsolatedHelpersTest < Test::Unit::TestCase
|
||||
class A < ActionController::Base
|
||||
def index
|
||||
render :inline => '<%= shout %>'
|
||||
end
|
||||
|
||||
def rescue_action(e) raise end
|
||||
end
|
||||
|
||||
class B < A
|
||||
helper { def shout; 'B' end }
|
||||
|
||||
def index
|
||||
render :inline => '<%= shout %>'
|
||||
end
|
||||
end
|
||||
|
||||
class C < A
|
||||
helper { def shout; 'C' end }
|
||||
|
||||
def index
|
||||
render :inline => '<%= shout %>'
|
||||
end
|
||||
end
|
||||
|
||||
def setup
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
@request.action = 'index'
|
||||
end
|
||||
|
||||
def test_helper_in_a
|
||||
assert_raise(NameError) { A.process(@request, @response) }
|
||||
end
|
||||
|
||||
def test_helper_in_b
|
||||
assert_equal 'B', B.process(@request, @response).body
|
||||
end
|
||||
|
||||
def test_helper_in_c
|
||||
assert_equal 'C', C.process(@request, @response).body
|
||||
end
|
||||
end
|
|
@ -1,148 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class DocumentTest < Test::Unit::TestCase
|
||||
def test_handle_doctype
|
||||
doc = nil
|
||||
assert_nothing_raised do
|
||||
doc = HTML::Document.new <<-HTML.strip
|
||||
<!DOCTYPE "blah" "blah" "blah">
|
||||
<html>
|
||||
</html>
|
||||
HTML
|
||||
end
|
||||
assert_equal 3, doc.root.children.length
|
||||
assert_equal %{<!DOCTYPE "blah" "blah" "blah">}, doc.root.children[0].content
|
||||
assert_match %r{\s+}m, doc.root.children[1].content
|
||||
assert_equal "html", doc.root.children[2].name
|
||||
end
|
||||
|
||||
def test_find_img
|
||||
doc = HTML::Document.new <<-HTML.strip
|
||||
<html>
|
||||
<body>
|
||||
<p><img src="hello.gif"></p>
|
||||
</body>
|
||||
</html>
|
||||
HTML
|
||||
assert doc.find(:tag=>"img", :attributes=>{"src"=>"hello.gif"})
|
||||
end
|
||||
|
||||
def test_find_all
|
||||
doc = HTML::Document.new <<-HTML.strip
|
||||
<html>
|
||||
<body>
|
||||
<p class="test"><img src="hello.gif"></p>
|
||||
<div class="foo">
|
||||
<p class="test">something</p>
|
||||
<p>here is <em class="test">more</em></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
HTML
|
||||
all = doc.find_all :attributes => { :class => "test" }
|
||||
assert_equal 3, all.length
|
||||
assert_equal [ "p", "p", "em" ], all.map { |n| n.name }
|
||||
end
|
||||
|
||||
def test_find_with_text
|
||||
doc = HTML::Document.new <<-HTML.strip
|
||||
<html>
|
||||
<body>
|
||||
<p>Some text</p>
|
||||
</body>
|
||||
</html>
|
||||
HTML
|
||||
assert doc.find(:content => "Some text")
|
||||
assert doc.find(:tag => "p", :child => { :content => "Some text" })
|
||||
assert doc.find(:tag => "p", :child => "Some text")
|
||||
assert doc.find(:tag => "p", :content => "Some text")
|
||||
end
|
||||
|
||||
def test_parse_xml
|
||||
assert_nothing_raised { HTML::Document.new("<tags><tag/></tags>", true, true) }
|
||||
assert_nothing_raised { HTML::Document.new("<outer><link>something</link></outer>", true, true) }
|
||||
end
|
||||
|
||||
def test_parse_document
|
||||
doc = HTML::Document.new(<<-HTML)
|
||||
<div>
|
||||
<h2>blah</h2>
|
||||
<table>
|
||||
</table>
|
||||
</div>
|
||||
HTML
|
||||
assert_not_nil doc.find(:tag => "div", :children => { :count => 1, :only => { :tag => "table" } })
|
||||
end
|
||||
|
||||
def test_tag_nesting_nothing_to_s
|
||||
doc = HTML::Document.new("<tag></tag>")
|
||||
assert_equal "<tag></tag>", doc.root.to_s
|
||||
end
|
||||
|
||||
def test_tag_nesting_space_to_s
|
||||
doc = HTML::Document.new("<tag> </tag>")
|
||||
assert_equal "<tag> </tag>", doc.root.to_s
|
||||
end
|
||||
|
||||
def test_tag_nesting_text_to_s
|
||||
doc = HTML::Document.new("<tag>text</tag>")
|
||||
assert_equal "<tag>text</tag>", doc.root.to_s
|
||||
end
|
||||
|
||||
def test_tag_nesting_tag_to_s
|
||||
doc = HTML::Document.new("<tag><nested /></tag>")
|
||||
assert_equal "<tag><nested /></tag>", doc.root.to_s
|
||||
end
|
||||
|
||||
def test_parse_cdata
|
||||
doc = HTML::Document.new(<<-HTML)
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
|
||||
<head>
|
||||
<title><![CDATA[<br>]]></title>
|
||||
</head>
|
||||
<body>
|
||||
<p>this document has <br> for a title</p>
|
||||
</body>
|
||||
</html>
|
||||
HTML
|
||||
|
||||
assert_nil doc.find(:tag => "title", :descendant => { :tag => "br" })
|
||||
assert doc.find(:tag => "title", :child => "<br>")
|
||||
end
|
||||
|
||||
def test_find_empty_tag
|
||||
doc = HTML::Document.new("<div id='map'></div>")
|
||||
assert_nil doc.find(:tag => "div", :attributes => { :id => "map" }, :content => /./)
|
||||
assert doc.find(:tag => "div", :attributes => { :id => "map" }, :content => /\A\Z/)
|
||||
assert doc.find(:tag => "div", :attributes => { :id => "map" }, :content => /^$/)
|
||||
assert doc.find(:tag => "div", :attributes => { :id => "map" }, :content => "")
|
||||
assert doc.find(:tag => "div", :attributes => { :id => "map" }, :content => nil)
|
||||
end
|
||||
|
||||
def test_parse_invalid_document
|
||||
assert_nothing_raised do
|
||||
doc = HTML::Document.new("<html>
|
||||
<table>
|
||||
<tr>
|
||||
<td style=\"color: #FFFFFF; height: 17px; onclick=\"window.location.href='http://www.rmeinc.com/about_rme.aspx'\" style=\"cursor:pointer; height: 17px;\"; nowrap onclick=\"window.location.href='http://www.rmeinc.com/about_rme.aspx'\" onmouseout=\"this.bgColor='#0066cc'; this.style.color='#FFFFFF'\" onmouseover=\"this.bgColor='#ffffff'; this.style.color='#0033cc'\">About Us</td>
|
||||
</tr>
|
||||
</table>
|
||||
</html>")
|
||||
end
|
||||
end
|
||||
|
||||
def test_invalid_document_raises_exception_when_strict
|
||||
assert_raises RuntimeError do
|
||||
doc = HTML::Document.new("<html>
|
||||
<table>
|
||||
<tr>
|
||||
<td style=\"color: #FFFFFF; height: 17px; onclick=\"window.location.href='http://www.rmeinc.com/about_rme.aspx'\" style=\"cursor:pointer; height: 17px;\"; nowrap onclick=\"window.location.href='http://www.rmeinc.com/about_rme.aspx'\" onmouseout=\"this.bgColor='#0066cc'; this.style.color='#FFFFFF'\" onmouseover=\"this.bgColor='#ffffff'; this.style.color='#0033cc'\">About Us</td>
|
||||
</tr>
|
||||
</table>
|
||||
</html>", true)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -1,68 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class NodeTest < Test::Unit::TestCase
|
||||
|
||||
class MockNode
|
||||
def initialize(matched, value)
|
||||
@matched = matched
|
||||
@value = value
|
||||
end
|
||||
|
||||
def find(conditions)
|
||||
@matched && self
|
||||
end
|
||||
|
||||
def to_s
|
||||
@value.to_s
|
||||
end
|
||||
end
|
||||
|
||||
def setup
|
||||
@node = HTML::Node.new("parent")
|
||||
@node.children.concat [MockNode.new(false,1), MockNode.new(true,"two"), MockNode.new(false,:three)]
|
||||
end
|
||||
|
||||
def test_match
|
||||
assert !@node.match("foo")
|
||||
end
|
||||
|
||||
def test_tag
|
||||
assert !@node.tag?
|
||||
end
|
||||
|
||||
def test_to_s
|
||||
assert_equal "1twothree", @node.to_s
|
||||
end
|
||||
|
||||
def test_find
|
||||
assert_equal "two", @node.find('blah').to_s
|
||||
end
|
||||
|
||||
def test_parse_strict
|
||||
s = "<b foo='hello'' bar='baz'>"
|
||||
assert_raise(RuntimeError) { HTML::Node.parse(nil,0,0,s) }
|
||||
end
|
||||
|
||||
def test_parse_relaxed
|
||||
s = "<b foo='hello'' bar='baz'>"
|
||||
node = nil
|
||||
assert_nothing_raised { node = HTML::Node.parse(nil,0,0,s,false) }
|
||||
assert node.attributes.has_key?("foo")
|
||||
assert !node.attributes.has_key?("bar")
|
||||
end
|
||||
|
||||
def test_to_s_with_boolean_attrs
|
||||
s = "<b foo bar>"
|
||||
node = HTML::Node.parse(nil,0,0,s)
|
||||
assert node.attributes.has_key?("foo")
|
||||
assert node.attributes.has_key?("bar")
|
||||
assert "<b foo bar>", node.to_s
|
||||
end
|
||||
|
||||
def test_parse_with_unclosed_tag
|
||||
s = "<span onmouseover='bang'"
|
||||
node = nil
|
||||
assert_nothing_raised { node = HTML::Node.parse(nil,0,0,s,false) }
|
||||
assert node.attributes.has_key?("onmouseover")
|
||||
end
|
||||
end
|
|
@ -1,259 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class SanitizerTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@sanitizer = nil # used by assert_sanitizer
|
||||
end
|
||||
|
||||
def test_strip_tags
|
||||
sanitizer = HTML::FullSanitizer.new
|
||||
assert_equal("<<<bad html", sanitizer.sanitize("<<<bad html"))
|
||||
assert_equal("<<", sanitizer.sanitize("<<<bad html>"))
|
||||
assert_equal("Dont touch me", sanitizer.sanitize("Dont touch me"))
|
||||
assert_equal("This is a test.", sanitizer.sanitize("<p>This <u>is<u> a <a href='test.html'><strong>test</strong></a>.</p>"))
|
||||
assert_equal("Weirdos", sanitizer.sanitize("Wei<<a>a onclick='alert(document.cookie);'</a>/>rdos"))
|
||||
assert_equal("This is a test.", sanitizer.sanitize("This is a test."))
|
||||
assert_equal(
|
||||
%{This is a test.\n\n\nIt no longer contains any HTML.\n}, sanitizer.sanitize(
|
||||
%{<title>This is <b>a <a href="" target="_blank">test</a></b>.</title>\n\n<!-- it has a comment -->\n\n<p>It no <b>longer <strong>contains <em>any <strike>HTML</strike></em>.</strong></b></p>\n}))
|
||||
assert_equal "This has a here.", sanitizer.sanitize("This has a <!-- comment --> here.")
|
||||
[nil, '', ' '].each { |blank| assert_equal blank, sanitizer.sanitize(blank) }
|
||||
end
|
||||
|
||||
def test_strip_links
|
||||
sanitizer = HTML::LinkSanitizer.new
|
||||
assert_equal "Dont touch me", sanitizer.sanitize("Dont touch me")
|
||||
assert_equal "on my mind\nall day long", sanitizer.sanitize("<a href='almost'>on my mind</a>\n<A href='almost'>all day long</A>")
|
||||
assert_equal "0wn3d", sanitizer.sanitize("<a href='http://www.rubyonrails.com/'><a href='http://www.rubyonrails.com/' onlclick='steal()'>0wn3d</a></a>")
|
||||
assert_equal "Magic", sanitizer.sanitize("<a href='http://www.rubyonrails.com/'>Mag<a href='http://www.ruby-lang.org/'>ic")
|
||||
assert_equal "FrrFox", sanitizer.sanitize("<href onlclick='steal()'>FrrFox</a></href>")
|
||||
assert_equal "My mind\nall <b>day</b> long", sanitizer.sanitize("<a href='almost'>My mind</a>\n<A href='almost'>all <b>day</b> long</A>")
|
||||
assert_equal "all <b>day</b> long", sanitizer.sanitize("<<a>a href='hello'>all <b>day</b> long<</A>/a>")
|
||||
|
||||
assert_equal "<a<a", sanitizer.sanitize("<a<a")
|
||||
end
|
||||
|
||||
def test_sanitize_form
|
||||
assert_sanitized "<form action=\"/foo/bar\" method=\"post\"><input></form>", ''
|
||||
end
|
||||
|
||||
def test_sanitize_plaintext
|
||||
raw = "<plaintext><span>foo</span></plaintext>"
|
||||
assert_sanitized raw, "<span>foo</span>"
|
||||
end
|
||||
|
||||
def test_sanitize_script
|
||||
assert_sanitized "a b c<script language=\"Javascript\">blah blah blah</script>d e f", "a b cd e f"
|
||||
end
|
||||
|
||||
# fucked
|
||||
def test_sanitize_js_handlers
|
||||
raw = %{onthis="do that" <a href="#" onclick="hello" name="foo" onbogus="remove me">hello</a>}
|
||||
assert_sanitized raw, %{onthis="do that" <a name="foo" href="#">hello</a>}
|
||||
end
|
||||
|
||||
def test_sanitize_javascript_href
|
||||
raw = %{href="javascript:bang" <a href="javascript:bang" name="hello">foo</a>, <span href="javascript:bang">bar</span>}
|
||||
assert_sanitized raw, %{href="javascript:bang" <a name="hello">foo</a>, <span>bar</span>}
|
||||
end
|
||||
|
||||
def test_sanitize_image_src
|
||||
raw = %{src="javascript:bang" <img src="javascript:bang" width="5">foo</img>, <span src="javascript:bang">bar</span>}
|
||||
assert_sanitized raw, %{src="javascript:bang" <img width="5">foo</img>, <span>bar</span>}
|
||||
end
|
||||
|
||||
HTML::WhiteListSanitizer.allowed_tags.each do |tag_name|
|
||||
define_method "test_should_allow_#{tag_name}_tag" do
|
||||
assert_sanitized "start <#{tag_name} title=\"1\" onclick=\"foo\">foo <bad>bar</bad> baz</#{tag_name}> end", %(start <#{tag_name} title="1">foo bar baz</#{tag_name}> end)
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_allow_anchors
|
||||
assert_sanitized %(<a href="foo" onclick="bar"><script>baz</script></a>), %(<a href="foo"></a>)
|
||||
end
|
||||
|
||||
# RFC 3986, sec 4.2
|
||||
def test_allow_colons_in_path_component
|
||||
assert_sanitized("<a href=\"./this:that\">foo</a>")
|
||||
end
|
||||
|
||||
%w(src width height alt).each do |img_attr|
|
||||
define_method "test_should_allow_image_#{img_attr}_attribute" do
|
||||
assert_sanitized %(<img #{img_attr}="foo" onclick="bar" />), %(<img #{img_attr}="foo" />)
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_handle_non_html
|
||||
assert_sanitized 'abc'
|
||||
end
|
||||
|
||||
def test_should_handle_blank_text
|
||||
assert_sanitized nil
|
||||
assert_sanitized ''
|
||||
end
|
||||
|
||||
def test_should_allow_custom_tags
|
||||
text = "<u>foo</u>"
|
||||
sanitizer = HTML::WhiteListSanitizer.new
|
||||
assert_equal(text, sanitizer.sanitize(text, :tags => %w(u)))
|
||||
end
|
||||
|
||||
def test_should_allow_only_custom_tags
|
||||
text = "<u>foo</u> with <i>bar</i>"
|
||||
sanitizer = HTML::WhiteListSanitizer.new
|
||||
assert_equal("<u>foo</u> with bar", sanitizer.sanitize(text, :tags => %w(u)))
|
||||
end
|
||||
|
||||
def test_should_allow_custom_tags_with_attributes
|
||||
text = %(<blockquote cite="http://example.com/">foo</blockquote>)
|
||||
sanitizer = HTML::WhiteListSanitizer.new
|
||||
assert_equal(text, sanitizer.sanitize(text))
|
||||
end
|
||||
|
||||
def test_should_allow_custom_tags_with_custom_attributes
|
||||
text = %(<blockquote foo="bar">Lorem ipsum</blockquote>)
|
||||
sanitizer = HTML::WhiteListSanitizer.new
|
||||
assert_equal(text, sanitizer.sanitize(text, :attributes => ['foo']))
|
||||
end
|
||||
|
||||
[%w(img src), %w(a href)].each do |(tag, attr)|
|
||||
define_method "test_should_strip_#{attr}_attribute_in_#{tag}_with_bad_protocols" do
|
||||
assert_sanitized %(<#{tag} #{attr}="javascript:bang" title="1">boo</#{tag}>), %(<#{tag} title="1">boo</#{tag}>)
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_flag_bad_protocols
|
||||
sanitizer = HTML::WhiteListSanitizer.new
|
||||
%w(about chrome data disk hcp help javascript livescript lynxcgi lynxexec ms-help ms-its mhtml mocha opera res resource shell vbscript view-source vnd.ms.radio wysiwyg).each do |proto|
|
||||
assert sanitizer.send(:contains_bad_protocols?, 'src', "#{proto}://bad")
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_accept_good_protocols
|
||||
sanitizer = HTML::WhiteListSanitizer.new
|
||||
HTML::WhiteListSanitizer.allowed_protocols.each do |proto|
|
||||
assert !sanitizer.send(:contains_bad_protocols?, 'src', "#{proto}://good")
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_reject_hex_codes_in_protocol
|
||||
assert_sanitized %(<a href="%6A%61%76%61%73%63%72%69%70%74%3A%61%6C%65%72%74%28%22%58%53%53%22%29">1</a>), "<a>1</a>"
|
||||
assert @sanitizer.send(:contains_bad_protocols?, 'src', "%6A%61%76%61%73%63%72%69%70%74%3A%61%6C%65%72%74%28%22%58%53%53%22%29")
|
||||
end
|
||||
|
||||
def test_should_block_script_tag
|
||||
assert_sanitized %(<SCRIPT\nSRC=http://ha.ckers.org/xss.js></SCRIPT>), ""
|
||||
end
|
||||
|
||||
[%(<IMG SRC="javascript:alert('XSS');">),
|
||||
%(<IMG SRC=javascript:alert('XSS')>),
|
||||
%(<IMG SRC=JaVaScRiPt:alert('XSS')>),
|
||||
%(<IMG """><SCRIPT>alert("XSS")</SCRIPT>">),
|
||||
%(<IMG SRC=javascript:alert("XSS")>),
|
||||
%(<IMG SRC=javascript:alert(String.fromCharCode(88,83,83))>),
|
||||
%(<IMG SRC=javascript:alert('XSS')>),
|
||||
%(<IMG SRC=javascript:alert('XSS')>),
|
||||
%(<IMG SRC=javascript:alert('XSS')>),
|
||||
%(<IMG SRC="jav\tascript:alert('XSS');">),
|
||||
%(<IMG SRC="jav	ascript:alert('XSS');">),
|
||||
%(<IMG SRC="jav
ascript:alert('XSS');">),
|
||||
%(<IMG SRC="jav
ascript:alert('XSS');">),
|
||||
%(<IMG SRC="  javascript:alert('XSS');">),
|
||||
%(<IMG SRC=`javascript:alert("RSnake says, 'XSS'")`>)].each_with_index do |img_hack, i|
|
||||
define_method "test_should_not_fall_for_xss_image_hack_#{i+1}" do
|
||||
assert_sanitized img_hack, "<img>"
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_sanitize_tag_broken_up_by_null
|
||||
assert_sanitized %(<SCR\0IPT>alert(\"XSS\")</SCR\0IPT>), "alert(\"XSS\")"
|
||||
end
|
||||
|
||||
def test_should_sanitize_invalid_script_tag
|
||||
assert_sanitized %(<SCRIPT/XSS SRC="http://ha.ckers.org/xss.js"></SCRIPT>), ""
|
||||
end
|
||||
|
||||
def test_should_sanitize_script_tag_with_multiple_open_brackets
|
||||
assert_sanitized %(<<SCRIPT>alert("XSS");//<</SCRIPT>), "<"
|
||||
assert_sanitized %(<iframe src=http://ha.ckers.org/scriptlet.html\n<a), %(<a)
|
||||
end
|
||||
|
||||
def test_should_sanitize_unclosed_script
|
||||
assert_sanitized %(<SCRIPT SRC=http://ha.ckers.org/xss.js?<B>), "<b>"
|
||||
end
|
||||
|
||||
def test_should_sanitize_half_open_scripts
|
||||
assert_sanitized %(<IMG SRC="javascript:alert('XSS')"), "<img>"
|
||||
end
|
||||
|
||||
def test_should_not_fall_for_ridiculous_hack
|
||||
img_hack = %(<IMG\nSRC\n=\n"\nj\na\nv\na\ns\nc\nr\ni\np\nt\n:\na\nl\ne\nr\nt\n(\n'\nX\nS\nS\n'\n)\n"\n>)
|
||||
assert_sanitized img_hack, "<img>"
|
||||
end
|
||||
|
||||
# fucked
|
||||
def test_should_sanitize_attributes
|
||||
assert_sanitized %(<SPAN title="'><script>alert()</script>">blah</SPAN>), %(<span title="'><script>alert()</script>">blah</span>)
|
||||
end
|
||||
|
||||
def test_should_sanitize_illegal_style_properties
|
||||
raw = %(display:block; position:absolute; left:0; top:0; width:100%; height:100%; z-index:1; background-color:black; background-image:url(http://www.ragingplatypus.com/i/cam-full.jpg); background-x:center; background-y:center; background-repeat:repeat;)
|
||||
expected = %(display: block; width: 100%; height: 100%; background-color: black; background-image: ; background-x: center; background-y: center;)
|
||||
assert_equal expected, sanitize_css(raw)
|
||||
end
|
||||
|
||||
def test_should_sanitize_with_trailing_space
|
||||
raw = "display:block; "
|
||||
expected = "display: block;"
|
||||
assert_equal expected, sanitize_css(raw)
|
||||
end
|
||||
|
||||
def test_should_sanitize_xul_style_attributes
|
||||
raw = %(-moz-binding:url('http://ha.ckers.org/xssmoz.xml#xss'))
|
||||
assert_equal '', sanitize_css(raw)
|
||||
end
|
||||
|
||||
def test_should_sanitize_invalid_tag_names
|
||||
assert_sanitized(%(a b c<script/XSS src="http://ha.ckers.org/xss.js"></script>d e f), "a b cd e f")
|
||||
end
|
||||
|
||||
def test_should_sanitize_non_alpha_and_non_digit_characters_in_tags
|
||||
assert_sanitized('<a onclick!#$%&()*~+-_.,:;?@[/|\]^`=alert("XSS")>foo</a>', "<a>foo</a>")
|
||||
end
|
||||
|
||||
def test_should_sanitize_invalid_tag_names_in_single_tags
|
||||
assert_sanitized('<img/src="http://ha.ckers.org/xss.js"/>', "<img />")
|
||||
end
|
||||
|
||||
def test_should_sanitize_img_dynsrc_lowsrc
|
||||
assert_sanitized(%(<img lowsrc="javascript:alert('XSS')" />), "<img />")
|
||||
end
|
||||
|
||||
def test_should_sanitize_div_background_image_unicode_encoded
|
||||
raw = %(background-image:\0075\0072\006C\0028'\006a\0061\0076\0061\0073\0063\0072\0069\0070\0074\003a\0061\006c\0065\0072\0074\0028.1027\0058.1053\0053\0027\0029'\0029)
|
||||
assert_equal '', sanitize_css(raw)
|
||||
end
|
||||
|
||||
def test_should_sanitize_div_style_expression
|
||||
raw = %(width: expression(alert('XSS'));)
|
||||
assert_equal '', sanitize_css(raw)
|
||||
end
|
||||
|
||||
def test_should_sanitize_img_vbscript
|
||||
assert_sanitized %(<img src='vbscript:msgbox("XSS")' />), '<img />'
|
||||
end
|
||||
|
||||
protected
|
||||
def assert_sanitized(input, expected = nil)
|
||||
@sanitizer ||= HTML::WhiteListSanitizer.new
|
||||
if input
|
||||
assert_dom_equal expected || input, @sanitizer.sanitize(input)
|
||||
else
|
||||
assert_nil @sanitizer.sanitize(input)
|
||||
end
|
||||
end
|
||||
|
||||
def sanitize_css(input)
|
||||
(@sanitizer ||= HTML::WhiteListSanitizer.new).sanitize_css(input)
|
||||
end
|
||||
end
|
|
@ -1,238 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class TagNodeTest < Test::Unit::TestCase
|
||||
def test_open_without_attributes
|
||||
node = tag("<tag>")
|
||||
assert_equal "tag", node.name
|
||||
assert_equal Hash.new, node.attributes
|
||||
assert_nil node.closing
|
||||
end
|
||||
|
||||
def test_open_with_attributes
|
||||
node = tag("<TAG1 foo=hey_ho x:bar=\"blah blah\" BAZ='blah blah blah' >")
|
||||
assert_equal "tag1", node.name
|
||||
assert_equal "hey_ho", node["foo"]
|
||||
assert_equal "blah blah", node["x:bar"]
|
||||
assert_equal "blah blah blah", node["baz"]
|
||||
end
|
||||
|
||||
def test_self_closing_without_attributes
|
||||
node = tag("<tag/>")
|
||||
assert_equal "tag", node.name
|
||||
assert_equal Hash.new, node.attributes
|
||||
assert_equal :self, node.closing
|
||||
end
|
||||
|
||||
def test_self_closing_with_attributes
|
||||
node = tag("<tag a=b/>")
|
||||
assert_equal "tag", node.name
|
||||
assert_equal( { "a" => "b" }, node.attributes )
|
||||
assert_equal :self, node.closing
|
||||
end
|
||||
|
||||
def test_closing_without_attributes
|
||||
node = tag("</tag>")
|
||||
assert_equal "tag", node.name
|
||||
assert_nil node.attributes
|
||||
assert_equal :close, node.closing
|
||||
end
|
||||
|
||||
def test_bracket_op_when_no_attributes
|
||||
node = tag("</tag>")
|
||||
assert_nil node["foo"]
|
||||
end
|
||||
|
||||
def test_bracket_op_when_attributes
|
||||
node = tag("<tag a=b/>")
|
||||
assert_equal "b", node["a"]
|
||||
end
|
||||
|
||||
def test_attributes_with_escaped_quotes
|
||||
node = tag("<tag a='b\\'c' b=\"bob \\\"float\\\"\">")
|
||||
assert_equal "b\\'c", node["a"]
|
||||
assert_equal "bob \\\"float\\\"", node["b"]
|
||||
end
|
||||
|
||||
def test_to_s
|
||||
node = tag("<a b=c d='f' g=\"h 'i'\" />")
|
||||
assert_equal %(<a b='c' d='f' g='h \\'i\\'' />), node.to_s
|
||||
end
|
||||
|
||||
def test_tag
|
||||
assert tag("<tag>").tag?
|
||||
end
|
||||
|
||||
def test_match_tag_as_string
|
||||
assert tag("<tag>").match(:tag => "tag")
|
||||
assert !tag("<tag>").match(:tag => "b")
|
||||
end
|
||||
|
||||
def test_match_tag_as_regexp
|
||||
assert tag("<tag>").match(:tag => /t.g/)
|
||||
assert !tag("<tag>").match(:tag => /t[bqs]g/)
|
||||
end
|
||||
|
||||
def test_match_attributes_as_string
|
||||
t = tag("<tag a=something b=else />")
|
||||
assert t.match(:attributes => {"a" => "something"})
|
||||
assert t.match(:attributes => {"b" => "else"})
|
||||
end
|
||||
|
||||
def test_match_attributes_as_regexp
|
||||
t = tag("<tag a=something b=else />")
|
||||
assert t.match(:attributes => {"a" => /^something$/})
|
||||
assert t.match(:attributes => {"b" => /e.*e/})
|
||||
assert t.match(:attributes => {"a" => /me..i/, "b" => /.ls.$/})
|
||||
end
|
||||
|
||||
def test_match_attributes_as_number
|
||||
t = tag("<tag a=15 b=3.1415 />")
|
||||
assert t.match(:attributes => {"a" => 15})
|
||||
assert t.match(:attributes => {"b" => 3.1415})
|
||||
assert t.match(:attributes => {"a" => 15, "b" => 3.1415})
|
||||
end
|
||||
|
||||
def test_match_attributes_exist
|
||||
t = tag("<tag a=15 b=3.1415 />")
|
||||
assert t.match(:attributes => {"a" => true})
|
||||
assert t.match(:attributes => {"b" => true})
|
||||
assert t.match(:attributes => {"a" => true, "b" => true})
|
||||
end
|
||||
|
||||
def test_match_attributes_not_exist
|
||||
t = tag("<tag a=15 b=3.1415 />")
|
||||
assert t.match(:attributes => {"c" => false})
|
||||
assert t.match(:attributes => {"c" => nil})
|
||||
assert t.match(:attributes => {"a" => true, "c" => false})
|
||||
end
|
||||
|
||||
def test_match_parent_success
|
||||
t = tag("<tag a=15 b='hello'>", tag("<foo k='value'>"))
|
||||
assert t.match(:parent => {:tag => "foo", :attributes => {"k" => /v.l/, "j" => false}})
|
||||
end
|
||||
|
||||
def test_match_parent_fail
|
||||
t = tag("<tag a=15 b='hello'>", tag("<foo k='value'>"))
|
||||
assert !t.match(:parent => {:tag => /kafka/})
|
||||
end
|
||||
|
||||
def test_match_child_success
|
||||
t = tag("<tag x:k='something'>")
|
||||
tag("<child v=john a=kelly>", t)
|
||||
tag("<sib m=vaughn v=james>", t)
|
||||
assert t.match(:child => { :tag => "sib", :attributes => {"v" => /j/}})
|
||||
assert t.match(:child => { :attributes => {"a" => "kelly"}})
|
||||
end
|
||||
|
||||
def test_match_child_fail
|
||||
t = tag("<tag x:k='something'>")
|
||||
tag("<child v=john a=kelly>", t)
|
||||
tag("<sib m=vaughn v=james>", t)
|
||||
assert !t.match(:child => { :tag => "sib", :attributes => {"v" => /r/}})
|
||||
assert !t.match(:child => { :attributes => {"v" => false}})
|
||||
end
|
||||
|
||||
def test_match_ancestor_success
|
||||
t = tag("<tag x:k='something'>", tag("<parent v=john a=kelly>", tag("<grandparent m=vaughn v=james>")))
|
||||
assert t.match(:ancestor => {:tag => "parent", :attributes => {"a" => /ll/}})
|
||||
assert t.match(:ancestor => {:attributes => {"m" => "vaughn"}})
|
||||
end
|
||||
|
||||
def test_match_ancestor_fail
|
||||
t = tag("<tag x:k='something'>", tag("<parent v=john a=kelly>", tag("<grandparent m=vaughn v=james>")))
|
||||
assert !t.match(:ancestor => {:tag => /^parent/, :attributes => {"v" => /m/}})
|
||||
assert !t.match(:ancestor => {:attributes => {"v" => false}})
|
||||
end
|
||||
|
||||
def test_match_descendant_success
|
||||
tag("<grandchild m=vaughn v=james>", tag("<child v=john a=kelly>", t = tag("<tag x:k='something'>")))
|
||||
assert t.match(:descendant => {:tag => "child", :attributes => {"a" => /ll/}})
|
||||
assert t.match(:descendant => {:attributes => {"m" => "vaughn"}})
|
||||
end
|
||||
|
||||
def test_match_descendant_fail
|
||||
tag("<grandchild m=vaughn v=james>", tag("<child v=john a=kelly>", t = tag("<tag x:k='something'>")))
|
||||
assert !t.match(:descendant => {:tag => /^child/, :attributes => {"v" => /m/}})
|
||||
assert !t.match(:descendant => {:attributes => {"v" => false}})
|
||||
end
|
||||
|
||||
def test_match_child_count
|
||||
t = tag("<tag x:k='something'>")
|
||||
tag("hello", t)
|
||||
tag("<child v=john a=kelly>", t)
|
||||
tag("<sib m=vaughn v=james>", t)
|
||||
assert t.match(:children => { :count => 2 })
|
||||
assert t.match(:children => { :count => 2..4 })
|
||||
assert t.match(:children => { :less_than => 4 })
|
||||
assert t.match(:children => { :greater_than => 1 })
|
||||
assert !t.match(:children => { :count => 3 })
|
||||
end
|
||||
|
||||
def test_conditions_as_strings
|
||||
t = tag("<tag x:k='something'>")
|
||||
assert t.match("tag" => "tag")
|
||||
assert t.match("attributes" => { "x:k" => "something" })
|
||||
assert !t.match("tag" => "gat")
|
||||
assert !t.match("attributes" => { "x:j" => "something" })
|
||||
end
|
||||
|
||||
def test_attributes_as_symbols
|
||||
t = tag("<child v=john a=kelly>")
|
||||
assert t.match(:attributes => { :v => /oh/ })
|
||||
assert t.match(:attributes => { :a => /ll/ })
|
||||
end
|
||||
|
||||
def test_match_sibling
|
||||
t = tag("<tag x:k='something'>")
|
||||
tag("hello", t)
|
||||
tag("<span a=b>", t)
|
||||
tag("world", t)
|
||||
m = tag("<span k=r>", t)
|
||||
tag("<span m=l>", t)
|
||||
|
||||
assert m.match(:sibling => {:tag => "span", :attributes => {:a => true}})
|
||||
assert m.match(:sibling => {:tag => "span", :attributes => {:m => true}})
|
||||
assert !m.match(:sibling => {:tag => "span", :attributes => {:k => true}})
|
||||
end
|
||||
|
||||
def test_match_sibling_before
|
||||
t = tag("<tag x:k='something'>")
|
||||
tag("hello", t)
|
||||
tag("<span a=b>", t)
|
||||
tag("world", t)
|
||||
m = tag("<span k=r>", t)
|
||||
tag("<span m=l>", t)
|
||||
|
||||
assert m.match(:before => {:tag => "span", :attributes => {:m => true}})
|
||||
assert !m.match(:before => {:tag => "span", :attributes => {:a => true}})
|
||||
assert !m.match(:before => {:tag => "span", :attributes => {:k => true}})
|
||||
end
|
||||
|
||||
def test_match_sibling_after
|
||||
t = tag("<tag x:k='something'>")
|
||||
tag("hello", t)
|
||||
tag("<span a=b>", t)
|
||||
tag("world", t)
|
||||
m = tag("<span k=r>", t)
|
||||
tag("<span m=l>", t)
|
||||
|
||||
assert m.match(:after => {:tag => "span", :attributes => {:a => true}})
|
||||
assert !m.match(:after => {:tag => "span", :attributes => {:m => true}})
|
||||
assert !m.match(:after => {:tag => "span", :attributes => {:k => true}})
|
||||
end
|
||||
|
||||
def test_to_s
|
||||
t = tag("<b x='foo'>")
|
||||
tag("hello", t)
|
||||
tag("<hr />", t)
|
||||
assert_equal %(<b x="foo">hello<hr /></b>), t.to_s
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def tag(content, parent=nil)
|
||||
node = HTML::Node.parse(parent,0,0,content)
|
||||
parent.children << node if parent
|
||||
node
|
||||
end
|
||||
end
|
|
@ -1,50 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class TextNodeTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@node = HTML::Text.new(nil, 0, 0, "hello, howdy, aloha, annyeong")
|
||||
end
|
||||
|
||||
def test_to_s
|
||||
assert_equal "hello, howdy, aloha, annyeong", @node.to_s
|
||||
end
|
||||
|
||||
def test_find_string
|
||||
assert_equal @node, @node.find("hello, howdy, aloha, annyeong")
|
||||
assert_equal false, @node.find("bogus")
|
||||
end
|
||||
|
||||
def test_find_regexp
|
||||
assert_equal @node, @node.find(/an+y/)
|
||||
assert_nil @node.find(/b/)
|
||||
end
|
||||
|
||||
def test_find_hash
|
||||
assert_equal @node, @node.find(:content => /howdy/)
|
||||
assert_nil @node.find(:content => /^howdy$/)
|
||||
assert_equal false, @node.find(:content => "howdy")
|
||||
end
|
||||
|
||||
def test_find_other
|
||||
assert_nil @node.find(:hello)
|
||||
end
|
||||
|
||||
def test_match_string
|
||||
assert @node.match("hello, howdy, aloha, annyeong")
|
||||
assert_equal false, @node.match("bogus")
|
||||
end
|
||||
|
||||
def test_match_regexp
|
||||
assert_not_nil @node, @node.match(/an+y/)
|
||||
assert_nil @node.match(/b/)
|
||||
end
|
||||
|
||||
def test_match_hash
|
||||
assert_not_nil @node, @node.match(:content => "howdy")
|
||||
assert_nil @node.match(:content => /^howdy$/)
|
||||
end
|
||||
|
||||
def test_match_other
|
||||
assert_nil @node.match(:hello)
|
||||
end
|
||||
end
|
|
@ -1,131 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class TokenizerTest < Test::Unit::TestCase
|
||||
|
||||
def test_blank
|
||||
tokenize ""
|
||||
assert_end
|
||||
end
|
||||
|
||||
def test_space
|
||||
tokenize " "
|
||||
assert_next " "
|
||||
assert_end
|
||||
end
|
||||
|
||||
def test_tag_simple_open
|
||||
tokenize "<tag>"
|
||||
assert_next "<tag>"
|
||||
assert_end
|
||||
end
|
||||
|
||||
def test_tag_simple_self_closing
|
||||
tokenize "<tag />"
|
||||
assert_next "<tag />"
|
||||
assert_end
|
||||
end
|
||||
|
||||
def test_tag_simple_closing
|
||||
tokenize "</tag>"
|
||||
assert_next "</tag>"
|
||||
end
|
||||
|
||||
def test_tag_with_single_quoted_attribute
|
||||
tokenize %{<tag a='hello'>x}
|
||||
assert_next %{<tag a='hello'>}
|
||||
end
|
||||
|
||||
def test_tag_with_single_quoted_attribute_with_escape
|
||||
tokenize %{<tag a='hello\\''>x}
|
||||
assert_next %{<tag a='hello\\''>}
|
||||
end
|
||||
|
||||
def test_tag_with_double_quoted_attribute
|
||||
tokenize %{<tag a="hello">x}
|
||||
assert_next %{<tag a="hello">}
|
||||
end
|
||||
|
||||
def test_tag_with_double_quoted_attribute_with_escape
|
||||
tokenize %{<tag a="hello\\"">x}
|
||||
assert_next %{<tag a="hello\\"">}
|
||||
end
|
||||
|
||||
def test_tag_with_unquoted_attribute
|
||||
tokenize %{<tag a=hello>x}
|
||||
assert_next %{<tag a=hello>}
|
||||
end
|
||||
|
||||
def test_tag_with_lt_char_in_attribute
|
||||
tokenize %{<tag a="x < y">x}
|
||||
assert_next %{<tag a="x < y">}
|
||||
end
|
||||
|
||||
def test_tag_with_gt_char_in_attribute
|
||||
tokenize %{<tag a="x > y">x}
|
||||
assert_next %{<tag a="x > y">}
|
||||
end
|
||||
|
||||
def test_doctype_tag
|
||||
tokenize %{<!DOCTYPE "blah" "blah" "blah">\n <html>}
|
||||
assert_next %{<!DOCTYPE "blah" "blah" "blah">}
|
||||
assert_next %{\n }
|
||||
assert_next %{<html>}
|
||||
end
|
||||
|
||||
def test_cdata_tag
|
||||
tokenize %{<![CDATA[<br>]]>}
|
||||
assert_next %{<![CDATA[<br>]]>}
|
||||
assert_end
|
||||
end
|
||||
|
||||
def test_unterminated_cdata_tag
|
||||
tokenize %{<content:encoded><![CDATA[ neverending...}
|
||||
assert_next %{<content:encoded>}
|
||||
assert_next %{<![CDATA[ neverending...}
|
||||
assert_end
|
||||
end
|
||||
|
||||
def test_less_than_with_space
|
||||
tokenize %{original < hello > world}
|
||||
assert_next %{original }
|
||||
assert_next %{< hello > world}
|
||||
end
|
||||
|
||||
def test_less_than_without_matching_greater_than
|
||||
tokenize %{hello <span onmouseover="gotcha"\n<b>foo</b>\nbar</span>}
|
||||
assert_next %{hello }
|
||||
assert_next %{<span onmouseover="gotcha"\n}
|
||||
assert_next %{<b>}
|
||||
assert_next %{foo}
|
||||
assert_next %{</b>}
|
||||
assert_next %{\nbar}
|
||||
assert_next %{</span>}
|
||||
assert_end
|
||||
end
|
||||
|
||||
def test_unterminated_comment
|
||||
tokenize %{hello <!-- neverending...}
|
||||
assert_next %{hello }
|
||||
assert_next %{<!-- neverending...}
|
||||
assert_end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def tokenize(text)
|
||||
@tokenizer = HTML::Tokenizer.new(text)
|
||||
end
|
||||
|
||||
def assert_next(expected, message=nil)
|
||||
token = @tokenizer.next
|
||||
assert_equal expected, token, message
|
||||
end
|
||||
|
||||
def assert_sequence(*expected)
|
||||
assert_next expected.shift until expected.empty?
|
||||
end
|
||||
|
||||
def assert_end(message=nil)
|
||||
assert_nil @tokenizer.next, message
|
||||
end
|
||||
end
|
|
@ -1,54 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class HttpBasicAuthenticationTest < Test::Unit::TestCase
|
||||
include ActionController::HttpAuthentication::Basic
|
||||
|
||||
class DummyController
|
||||
attr_accessor :headers, :renders, :request
|
||||
|
||||
def initialize
|
||||
@headers, @renders = {}, []
|
||||
@request = ActionController::TestRequest.new
|
||||
end
|
||||
|
||||
def render(options)
|
||||
self.renders << options
|
||||
end
|
||||
end
|
||||
|
||||
def setup
|
||||
@controller = DummyController.new
|
||||
@credentials = ActionController::HttpAuthentication::Basic.encode_credentials("dhh", "secret")
|
||||
end
|
||||
|
||||
def test_successful_authentication
|
||||
login = Proc.new { |user_name, password| user_name == "dhh" && password == "secret" }
|
||||
set_headers
|
||||
assert authenticate(@controller, &login)
|
||||
|
||||
set_headers ''
|
||||
assert_nothing_raised do
|
||||
assert !authenticate(@controller, &login)
|
||||
end
|
||||
|
||||
set_headers nil
|
||||
set_headers @credentials, 'REDIRECT_X_HTTP_AUTHORIZATION'
|
||||
assert authenticate(@controller, &login)
|
||||
end
|
||||
|
||||
def test_failing_authentication
|
||||
set_headers
|
||||
assert !authenticate(@controller) { |user_name, password| user_name == "dhh" && password == "incorrect" }
|
||||
end
|
||||
|
||||
def test_authentication_request
|
||||
authentication_request(@controller, "Megaglobalapp")
|
||||
assert_equal 'Basic realm="Megaglobalapp"', @controller.headers["WWW-Authenticate"]
|
||||
assert_equal :unauthorized, @controller.renders.first[:status]
|
||||
end
|
||||
|
||||
private
|
||||
def set_headers(value = @credentials, name = 'HTTP_AUTHORIZATION')
|
||||
@controller.request.env[name] = value
|
||||
end
|
||||
end
|
|
@ -1,252 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
require 'action_controller/integration'
|
||||
|
||||
uses_mocha 'integration' do
|
||||
|
||||
module IntegrationSessionStubbing
|
||||
def stub_integration_session(session)
|
||||
session.stubs(:process)
|
||||
session.stubs(:generic_url_rewriter)
|
||||
end
|
||||
end
|
||||
|
||||
class SessionTest < Test::Unit::TestCase
|
||||
include IntegrationSessionStubbing
|
||||
|
||||
def setup
|
||||
@session = ActionController::Integration::Session.new
|
||||
stub_integration_session(@session)
|
||||
end
|
||||
|
||||
def test_https_bang_works_and_sets_truth_by_default
|
||||
assert !@session.https?
|
||||
@session.https!
|
||||
assert @session.https?
|
||||
@session.https! false
|
||||
assert !@session.https?
|
||||
end
|
||||
|
||||
def test_host!
|
||||
assert_not_equal "glu.ttono.us", @session.host
|
||||
@session.host! "rubyonrails.com"
|
||||
assert_equal "rubyonrails.com", @session.host
|
||||
end
|
||||
|
||||
def test_follow_redirect_raises_when_no_redirect
|
||||
@session.stubs(:redirect?).returns(false)
|
||||
assert_raise(RuntimeError) { @session.follow_redirect! }
|
||||
end
|
||||
|
||||
def test_follow_redirect_calls_get_and_returns_status
|
||||
@session.stubs(:redirect?).returns(true)
|
||||
@session.stubs(:headers).returns({"location" => ["www.google.com"]})
|
||||
@session.stubs(:status).returns(200)
|
||||
@session.expects(:get)
|
||||
assert_equal 200, @session.follow_redirect!
|
||||
end
|
||||
|
||||
def test_request_via_redirect_uses_given_method
|
||||
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"}
|
||||
@session.expects(:put).with(path, args, headers)
|
||||
@session.stubs(:redirect?).returns(false)
|
||||
@session.request_via_redirect(:put, path, args, headers)
|
||||
end
|
||||
|
||||
def test_request_via_redirect_follows_redirects
|
||||
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"}
|
||||
@session.stubs(:redirect?).returns(true, true, false)
|
||||
@session.expects(:follow_redirect!).times(2)
|
||||
@session.request_via_redirect(:get, path, args, headers)
|
||||
end
|
||||
|
||||
def test_request_via_redirect_returns_status
|
||||
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"}
|
||||
@session.stubs(:redirect?).returns(false)
|
||||
@session.stubs(:status).returns(200)
|
||||
assert_equal 200, @session.request_via_redirect(:get, path, args, headers)
|
||||
end
|
||||
|
||||
def test_get_via_redirect
|
||||
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
|
||||
@session.expects(:request_via_redirect).with(:get, path, args, headers)
|
||||
@session.get_via_redirect(path, args, headers)
|
||||
end
|
||||
|
||||
def test_post_via_redirect
|
||||
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
|
||||
@session.expects(:request_via_redirect).with(:post, path, args, headers)
|
||||
@session.post_via_redirect(path, args, headers)
|
||||
end
|
||||
|
||||
def test_put_via_redirect
|
||||
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
|
||||
@session.expects(:request_via_redirect).with(:put, path, args, headers)
|
||||
@session.put_via_redirect(path, args, headers)
|
||||
end
|
||||
|
||||
def test_delete_via_redirect
|
||||
path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
|
||||
@session.expects(:request_via_redirect).with(:delete, path, args, headers)
|
||||
@session.delete_via_redirect(path, args, headers)
|
||||
end
|
||||
|
||||
def test_url_for_with_controller
|
||||
options = {:action => 'show'}
|
||||
mock_controller = mock()
|
||||
mock_controller.expects(:url_for).with(options).returns('/show')
|
||||
@session.stubs(:controller).returns(mock_controller)
|
||||
assert_equal '/show', @session.url_for(options)
|
||||
end
|
||||
|
||||
def test_url_for_without_controller
|
||||
options = {:action => 'show'}
|
||||
mock_rewriter = mock()
|
||||
mock_rewriter.expects(:rewrite).with(options).returns('/show')
|
||||
@session.stubs(:generic_url_rewriter).returns(mock_rewriter)
|
||||
@session.stubs(:controller).returns(nil)
|
||||
assert_equal '/show', @session.url_for(options)
|
||||
end
|
||||
|
||||
def test_redirect_bool_with_status_in_300s
|
||||
@session.stubs(:status).returns 301
|
||||
assert @session.redirect?
|
||||
end
|
||||
|
||||
def test_redirect_bool_with_status_in_200s
|
||||
@session.stubs(:status).returns 200
|
||||
assert !@session.redirect?
|
||||
end
|
||||
|
||||
def test_get
|
||||
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
||||
@session.expects(:process).with(:get,path,params,headers)
|
||||
@session.get(path,params,headers)
|
||||
end
|
||||
|
||||
def test_post
|
||||
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
||||
@session.expects(:process).with(:post,path,params,headers)
|
||||
@session.post(path,params,headers)
|
||||
end
|
||||
|
||||
def test_put
|
||||
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
||||
@session.expects(:process).with(:put,path,params,headers)
|
||||
@session.put(path,params,headers)
|
||||
end
|
||||
|
||||
def test_delete
|
||||
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
||||
@session.expects(:process).with(:delete,path,params,headers)
|
||||
@session.delete(path,params,headers)
|
||||
end
|
||||
|
||||
def test_head
|
||||
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
||||
@session.expects(:process).with(:head,path,params,headers)
|
||||
@session.head(path,params,headers)
|
||||
end
|
||||
|
||||
def test_xml_http_request_get
|
||||
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
||||
headers_after_xhr = headers.merge(
|
||||
"X-Requested-With" => "XMLHttpRequest",
|
||||
"Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
|
||||
)
|
||||
@session.expects(:process).with(:get,path,params,headers_after_xhr)
|
||||
@session.xml_http_request(:get,path,params,headers)
|
||||
end
|
||||
|
||||
def test_xml_http_request_post
|
||||
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
||||
headers_after_xhr = headers.merge(
|
||||
"X-Requested-With" => "XMLHttpRequest",
|
||||
"Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
|
||||
)
|
||||
@session.expects(:process).with(:post,path,params,headers_after_xhr)
|
||||
@session.xml_http_request(:post,path,params,headers)
|
||||
end
|
||||
|
||||
def test_xml_http_request_put
|
||||
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
||||
headers_after_xhr = headers.merge(
|
||||
"X-Requested-With" => "XMLHttpRequest",
|
||||
"Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
|
||||
)
|
||||
@session.expects(:process).with(:put,path,params,headers_after_xhr)
|
||||
@session.xml_http_request(:put,path,params,headers)
|
||||
end
|
||||
|
||||
def test_xml_http_request_delete
|
||||
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
||||
headers_after_xhr = headers.merge(
|
||||
"X-Requested-With" => "XMLHttpRequest",
|
||||
"Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
|
||||
)
|
||||
@session.expects(:process).with(:delete,path,params,headers_after_xhr)
|
||||
@session.xml_http_request(:delete,path,params,headers)
|
||||
end
|
||||
|
||||
def test_xml_http_request_head
|
||||
path = "/index"; params = "blah"; headers = {:location => 'blah'}
|
||||
headers_after_xhr = headers.merge(
|
||||
"X-Requested-With" => "XMLHttpRequest",
|
||||
"Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
|
||||
)
|
||||
@session.expects(:process).with(:head,path,params,headers_after_xhr)
|
||||
@session.xml_http_request(:head,path,params,headers)
|
||||
end
|
||||
|
||||
def test_xml_http_request_override_accept
|
||||
path = "/index"; params = "blah"; headers = {:location => 'blah', "Accept" => "application/xml"}
|
||||
headers_after_xhr = headers.merge(
|
||||
"X-Requested-With" => "XMLHttpRequest"
|
||||
)
|
||||
@session.expects(:process).with(:post,path,params,headers_after_xhr)
|
||||
@session.xml_http_request(:post,path,params,headers)
|
||||
end
|
||||
end
|
||||
|
||||
class IntegrationTestTest < Test::Unit::TestCase
|
||||
include IntegrationSessionStubbing
|
||||
|
||||
def setup
|
||||
@test = ::ActionController::IntegrationTest.new(:default_test)
|
||||
@test.class.stubs(:fixture_table_names).returns([])
|
||||
@session = @test.open_session
|
||||
stub_integration_session(@session)
|
||||
end
|
||||
|
||||
def test_opens_new_session
|
||||
@test.class.expects(:fixture_table_names).times(2).returns(['foo'])
|
||||
|
||||
session1 = @test.open_session { |sess| }
|
||||
session2 = @test.open_session # implicit session
|
||||
|
||||
assert_equal ::ActionController::Integration::Session, session1.class
|
||||
assert_equal ::ActionController::Integration::Session, session2.class
|
||||
assert_not_equal session1, session2
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# Tests that integration tests don't call Controller test methods for processing.
|
||||
# Integration tests have their own setup and teardown.
|
||||
class IntegrationTestUsesCorrectClass < ActionController::IntegrationTest
|
||||
include IntegrationSessionStubbing
|
||||
|
||||
def self.fixture_table_names
|
||||
[]
|
||||
end
|
||||
|
||||
def test_integration_methods_called
|
||||
reset!
|
||||
stub_integration_session(@integration_session)
|
||||
%w( get post head put delete ).each do |verb|
|
||||
assert_nothing_raised("'#{verb}' should use integration test methods") { send!(verb, '/') }
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end # uses_mocha
|
|
@ -1,43 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
require 'action_controller/integration'
|
||||
require 'action_controller/routing'
|
||||
|
||||
unless defined? ApplicationController
|
||||
class ApplicationController < ActionController::Base
|
||||
end
|
||||
end
|
||||
|
||||
class UploadTestController < ActionController::Base
|
||||
session :off
|
||||
|
||||
def update
|
||||
SessionUploadTest.last_request_type = ActionController::Base.param_parsers[request.content_type]
|
||||
render :text => "got here"
|
||||
end
|
||||
end
|
||||
|
||||
class SessionUploadTest < ActionController::IntegrationTest
|
||||
FILES_DIR = File.dirname(__FILE__) + '/../fixtures/multipart'
|
||||
|
||||
class << self
|
||||
attr_accessor :last_request_type
|
||||
end
|
||||
|
||||
# def setup
|
||||
# @session = ActionController::Integration::Session.new
|
||||
# end
|
||||
def test_post_with_upload
|
||||
uses_mocha "test_post_with_upload" do
|
||||
ActiveSupport::Dependencies.stubs(:load?).returns(false)
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.update 'update', :controller => "upload_test", :action => "update", :method => :post
|
||||
end
|
||||
|
||||
params = { :uploaded_data => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg") }
|
||||
post '/update', params, :location => 'blah'
|
||||
assert_equal(:multipart_form, SessionUploadTest.last_request_type)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,259 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
# The view_paths array must be set on Base and not LayoutTest so that LayoutTest's inherited
|
||||
# method has access to the view_paths array when looking for a layout to automatically assign.
|
||||
old_load_paths = ActionController::Base.view_paths
|
||||
ActionController::Base.view_paths = [ File.dirname(__FILE__) + '/../fixtures/layout_tests/' ]
|
||||
|
||||
class LayoutTest < ActionController::Base
|
||||
def self.controller_path; 'views' end
|
||||
self.view_paths = ActionController::Base.view_paths.dup
|
||||
end
|
||||
|
||||
# Restore view_paths to previous value
|
||||
ActionController::Base.view_paths = old_load_paths
|
||||
|
||||
class ProductController < LayoutTest
|
||||
end
|
||||
|
||||
class ItemController < LayoutTest
|
||||
end
|
||||
|
||||
class ThirdPartyTemplateLibraryController < LayoutTest
|
||||
end
|
||||
|
||||
module ControllerNameSpace
|
||||
end
|
||||
|
||||
class ControllerNameSpace::NestedController < LayoutTest
|
||||
end
|
||||
|
||||
class MultipleExtensions < LayoutTest
|
||||
end
|
||||
|
||||
class MabView < ActionView::TemplateHandler
|
||||
def initialize(view)
|
||||
end
|
||||
|
||||
def render(template)
|
||||
template.source
|
||||
end
|
||||
end
|
||||
|
||||
ActionView::Template::register_template_handler :mab, MabView
|
||||
|
||||
class LayoutAutoDiscoveryTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
|
||||
@request.host = "www.nextangle.com"
|
||||
end
|
||||
|
||||
def test_application_layout_is_default_when_no_controller_match
|
||||
@controller = ProductController.new
|
||||
get :hello
|
||||
assert_equal 'layout_test.rhtml hello.rhtml', @response.body
|
||||
end
|
||||
|
||||
def test_controller_name_layout_name_match
|
||||
@controller = ItemController.new
|
||||
get :hello
|
||||
assert_equal 'item.rhtml hello.rhtml', @response.body
|
||||
end
|
||||
|
||||
def test_third_party_template_library_auto_discovers_layout
|
||||
@controller = ThirdPartyTemplateLibraryController.new
|
||||
get :hello
|
||||
assert_equal 'layouts/third_party_template_library', @controller.active_layout
|
||||
assert_equal 'layouts/third_party_template_library', @response.layout
|
||||
assert_response :success
|
||||
assert_equal 'Mab', @response.body
|
||||
end
|
||||
|
||||
def test_namespaced_controllers_auto_detect_layouts
|
||||
@controller = ControllerNameSpace::NestedController.new
|
||||
get :hello
|
||||
assert_equal 'layouts/controller_name_space/nested', @controller.active_layout
|
||||
assert_equal 'controller_name_space/nested.rhtml hello.rhtml', @response.body
|
||||
end
|
||||
|
||||
def test_namespaced_controllers_auto_detect_layouts
|
||||
@controller = MultipleExtensions.new
|
||||
get :hello
|
||||
assert_equal 'layouts/multiple_extensions', @controller.active_layout
|
||||
assert_equal 'multiple_extensions.html.erb hello.rhtml', @response.body.strip
|
||||
end
|
||||
end
|
||||
|
||||
class ExemptFromLayoutTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = LayoutTest.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
def test_rjs_exempt_from_layout
|
||||
assert @controller.send!(:template_exempt_from_layout?, 'test.rjs')
|
||||
end
|
||||
|
||||
def test_rhtml_and_rxml_not_exempt_from_layout
|
||||
assert !@controller.send!(:template_exempt_from_layout?, 'test.rhtml')
|
||||
assert !@controller.send!(:template_exempt_from_layout?, 'test.rxml')
|
||||
end
|
||||
|
||||
def test_other_extension_not_exempt_from_layout
|
||||
assert !@controller.send!(:template_exempt_from_layout?, 'test.random')
|
||||
end
|
||||
|
||||
def test_add_extension_to_exempt_from_layout
|
||||
['rpdf', :rpdf].each do |ext|
|
||||
assert_nothing_raised do
|
||||
ActionController::Base.exempt_from_layout ext
|
||||
end
|
||||
assert @controller.send!(:template_exempt_from_layout?, "test.#{ext}")
|
||||
end
|
||||
end
|
||||
|
||||
def test_add_regexp_to_exempt_from_layout
|
||||
ActionController::Base.exempt_from_layout /\.rdoc/
|
||||
assert @controller.send!(:template_exempt_from_layout?, 'test.rdoc')
|
||||
end
|
||||
|
||||
def test_rhtml_exempt_from_layout_status_should_prevent_layout_render
|
||||
ActionController::Base.exempt_from_layout :rhtml
|
||||
|
||||
assert @controller.send!(:template_exempt_from_layout?, 'test.rhtml')
|
||||
assert @controller.send!(:template_exempt_from_layout?, 'hello.rhtml')
|
||||
|
||||
get :hello
|
||||
assert_equal 'hello.rhtml', @response.body
|
||||
ActionController::Base.exempt_from_layout.delete(/\.rhtml$/)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class DefaultLayoutController < LayoutTest
|
||||
end
|
||||
|
||||
class HasOwnLayoutController < LayoutTest
|
||||
layout 'item'
|
||||
end
|
||||
|
||||
class SetsLayoutInRenderController < LayoutTest
|
||||
def hello
|
||||
render :layout => 'third_party_template_library'
|
||||
end
|
||||
end
|
||||
|
||||
class RendersNoLayoutController < LayoutTest
|
||||
def hello
|
||||
render :layout => false
|
||||
end
|
||||
end
|
||||
|
||||
class LayoutSetInResponseTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
def test_layout_set_when_using_default_layout
|
||||
@controller = DefaultLayoutController.new
|
||||
get :hello
|
||||
assert_equal 'layouts/layout_test', @response.layout
|
||||
end
|
||||
|
||||
def test_layout_set_when_set_in_controller
|
||||
@controller = HasOwnLayoutController.new
|
||||
get :hello
|
||||
assert_equal 'layouts/item', @response.layout
|
||||
end
|
||||
|
||||
def test_layout_set_when_using_render
|
||||
@controller = SetsLayoutInRenderController.new
|
||||
get :hello
|
||||
assert_equal 'layouts/third_party_template_library', @response.layout
|
||||
end
|
||||
|
||||
def test_layout_is_not_set_when_none_rendered
|
||||
@controller = RendersNoLayoutController.new
|
||||
get :hello
|
||||
assert_nil @response.layout
|
||||
end
|
||||
|
||||
def test_exempt_from_layout_honored_by_render_template
|
||||
ActionController::Base.exempt_from_layout :rhtml
|
||||
@controller = RenderWithTemplateOptionController.new
|
||||
|
||||
assert @controller.send(:template_exempt_from_layout?, 'alt/hello.rhtml')
|
||||
|
||||
get :hello
|
||||
assert_equal "alt/hello.rhtml", @response.body.strip
|
||||
|
||||
ensure
|
||||
ActionController::Base.exempt_from_layout.delete(/\.rhtml$/)
|
||||
end
|
||||
end
|
||||
|
||||
class RenderWithTemplateOptionController < LayoutTest
|
||||
def hello
|
||||
render :template => 'alt/hello'
|
||||
end
|
||||
end
|
||||
|
||||
class SetsNonExistentLayoutFile < LayoutTest
|
||||
layout "nofile.rhtml"
|
||||
end
|
||||
|
||||
class LayoutExceptionRaised < Test::Unit::TestCase
|
||||
def setup
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
def test_exception_raised_when_layout_file_not_found
|
||||
@controller = SetsNonExistentLayoutFile.new
|
||||
get :hello
|
||||
@response.template.class.module_eval { attr_accessor :exception }
|
||||
assert_equal ActionView::MissingTemplate, @response.template.exception.class
|
||||
end
|
||||
end
|
||||
|
||||
class LayoutStatusIsRendered < LayoutTest
|
||||
def hello
|
||||
render :status => 401
|
||||
end
|
||||
end
|
||||
|
||||
class LayoutStatusIsRenderedTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
def test_layout_status_is_rendered
|
||||
@controller = LayoutStatusIsRendered.new
|
||||
get :hello
|
||||
assert_response 401
|
||||
end
|
||||
end
|
||||
|
||||
class LayoutSymlinkedTest < LayoutTest
|
||||
layout "symlinked/symlinked_layout"
|
||||
end
|
||||
|
||||
class LayoutSymlinkedIsRenderedTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
def test_symlinked_layout_is_rendered
|
||||
@controller = LayoutSymlinkedTest.new
|
||||
get :hello
|
||||
assert_response 200
|
||||
assert_equal "layouts/symlinked/symlinked_layout", @response.layout
|
||||
end
|
||||
end
|
||||
|
|
@ -1,538 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class RespondToController < ActionController::Base
|
||||
layout :set_layout
|
||||
|
||||
def html_xml_or_rss
|
||||
respond_to do |type|
|
||||
type.html { render :text => "HTML" }
|
||||
type.xml { render :text => "XML" }
|
||||
type.rss { render :text => "RSS" }
|
||||
type.all { render :text => "Nothing" }
|
||||
end
|
||||
end
|
||||
|
||||
def js_or_html
|
||||
respond_to do |type|
|
||||
type.html { render :text => "HTML" }
|
||||
type.js { render :text => "JS" }
|
||||
type.all { render :text => "Nothing" }
|
||||
end
|
||||
end
|
||||
|
||||
def json_or_yaml
|
||||
respond_to do |type|
|
||||
type.json { render :text => "JSON" }
|
||||
type.yaml { render :text => "YAML" }
|
||||
end
|
||||
end
|
||||
|
||||
def html_or_xml
|
||||
respond_to do |type|
|
||||
type.html { render :text => "HTML" }
|
||||
type.xml { render :text => "XML" }
|
||||
type.all { render :text => "Nothing" }
|
||||
end
|
||||
end
|
||||
|
||||
def forced_xml
|
||||
request.format = :xml
|
||||
|
||||
respond_to do |type|
|
||||
type.html { render :text => "HTML" }
|
||||
type.xml { render :text => "XML" }
|
||||
end
|
||||
end
|
||||
|
||||
def just_xml
|
||||
respond_to do |type|
|
||||
type.xml { render :text => "XML" }
|
||||
end
|
||||
end
|
||||
|
||||
def using_defaults
|
||||
respond_to do |type|
|
||||
type.html
|
||||
type.js
|
||||
type.xml
|
||||
end
|
||||
end
|
||||
|
||||
def using_defaults_with_type_list
|
||||
respond_to(:html, :js, :xml)
|
||||
end
|
||||
|
||||
def made_for_content_type
|
||||
respond_to do |type|
|
||||
type.rss { render :text => "RSS" }
|
||||
type.atom { render :text => "ATOM" }
|
||||
type.all { render :text => "Nothing" }
|
||||
end
|
||||
end
|
||||
|
||||
def custom_type_handling
|
||||
respond_to do |type|
|
||||
type.html { render :text => "HTML" }
|
||||
type.custom("application/crazy-xml") { render :text => "Crazy XML" }
|
||||
type.all { render :text => "Nothing" }
|
||||
end
|
||||
end
|
||||
|
||||
def custom_constant_handling
|
||||
Mime::Type.register("text/x-mobile", :mobile)
|
||||
|
||||
respond_to do |type|
|
||||
type.html { render :text => "HTML" }
|
||||
type.mobile { render :text => "Mobile" }
|
||||
end
|
||||
ensure
|
||||
Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
|
||||
end
|
||||
|
||||
def custom_constant_handling_without_block
|
||||
Mime::Type.register("text/x-mobile", :mobile)
|
||||
|
||||
respond_to do |type|
|
||||
type.html { render :text => "HTML" }
|
||||
type.mobile
|
||||
end
|
||||
|
||||
ensure
|
||||
Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
|
||||
end
|
||||
|
||||
def handle_any
|
||||
respond_to do |type|
|
||||
type.html { render :text => "HTML" }
|
||||
type.any(:js, :xml) { render :text => "Either JS or XML" }
|
||||
end
|
||||
end
|
||||
|
||||
def handle_any_any
|
||||
respond_to do |type|
|
||||
type.html { render :text => 'HTML' }
|
||||
type.any { render :text => 'Whatever you ask for, I got it' }
|
||||
end
|
||||
end
|
||||
|
||||
def all_types_with_layout
|
||||
respond_to do |type|
|
||||
type.html
|
||||
type.js
|
||||
end
|
||||
end
|
||||
|
||||
def iphone_with_html_response_type
|
||||
Mime::Type.register_alias("text/html", :iphone)
|
||||
request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone"
|
||||
|
||||
respond_to do |type|
|
||||
type.html { @type = "Firefox" }
|
||||
type.iphone { @type = "iPhone" }
|
||||
end
|
||||
|
||||
ensure
|
||||
Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
|
||||
end
|
||||
|
||||
def iphone_with_html_response_type_without_layout
|
||||
Mime::Type.register_alias("text/html", :iphone)
|
||||
request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
|
||||
|
||||
respond_to do |type|
|
||||
type.html { @type = "Firefox"; render :action => "iphone_with_html_response_type" }
|
||||
type.iphone { @type = "iPhone" ; render :action => "iphone_with_html_response_type" }
|
||||
end
|
||||
|
||||
ensure
|
||||
Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
|
||||
end
|
||||
|
||||
def rescue_action(e)
|
||||
raise
|
||||
end
|
||||
|
||||
protected
|
||||
def set_layout
|
||||
if ["all_types_with_layout", "iphone_with_html_response_type"].include?(action_name)
|
||||
"respond_to/layouts/standard"
|
||||
elsif action_name == "iphone_with_html_response_type_without_layout"
|
||||
"respond_to/layouts/missing"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
RespondToController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
|
||||
class MimeControllerTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
|
||||
@controller = RespondToController.new
|
||||
@request.host = "www.example.com"
|
||||
end
|
||||
|
||||
def test_html
|
||||
@request.env["HTTP_ACCEPT"] = "text/html"
|
||||
get :js_or_html
|
||||
assert_equal 'HTML', @response.body
|
||||
|
||||
get :html_or_xml
|
||||
assert_equal 'HTML', @response.body
|
||||
|
||||
get :just_xml
|
||||
assert_response 406
|
||||
end
|
||||
|
||||
def test_all
|
||||
@request.env["HTTP_ACCEPT"] = "*/*"
|
||||
get :js_or_html
|
||||
assert_equal 'HTML', @response.body # js is not part of all
|
||||
|
||||
get :html_or_xml
|
||||
assert_equal 'HTML', @response.body
|
||||
|
||||
get :just_xml
|
||||
assert_equal 'XML', @response.body
|
||||
end
|
||||
|
||||
def test_xml
|
||||
@request.env["HTTP_ACCEPT"] = "application/xml"
|
||||
get :html_xml_or_rss
|
||||
assert_equal 'XML', @response.body
|
||||
end
|
||||
|
||||
def test_js_or_html
|
||||
@request.env["HTTP_ACCEPT"] = "text/javascript, text/html"
|
||||
get :js_or_html
|
||||
assert_equal 'JS', @response.body
|
||||
|
||||
get :html_or_xml
|
||||
assert_equal 'HTML', @response.body
|
||||
|
||||
get :just_xml
|
||||
assert_response 406
|
||||
end
|
||||
|
||||
def test_json_or_yaml
|
||||
get :json_or_yaml
|
||||
assert_equal 'JSON', @response.body
|
||||
|
||||
get :json_or_yaml, :format => 'json'
|
||||
assert_equal 'JSON', @response.body
|
||||
|
||||
get :json_or_yaml, :format => 'yaml'
|
||||
assert_equal 'YAML', @response.body
|
||||
|
||||
{ 'YAML' => %w(text/yaml),
|
||||
'JSON' => %w(application/json text/x-json)
|
||||
}.each do |body, content_types|
|
||||
content_types.each do |content_type|
|
||||
@request.env['HTTP_ACCEPT'] = content_type
|
||||
get :json_or_yaml
|
||||
assert_equal body, @response.body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_js_or_anything
|
||||
@request.env["HTTP_ACCEPT"] = "text/javascript, */*"
|
||||
get :js_or_html
|
||||
assert_equal 'JS', @response.body
|
||||
|
||||
get :html_or_xml
|
||||
assert_equal 'HTML', @response.body
|
||||
|
||||
get :just_xml
|
||||
assert_equal 'XML', @response.body
|
||||
end
|
||||
|
||||
def test_using_defaults
|
||||
@request.env["HTTP_ACCEPT"] = "*/*"
|
||||
get :using_defaults
|
||||
assert_equal "text/html", @response.content_type
|
||||
assert_equal 'Hello world!', @response.body
|
||||
|
||||
@request.env["HTTP_ACCEPT"] = "text/javascript"
|
||||
get :using_defaults
|
||||
assert_equal "text/javascript", @response.content_type
|
||||
assert_equal '$("body").visualEffect("highlight");', @response.body
|
||||
|
||||
@request.env["HTTP_ACCEPT"] = "application/xml"
|
||||
get :using_defaults
|
||||
assert_equal "application/xml", @response.content_type
|
||||
assert_equal "<p>Hello world!</p>\n", @response.body
|
||||
end
|
||||
|
||||
def test_using_defaults_with_type_list
|
||||
@request.env["HTTP_ACCEPT"] = "*/*"
|
||||
get :using_defaults_with_type_list
|
||||
assert_equal "text/html", @response.content_type
|
||||
assert_equal 'Hello world!', @response.body
|
||||
|
||||
@request.env["HTTP_ACCEPT"] = "text/javascript"
|
||||
get :using_defaults_with_type_list
|
||||
assert_equal "text/javascript", @response.content_type
|
||||
assert_equal '$("body").visualEffect("highlight");', @response.body
|
||||
|
||||
@request.env["HTTP_ACCEPT"] = "application/xml"
|
||||
get :using_defaults_with_type_list
|
||||
assert_equal "application/xml", @response.content_type
|
||||
assert_equal "<p>Hello world!</p>\n", @response.body
|
||||
end
|
||||
|
||||
def test_with_atom_content_type
|
||||
@request.env["CONTENT_TYPE"] = "application/atom+xml"
|
||||
get :made_for_content_type
|
||||
assert_equal "ATOM", @response.body
|
||||
end
|
||||
|
||||
def test_with_rss_content_type
|
||||
@request.env["CONTENT_TYPE"] = "application/rss+xml"
|
||||
get :made_for_content_type
|
||||
assert_equal "RSS", @response.body
|
||||
end
|
||||
|
||||
def test_synonyms
|
||||
@request.env["HTTP_ACCEPT"] = "application/javascript"
|
||||
get :js_or_html
|
||||
assert_equal 'JS', @response.body
|
||||
|
||||
@request.env["HTTP_ACCEPT"] = "application/x-xml"
|
||||
get :html_xml_or_rss
|
||||
assert_equal "XML", @response.body
|
||||
end
|
||||
|
||||
def test_custom_types
|
||||
@request.env["HTTP_ACCEPT"] = "application/crazy-xml"
|
||||
get :custom_type_handling
|
||||
assert_equal "application/crazy-xml", @response.content_type
|
||||
assert_equal 'Crazy XML', @response.body
|
||||
|
||||
@request.env["HTTP_ACCEPT"] = "text/html"
|
||||
get :custom_type_handling
|
||||
assert_equal "text/html", @response.content_type
|
||||
assert_equal 'HTML', @response.body
|
||||
end
|
||||
|
||||
def test_xhtml_alias
|
||||
@request.env["HTTP_ACCEPT"] = "application/xhtml+xml,application/xml"
|
||||
get :html_or_xml
|
||||
assert_equal 'HTML', @response.body
|
||||
end
|
||||
|
||||
def test_firefox_simulation
|
||||
@request.env["HTTP_ACCEPT"] = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
|
||||
get :html_or_xml
|
||||
assert_equal 'HTML', @response.body
|
||||
end
|
||||
|
||||
def test_handle_any
|
||||
@request.env["HTTP_ACCEPT"] = "*/*"
|
||||
get :handle_any
|
||||
assert_equal 'HTML', @response.body
|
||||
|
||||
@request.env["HTTP_ACCEPT"] = "text/javascript"
|
||||
get :handle_any
|
||||
assert_equal 'Either JS or XML', @response.body
|
||||
|
||||
@request.env["HTTP_ACCEPT"] = "text/xml"
|
||||
get :handle_any
|
||||
assert_equal 'Either JS or XML', @response.body
|
||||
end
|
||||
|
||||
def test_handle_any_any
|
||||
@request.env["HTTP_ACCEPT"] = "*/*"
|
||||
get :handle_any_any
|
||||
assert_equal 'HTML', @response.body
|
||||
end
|
||||
|
||||
def test_handle_any_any_parameter_format
|
||||
get :handle_any_any, {:format=>'html'}
|
||||
assert_equal 'HTML', @response.body
|
||||
end
|
||||
|
||||
def test_handle_any_any_explicit_html
|
||||
@request.env["HTTP_ACCEPT"] = "text/html"
|
||||
get :handle_any_any
|
||||
assert_equal 'HTML', @response.body
|
||||
end
|
||||
|
||||
def test_handle_any_any_javascript
|
||||
@request.env["HTTP_ACCEPT"] = "text/javascript"
|
||||
get :handle_any_any
|
||||
assert_equal 'Whatever you ask for, I got it', @response.body
|
||||
end
|
||||
|
||||
def test_handle_any_any_xml
|
||||
@request.env["HTTP_ACCEPT"] = "text/xml"
|
||||
get :handle_any_any
|
||||
assert_equal 'Whatever you ask for, I got it', @response.body
|
||||
end
|
||||
|
||||
def test_rjs_type_skips_layout
|
||||
@request.env["HTTP_ACCEPT"] = "text/javascript"
|
||||
get :all_types_with_layout
|
||||
assert_equal 'RJS for all_types_with_layout', @response.body
|
||||
end
|
||||
|
||||
def test_html_type_with_layout
|
||||
@request.env["HTTP_ACCEPT"] = "text/html"
|
||||
get :all_types_with_layout
|
||||
assert_equal '<html><div id="html">HTML for all_types_with_layout</div></html>', @response.body
|
||||
end
|
||||
|
||||
def test_xhr
|
||||
xhr :get, :js_or_html
|
||||
assert_equal 'JS', @response.body
|
||||
|
||||
xhr :get, :using_defaults
|
||||
assert_equal '$("body").visualEffect("highlight");', @response.body
|
||||
end
|
||||
|
||||
def test_custom_constant
|
||||
get :custom_constant_handling, :format => "mobile"
|
||||
assert_equal "text/x-mobile", @response.content_type
|
||||
assert_equal "Mobile", @response.body
|
||||
end
|
||||
|
||||
def test_custom_constant_handling_without_block
|
||||
get :custom_constant_handling_without_block, :format => "mobile"
|
||||
assert_equal "text/x-mobile", @response.content_type
|
||||
assert_equal "Mobile", @response.body
|
||||
end
|
||||
|
||||
def test_forced_format
|
||||
get :html_xml_or_rss
|
||||
assert_equal "HTML", @response.body
|
||||
|
||||
get :html_xml_or_rss, :format => "html"
|
||||
assert_equal "HTML", @response.body
|
||||
|
||||
get :html_xml_or_rss, :format => "xml"
|
||||
assert_equal "XML", @response.body
|
||||
|
||||
get :html_xml_or_rss, :format => "rss"
|
||||
assert_equal "RSS", @response.body
|
||||
end
|
||||
|
||||
def test_internally_forced_format
|
||||
get :forced_xml
|
||||
assert_equal "XML", @response.body
|
||||
|
||||
get :forced_xml, :format => "html"
|
||||
assert_equal "XML", @response.body
|
||||
end
|
||||
|
||||
def test_extension_synonyms
|
||||
get :html_xml_or_rss, :format => "xhtml"
|
||||
assert_equal "HTML", @response.body
|
||||
end
|
||||
|
||||
def test_render_action_for_html
|
||||
@controller.instance_eval do
|
||||
def render(*args)
|
||||
unless args.empty?
|
||||
@action = args.first[:action]
|
||||
end
|
||||
response.body = "#{@action} - #{@template.template_format}"
|
||||
end
|
||||
end
|
||||
|
||||
get :using_defaults
|
||||
assert_equal "using_defaults - html", @response.body
|
||||
|
||||
get :using_defaults, :format => "xml"
|
||||
assert_equal "using_defaults - xml", @response.body
|
||||
end
|
||||
|
||||
def test_format_with_custom_response_type
|
||||
get :iphone_with_html_response_type
|
||||
assert_equal '<html><div id="html">Hello future from Firefox!</div></html>', @response.body
|
||||
|
||||
get :iphone_with_html_response_type, :format => "iphone"
|
||||
assert_equal "text/html", @response.content_type
|
||||
assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
|
||||
end
|
||||
|
||||
def test_format_with_custom_response_type_and_request_headers
|
||||
@request.env["HTTP_ACCEPT"] = "text/iphone"
|
||||
get :iphone_with_html_response_type
|
||||
assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
|
||||
assert_equal "text/html", @response.content_type
|
||||
end
|
||||
|
||||
def test_format_with_custom_response_type_and_request_headers_with_only_one_layout_present
|
||||
get :iphone_with_html_response_type_without_layout
|
||||
assert_equal '<html><div id="html_missing">Hello future from Firefox!</div></html>', @response.body
|
||||
|
||||
@request.env["HTTP_ACCEPT"] = "text/iphone"
|
||||
assert_raises(ActionView::MissingTemplate) { get :iphone_with_html_response_type_without_layout }
|
||||
end
|
||||
end
|
||||
|
||||
class AbstractPostController < ActionController::Base
|
||||
self.view_paths = File.dirname(__FILE__) + "/../fixtures/post_test/"
|
||||
end
|
||||
|
||||
# For testing layouts which are set automatically
|
||||
class PostController < AbstractPostController
|
||||
around_filter :with_iphone
|
||||
|
||||
def index
|
||||
respond_to do |type|
|
||||
type.html
|
||||
type.iphone
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
def with_iphone
|
||||
Mime::Type.register_alias("text/html", :iphone)
|
||||
request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
|
||||
yield
|
||||
ensure
|
||||
Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
|
||||
end
|
||||
end
|
||||
|
||||
class SuperPostController < PostController
|
||||
def index
|
||||
respond_to do |type|
|
||||
type.html
|
||||
type.iphone
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class MimeControllerLayoutsTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
|
||||
@controller = PostController.new
|
||||
@request.host = "www.example.com"
|
||||
end
|
||||
|
||||
def test_missing_layout_renders_properly
|
||||
get :index
|
||||
assert_equal '<html><div id="html">Hello Firefox</div></html>', @response.body
|
||||
|
||||
@request.env["HTTP_ACCEPT"] = "text/iphone"
|
||||
get :index
|
||||
assert_equal 'Hello iPhone', @response.body
|
||||
end
|
||||
|
||||
def test_format_with_inherited_layouts
|
||||
@controller = SuperPostController.new
|
||||
|
||||
get :index
|
||||
assert_equal 'Super Firefox', @response.body
|
||||
|
||||
@request.env["HTTP_ACCEPT"] = "text/iphone"
|
||||
get :index
|
||||
assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body
|
||||
end
|
||||
end
|
||||
|
|
@ -1,84 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class MimeTypeTest < Test::Unit::TestCase
|
||||
Mime::Type.register "image/png", :png
|
||||
Mime::Type.register "application/pdf", :pdf
|
||||
|
||||
def test_parse_single
|
||||
Mime::LOOKUP.keys.each do |mime_type|
|
||||
assert_equal [Mime::Type.lookup(mime_type)], Mime::Type.parse(mime_type)
|
||||
end
|
||||
end
|
||||
|
||||
def test_parse_without_q
|
||||
accept = "text/xml,application/xhtml+xml,text/yaml,application/xml,text/html,image/png,text/plain,application/pdf,*/*"
|
||||
expect = [Mime::HTML, Mime::XML, Mime::YAML, Mime::PNG, Mime::TEXT, Mime::PDF, Mime::ALL]
|
||||
assert_equal expect, Mime::Type.parse(accept)
|
||||
end
|
||||
|
||||
def test_parse_with_q
|
||||
accept = "text/xml,application/xhtml+xml,text/yaml; q=0.3,application/xml,text/html; q=0.8,image/png,text/plain; q=0.5,application/pdf,*/*; q=0.2"
|
||||
expect = [Mime::HTML, Mime::XML, Mime::PNG, Mime::PDF, Mime::TEXT, Mime::YAML, Mime::ALL]
|
||||
assert_equal expect, Mime::Type.parse(accept)
|
||||
end
|
||||
|
||||
# Accept header send with user HTTP_USER_AGENT: Sunrise/0.42j (Windows XP)
|
||||
def test_parse_crappy_broken_acceptlines
|
||||
accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/*,,*/*;q=0.5"
|
||||
expect = [Mime::HTML, Mime::XML, "image/*", Mime::TEXT, Mime::ALL]
|
||||
assert_equal expect, Mime::Type.parse(accept).collect { |c| c.to_s }
|
||||
end
|
||||
|
||||
# Accept header send with user HTTP_USER_AGENT: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; InfoPath.1)
|
||||
def test_parse_crappy_broken_acceptlines2
|
||||
accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, , pronto/1.00.00, sslvpn/1.00.00.00, */*"
|
||||
expect = ['image/gif', 'image/x-xbitmap', 'image/jpeg','image/pjpeg', 'application/x-shockwave-flash', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint', 'application/msword', 'pronto/1.00.00', 'sslvpn/1.00.00.00', Mime::ALL ]
|
||||
assert_equal expect, Mime::Type.parse(accept).collect { |c| c.to_s }
|
||||
end
|
||||
|
||||
def test_custom_type
|
||||
Mime::Type.register("image/gif", :gif)
|
||||
assert_nothing_raised do
|
||||
Mime::GIF
|
||||
assert_equal Mime::GIF, Mime::SET.last
|
||||
end
|
||||
ensure
|
||||
Mime.module_eval { remove_const :GIF if const_defined?(:GIF) }
|
||||
end
|
||||
|
||||
def test_type_should_be_equal_to_symbol
|
||||
assert_equal Mime::HTML, 'application/xhtml+xml'
|
||||
assert_equal Mime::HTML, :html
|
||||
end
|
||||
|
||||
def test_type_convenience_methods
|
||||
# Don't test Mime::ALL, since it Mime::ALL#html? == true
|
||||
types = Mime::SET.to_a.map(&:to_sym).uniq - [:all]
|
||||
|
||||
# Remove custom Mime::Type instances set in other tests, like Mime::GIF and Mime::IPHONE
|
||||
types.delete_if { |type| !Mime.const_defined?(type.to_s.upcase) }
|
||||
|
||||
types.each do |type|
|
||||
mime = Mime.const_get(type.to_s.upcase)
|
||||
assert mime.send("#{type}?"), "#{mime.inspect} is not #{type}?"
|
||||
(types - [type]).each { |other_type| assert !mime.send("#{other_type}?"), "#{mime.inspect} is #{other_type}?" }
|
||||
end
|
||||
end
|
||||
|
||||
def test_mime_all_is_html
|
||||
assert Mime::ALL.all?, "Mime::ALL is not all?"
|
||||
assert Mime::ALL.html?, "Mime::ALL is not html?"
|
||||
end
|
||||
|
||||
def test_verifiable_mime_types
|
||||
unverified_types = Mime::Type.unverifiable_types
|
||||
all_types = Mime::SET.to_a.map(&:to_sym)
|
||||
all_types.uniq!
|
||||
# Remove custom Mime::Type instances set in other tests, like Mime::GIF and Mime::IPHONE
|
||||
all_types.delete_if { |type| !Mime.const_defined?(type.to_s.upcase) }
|
||||
|
||||
unverified, verified = all_types.partition { |type| Mime::Type.unverifiable_types.include? type }
|
||||
assert verified.all? { |type| Mime.const_get(type.to_s.upcase).verify_request? }, "Not all Mime Types are verified: #{verified.inspect}"
|
||||
assert unverified.all? { |type| !Mime.const_get(type.to_s.upcase).verify_request? }, "Some Mime Types are verified: #{unverified.inspect}"
|
||||
end
|
||||
end
|
|
@ -1,945 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
require 'controller/fake_models'
|
||||
|
||||
class CustomersController < ActionController::Base
|
||||
end
|
||||
|
||||
module Fun
|
||||
class GamesController < ActionController::Base
|
||||
def hello_world
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module NewRenderTestHelper
|
||||
def rjs_helper_method_from_module
|
||||
page.visual_effect :highlight
|
||||
end
|
||||
end
|
||||
|
||||
class LabellingFormBuilder < ActionView::Helpers::FormBuilder
|
||||
end
|
||||
|
||||
class NewRenderTestController < ActionController::Base
|
||||
layout :determine_layout
|
||||
|
||||
def self.controller_name; "test"; end
|
||||
def self.controller_path; "test"; end
|
||||
|
||||
def hello_world
|
||||
end
|
||||
|
||||
def render_hello_world
|
||||
render :template => "test/hello_world"
|
||||
end
|
||||
|
||||
def render_hello_world_from_variable
|
||||
@person = "david"
|
||||
render :text => "hello #{@person}"
|
||||
end
|
||||
|
||||
def render_action_hello_world
|
||||
render :action => "hello_world"
|
||||
end
|
||||
|
||||
def render_action_hello_world_as_symbol
|
||||
render :action => :hello_world
|
||||
end
|
||||
|
||||
def render_text_hello_world
|
||||
render :text => "hello world"
|
||||
end
|
||||
|
||||
def render_text_hello_world_with_layout
|
||||
@variable_for_layout = ", I'm here!"
|
||||
render :text => "hello world", :layout => true
|
||||
end
|
||||
|
||||
def hello_world_with_layout_false
|
||||
render :layout => false
|
||||
end
|
||||
|
||||
def render_custom_code
|
||||
render :text => "hello world", :status => "404 Moved"
|
||||
end
|
||||
|
||||
def render_file_with_instance_variables
|
||||
@secret = 'in the sauce'
|
||||
path = File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_ivar.erb')
|
||||
render :file => path
|
||||
end
|
||||
|
||||
def render_file_from_template
|
||||
@secret = 'in the sauce'
|
||||
@path = File.expand_path(File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_ivar.erb'))
|
||||
end
|
||||
|
||||
def render_file_with_locals
|
||||
path = File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_locals.erb')
|
||||
render :file => path, :locals => {:secret => 'in the sauce'}
|
||||
end
|
||||
|
||||
def render_file_not_using_full_path
|
||||
@secret = 'in the sauce'
|
||||
render :file => 'test/render_file_with_ivar', :use_full_path => true
|
||||
end
|
||||
|
||||
def render_file_not_using_full_path_with_dot_in_path
|
||||
@secret = 'in the sauce'
|
||||
render :file => 'test/dot.directory/render_file_with_ivar', :use_full_path => true
|
||||
end
|
||||
|
||||
def render_xml_hello
|
||||
@name = "David"
|
||||
render :template => "test/hello"
|
||||
end
|
||||
|
||||
def greeting
|
||||
# let's just rely on the template
|
||||
end
|
||||
|
||||
def layout_test
|
||||
render :action => "hello_world"
|
||||
end
|
||||
|
||||
def layout_test_with_different_layout
|
||||
render :action => "hello_world", :layout => "standard"
|
||||
end
|
||||
|
||||
def rendering_without_layout
|
||||
render :action => "hello_world", :layout => false
|
||||
end
|
||||
|
||||
def layout_overriding_layout
|
||||
render :action => "hello_world", :layout => "standard"
|
||||
end
|
||||
|
||||
def rendering_nothing_on_layout
|
||||
render :nothing => true
|
||||
end
|
||||
|
||||
def builder_layout_test
|
||||
render :action => "hello"
|
||||
end
|
||||
|
||||
def partials_list
|
||||
@test_unchanged = 'hello'
|
||||
@customers = [ Customer.new("david"), Customer.new("mary") ]
|
||||
render :action => "list"
|
||||
end
|
||||
|
||||
def partial_only
|
||||
render :partial => true
|
||||
end
|
||||
|
||||
def partial_only_with_layout
|
||||
render :partial => "partial_only", :layout => true
|
||||
end
|
||||
|
||||
def partial_with_locals
|
||||
render :partial => "customer", :locals => { :customer => Customer.new("david") }
|
||||
end
|
||||
|
||||
def partial_with_form_builder
|
||||
render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, @template, {}, Proc.new {})
|
||||
end
|
||||
|
||||
def partial_with_form_builder_subclass
|
||||
render :partial => LabellingFormBuilder.new(:post, nil, @template, {}, Proc.new {})
|
||||
end
|
||||
|
||||
def partial_collection
|
||||
render :partial => "customer", :collection => [ Customer.new("david"), Customer.new("mary") ]
|
||||
end
|
||||
|
||||
def partial_collection_with_spacer
|
||||
render :partial => "customer", :spacer_template => "partial_only", :collection => [ Customer.new("david"), Customer.new("mary") ]
|
||||
end
|
||||
|
||||
def partial_collection_with_counter
|
||||
render :partial => "customer_counter", :collection => [ Customer.new("david"), Customer.new("mary") ]
|
||||
end
|
||||
|
||||
def partial_collection_with_locals
|
||||
render :partial => "customer_greeting", :collection => [ Customer.new("david"), Customer.new("mary") ], :locals => { :greeting => "Bonjour" }
|
||||
end
|
||||
|
||||
def partial_collection_shorthand_with_locals
|
||||
render :partial => [ Customer.new("david"), Customer.new("mary") ], :locals => { :greeting => "Bonjour" }
|
||||
end
|
||||
|
||||
def partial_collection_shorthand_with_different_types_of_records
|
||||
render :partial => [
|
||||
BadCustomer.new("mark"),
|
||||
GoodCustomer.new("craig"),
|
||||
BadCustomer.new("john"),
|
||||
GoodCustomer.new("zach"),
|
||||
GoodCustomer.new("brandon"),
|
||||
BadCustomer.new("dan") ],
|
||||
:locals => { :greeting => "Bonjour" }
|
||||
end
|
||||
|
||||
def partial_collection_shorthand_with_different_types_of_records_with_counter
|
||||
partial_collection_shorthand_with_different_types_of_records
|
||||
end
|
||||
|
||||
def empty_partial_collection
|
||||
render :partial => "customer", :collection => []
|
||||
end
|
||||
|
||||
def partial_with_hash_object
|
||||
render :partial => "hash_object", :object => {:first_name => "Sam"}
|
||||
end
|
||||
|
||||
def partial_hash_collection
|
||||
render :partial => "hash_object", :collection => [ {:first_name => "Pratik"}, {:first_name => "Amy"} ]
|
||||
end
|
||||
|
||||
def partial_hash_collection_with_locals
|
||||
render :partial => "hash_greeting", :collection => [ {:first_name => "Pratik"}, {:first_name => "Amy"} ], :locals => { :greeting => "Hola" }
|
||||
end
|
||||
|
||||
def partial_with_implicit_local_assignment
|
||||
@customer = Customer.new("Marcel")
|
||||
render :partial => "customer"
|
||||
end
|
||||
|
||||
def missing_partial
|
||||
render :partial => 'thisFileIsntHere'
|
||||
end
|
||||
|
||||
def hello_in_a_string
|
||||
@customers = [ Customer.new("david"), Customer.new("mary") ]
|
||||
render :text => "How's there? " << render_to_string(:template => "test/list")
|
||||
end
|
||||
|
||||
def render_to_string_with_assigns
|
||||
@before = "i'm before the render"
|
||||
render_to_string :text => "foo"
|
||||
@after = "i'm after the render"
|
||||
render :action => "test/hello_world"
|
||||
end
|
||||
|
||||
def render_to_string_with_partial
|
||||
@partial_only = render_to_string :partial => "partial_only"
|
||||
@partial_with_locals = render_to_string :partial => "customer", :locals => { :customer => Customer.new("david") }
|
||||
render :action => "test/hello_world"
|
||||
end
|
||||
|
||||
def render_to_string_with_exception
|
||||
render_to_string :file => "exception that will not be caught - this will certainly not work", :use_full_path => true
|
||||
end
|
||||
|
||||
def render_to_string_with_caught_exception
|
||||
@before = "i'm before the render"
|
||||
begin
|
||||
render_to_string :file => "exception that will be caught- hope my future instance vars still work!", :use_full_path => true
|
||||
rescue
|
||||
end
|
||||
@after = "i'm after the render"
|
||||
render :action => "test/hello_world"
|
||||
end
|
||||
|
||||
def accessing_params_in_template
|
||||
render :inline => "Hello: <%= params[:name] %>"
|
||||
end
|
||||
|
||||
def accessing_request_in_template
|
||||
render :inline => "Hello: <%= request.host %>"
|
||||
end
|
||||
|
||||
def accessing_logger_in_template
|
||||
render :inline => "<%= logger.class %>"
|
||||
end
|
||||
|
||||
def accessing_action_name_in_template
|
||||
render :inline => "<%= action_name %>"
|
||||
end
|
||||
|
||||
def accessing_params_in_template_with_layout
|
||||
render :layout => nil, :inline => "Hello: <%= params[:name] %>"
|
||||
end
|
||||
|
||||
def render_with_explicit_template
|
||||
render :template => "test/hello_world"
|
||||
end
|
||||
|
||||
def render_with_explicit_template_with_locals
|
||||
render :template => "test/render_file_with_locals", :locals => { :secret => 'area51' }
|
||||
end
|
||||
|
||||
def double_render
|
||||
render :text => "hello"
|
||||
render :text => "world"
|
||||
end
|
||||
|
||||
def double_redirect
|
||||
redirect_to :action => "double_render"
|
||||
redirect_to :action => "double_render"
|
||||
end
|
||||
|
||||
def render_and_redirect
|
||||
render :text => "hello"
|
||||
redirect_to :action => "double_render"
|
||||
end
|
||||
|
||||
def render_to_string_and_render
|
||||
@stuff = render_to_string :text => "here is some cached stuff"
|
||||
render :text => "Hi web users! #{@stuff}"
|
||||
end
|
||||
|
||||
def rendering_with_conflicting_local_vars
|
||||
@name = "David"
|
||||
def @template.name() nil end
|
||||
render :action => "potential_conflicts"
|
||||
end
|
||||
|
||||
def hello_world_from_rxml_using_action
|
||||
render :action => "hello_world_from_rxml.builder"
|
||||
end
|
||||
|
||||
def hello_world_from_rxml_using_template
|
||||
render :template => "test/hello_world_from_rxml.builder"
|
||||
end
|
||||
|
||||
def head_with_location_header
|
||||
head :location => "/foo"
|
||||
end
|
||||
|
||||
def head_with_symbolic_status
|
||||
head :status => params[:status].intern
|
||||
end
|
||||
|
||||
def head_with_integer_status
|
||||
head :status => params[:status].to_i
|
||||
end
|
||||
|
||||
def head_with_string_status
|
||||
head :status => params[:status]
|
||||
end
|
||||
|
||||
def head_with_custom_header
|
||||
head :x_custom_header => "something"
|
||||
end
|
||||
|
||||
def head_with_status_code_first
|
||||
head :forbidden, :x_custom_header => "something"
|
||||
end
|
||||
|
||||
def render_with_location
|
||||
render :xml => "<hello/>", :location => "http://example.com", :status => 201
|
||||
end
|
||||
|
||||
def render_with_object_location
|
||||
customer = Customer.new("Some guy", 1)
|
||||
render :xml => "<customer/>", :location => customer_url(customer), :status => :created
|
||||
end
|
||||
|
||||
def render_with_to_xml
|
||||
to_xmlable = Class.new do
|
||||
def to_xml
|
||||
"<i-am-xml/>"
|
||||
end
|
||||
end.new
|
||||
|
||||
render :xml => to_xmlable
|
||||
end
|
||||
|
||||
helper NewRenderTestHelper
|
||||
helper do
|
||||
def rjs_helper_method(value)
|
||||
page.visual_effect :highlight, value
|
||||
end
|
||||
end
|
||||
|
||||
def enum_rjs_test
|
||||
render :update do |page|
|
||||
page.select('.product').each do |value|
|
||||
page.rjs_helper_method_from_module
|
||||
page.rjs_helper_method(value)
|
||||
page.sortable(value, :url => { :action => "order" })
|
||||
page.draggable(value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def delete_with_js
|
||||
@project_id = 4
|
||||
end
|
||||
|
||||
def render_js_with_explicit_template
|
||||
@project_id = 4
|
||||
render :template => 'test/delete_with_js'
|
||||
end
|
||||
|
||||
def render_js_with_explicit_action_template
|
||||
@project_id = 4
|
||||
render :action => 'delete_with_js'
|
||||
end
|
||||
|
||||
def update_page
|
||||
render :update do |page|
|
||||
page.replace_html 'balance', '$37,000,000.00'
|
||||
page.visual_effect :highlight, 'balance'
|
||||
end
|
||||
end
|
||||
|
||||
def update_page_with_instance_variables
|
||||
@money = '$37,000,000.00'
|
||||
@div_id = 'balance'
|
||||
render :update do |page|
|
||||
page.replace_html @div_id, @money
|
||||
page.visual_effect :highlight, @div_id
|
||||
end
|
||||
end
|
||||
|
||||
def action_talk_to_layout
|
||||
# Action template sets variable that's picked up by layout
|
||||
end
|
||||
|
||||
def render_text_with_assigns
|
||||
@hello = "world"
|
||||
render :text => "foo"
|
||||
end
|
||||
|
||||
def yield_content_for
|
||||
render :action => "content_for", :layout => "yield"
|
||||
end
|
||||
|
||||
def render_content_type_from_body
|
||||
response.content_type = Mime::RSS
|
||||
render :text => "hello world!"
|
||||
end
|
||||
|
||||
def render_call_to_partial_with_layout
|
||||
render :action => "calling_partial_with_layout"
|
||||
end
|
||||
|
||||
def render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout
|
||||
render :action => "calling_partial_with_layout"
|
||||
end
|
||||
|
||||
def render_using_layout_around_block
|
||||
render :action => "using_layout_around_block"
|
||||
end
|
||||
|
||||
def render_using_layout_around_block_in_main_layout_and_within_content_for_layout
|
||||
render :action => "using_layout_around_block"
|
||||
end
|
||||
|
||||
def rescue_action(e) raise end
|
||||
|
||||
private
|
||||
def determine_layout
|
||||
case action_name
|
||||
when "hello_world", "layout_test", "rendering_without_layout",
|
||||
"rendering_nothing_on_layout", "render_text_hello_world",
|
||||
"render_text_hello_world_with_layout",
|
||||
"hello_world_with_layout_false",
|
||||
"partial_only", "partial_only_with_layout",
|
||||
"accessing_params_in_template",
|
||||
"accessing_params_in_template_with_layout",
|
||||
"render_with_explicit_template",
|
||||
"render_js_with_explicit_template",
|
||||
"render_js_with_explicit_action_template",
|
||||
"delete_with_js", "update_page", "update_page_with_instance_variables"
|
||||
|
||||
"layouts/standard"
|
||||
when "builder_layout_test"
|
||||
"layouts/builder"
|
||||
when "action_talk_to_layout", "layout_overriding_layout"
|
||||
"layouts/talk_from_action"
|
||||
when "render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout"
|
||||
"layouts/partial_with_layout"
|
||||
when "render_using_layout_around_block_in_main_layout_and_within_content_for_layout"
|
||||
"layouts/block_with_layout"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
NewRenderTestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
Fun::GamesController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
|
||||
class NewRenderTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = NewRenderTestController.new
|
||||
|
||||
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
|
||||
# a more accurate simulation of what happens in "real life".
|
||||
@controller.logger = Logger.new(nil)
|
||||
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
|
||||
@request.host = "www.nextangle.com"
|
||||
end
|
||||
|
||||
def test_simple_show
|
||||
get :hello_world
|
||||
assert_response :success
|
||||
assert_template "test/hello_world"
|
||||
assert_equal "<html>Hello world!</html>", @response.body
|
||||
end
|
||||
|
||||
def test_do_with_render
|
||||
get :render_hello_world
|
||||
assert_template "test/hello_world"
|
||||
end
|
||||
|
||||
def test_do_with_render_from_variable
|
||||
get :render_hello_world_from_variable
|
||||
assert_equal "hello david", @response.body
|
||||
end
|
||||
|
||||
def test_do_with_render_action
|
||||
get :render_action_hello_world
|
||||
assert_template "test/hello_world"
|
||||
end
|
||||
|
||||
def test_do_with_render_action_as_symbol
|
||||
get :render_action_hello_world_as_symbol
|
||||
assert_template "test/hello_world"
|
||||
end
|
||||
|
||||
def test_do_with_render_text
|
||||
get :render_text_hello_world
|
||||
assert_equal "hello world", @response.body
|
||||
end
|
||||
|
||||
def test_do_with_render_text_and_layout
|
||||
get :render_text_hello_world_with_layout
|
||||
assert_equal "<html>hello world, I'm here!</html>", @response.body
|
||||
end
|
||||
|
||||
def test_do_with_render_action_and_layout_false
|
||||
get :hello_world_with_layout_false
|
||||
assert_equal 'Hello world!', @response.body
|
||||
end
|
||||
|
||||
def test_do_with_render_custom_code
|
||||
get :render_custom_code
|
||||
assert_response :missing
|
||||
end
|
||||
|
||||
def test_render_file_with_instance_variables
|
||||
get :render_file_with_instance_variables
|
||||
assert_equal "The secret is in the sauce\n", @response.body
|
||||
end
|
||||
|
||||
def test_render_file_not_using_full_path
|
||||
get :render_file_not_using_full_path
|
||||
assert_equal "The secret is in the sauce\n", @response.body
|
||||
end
|
||||
|
||||
def test_render_file_not_using_full_path_with_dot_in_path
|
||||
get :render_file_not_using_full_path_with_dot_in_path
|
||||
assert_equal "The secret is in the sauce\n", @response.body
|
||||
end
|
||||
|
||||
def test_render_file_with_locals
|
||||
get :render_file_with_locals
|
||||
assert_equal "The secret is in the sauce\n", @response.body
|
||||
end
|
||||
|
||||
def test_render_file_from_template
|
||||
get :render_file_from_template
|
||||
assert_equal "The secret is in the sauce\n", @response.body
|
||||
end
|
||||
|
||||
def test_attempt_to_access_object_method
|
||||
assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
|
||||
end
|
||||
|
||||
def test_private_methods
|
||||
assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
|
||||
end
|
||||
|
||||
def test_access_to_request_in_view
|
||||
get :accessing_request_in_template
|
||||
assert_equal "Hello: www.nextangle.com", @response.body
|
||||
end
|
||||
|
||||
def test_access_to_logger_in_view
|
||||
get :accessing_logger_in_template
|
||||
assert_equal "Logger", @response.body
|
||||
end
|
||||
|
||||
def test_access_to_action_name_in_view
|
||||
get :accessing_action_name_in_template
|
||||
assert_equal "accessing_action_name_in_template", @response.body
|
||||
end
|
||||
|
||||
def test_render_xml
|
||||
get :render_xml_hello
|
||||
assert_equal "<html>\n <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
|
||||
end
|
||||
|
||||
def test_enum_rjs_test
|
||||
get :enum_rjs_test
|
||||
assert_equal <<-EOS.strip, @response.body
|
||||
$$(".product").each(function(value, index) {
|
||||
new Effect.Highlight(element,{});
|
||||
new Effect.Highlight(value,{});
|
||||
Sortable.create(value, {onUpdate:function(){new Ajax.Request('/test/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(value)})}});
|
||||
new Draggable(value, {});
|
||||
});
|
||||
EOS
|
||||
end
|
||||
|
||||
def test_render_xml_with_default
|
||||
get :greeting
|
||||
assert_equal "<p>This is grand!</p>\n", @response.body
|
||||
end
|
||||
|
||||
def test_render_with_default_from_accept_header
|
||||
@request.env["HTTP_ACCEPT"] = "text/javascript"
|
||||
get :greeting
|
||||
assert_equal "$(\"body\").visualEffect(\"highlight\");", @response.body
|
||||
end
|
||||
|
||||
def test_render_rjs_with_default
|
||||
get :delete_with_js
|
||||
assert_equal %!Element.remove("person");\nnew Effect.Highlight(\"project-4\",{});!, @response.body
|
||||
end
|
||||
|
||||
def test_render_rjs_template_explicitly
|
||||
get :render_js_with_explicit_template
|
||||
assert_equal %!Element.remove("person");\nnew Effect.Highlight(\"project-4\",{});!, @response.body
|
||||
end
|
||||
|
||||
def test_rendering_rjs_action_explicitly
|
||||
get :render_js_with_explicit_action_template
|
||||
assert_equal %!Element.remove("person");\nnew Effect.Highlight(\"project-4\",{});!, @response.body
|
||||
end
|
||||
|
||||
def test_layout_rendering
|
||||
get :layout_test
|
||||
assert_equal "<html>Hello world!</html>", @response.body
|
||||
end
|
||||
|
||||
def test_layout_test_with_different_layout
|
||||
get :layout_test_with_different_layout
|
||||
assert_equal "<html>Hello world!</html>", @response.body
|
||||
end
|
||||
|
||||
def test_rendering_without_layout
|
||||
get :rendering_without_layout
|
||||
assert_equal "Hello world!", @response.body
|
||||
end
|
||||
|
||||
def test_layout_overriding_layout
|
||||
get :layout_overriding_layout
|
||||
assert_no_match %r{<title>}, @response.body
|
||||
end
|
||||
|
||||
def test_rendering_nothing_on_layout
|
||||
get :rendering_nothing_on_layout
|
||||
assert_equal " ", @response.body
|
||||
end
|
||||
|
||||
def test_render_xml_with_layouts
|
||||
get :builder_layout_test
|
||||
assert_equal "<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", @response.body
|
||||
end
|
||||
|
||||
def test_partial_only
|
||||
get :partial_only
|
||||
assert_equal "only partial", @response.body
|
||||
end
|
||||
|
||||
def test_partial_only_with_layout
|
||||
get :partial_only_with_layout
|
||||
assert_equal "<html>only partial</html>", @response.body
|
||||
end
|
||||
|
||||
def test_render_to_string
|
||||
assert_not_deprecated { get :hello_in_a_string }
|
||||
assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
|
||||
end
|
||||
|
||||
def test_render_to_string_doesnt_break_assigns
|
||||
get :render_to_string_with_assigns
|
||||
assert_equal "i'm before the render", assigns(:before)
|
||||
assert_equal "i'm after the render", assigns(:after)
|
||||
end
|
||||
|
||||
def test_render_to_string_partial
|
||||
get :render_to_string_with_partial
|
||||
assert_equal "only partial", assigns(:partial_only)
|
||||
assert_equal "Hello: david", assigns(:partial_with_locals)
|
||||
end
|
||||
|
||||
def test_bad_render_to_string_still_throws_exception
|
||||
assert_raises(ActionView::MissingTemplate) { get :render_to_string_with_exception }
|
||||
end
|
||||
|
||||
def test_render_to_string_that_throws_caught_exception_doesnt_break_assigns
|
||||
assert_nothing_raised { get :render_to_string_with_caught_exception }
|
||||
assert_equal "i'm before the render", assigns(:before)
|
||||
assert_equal "i'm after the render", assigns(:after)
|
||||
end
|
||||
|
||||
def test_nested_rendering
|
||||
get :hello_world
|
||||
assert_equal "Living in a nested world", Fun::GamesController.process(@request, @response).body
|
||||
end
|
||||
|
||||
def test_accessing_params_in_template
|
||||
get :accessing_params_in_template, :name => "David"
|
||||
assert_equal "Hello: David", @response.body
|
||||
end
|
||||
|
||||
def test_accessing_params_in_template_with_layout
|
||||
get :accessing_params_in_template_with_layout, :name => "David"
|
||||
assert_equal "<html>Hello: David</html>", @response.body
|
||||
end
|
||||
|
||||
def test_render_with_explicit_template
|
||||
get :render_with_explicit_template
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
def test_double_render
|
||||
assert_raises(ActionController::DoubleRenderError) { get :double_render }
|
||||
end
|
||||
|
||||
def test_double_redirect
|
||||
assert_raises(ActionController::DoubleRenderError) { get :double_redirect }
|
||||
end
|
||||
|
||||
def test_render_and_redirect
|
||||
assert_raises(ActionController::DoubleRenderError) { get :render_and_redirect }
|
||||
end
|
||||
|
||||
# specify the one exception to double render rule - render_to_string followed by render
|
||||
def test_render_to_string_and_render
|
||||
get :render_to_string_and_render
|
||||
assert_equal("Hi web users! here is some cached stuff", @response.body)
|
||||
end
|
||||
|
||||
def test_rendering_with_conflicting_local_vars
|
||||
get :rendering_with_conflicting_local_vars
|
||||
assert_equal("First: David\nSecond: Stephan\nThird: David\nFourth: David\nFifth: ", @response.body)
|
||||
end
|
||||
|
||||
def test_action_talk_to_layout
|
||||
get :action_talk_to_layout
|
||||
assert_equal "<title>Talking to the layout</title>\nAction was here!", @response.body
|
||||
end
|
||||
|
||||
def test_partials_list
|
||||
get :partials_list
|
||||
assert_equal "goodbyeHello: davidHello: marygoodbye\n", @response.body
|
||||
end
|
||||
|
||||
def test_partial_with_locals
|
||||
get :partial_with_locals
|
||||
assert_equal "Hello: david", @response.body
|
||||
end
|
||||
|
||||
def test_partial_with_form_builder
|
||||
get :partial_with_form_builder
|
||||
assert_match(/<label/, @response.body)
|
||||
assert_template('test/_form')
|
||||
end
|
||||
|
||||
def test_partial_with_form_builder_subclass
|
||||
get :partial_with_form_builder_subclass
|
||||
assert_match(/<label/, @response.body)
|
||||
assert_template('test/_labelling_form')
|
||||
end
|
||||
|
||||
def test_partial_collection
|
||||
get :partial_collection
|
||||
assert_equal "Hello: davidHello: mary", @response.body
|
||||
end
|
||||
|
||||
def test_partial_collection_with_counter
|
||||
get :partial_collection_with_counter
|
||||
assert_equal "david0mary1", @response.body
|
||||
end
|
||||
|
||||
def test_partial_collection_with_locals
|
||||
get :partial_collection_with_locals
|
||||
assert_equal "Bonjour: davidBonjour: mary", @response.body
|
||||
end
|
||||
|
||||
def test_partial_collection_with_spacer
|
||||
get :partial_collection_with_spacer
|
||||
assert_equal "Hello: davidonly partialHello: mary", @response.body
|
||||
end
|
||||
|
||||
def test_partial_collection_shorthand_with_locals
|
||||
get :partial_collection_shorthand_with_locals
|
||||
assert_equal "Bonjour: davidBonjour: mary", @response.body
|
||||
end
|
||||
|
||||
def test_partial_collection_shorthand_with_different_types_of_records
|
||||
get :partial_collection_shorthand_with_different_types_of_records
|
||||
assert_equal "Bonjour bad customer: mark0Bonjour good customer: craig1Bonjour bad customer: john2Bonjour good customer: zach3Bonjour good customer: brandon4Bonjour bad customer: dan5", @response.body
|
||||
end
|
||||
|
||||
def test_empty_partial_collection
|
||||
get :empty_partial_collection
|
||||
assert_equal " ", @response.body
|
||||
end
|
||||
|
||||
def test_partial_with_hash_object
|
||||
get :partial_with_hash_object
|
||||
assert_equal "Sam\nmaS\n", @response.body
|
||||
end
|
||||
|
||||
def test_hash_partial_collection
|
||||
get :partial_hash_collection
|
||||
assert_equal "Pratik\nkitarP\nAmy\nymA\n", @response.body
|
||||
end
|
||||
|
||||
def test_partial_hash_collection_with_locals
|
||||
get :partial_hash_collection_with_locals
|
||||
assert_equal "Hola: PratikHola: Amy", @response.body
|
||||
end
|
||||
|
||||
def test_partial_with_implicit_local_assignment
|
||||
get :partial_with_implicit_local_assignment
|
||||
assert_equal "Hello: Marcel", @response.body
|
||||
end
|
||||
|
||||
def test_render_missing_partial_template
|
||||
assert_raises(ActionView::MissingTemplate) do
|
||||
get :missing_partial
|
||||
end
|
||||
end
|
||||
|
||||
def test_render_text_with_assigns
|
||||
get :render_text_with_assigns
|
||||
assert_equal "world", assigns["hello"]
|
||||
end
|
||||
|
||||
def test_template_with_locals
|
||||
get :render_with_explicit_template_with_locals
|
||||
assert_equal "The secret is area51\n", @response.body
|
||||
end
|
||||
|
||||
def test_update_page
|
||||
get :update_page
|
||||
assert_template nil
|
||||
assert_equal 'text/javascript; charset=utf-8', @response.headers['type']
|
||||
assert_equal 2, @response.body.split($/).length
|
||||
end
|
||||
|
||||
def test_update_page_with_instance_variables
|
||||
get :update_page_with_instance_variables
|
||||
assert_template nil
|
||||
assert_equal 'text/javascript; charset=utf-8', @response.headers['type']
|
||||
assert_match /balance/, @response.body
|
||||
assert_match /\$37/, @response.body
|
||||
end
|
||||
|
||||
def test_yield_content_for
|
||||
assert_not_deprecated { get :yield_content_for }
|
||||
assert_equal "<title>Putting stuff in the title!</title>\n\nGreat stuff!\n", @response.body
|
||||
end
|
||||
|
||||
|
||||
def test_overwritting_rendering_relative_file_with_extension
|
||||
get :hello_world_from_rxml_using_template
|
||||
assert_equal "<html>\n <p>Hello</p>\n</html>\n", @response.body
|
||||
|
||||
get :hello_world_from_rxml_using_action
|
||||
assert_equal "<html>\n <p>Hello</p>\n</html>\n", @response.body
|
||||
end
|
||||
|
||||
|
||||
def test_head_with_location_header
|
||||
get :head_with_location_header
|
||||
assert @response.body.blank?
|
||||
assert_equal "/foo", @response.headers["Location"]
|
||||
assert_response :ok
|
||||
end
|
||||
|
||||
def test_head_with_custom_header
|
||||
get :head_with_custom_header
|
||||
assert @response.body.blank?
|
||||
assert_equal "something", @response.headers["X-Custom-Header"]
|
||||
assert_response :ok
|
||||
end
|
||||
|
||||
def test_head_with_symbolic_status
|
||||
get :head_with_symbolic_status, :status => "ok"
|
||||
assert_equal "200 OK", @response.headers["Status"]
|
||||
assert_response :ok
|
||||
|
||||
get :head_with_symbolic_status, :status => "not_found"
|
||||
assert_equal "404 Not Found", @response.headers["Status"]
|
||||
assert_response :not_found
|
||||
|
||||
ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE.each do |status, code|
|
||||
get :head_with_symbolic_status, :status => status.to_s
|
||||
assert_equal code, @response.response_code
|
||||
assert_response status
|
||||
end
|
||||
end
|
||||
|
||||
def test_head_with_integer_status
|
||||
ActionController::StatusCodes::STATUS_CODES.each do |code, message|
|
||||
get :head_with_integer_status, :status => code.to_s
|
||||
assert_equal message, @response.message
|
||||
end
|
||||
end
|
||||
|
||||
def test_head_with_string_status
|
||||
get :head_with_string_status, :status => "404 Eat Dirt"
|
||||
assert_equal 404, @response.response_code
|
||||
assert_equal "Eat Dirt", @response.message
|
||||
assert_response :not_found
|
||||
end
|
||||
|
||||
def test_head_with_status_code_first
|
||||
get :head_with_status_code_first
|
||||
assert_equal 403, @response.response_code
|
||||
assert_equal "Forbidden", @response.message
|
||||
assert_equal "something", @response.headers["X-Custom-Header"]
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
def test_rendering_with_location_should_set_header
|
||||
get :render_with_location
|
||||
assert_equal "http://example.com", @response.headers["Location"]
|
||||
end
|
||||
|
||||
def test_rendering_xml_should_call_to_xml_if_possible
|
||||
get :render_with_to_xml
|
||||
assert_equal "<i-am-xml/>", @response.body
|
||||
end
|
||||
|
||||
def test_rendering_with_object_location_should_set_header_with_url_for
|
||||
ActionController::Routing::Routes.draw do |map|
|
||||
map.resources :customers
|
||||
map.connect ':controller/:action/:id'
|
||||
end
|
||||
|
||||
get :render_with_object_location
|
||||
assert_equal "http://www.nextangle.com/customers/1", @response.headers["Location"]
|
||||
end
|
||||
|
||||
def test_render_call_to_partial_with_layout
|
||||
get :render_call_to_partial_with_layout
|
||||
assert_equal "Before (David)\nInside from partial (David)\nAfter", @response.body
|
||||
end
|
||||
|
||||
def test_render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout
|
||||
get :render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout
|
||||
assert_equal "Before (Anthony)\nInside from partial (Anthony)\nAfter\nBefore (David)\nInside from partial (David)\nAfter\nBefore (Ramm)\nInside from partial (Ramm)\nAfter", @response.body
|
||||
end
|
||||
|
||||
def test_using_layout_around_block
|
||||
get :render_using_layout_around_block
|
||||
assert_equal "Before (David)\nInside from block\nAfter", @response.body
|
||||
end
|
||||
|
||||
def test_using_layout_around_block_in_main_layout_and_within_content_for_layout
|
||||
get :render_using_layout_around_block_in_main_layout_and_within_content_for_layout
|
||||
assert_equal "Before (Anthony)\nInside from first block in layout\nAfter\nBefore (David)\nInside from block\nAfter\nBefore (Ramm)\nInside from second block in layout\nAfter\n", @response.body
|
||||
end
|
||||
|
||||
end
|
|
@ -1,174 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class Article
|
||||
attr_reader :id
|
||||
def save; @id = 1 end
|
||||
def new_record?; @id.nil? end
|
||||
def name
|
||||
model = self.class.name.downcase
|
||||
@id.nil? ? "new #{model}" : "#{model} ##{@id}"
|
||||
end
|
||||
end
|
||||
|
||||
class Response < Article
|
||||
def post_id; 1 end
|
||||
end
|
||||
|
||||
class Tag < Article
|
||||
def response_id; 1 end
|
||||
end
|
||||
|
||||
# TODO: test nested models
|
||||
class Response::Nested < Response; end
|
||||
|
||||
uses_mocha 'polymorphic URL helpers' do
|
||||
class PolymorphicRoutesTest < Test::Unit::TestCase
|
||||
|
||||
include ActionController::PolymorphicRoutes
|
||||
|
||||
def setup
|
||||
@article = Article.new
|
||||
@response = Response.new
|
||||
end
|
||||
|
||||
def test_with_record
|
||||
@article.save
|
||||
expects(:article_url).with(@article)
|
||||
polymorphic_url(@article)
|
||||
end
|
||||
|
||||
def test_with_new_record
|
||||
expects(:articles_url).with()
|
||||
@article.expects(:new_record?).returns(true)
|
||||
polymorphic_url(@article)
|
||||
end
|
||||
|
||||
def test_with_record_and_action
|
||||
expects(:new_article_url).with()
|
||||
@article.expects(:new_record?).never
|
||||
polymorphic_url(@article, :action => 'new')
|
||||
end
|
||||
|
||||
def test_url_helper_prefixed_with_new
|
||||
expects(:new_article_url).with()
|
||||
new_polymorphic_url(@article)
|
||||
end
|
||||
|
||||
def test_url_helper_prefixed_with_edit
|
||||
@article.save
|
||||
expects(:edit_article_url).with(@article)
|
||||
edit_polymorphic_url(@article)
|
||||
end
|
||||
|
||||
def test_formatted_url_helper
|
||||
expects(:formatted_article_url).with(@article, :pdf)
|
||||
formatted_polymorphic_url([@article, :pdf])
|
||||
end
|
||||
|
||||
def test_format_option
|
||||
@article.save
|
||||
expects(:article_url).with(@article, :pdf)
|
||||
polymorphic_url(@article, :format => :pdf)
|
||||
end
|
||||
|
||||
def test_id_and_format_option
|
||||
@article.save
|
||||
expects(:article_url).with(:id => @article, :format => :pdf)
|
||||
polymorphic_url(:id => @article, :format => :pdf)
|
||||
end
|
||||
|
||||
def test_with_nested
|
||||
@response.save
|
||||
expects(:article_response_url).with(@article, @response)
|
||||
polymorphic_url([@article, @response])
|
||||
end
|
||||
|
||||
def test_with_nested_unsaved
|
||||
expects(:article_responses_url).with(@article)
|
||||
polymorphic_url([@article, @response])
|
||||
end
|
||||
|
||||
def test_new_with_array_and_namespace
|
||||
expects(:new_admin_article_url).with()
|
||||
polymorphic_url([:admin, @article], :action => 'new')
|
||||
end
|
||||
|
||||
def test_unsaved_with_array_and_namespace
|
||||
expects(:admin_articles_url).with()
|
||||
polymorphic_url([:admin, @article])
|
||||
end
|
||||
|
||||
def test_nested_unsaved_with_array_and_namespace
|
||||
@article.save
|
||||
expects(:admin_article_url).with(@article)
|
||||
polymorphic_url([:admin, @article])
|
||||
expects(:admin_article_responses_url).with(@article)
|
||||
polymorphic_url([:admin, @article, @response])
|
||||
end
|
||||
|
||||
def test_nested_with_array_and_namespace
|
||||
@response.save
|
||||
expects(:admin_article_response_url).with(@article, @response)
|
||||
polymorphic_url([:admin, @article, @response])
|
||||
|
||||
# a ridiculously long named route tests correct ordering of namespaces and nesting:
|
||||
@tag = Tag.new
|
||||
@tag.save
|
||||
expects(:site_admin_article_response_tag_url).with(@article, @response, @tag)
|
||||
polymorphic_url([:site, :admin, @article, @response, @tag])
|
||||
end
|
||||
|
||||
def test_nesting_with_array_ending_in_singleton_resource
|
||||
expects(:article_response_url).with(@article)
|
||||
polymorphic_url([@article, :response])
|
||||
end
|
||||
|
||||
def test_nesting_with_array_containing_singleton_resource
|
||||
@tag = Tag.new
|
||||
@tag.save
|
||||
expects(:article_response_tag_url).with(@article, @tag)
|
||||
polymorphic_url([@article, :response, @tag])
|
||||
end
|
||||
|
||||
def test_nesting_with_array_containing_namespace_and_singleton_resource
|
||||
@tag = Tag.new
|
||||
@tag.save
|
||||
expects(:admin_article_response_tag_url).with(@article, @tag)
|
||||
polymorphic_url([:admin, @article, :response, @tag])
|
||||
end
|
||||
|
||||
def test_nesting_with_array_containing_singleton_resource_and_format
|
||||
@tag = Tag.new
|
||||
@tag.save
|
||||
expects(:formatted_article_response_tag_url).with(@article, @tag, :pdf)
|
||||
formatted_polymorphic_url([@article, :response, @tag, :pdf])
|
||||
end
|
||||
|
||||
def test_nesting_with_array_containing_singleton_resource_and_format_option
|
||||
@tag = Tag.new
|
||||
@tag.save
|
||||
expects(:article_response_tag_url).with(@article, @tag, :pdf)
|
||||
polymorphic_url([@article, :response, @tag], :format => :pdf)
|
||||
end
|
||||
|
||||
# TODO: Needs to be updated to correctly know about whether the object is in a hash or not
|
||||
def xtest_with_hash
|
||||
expects(:article_url).with(@article)
|
||||
@article.save
|
||||
polymorphic_url(:id => @article)
|
||||
end
|
||||
|
||||
def test_polymorphic_path_accepts_options
|
||||
expects(:new_article_path).with()
|
||||
polymorphic_path(@article, :action => :new)
|
||||
end
|
||||
|
||||
def test_polymorphic_path_does_not_modify_arguments
|
||||
expects(:admin_article_responses_url).with(@article)
|
||||
path = [:admin, @article, @response]
|
||||
assert_no_difference 'path.size' do
|
||||
polymorphic_url(path)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,139 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class Comment
|
||||
attr_reader :id
|
||||
def save; @id = 1 end
|
||||
def new_record?; @id.nil? end
|
||||
def name
|
||||
@id.nil? ? 'new comment' : "comment ##{@id}"
|
||||
end
|
||||
end
|
||||
|
||||
class Comment::Nested < Comment; end
|
||||
|
||||
class Test::Unit::TestCase
|
||||
protected
|
||||
def comments_url
|
||||
'http://www.example.com/comments'
|
||||
end
|
||||
|
||||
def comment_url(comment)
|
||||
"http://www.example.com/comments/#{comment.id}"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class RecordIdentifierTest < Test::Unit::TestCase
|
||||
include ActionController::RecordIdentifier
|
||||
|
||||
def setup
|
||||
@klass = Comment
|
||||
@record = @klass.new
|
||||
@singular = 'comment'
|
||||
@plural = 'comments'
|
||||
end
|
||||
|
||||
def test_dom_id_with_new_record
|
||||
assert_equal "new_#{@singular}", dom_id(@record)
|
||||
end
|
||||
|
||||
def test_dom_id_with_new_record_and_prefix
|
||||
assert_equal "custom_prefix_#{@singular}", dom_id(@record, :custom_prefix)
|
||||
end
|
||||
|
||||
def test_dom_id_with_saved_record
|
||||
@record.save
|
||||
assert_equal "#{@singular}_1", dom_id(@record)
|
||||
end
|
||||
|
||||
def test_dom_id_with_prefix
|
||||
@record.save
|
||||
assert_equal "edit_#{@singular}_1", dom_id(@record, :edit)
|
||||
end
|
||||
|
||||
def test_partial_path
|
||||
expected = "#{@plural}/#{@singular}"
|
||||
assert_equal expected, partial_path(@record)
|
||||
assert_equal expected, partial_path(Comment)
|
||||
end
|
||||
|
||||
def test_partial_path_with_namespaced_controller_path
|
||||
expected = "admin/#{@plural}/#{@singular}"
|
||||
assert_equal expected, partial_path(@record, "admin/posts")
|
||||
assert_equal expected, partial_path(@klass, "admin/posts")
|
||||
end
|
||||
|
||||
def test_partial_path_with_not_namespaced_controller_path
|
||||
expected = "#{@plural}/#{@singular}"
|
||||
assert_equal expected, partial_path(@record, "posts")
|
||||
assert_equal expected, partial_path(@klass, "posts")
|
||||
end
|
||||
|
||||
def test_dom_class
|
||||
assert_equal @singular, dom_class(@record)
|
||||
end
|
||||
|
||||
def test_dom_class_with_prefix
|
||||
assert_equal "custom_prefix_#{@singular}", dom_class(@record, :custom_prefix)
|
||||
end
|
||||
|
||||
def test_singular_class_name
|
||||
assert_equal @singular, singular_class_name(@record)
|
||||
end
|
||||
|
||||
def test_singular_class_name_for_class
|
||||
assert_equal @singular, singular_class_name(@klass)
|
||||
end
|
||||
|
||||
def test_plural_class_name
|
||||
assert_equal @plural, plural_class_name(@record)
|
||||
end
|
||||
|
||||
def test_plural_class_name_for_class
|
||||
assert_equal @plural, plural_class_name(@klass)
|
||||
end
|
||||
|
||||
private
|
||||
def method_missing(method, *args)
|
||||
RecordIdentifier.send(method, *args)
|
||||
end
|
||||
end
|
||||
|
||||
class NestedRecordIdentifierTest < RecordIdentifierTest
|
||||
def setup
|
||||
@klass = Comment::Nested
|
||||
@record = @klass.new
|
||||
@singular = 'comment_nested'
|
||||
@plural = 'comment_nesteds'
|
||||
end
|
||||
|
||||
def test_partial_path
|
||||
expected = "comment/nesteds/nested"
|
||||
assert_equal expected, partial_path(@record)
|
||||
assert_equal expected, partial_path(Comment::Nested)
|
||||
end
|
||||
|
||||
def test_partial_path_with_namespaced_controller_path
|
||||
expected = "admin/comment/nesteds/nested"
|
||||
assert_equal expected, partial_path(@record, "admin/posts")
|
||||
assert_equal expected, partial_path(@klass, "admin/posts")
|
||||
end
|
||||
|
||||
def test_partial_path_with_deeper_namespaced_controller_path
|
||||
expected = "deeper/admin/comment/nesteds/nested"
|
||||
assert_equal expected, partial_path(@record, "deeper/admin/posts")
|
||||
assert_equal expected, partial_path(@klass, "deeper/admin/posts")
|
||||
end
|
||||
|
||||
def test_partial_path_with_even_deeper_namespaced_controller_path
|
||||
expected = "even/more/deeper/admin/comment/nesteds/nested"
|
||||
assert_equal expected, partial_path(@record, "even/more/deeper/admin/posts")
|
||||
assert_equal expected, partial_path(@klass, "even/more/deeper/admin/posts")
|
||||
end
|
||||
|
||||
def test_partial_path_with_not_namespaced_controller_path
|
||||
expected = "comment/nesteds/nested"
|
||||
assert_equal expected, partial_path(@record, "posts")
|
||||
assert_equal expected, partial_path(@klass, "posts")
|
||||
end
|
||||
end
|
|
@ -1,289 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class WorkshopsController < ActionController::Base
|
||||
end
|
||||
|
||||
class Workshop
|
||||
attr_accessor :id, :new_record
|
||||
|
||||
def initialize(id, new_record)
|
||||
@id, @new_record = id, new_record
|
||||
end
|
||||
|
||||
def new_record?
|
||||
@new_record
|
||||
end
|
||||
|
||||
def to_s
|
||||
id.to_s
|
||||
end
|
||||
end
|
||||
|
||||
class RedirectController < ActionController::Base
|
||||
def simple_redirect
|
||||
redirect_to :action => "hello_world"
|
||||
end
|
||||
|
||||
def redirect_with_status
|
||||
redirect_to({:action => "hello_world", :status => 301})
|
||||
end
|
||||
|
||||
def redirect_with_status_hash
|
||||
redirect_to({:action => "hello_world"}, {:status => 301})
|
||||
end
|
||||
|
||||
def url_redirect_with_status
|
||||
redirect_to("http://www.example.com", :status => :moved_permanently)
|
||||
end
|
||||
|
||||
def url_redirect_with_status_hash
|
||||
redirect_to("http://www.example.com", {:status => 301})
|
||||
end
|
||||
|
||||
def relative_url_redirect_with_status
|
||||
redirect_to("/things/stuff", :status => :found)
|
||||
end
|
||||
|
||||
def relative_url_redirect_with_status_hash
|
||||
redirect_to("/things/stuff", {:status => 301})
|
||||
end
|
||||
|
||||
def redirect_to_back_with_status
|
||||
redirect_to :back, :status => 307
|
||||
end
|
||||
|
||||
def host_redirect
|
||||
redirect_to :action => "other_host", :only_path => false, :host => 'other.test.host'
|
||||
end
|
||||
|
||||
def module_redirect
|
||||
redirect_to :controller => 'module_test/module_redirect', :action => "hello_world"
|
||||
end
|
||||
|
||||
def redirect_with_assigns
|
||||
@hello = "world"
|
||||
redirect_to :action => "hello_world"
|
||||
end
|
||||
|
||||
def redirect_to_url
|
||||
redirect_to "http://www.rubyonrails.org/"
|
||||
end
|
||||
|
||||
def redirect_to_url_with_unescaped_query_string
|
||||
redirect_to "http://dev.rubyonrails.org/query?status=new"
|
||||
end
|
||||
|
||||
def redirect_to_back
|
||||
redirect_to :back
|
||||
end
|
||||
|
||||
def redirect_to_existing_record
|
||||
redirect_to Workshop.new(5, false)
|
||||
end
|
||||
|
||||
def redirect_to_new_record
|
||||
redirect_to Workshop.new(5, true)
|
||||
end
|
||||
|
||||
def redirect_to_nil
|
||||
redirect_to nil
|
||||
end
|
||||
|
||||
def rescue_errors(e) raise e end
|
||||
|
||||
def rescue_action(e) raise end
|
||||
|
||||
protected
|
||||
def dashbord_url(id, message)
|
||||
url_for :action => "dashboard", :params => { "id" => id, "message" => message }
|
||||
end
|
||||
end
|
||||
|
||||
class RedirectTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = RedirectController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
def test_simple_redirect
|
||||
get :simple_redirect
|
||||
assert_response :redirect
|
||||
assert_equal "http://test.host/redirect/hello_world", redirect_to_url
|
||||
end
|
||||
|
||||
def test_redirect_with_no_status
|
||||
get :simple_redirect
|
||||
assert_response 302
|
||||
assert_equal "http://test.host/redirect/hello_world", redirect_to_url
|
||||
end
|
||||
|
||||
def test_redirect_with_status
|
||||
get :redirect_with_status
|
||||
assert_response 301
|
||||
assert_equal "http://test.host/redirect/hello_world", redirect_to_url
|
||||
end
|
||||
|
||||
def test_redirect_with_status_hash
|
||||
get :redirect_with_status_hash
|
||||
assert_response 301
|
||||
assert_equal "http://test.host/redirect/hello_world", redirect_to_url
|
||||
end
|
||||
|
||||
def test_url_redirect_with_status
|
||||
get :url_redirect_with_status
|
||||
assert_response 301
|
||||
assert_equal "http://www.example.com", redirect_to_url
|
||||
end
|
||||
|
||||
def test_url_redirect_with_status_hash
|
||||
get :url_redirect_with_status_hash
|
||||
assert_response 301
|
||||
assert_equal "http://www.example.com", redirect_to_url
|
||||
end
|
||||
|
||||
|
||||
def test_relative_url_redirect_with_status
|
||||
get :relative_url_redirect_with_status
|
||||
assert_response 302
|
||||
assert_equal "http://test.host/things/stuff", redirect_to_url
|
||||
end
|
||||
|
||||
def test_relative_url_redirect_with_status_hash
|
||||
get :relative_url_redirect_with_status_hash
|
||||
assert_response 301
|
||||
assert_equal "http://test.host/things/stuff", redirect_to_url
|
||||
end
|
||||
|
||||
def test_redirect_to_back_with_status
|
||||
@request.env["HTTP_REFERER"] = "http://www.example.com/coming/from"
|
||||
get :redirect_to_back_with_status
|
||||
assert_response 307
|
||||
assert_equal "http://www.example.com/coming/from", redirect_to_url
|
||||
end
|
||||
|
||||
def test_simple_redirect_using_options
|
||||
get :host_redirect
|
||||
assert_response :redirect
|
||||
assert_redirected_to :action => "other_host", :only_path => false, :host => 'other.test.host'
|
||||
end
|
||||
|
||||
def test_redirect_error_with_pretty_diff
|
||||
get :host_redirect
|
||||
assert_response :redirect
|
||||
begin
|
||||
assert_redirected_to :action => "other_host", :only_path => true
|
||||
rescue Test::Unit::AssertionFailedError => err
|
||||
expected_msg, redirection_msg, diff_msg = err.message.scan(/<\{[^\}]+\}>/).collect { |s| s[2..-3] }
|
||||
assert_match %r("only_path"=>false), redirection_msg
|
||||
assert_match %r("host"=>"other.test.host"), redirection_msg
|
||||
assert_match %r("action"=>"other_host"), redirection_msg
|
||||
assert_match %r("only_path"=>false), diff_msg
|
||||
assert_match %r("host"=>"other.test.host"), diff_msg
|
||||
end
|
||||
end
|
||||
|
||||
def test_module_redirect
|
||||
get :module_redirect
|
||||
assert_response :redirect
|
||||
assert_redirected_to "http://test.host/module_test/module_redirect/hello_world"
|
||||
end
|
||||
|
||||
def test_module_redirect_using_options
|
||||
get :module_redirect
|
||||
assert_response :redirect
|
||||
assert_redirected_to :controller => 'module_test/module_redirect', :action => 'hello_world'
|
||||
end
|
||||
|
||||
def test_redirect_with_assigns
|
||||
get :redirect_with_assigns
|
||||
assert_response :redirect
|
||||
assert_equal "world", assigns["hello"]
|
||||
end
|
||||
|
||||
def test_redirect_to_url
|
||||
get :redirect_to_url
|
||||
assert_response :redirect
|
||||
assert_redirected_to "http://www.rubyonrails.org/"
|
||||
end
|
||||
|
||||
def test_redirect_to_url_with_unescaped_query_string
|
||||
get :redirect_to_url_with_unescaped_query_string
|
||||
assert_response :redirect
|
||||
assert_redirected_to "http://dev.rubyonrails.org/query?status=new"
|
||||
end
|
||||
|
||||
def test_redirect_to_back
|
||||
@request.env["HTTP_REFERER"] = "http://www.example.com/coming/from"
|
||||
get :redirect_to_back
|
||||
assert_response :redirect
|
||||
assert_equal "http://www.example.com/coming/from", redirect_to_url
|
||||
end
|
||||
|
||||
def test_redirect_to_back_with_no_referer
|
||||
assert_raises(ActionController::RedirectBackError) {
|
||||
@request.env["HTTP_REFERER"] = nil
|
||||
get :redirect_to_back
|
||||
}
|
||||
end
|
||||
|
||||
def test_redirect_to_record
|
||||
ActionController::Routing::Routes.draw do |map|
|
||||
map.resources :workshops
|
||||
map.connect ':controller/:action/:id'
|
||||
end
|
||||
|
||||
get :redirect_to_existing_record
|
||||
assert_equal "http://test.host/workshops/5", redirect_to_url
|
||||
|
||||
get :redirect_to_new_record
|
||||
assert_equal "http://test.host/workshops", redirect_to_url
|
||||
end
|
||||
|
||||
def test_redirect_to_nil
|
||||
assert_raises(ActionController::ActionControllerError) do
|
||||
get :redirect_to_nil
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
module ModuleTest
|
||||
class ModuleRedirectController < ::RedirectController
|
||||
def module_redirect
|
||||
redirect_to :controller => '/redirect', :action => "hello_world"
|
||||
end
|
||||
end
|
||||
|
||||
class ModuleRedirectTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = ModuleRedirectController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
def test_simple_redirect
|
||||
get :simple_redirect
|
||||
assert_response :redirect
|
||||
assert_equal "http://test.host/module_test/module_redirect/hello_world", redirect_to_url
|
||||
end
|
||||
|
||||
def test_simple_redirect_using_options
|
||||
get :host_redirect
|
||||
assert_response :redirect
|
||||
assert_redirected_to :action => "other_host", :only_path => false, :host => 'other.test.host'
|
||||
end
|
||||
|
||||
def test_module_redirect
|
||||
get :module_redirect
|
||||
assert_response :redirect
|
||||
assert_equal "http://test.host/redirect/hello_world", redirect_to_url
|
||||
end
|
||||
|
||||
def test_module_redirect_using_options
|
||||
get :module_redirect
|
||||
assert_response :redirect
|
||||
assert_redirected_to :controller => 'redirect', :action => "hello_world"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,527 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
require 'controller/fake_models'
|
||||
|
||||
module Fun
|
||||
class GamesController < ActionController::Base
|
||||
def hello_world
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# FIXME: crashes Ruby 1.9
|
||||
class TestController < ActionController::Base
|
||||
layout :determine_layout
|
||||
|
||||
def hello_world
|
||||
end
|
||||
|
||||
def render_hello_world
|
||||
render :template => "test/hello_world"
|
||||
end
|
||||
|
||||
def render_hello_world_with_forward_slash
|
||||
render :template => "/test/hello_world"
|
||||
end
|
||||
|
||||
def render_template_in_top_directory
|
||||
render :template => 'shared'
|
||||
end
|
||||
|
||||
def render_template_in_top_directory_with_slash
|
||||
render :template => '/shared'
|
||||
end
|
||||
|
||||
def render_hello_world_from_variable
|
||||
@person = "david"
|
||||
render :text => "hello #{@person}"
|
||||
end
|
||||
|
||||
def render_action_hello_world
|
||||
render :action => "hello_world"
|
||||
end
|
||||
|
||||
def render_action_hello_world_with_symbol
|
||||
render :action => :hello_world
|
||||
end
|
||||
|
||||
def render_text_hello_world
|
||||
render :text => "hello world"
|
||||
end
|
||||
|
||||
def render_json_hello_world
|
||||
render :json => {:hello => 'world'}.to_json
|
||||
end
|
||||
|
||||
def render_json_hello_world_with_callback
|
||||
render :json => {:hello => 'world'}.to_json, :callback => 'alert'
|
||||
end
|
||||
|
||||
def render_json_with_custom_content_type
|
||||
render :json => {:hello => 'world'}.to_json, :content_type => 'text/javascript'
|
||||
end
|
||||
|
||||
def render_symbol_json
|
||||
render :json => {:hello => 'world'}.to_json
|
||||
end
|
||||
|
||||
def render_custom_code
|
||||
render :text => "hello world", :status => 404
|
||||
end
|
||||
|
||||
def render_custom_code_rjs
|
||||
render :update, :status => 404 do |page|
|
||||
page.replace :foo, :partial => 'partial'
|
||||
end
|
||||
end
|
||||
|
||||
def render_text_with_nil
|
||||
render :text => nil
|
||||
end
|
||||
|
||||
def render_text_with_false
|
||||
render :text => false
|
||||
end
|
||||
|
||||
def render_nothing_with_appendix
|
||||
render :text => "appended"
|
||||
end
|
||||
|
||||
def render_invalid_args
|
||||
render("test/hello")
|
||||
end
|
||||
|
||||
def render_xml_hello
|
||||
@name = "David"
|
||||
render :template => "test/hello"
|
||||
end
|
||||
|
||||
def render_xml_with_custom_content_type
|
||||
render :xml => "<blah/>", :content_type => "application/atomsvc+xml"
|
||||
end
|
||||
|
||||
def render_line_offset
|
||||
begin
|
||||
render :inline => '<% raise %>', :locals => {:foo => 'bar'}
|
||||
rescue RuntimeError => exc
|
||||
end
|
||||
line = exc.backtrace.first
|
||||
render :text => line
|
||||
end
|
||||
|
||||
def heading
|
||||
head :ok
|
||||
end
|
||||
|
||||
def greeting
|
||||
# let's just rely on the template
|
||||
end
|
||||
|
||||
def layout_test
|
||||
render :action => "hello_world"
|
||||
end
|
||||
|
||||
def builder_layout_test
|
||||
render :action => "hello"
|
||||
end
|
||||
|
||||
def builder_partial_test
|
||||
render :action => "hello_world_container"
|
||||
end
|
||||
|
||||
def partials_list
|
||||
@test_unchanged = 'hello'
|
||||
@customers = [ Customer.new("david"), Customer.new("mary") ]
|
||||
render :action => "list"
|
||||
end
|
||||
|
||||
def partial_only
|
||||
render :partial => true
|
||||
end
|
||||
|
||||
def hello_in_a_string
|
||||
@customers = [ Customer.new("david"), Customer.new("mary") ]
|
||||
render :text => "How's there? " + render_to_string(:template => "test/list")
|
||||
end
|
||||
|
||||
def accessing_params_in_template
|
||||
render :inline => "Hello: <%= params[:name] %>"
|
||||
end
|
||||
|
||||
def accessing_local_assigns_in_inline_template
|
||||
name = params[:local_name]
|
||||
render :inline => "<%= 'Goodbye, ' + local_name %>",
|
||||
:locals => { :local_name => name }
|
||||
end
|
||||
|
||||
def formatted_html_erb
|
||||
end
|
||||
|
||||
def formatted_xml_erb
|
||||
end
|
||||
|
||||
def render_to_string_test
|
||||
@foo = render_to_string :inline => "this is a test"
|
||||
end
|
||||
|
||||
def partial
|
||||
render :partial => 'partial'
|
||||
end
|
||||
|
||||
def partial_dot_html
|
||||
render :partial => 'partial.html.erb'
|
||||
end
|
||||
|
||||
def partial_as_rjs
|
||||
render :update do |page|
|
||||
page.replace :foo, :partial => 'partial'
|
||||
end
|
||||
end
|
||||
|
||||
def respond_to_partial_as_rjs
|
||||
respond_to do |format|
|
||||
format.js do
|
||||
render :update do |page|
|
||||
page.replace :foo, :partial => 'partial'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def default_render
|
||||
if @alternate_default_render
|
||||
@alternate_default_render.call
|
||||
else
|
||||
render
|
||||
end
|
||||
end
|
||||
|
||||
def render_alternate_default
|
||||
# For this test, the method "default_render" is overridden:
|
||||
@alternate_default_render = lambda {
|
||||
render :update do |page|
|
||||
page.replace :foo, :partial => 'partial'
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
def rescue_action(e) raise end
|
||||
|
||||
private
|
||||
def determine_layout
|
||||
case action_name
|
||||
when "layout_test"; "layouts/standard"
|
||||
when "builder_layout_test"; "layouts/builder"
|
||||
when "render_symbol_json"; "layouts/standard" # to make sure layouts don't interfere
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
TestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
Fun::GamesController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
|
||||
class RenderTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
@controller = TestController.new
|
||||
|
||||
@request.host = "www.nextangle.com"
|
||||
end
|
||||
|
||||
def test_simple_show
|
||||
get :hello_world
|
||||
assert_response 200
|
||||
assert_template "test/hello_world"
|
||||
end
|
||||
|
||||
def test_render
|
||||
get :render_hello_world
|
||||
assert_template "test/hello_world"
|
||||
end
|
||||
|
||||
def test_line_offset
|
||||
get :render_line_offset
|
||||
line = @response.body
|
||||
assert(line =~ %r{:(\d+):})
|
||||
assert_equal "1", $1
|
||||
end
|
||||
|
||||
def test_render_with_forward_slash
|
||||
get :render_hello_world_with_forward_slash
|
||||
assert_template "test/hello_world"
|
||||
end
|
||||
|
||||
def test_render_in_top_directory
|
||||
get :render_template_in_top_directory
|
||||
assert_template "shared"
|
||||
assert_equal "Elastica", @response.body
|
||||
end
|
||||
|
||||
def test_render_in_top_directory_with_slash
|
||||
get :render_template_in_top_directory_with_slash
|
||||
assert_template "shared"
|
||||
assert_equal "Elastica", @response.body
|
||||
end
|
||||
|
||||
def test_render_from_variable
|
||||
get :render_hello_world_from_variable
|
||||
assert_equal "hello david", @response.body
|
||||
end
|
||||
|
||||
def test_render_action
|
||||
get :render_action_hello_world
|
||||
assert_template "test/hello_world"
|
||||
end
|
||||
|
||||
def test_render_action_with_symbol
|
||||
get :render_action_hello_world_with_symbol
|
||||
assert_template "test/hello_world"
|
||||
end
|
||||
|
||||
def test_render_text
|
||||
get :render_text_hello_world
|
||||
assert_equal "hello world", @response.body
|
||||
end
|
||||
|
||||
def test_render_json
|
||||
get :render_json_hello_world
|
||||
assert_equal '{"hello": "world"}', @response.body
|
||||
assert_equal 'application/json', @response.content_type
|
||||
end
|
||||
|
||||
def test_render_json_with_callback
|
||||
get :render_json_hello_world_with_callback
|
||||
assert_equal 'alert({"hello": "world"})', @response.body
|
||||
assert_equal 'application/json', @response.content_type
|
||||
end
|
||||
|
||||
def test_render_json_with_custom_content_type
|
||||
get :render_json_with_custom_content_type
|
||||
assert_equal '{"hello": "world"}', @response.body
|
||||
assert_equal 'text/javascript', @response.content_type
|
||||
end
|
||||
|
||||
def test_render_symbol_json
|
||||
get :render_symbol_json
|
||||
assert_equal '{"hello": "world"}', @response.body
|
||||
assert_equal 'application/json', @response.content_type
|
||||
end
|
||||
|
||||
def test_render_custom_code
|
||||
get :render_custom_code
|
||||
assert_response 404
|
||||
assert_equal 'hello world', @response.body
|
||||
end
|
||||
|
||||
def test_render_custom_code_rjs
|
||||
get :render_custom_code_rjs
|
||||
assert_response 404
|
||||
assert_equal %(Element.replace("foo", "partial html");), @response.body
|
||||
end
|
||||
|
||||
def test_render_text_with_nil
|
||||
get :render_text_with_nil
|
||||
assert_response 200
|
||||
assert_equal '', @response.body
|
||||
end
|
||||
|
||||
def test_render_text_with_false
|
||||
get :render_text_with_false
|
||||
assert_equal 'false', @response.body
|
||||
end
|
||||
|
||||
def test_render_nothing_with_appendix
|
||||
get :render_nothing_with_appendix
|
||||
assert_response 200
|
||||
assert_equal 'appended', @response.body
|
||||
end
|
||||
|
||||
def test_attempt_to_render_with_invalid_arguments
|
||||
assert_raises(ActionController::RenderError) { get :render_invalid_args }
|
||||
end
|
||||
|
||||
def test_attempt_to_access_object_method
|
||||
assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
|
||||
end
|
||||
|
||||
def test_private_methods
|
||||
assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
|
||||
end
|
||||
|
||||
def test_render_xml
|
||||
get :render_xml_hello
|
||||
assert_equal "<html>\n <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
|
||||
assert_equal "application/xml", @response.content_type
|
||||
end
|
||||
|
||||
def test_render_xml_with_default
|
||||
get :greeting
|
||||
assert_equal "<p>This is grand!</p>\n", @response.body
|
||||
end
|
||||
|
||||
def test_render_xml_with_partial
|
||||
get :builder_partial_test
|
||||
assert_equal "<test>\n <hello/>\n</test>\n", @response.body
|
||||
end
|
||||
|
||||
def test_layout_rendering
|
||||
get :layout_test
|
||||
assert_equal "<html>Hello world!</html>", @response.body
|
||||
end
|
||||
|
||||
def test_render_xml_with_layouts
|
||||
get :builder_layout_test
|
||||
assert_equal "<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", @response.body
|
||||
end
|
||||
|
||||
# def test_partials_list
|
||||
# get :partials_list
|
||||
# assert_equal "goodbyeHello: davidHello: marygoodbye\n", process_request.body
|
||||
# end
|
||||
|
||||
def test_partial_only
|
||||
get :partial_only
|
||||
assert_equal "only partial", @response.body
|
||||
end
|
||||
|
||||
def test_render_to_string
|
||||
get :hello_in_a_string
|
||||
assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
|
||||
end
|
||||
|
||||
def test_render_to_string_resets_assigns
|
||||
get :render_to_string_test
|
||||
assert_equal "The value of foo is: ::this is a test::\n", @response.body
|
||||
end
|
||||
|
||||
def test_nested_rendering
|
||||
@controller = Fun::GamesController.new
|
||||
get :hello_world
|
||||
assert_equal "Living in a nested world", @response.body
|
||||
end
|
||||
|
||||
def test_accessing_params_in_template
|
||||
get :accessing_params_in_template, :name => "David"
|
||||
assert_equal "Hello: David", @response.body
|
||||
end
|
||||
|
||||
def test_accessing_local_assigns_in_inline_template
|
||||
get :accessing_local_assigns_in_inline_template, :local_name => "Local David"
|
||||
assert_equal "Goodbye, Local David", @response.body
|
||||
end
|
||||
|
||||
def test_render_200_should_set_etag
|
||||
get :render_hello_world_from_variable
|
||||
assert_equal etag_for("hello david"), @response.headers['ETag']
|
||||
assert_equal "private, max-age=0, must-revalidate", @response.headers['Cache-Control']
|
||||
end
|
||||
|
||||
def test_render_against_etag_request_should_304_when_match
|
||||
@request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello david")
|
||||
get :render_hello_world_from_variable
|
||||
assert_equal "304 Not Modified", @response.headers['Status']
|
||||
assert @response.body.empty?
|
||||
end
|
||||
|
||||
def test_render_against_etag_request_should_200_when_no_match
|
||||
@request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello somewhere else")
|
||||
get :render_hello_world_from_variable
|
||||
assert_equal "200 OK", @response.headers['Status']
|
||||
assert !@response.body.empty?
|
||||
end
|
||||
|
||||
def test_render_with_etag
|
||||
get :render_hello_world_from_variable
|
||||
expected_etag = etag_for('hello david')
|
||||
assert_equal expected_etag, @response.headers['ETag']
|
||||
|
||||
@request.headers["HTTP_IF_NONE_MATCH"] = expected_etag
|
||||
get :render_hello_world_from_variable
|
||||
assert_equal "304 Not Modified", @response.headers['Status']
|
||||
|
||||
@request.headers["HTTP_IF_NONE_MATCH"] = "\"diftag\""
|
||||
get :render_hello_world_from_variable
|
||||
assert_equal "200 OK", @response.headers['Status']
|
||||
end
|
||||
|
||||
def render_with_404_shouldnt_have_etag
|
||||
get :render_custom_code
|
||||
assert_nil @response.headers['ETag']
|
||||
end
|
||||
|
||||
def test_etag_should_not_be_changed_when_already_set
|
||||
expected_etag = etag_for("hello somewhere else")
|
||||
@response.headers["ETag"] = expected_etag
|
||||
get :render_hello_world_from_variable
|
||||
assert_equal expected_etag, @response.headers['ETag']
|
||||
end
|
||||
|
||||
def test_etag_should_govern_renders_with_layouts_too
|
||||
get :builder_layout_test
|
||||
assert_equal "<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", @response.body
|
||||
assert_equal etag_for("<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n"), @response.headers['ETag']
|
||||
end
|
||||
|
||||
def test_should_render_formatted_template
|
||||
get :formatted_html_erb
|
||||
assert_equal 'formatted html erb', @response.body
|
||||
end
|
||||
|
||||
def test_should_render_formatted_xml_erb_template
|
||||
get :formatted_xml_erb, :format => :xml
|
||||
assert_equal '<test>passed formatted xml erb</test>', @response.body
|
||||
end
|
||||
|
||||
def test_should_render_formatted_html_erb_template
|
||||
get :formatted_xml_erb
|
||||
assert_equal '<test>passed formatted html erb</test>', @response.body
|
||||
end
|
||||
|
||||
def test_should_render_formatted_html_erb_template_with_faulty_accepts_header
|
||||
@request.env["HTTP_ACCEPT"] = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, appliction/x-shockwave-flash, */*"
|
||||
get :formatted_xml_erb
|
||||
assert_equal '<test>passed formatted html erb</test>', @response.body
|
||||
end
|
||||
|
||||
def test_should_render_html_formatted_partial
|
||||
get :partial
|
||||
assert_equal 'partial html', @response.body
|
||||
end
|
||||
|
||||
def test_should_render_html_partial_with_dot
|
||||
get :partial_dot_html
|
||||
assert_equal 'partial html', @response.body
|
||||
end
|
||||
|
||||
def test_should_render_html_formatted_partial_with_rjs
|
||||
xhr :get, :partial_as_rjs
|
||||
assert_equal %(Element.replace("foo", "partial html");), @response.body
|
||||
end
|
||||
|
||||
def test_should_render_html_formatted_partial_with_rjs_and_js_format
|
||||
xhr :get, :respond_to_partial_as_rjs
|
||||
assert_equal %(Element.replace("foo", "partial html");), @response.body
|
||||
end
|
||||
|
||||
def test_should_render_js_partial
|
||||
xhr :get, :partial, :format => 'js'
|
||||
assert_equal 'partial js', @response.body
|
||||
end
|
||||
|
||||
def test_should_render_with_alternate_default_render
|
||||
xhr :get, :render_alternate_default
|
||||
assert_equal %(Element.replace("foo", "partial html");), @response.body
|
||||
end
|
||||
|
||||
def test_should_render_xml_but_keep_custom_content_type
|
||||
get :render_xml_with_custom_content_type
|
||||
assert_equal "application/atomsvc+xml", @response.content_type
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def etag_for(text)
|
||||
%("#{Digest::MD5.hexdigest(text)}")
|
||||
end
|
||||
end
|
|
@ -1,313 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
require 'digest/sha1'
|
||||
|
||||
ActionController::Routing::Routes.draw do |map|
|
||||
map.connect ':controller/:action/:id'
|
||||
end
|
||||
|
||||
# simulates cookie session store
|
||||
class FakeSessionDbMan
|
||||
def self.generate_digest(data)
|
||||
Digest::SHA1.hexdigest("secure")
|
||||
end
|
||||
end
|
||||
|
||||
# common controller actions
|
||||
module RequestForgeryProtectionActions
|
||||
def index
|
||||
render :inline => "<%= form_tag('/') {} %>"
|
||||
end
|
||||
|
||||
def show_button
|
||||
render :inline => "<%= button_to('New', '/') {} %>"
|
||||
end
|
||||
|
||||
def remote_form
|
||||
render :inline => "<% form_remote_tag(:url => '/') {} %>"
|
||||
end
|
||||
|
||||
def unsafe
|
||||
render :text => 'pwn'
|
||||
end
|
||||
|
||||
def rescue_action(e) raise e end
|
||||
end
|
||||
|
||||
# sample controllers
|
||||
class RequestForgeryProtectionController < ActionController::Base
|
||||
include RequestForgeryProtectionActions
|
||||
protect_from_forgery :only => :index, :secret => 'abc'
|
||||
end
|
||||
|
||||
class RequestForgeryProtectionWithoutSecretController < ActionController::Base
|
||||
include RequestForgeryProtectionActions
|
||||
protect_from_forgery
|
||||
end
|
||||
|
||||
# no token is given, assume the cookie store is used
|
||||
class CsrfCookieMonsterController < ActionController::Base
|
||||
include RequestForgeryProtectionActions
|
||||
protect_from_forgery :only => :index
|
||||
end
|
||||
|
||||
# sessions are turned off
|
||||
class SessionOffController < ActionController::Base
|
||||
protect_from_forgery :secret => 'foobar'
|
||||
session :off
|
||||
def rescue_action(e) raise e end
|
||||
include RequestForgeryProtectionActions
|
||||
end
|
||||
|
||||
class FreeCookieController < CsrfCookieMonsterController
|
||||
self.allow_forgery_protection = false
|
||||
|
||||
def index
|
||||
render :inline => "<%= form_tag('/') {} %>"
|
||||
end
|
||||
|
||||
def show_button
|
||||
render :inline => "<%= button_to('New', '/') {} %>"
|
||||
end
|
||||
end
|
||||
|
||||
# common test methods
|
||||
|
||||
module RequestForgeryProtectionTests
|
||||
def teardown
|
||||
ActionController::Base.request_forgery_protection_token = nil
|
||||
end
|
||||
|
||||
def test_should_render_form_with_token_tag
|
||||
get :index
|
||||
assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token
|
||||
end
|
||||
|
||||
def test_should_render_button_to_with_token_tag
|
||||
get :show_button
|
||||
assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token
|
||||
end
|
||||
|
||||
def test_should_render_remote_form_with_only_one_token_parameter
|
||||
get :remote_form
|
||||
assert_equal 1, @response.body.scan(@token).size
|
||||
end
|
||||
|
||||
def test_should_allow_get
|
||||
get :index
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
def test_should_allow_post_without_token_on_unsafe_action
|
||||
post :unsafe
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
def test_should_not_allow_post_without_token
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) { post :index }
|
||||
end
|
||||
|
||||
def test_should_not_allow_put_without_token
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) { put :index }
|
||||
end
|
||||
|
||||
def test_should_not_allow_delete_without_token
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) { delete :index }
|
||||
end
|
||||
|
||||
def test_should_not_allow_api_formatted_post_without_token
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) do
|
||||
post :index, :format => 'xml'
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_not_allow_api_formatted_put_without_token
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) do
|
||||
put :index, :format => 'xml'
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_not_allow_api_formatted_delete_without_token
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) do
|
||||
delete :index, :format => 'xml'
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_not_allow_api_formatted_post_sent_as_url_encoded_form_without_token
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) do
|
||||
@request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
|
||||
post :index, :format => 'xml'
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_not_allow_api_formatted_put_sent_as_url_encoded_form_without_token
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) do
|
||||
@request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
|
||||
put :index, :format => 'xml'
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_not_allow_api_formatted_delete_sent_as_url_encoded_form_without_token
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) do
|
||||
@request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
|
||||
delete :index, :format => 'xml'
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_not_allow_api_formatted_post_sent_as_multipart_form_without_token
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) do
|
||||
@request.env['CONTENT_TYPE'] = Mime::MULTIPART_FORM.to_s
|
||||
post :index, :format => 'xml'
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_not_allow_api_formatted_put_sent_as_multipart_form_without_token
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) do
|
||||
@request.env['CONTENT_TYPE'] = Mime::MULTIPART_FORM.to_s
|
||||
put :index, :format => 'xml'
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_not_allow_api_formatted_delete_sent_as_multipart_form_without_token
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) do
|
||||
@request.env['CONTENT_TYPE'] = Mime::MULTIPART_FORM.to_s
|
||||
delete :index, :format => 'xml'
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_not_allow_xhr_post_without_token
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) { xhr :post, :index }
|
||||
end
|
||||
|
||||
def test_should_not_allow_xhr_put_without_token
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) { xhr :put, :index }
|
||||
end
|
||||
|
||||
def test_should_not_allow_xhr_delete_without_token
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) { xhr :delete, :index }
|
||||
end
|
||||
|
||||
def test_should_allow_post_with_token
|
||||
post :index, :authenticity_token => @token
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
def test_should_allow_put_with_token
|
||||
put :index, :authenticity_token => @token
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
def test_should_allow_delete_with_token
|
||||
delete :index, :authenticity_token => @token
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
def test_should_allow_post_with_xml
|
||||
@request.env['CONTENT_TYPE'] = Mime::XML.to_s
|
||||
post :index, :format => 'xml'
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
def test_should_allow_put_with_xml
|
||||
@request.env['CONTENT_TYPE'] = Mime::XML.to_s
|
||||
put :index, :format => 'xml'
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
def test_should_allow_delete_with_xml
|
||||
@request.env['CONTENT_TYPE'] = Mime::XML.to_s
|
||||
delete :index, :format => 'xml'
|
||||
assert_response :success
|
||||
end
|
||||
end
|
||||
|
||||
# OK let's get our test on
|
||||
|
||||
class RequestForgeryProtectionControllerTest < Test::Unit::TestCase
|
||||
include RequestForgeryProtectionTests
|
||||
def setup
|
||||
@controller = RequestForgeryProtectionController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
class << @request.session
|
||||
def session_id() '123' end
|
||||
end
|
||||
@token = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('SHA1'), 'abc', '123')
|
||||
ActionController::Base.request_forgery_protection_token = :authenticity_token
|
||||
end
|
||||
end
|
||||
|
||||
class RequestForgeryProtectionWithoutSecretControllerTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = RequestForgeryProtectionWithoutSecretController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
class << @request.session
|
||||
def session_id() '123' end
|
||||
end
|
||||
@token = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('SHA1'), 'abc', '123')
|
||||
ActionController::Base.request_forgery_protection_token = :authenticity_token
|
||||
end
|
||||
|
||||
def test_should_raise_error_without_secret
|
||||
assert_raises ActionController::InvalidAuthenticityToken do
|
||||
get :index
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class CsrfCookieMonsterControllerTest < Test::Unit::TestCase
|
||||
include RequestForgeryProtectionTests
|
||||
def setup
|
||||
@controller = CsrfCookieMonsterController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
class << @request.session
|
||||
attr_accessor :dbman
|
||||
end
|
||||
# simulate a cookie session store
|
||||
@request.session.dbman = FakeSessionDbMan
|
||||
@token = Digest::SHA1.hexdigest("secure")
|
||||
ActionController::Base.request_forgery_protection_token = :authenticity_token
|
||||
end
|
||||
end
|
||||
|
||||
class FreeCookieControllerTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = FreeCookieController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
@token = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('SHA1'), 'abc', '123')
|
||||
end
|
||||
|
||||
def test_should_not_render_form_with_token_tag
|
||||
get :index
|
||||
assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token, false
|
||||
end
|
||||
|
||||
def test_should_not_render_button_to_with_token_tag
|
||||
get :show_button
|
||||
assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token, false
|
||||
end
|
||||
|
||||
def test_should_allow_all_methods_without_token
|
||||
[:post, :put, :delete].each do |method|
|
||||
assert_nothing_raised { send(method, :index)}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class SessionOffControllerTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@controller = SessionOffController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
@token = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('SHA1'), 'abc', '123')
|
||||
end
|
||||
|
||||
def test_should_raise_correct_exception
|
||||
@request.session = {} # session(:off) doesn't appear to work with controller tests
|
||||
assert_raises(ActionController::InvalidAuthenticityToken) do
|
||||
post :index, :authenticity_token => @token
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,928 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
require 'action_controller/integration'
|
||||
|
||||
class RequestTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@request = ActionController::TestRequest.new
|
||||
end
|
||||
|
||||
def test_remote_ip
|
||||
assert_equal '0.0.0.0', @request.remote_ip
|
||||
|
||||
@request.remote_addr = '1.2.3.4'
|
||||
assert_equal '1.2.3.4', @request.remote_ip
|
||||
|
||||
@request.remote_addr = '1.2.3.4,3.4.5.6'
|
||||
assert_equal '1.2.3.4', @request.remote_ip
|
||||
|
||||
@request.env['HTTP_CLIENT_IP'] = '2.3.4.5'
|
||||
assert_equal '1.2.3.4', @request.remote_ip
|
||||
|
||||
@request.remote_addr = '192.168.0.1'
|
||||
assert_equal '2.3.4.5', @request.remote_ip
|
||||
@request.env.delete 'HTTP_CLIENT_IP'
|
||||
|
||||
@request.remote_addr = '1.2.3.4'
|
||||
@request.env['HTTP_X_FORWARDED_FOR'] = '3.4.5.6'
|
||||
assert_equal '1.2.3.4', @request.remote_ip
|
||||
|
||||
@request.remote_addr = '127.0.0.1'
|
||||
@request.env['HTTP_X_FORWARDED_FOR'] = '3.4.5.6'
|
||||
assert_equal '3.4.5.6', @request.remote_ip
|
||||
|
||||
@request.env['HTTP_X_FORWARDED_FOR'] = 'unknown,3.4.5.6'
|
||||
assert_equal '3.4.5.6', @request.remote_ip
|
||||
|
||||
@request.env['HTTP_X_FORWARDED_FOR'] = '172.16.0.1,3.4.5.6'
|
||||
assert_equal '3.4.5.6', @request.remote_ip
|
||||
|
||||
@request.env['HTTP_X_FORWARDED_FOR'] = '192.168.0.1,3.4.5.6'
|
||||
assert_equal '3.4.5.6', @request.remote_ip
|
||||
|
||||
@request.env['HTTP_X_FORWARDED_FOR'] = '10.0.0.1,3.4.5.6'
|
||||
assert_equal '3.4.5.6', @request.remote_ip
|
||||
|
||||
@request.env['HTTP_X_FORWARDED_FOR'] = '10.0.0.1, 10.0.0.1, 3.4.5.6'
|
||||
assert_equal '3.4.5.6', @request.remote_ip
|
||||
|
||||
@request.env['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,3.4.5.6'
|
||||
assert_equal '3.4.5.6', @request.remote_ip
|
||||
|
||||
@request.env['HTTP_X_FORWARDED_FOR'] = 'unknown,192.168.0.1'
|
||||
assert_equal 'unknown', @request.remote_ip
|
||||
|
||||
@request.env['HTTP_X_FORWARDED_FOR'] = '9.9.9.9, 3.4.5.6, 10.0.0.1, 172.31.4.4'
|
||||
assert_equal '3.4.5.6', @request.remote_ip
|
||||
|
||||
@request.env['HTTP_CLIENT_IP'] = '8.8.8.8'
|
||||
e = assert_raises(ActionController::ActionControllerError) {
|
||||
@request.remote_ip
|
||||
}
|
||||
assert_match /IP spoofing attack/, e.message
|
||||
assert_match /HTTP_X_FORWARDED_FOR="9.9.9.9, 3.4.5.6, 10.0.0.1, 172.31.4.4"/, e.message
|
||||
assert_match /HTTP_CLIENT_IP="8.8.8.8"/, e.message
|
||||
|
||||
@request.env['HTTP_X_FORWARDED_FOR'] = '8.8.8.8, 9.9.9.9'
|
||||
assert_equal '8.8.8.8', @request.remote_ip
|
||||
|
||||
@request.env.delete 'HTTP_CLIENT_IP'
|
||||
@request.env.delete 'HTTP_X_FORWARDED_FOR'
|
||||
end
|
||||
|
||||
def test_domains
|
||||
@request.host = "www.rubyonrails.org"
|
||||
assert_equal "rubyonrails.org", @request.domain
|
||||
|
||||
@request.host = "www.rubyonrails.co.uk"
|
||||
assert_equal "rubyonrails.co.uk", @request.domain(2)
|
||||
|
||||
@request.host = "192.168.1.200"
|
||||
assert_nil @request.domain
|
||||
|
||||
@request.host = "foo.192.168.1.200"
|
||||
assert_nil @request.domain
|
||||
|
||||
@request.host = "192.168.1.200.com"
|
||||
assert_equal "200.com", @request.domain
|
||||
|
||||
@request.host = nil
|
||||
assert_nil @request.domain
|
||||
end
|
||||
|
||||
def test_subdomains
|
||||
@request.host = "www.rubyonrails.org"
|
||||
assert_equal %w( www ), @request.subdomains
|
||||
|
||||
@request.host = "www.rubyonrails.co.uk"
|
||||
assert_equal %w( www ), @request.subdomains(2)
|
||||
|
||||
@request.host = "dev.www.rubyonrails.co.uk"
|
||||
assert_equal %w( dev www ), @request.subdomains(2)
|
||||
|
||||
@request.host = "foobar.foobar.com"
|
||||
assert_equal %w( foobar ), @request.subdomains
|
||||
|
||||
@request.host = "192.168.1.200"
|
||||
assert_equal [], @request.subdomains
|
||||
|
||||
@request.host = "foo.192.168.1.200"
|
||||
assert_equal [], @request.subdomains
|
||||
|
||||
@request.host = "192.168.1.200.com"
|
||||
assert_equal %w( 192 168 1 ), @request.subdomains
|
||||
|
||||
@request.host = nil
|
||||
assert_equal [], @request.subdomains
|
||||
end
|
||||
|
||||
def test_port_string
|
||||
@request.port = 80
|
||||
assert_equal "", @request.port_string
|
||||
|
||||
@request.port = 8080
|
||||
assert_equal ":8080", @request.port_string
|
||||
end
|
||||
|
||||
def test_relative_url_root
|
||||
@request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
|
||||
@request.env['SERVER_SOFTWARE'] = 'lighttpd/1.2.3'
|
||||
assert_equal '', @request.relative_url_root, "relative_url_root should be disabled on lighttpd"
|
||||
|
||||
@request.env['SERVER_SOFTWARE'] = 'apache/1.2.3 some random text'
|
||||
|
||||
@request.env['SCRIPT_NAME'] = nil
|
||||
assert_equal "", @request.relative_url_root
|
||||
|
||||
@request.env['SCRIPT_NAME'] = "/dispatch.cgi"
|
||||
assert_equal "", @request.relative_url_root
|
||||
|
||||
@request.env['SCRIPT_NAME'] = "/myapp.rb"
|
||||
assert_equal "", @request.relative_url_root
|
||||
|
||||
@request.relative_url_root = nil
|
||||
@request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
|
||||
assert_equal "/hieraki", @request.relative_url_root
|
||||
|
||||
@request.relative_url_root = nil
|
||||
@request.env['SCRIPT_NAME'] = "/collaboration/hieraki/dispatch.cgi"
|
||||
assert_equal "/collaboration/hieraki", @request.relative_url_root
|
||||
|
||||
# apache/scgi case
|
||||
@request.relative_url_root = nil
|
||||
@request.env['SCRIPT_NAME'] = "/collaboration/hieraki"
|
||||
assert_equal "/collaboration/hieraki", @request.relative_url_root
|
||||
|
||||
@request.relative_url_root = nil
|
||||
@request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
|
||||
@request.env['SERVER_SOFTWARE'] = 'lighttpd/1.2.3'
|
||||
@request.env['RAILS_RELATIVE_URL_ROOT'] = "/hieraki"
|
||||
assert_equal "/hieraki", @request.relative_url_root
|
||||
|
||||
# @env overrides path guess
|
||||
@request.relative_url_root = nil
|
||||
@request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
|
||||
@request.env['SERVER_SOFTWARE'] = 'apache/1.2.3 some random text'
|
||||
@request.env['RAILS_RELATIVE_URL_ROOT'] = "/real_url"
|
||||
assert_equal "/real_url", @request.relative_url_root
|
||||
end
|
||||
|
||||
def test_request_uri
|
||||
@request.env['SERVER_SOFTWARE'] = 'Apache 42.342.3432'
|
||||
|
||||
@request.relative_url_root = nil
|
||||
@request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri?mapped=1"
|
||||
assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
|
||||
assert_equal "/path/of/some/uri", @request.path
|
||||
|
||||
@request.relative_url_root = nil
|
||||
@request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri"
|
||||
assert_equal "/path/of/some/uri", @request.request_uri
|
||||
assert_equal "/path/of/some/uri", @request.path
|
||||
|
||||
@request.relative_url_root = nil
|
||||
@request.set_REQUEST_URI "/path/of/some/uri"
|
||||
assert_equal "/path/of/some/uri", @request.request_uri
|
||||
assert_equal "/path/of/some/uri", @request.path
|
||||
|
||||
@request.relative_url_root = nil
|
||||
@request.set_REQUEST_URI "/"
|
||||
assert_equal "/", @request.request_uri
|
||||
assert_equal "/", @request.path
|
||||
|
||||
@request.relative_url_root = nil
|
||||
@request.set_REQUEST_URI "/?m=b"
|
||||
assert_equal "/?m=b", @request.request_uri
|
||||
assert_equal "/", @request.path
|
||||
|
||||
@request.relative_url_root = nil
|
||||
@request.set_REQUEST_URI "/"
|
||||
@request.env['SCRIPT_NAME'] = "/dispatch.cgi"
|
||||
assert_equal "/", @request.request_uri
|
||||
assert_equal "/", @request.path
|
||||
|
||||
@request.relative_url_root = nil
|
||||
@request.set_REQUEST_URI "/hieraki/"
|
||||
@request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
|
||||
assert_equal "/hieraki/", @request.request_uri
|
||||
assert_equal "/", @request.path
|
||||
|
||||
@request.relative_url_root = nil
|
||||
@request.set_REQUEST_URI "/collaboration/hieraki/books/edit/2"
|
||||
@request.env['SCRIPT_NAME'] = "/collaboration/hieraki/dispatch.cgi"
|
||||
assert_equal "/collaboration/hieraki/books/edit/2", @request.request_uri
|
||||
assert_equal "/books/edit/2", @request.path
|
||||
|
||||
# The following tests are for when REQUEST_URI is not supplied (as in IIS)
|
||||
@request.relative_url_root = nil
|
||||
@request.set_REQUEST_URI nil
|
||||
@request.env['PATH_INFO'] = "/path/of/some/uri?mapped=1"
|
||||
@request.env['SCRIPT_NAME'] = nil #"/path/dispatch.rb"
|
||||
assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
|
||||
assert_equal "/path/of/some/uri", @request.path
|
||||
|
||||
@request.set_REQUEST_URI nil
|
||||
@request.relative_url_root = nil
|
||||
@request.env['PATH_INFO'] = "/path/of/some/uri?mapped=1"
|
||||
@request.env['SCRIPT_NAME'] = "/path/dispatch.rb"
|
||||
assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
|
||||
assert_equal "/of/some/uri", @request.path
|
||||
|
||||
@request.set_REQUEST_URI nil
|
||||
@request.relative_url_root = nil
|
||||
@request.env['PATH_INFO'] = "/path/of/some/uri"
|
||||
@request.env['SCRIPT_NAME'] = nil
|
||||
assert_equal "/path/of/some/uri", @request.request_uri
|
||||
assert_equal "/path/of/some/uri", @request.path
|
||||
|
||||
@request.set_REQUEST_URI nil
|
||||
@request.relative_url_root = nil
|
||||
@request.env['PATH_INFO'] = "/"
|
||||
assert_equal "/", @request.request_uri
|
||||
assert_equal "/", @request.path
|
||||
|
||||
@request.set_REQUEST_URI nil
|
||||
@request.relative_url_root = nil
|
||||
@request.env['PATH_INFO'] = "/?m=b"
|
||||
assert_equal "/?m=b", @request.request_uri
|
||||
assert_equal "/", @request.path
|
||||
|
||||
@request.set_REQUEST_URI nil
|
||||
@request.relative_url_root = nil
|
||||
@request.env['PATH_INFO'] = "/"
|
||||
@request.env['SCRIPT_NAME'] = "/dispatch.cgi"
|
||||
assert_equal "/", @request.request_uri
|
||||
assert_equal "/", @request.path
|
||||
|
||||
@request.set_REQUEST_URI nil
|
||||
@request.relative_url_root = nil
|
||||
@request.env['PATH_INFO'] = "/hieraki/"
|
||||
@request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
|
||||
assert_equal "/hieraki/", @request.request_uri
|
||||
assert_equal "/", @request.path
|
||||
|
||||
@request.set_REQUEST_URI '/hieraki/dispatch.cgi'
|
||||
@request.relative_url_root = '/hieraki'
|
||||
assert_equal "/dispatch.cgi", @request.path
|
||||
@request.relative_url_root = nil
|
||||
|
||||
@request.set_REQUEST_URI '/hieraki/dispatch.cgi'
|
||||
@request.relative_url_root = '/foo'
|
||||
assert_equal "/hieraki/dispatch.cgi", @request.path
|
||||
@request.relative_url_root = nil
|
||||
|
||||
# This test ensures that Rails uses REQUEST_URI over PATH_INFO
|
||||
@request.relative_url_root = nil
|
||||
@request.env['REQUEST_URI'] = "/some/path"
|
||||
@request.env['PATH_INFO'] = "/another/path"
|
||||
@request.env['SCRIPT_NAME'] = "/dispatch.cgi"
|
||||
assert_equal "/some/path", @request.request_uri
|
||||
assert_equal "/some/path", @request.path
|
||||
end
|
||||
|
||||
|
||||
def test_host_with_default_port
|
||||
@request.host = "rubyonrails.org"
|
||||
@request.port = 80
|
||||
assert_equal "rubyonrails.org", @request.host_with_port
|
||||
end
|
||||
|
||||
def test_host_with_non_default_port
|
||||
@request.host = "rubyonrails.org"
|
||||
@request.port = 81
|
||||
assert_equal "rubyonrails.org:81", @request.host_with_port
|
||||
end
|
||||
|
||||
def test_server_software
|
||||
assert_equal nil, @request.server_software
|
||||
|
||||
@request.env['SERVER_SOFTWARE'] = 'Apache3.422'
|
||||
assert_equal 'apache', @request.server_software
|
||||
|
||||
@request.env['SERVER_SOFTWARE'] = 'lighttpd(1.1.4)'
|
||||
assert_equal 'lighttpd', @request.server_software
|
||||
end
|
||||
|
||||
def test_xml_http_request
|
||||
assert !@request.xml_http_request?
|
||||
assert !@request.xhr?
|
||||
|
||||
@request.env['HTTP_X_REQUESTED_WITH'] = "DefinitelyNotAjax1.0"
|
||||
assert !@request.xml_http_request?
|
||||
assert !@request.xhr?
|
||||
|
||||
@request.env['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest"
|
||||
assert @request.xml_http_request?
|
||||
assert @request.xhr?
|
||||
end
|
||||
|
||||
def test_reports_ssl
|
||||
assert !@request.ssl?
|
||||
@request.env['HTTPS'] = 'on'
|
||||
assert @request.ssl?
|
||||
end
|
||||
|
||||
def test_reports_ssl_when_proxied_via_lighttpd
|
||||
assert !@request.ssl?
|
||||
@request.env['HTTP_X_FORWARDED_PROTO'] = 'https'
|
||||
assert @request.ssl?
|
||||
end
|
||||
|
||||
def test_symbolized_request_methods
|
||||
[:get, :post, :put, :delete].each do |method|
|
||||
set_request_method_to method
|
||||
assert_equal method, @request.method
|
||||
end
|
||||
end
|
||||
|
||||
def test_invalid_http_method_raises_exception
|
||||
set_request_method_to :random_method
|
||||
assert_raises(ActionController::UnknownHttpMethod) do
|
||||
@request.method
|
||||
end
|
||||
end
|
||||
|
||||
def test_allow_method_hacking_on_post
|
||||
set_request_method_to :post
|
||||
[:get, :head, :options, :put, :post, :delete].each do |method|
|
||||
@request.instance_eval { @parameters = { :_method => method } ; @request_method = nil }
|
||||
assert_equal(method == :head ? :get : method, @request.method)
|
||||
end
|
||||
end
|
||||
|
||||
def test_invalid_method_hacking_on_post_raises_exception
|
||||
set_request_method_to :post
|
||||
@request.instance_eval { @parameters = { :_method => :random_method } ; @request_method = nil }
|
||||
assert_raises(ActionController::UnknownHttpMethod) do
|
||||
@request.method
|
||||
end
|
||||
end
|
||||
|
||||
def test_restrict_method_hacking
|
||||
@request.instance_eval { @parameters = { :_method => 'put' } }
|
||||
[:get, :put, :delete].each do |method|
|
||||
set_request_method_to method
|
||||
assert_equal method, @request.method
|
||||
end
|
||||
end
|
||||
|
||||
def test_head_masquarading_as_get
|
||||
set_request_method_to :head
|
||||
assert_equal :get, @request.method
|
||||
assert @request.get?
|
||||
assert @request.head?
|
||||
end
|
||||
|
||||
def test_xml_format
|
||||
@request.instance_eval { @parameters = { :format => 'xml' } }
|
||||
assert_equal Mime::XML, @request.format
|
||||
end
|
||||
|
||||
def test_xhtml_format
|
||||
@request.instance_eval { @parameters = { :format => 'xhtml' } }
|
||||
assert_equal Mime::HTML, @request.format
|
||||
end
|
||||
|
||||
def test_txt_format
|
||||
@request.instance_eval { @parameters = { :format => 'txt' } }
|
||||
assert_equal Mime::TEXT, @request.format
|
||||
end
|
||||
|
||||
def test_nil_format
|
||||
@request.instance_eval { @parameters = { :format => nil } }
|
||||
@request.env["HTTP_ACCEPT"] = "text/javascript"
|
||||
assert_equal Mime::JS, @request.format
|
||||
end
|
||||
|
||||
def test_content_type
|
||||
@request.env["CONTENT_TYPE"] = "text/html"
|
||||
assert_equal Mime::HTML, @request.content_type
|
||||
end
|
||||
|
||||
def test_format_assignment_should_set_format
|
||||
@request.instance_eval { self.format = :txt }
|
||||
assert !@request.format.xml?
|
||||
@request.instance_eval { self.format = :xml }
|
||||
assert @request.format.xml?
|
||||
end
|
||||
|
||||
def test_content_no_type
|
||||
assert_equal nil, @request.content_type
|
||||
end
|
||||
|
||||
def test_content_type_xml
|
||||
@request.env["CONTENT_TYPE"] = "application/xml"
|
||||
assert_equal Mime::XML, @request.content_type
|
||||
end
|
||||
|
||||
def test_content_type_with_charset
|
||||
@request.env["CONTENT_TYPE"] = "application/xml; charset=UTF-8"
|
||||
assert_equal Mime::XML, @request.content_type
|
||||
end
|
||||
|
||||
def test_user_agent
|
||||
assert_not_nil @request.user_agent
|
||||
end
|
||||
|
||||
def test_parameters
|
||||
@request.instance_eval { @request_parameters = { "foo" => 1 } }
|
||||
@request.instance_eval { @query_parameters = { "bar" => 2 } }
|
||||
|
||||
assert_equal({"foo" => 1, "bar" => 2}, @request.parameters)
|
||||
assert_equal({"foo" => 1}, @request.request_parameters)
|
||||
assert_equal({"bar" => 2}, @request.query_parameters)
|
||||
end
|
||||
|
||||
protected
|
||||
def set_request_method_to(method)
|
||||
@request.env['REQUEST_METHOD'] = method.to_s.upcase
|
||||
@request.instance_eval { @request_method = nil }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class UrlEncodedRequestParameterParsingTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@query_string = "action=create_customer&full_name=David%20Heinemeier%20Hansson&customerId=1"
|
||||
@query_string_with_empty = "action=create_customer&full_name="
|
||||
@query_string_with_array = "action=create_customer&selected[]=1&selected[]=2&selected[]=3"
|
||||
@query_string_with_amps = "action=create_customer&name=Don%27t+%26+Does"
|
||||
@query_string_with_multiple_of_same_name =
|
||||
"action=update_order&full_name=Lau%20Taarnskov&products=4&products=2&products=3"
|
||||
@query_string_with_many_equal = "action=create_customer&full_name=abc=def=ghi"
|
||||
@query_string_without_equal = "action"
|
||||
@query_string_with_many_ampersands =
|
||||
"&action=create_customer&&&full_name=David%20Heinemeier%20Hansson"
|
||||
@query_string_with_empty_key = "action=create_customer&full_name=David%20Heinemeier%20Hansson&=Save"
|
||||
end
|
||||
|
||||
def test_query_string
|
||||
assert_equal(
|
||||
{ "action" => "create_customer", "full_name" => "David Heinemeier Hansson", "customerId" => "1"},
|
||||
ActionController::AbstractRequest.parse_query_parameters(@query_string)
|
||||
)
|
||||
end
|
||||
|
||||
def test_deep_query_string
|
||||
expected = {'x' => {'y' => {'z' => '10'}}}
|
||||
assert_equal(expected, ActionController::AbstractRequest.parse_query_parameters('x[y][z]=10'))
|
||||
end
|
||||
|
||||
def test_deep_query_string_with_array
|
||||
assert_equal({'x' => {'y' => {'z' => ['10']}}}, ActionController::AbstractRequest.parse_query_parameters('x[y][z][]=10'))
|
||||
assert_equal({'x' => {'y' => {'z' => ['10', '5']}}}, ActionController::AbstractRequest.parse_query_parameters('x[y][z][]=10&x[y][z][]=5'))
|
||||
end
|
||||
|
||||
def test_deep_query_string_with_array_of_hash
|
||||
assert_equal({'x' => {'y' => [{'z' => '10'}]}}, ActionController::AbstractRequest.parse_query_parameters('x[y][][z]=10'))
|
||||
assert_equal({'x' => {'y' => [{'z' => '10', 'w' => '10'}]}}, ActionController::AbstractRequest.parse_query_parameters('x[y][][z]=10&x[y][][w]=10'))
|
||||
end
|
||||
|
||||
def test_deep_query_string_with_array_of_hashes_with_one_pair
|
||||
assert_equal({'x' => {'y' => [{'z' => '10'}, {'z' => '20'}]}}, ActionController::AbstractRequest.parse_query_parameters('x[y][][z]=10&x[y][][z]=20'))
|
||||
assert_equal("10", ActionController::AbstractRequest.parse_query_parameters('x[y][][z]=10&x[y][][z]=20')["x"]["y"].first["z"])
|
||||
assert_equal("10", ActionController::AbstractRequest.parse_query_parameters('x[y][][z]=10&x[y][][z]=20').with_indifferent_access[:x][:y].first[:z])
|
||||
end
|
||||
|
||||
def test_deep_query_string_with_array_of_hashes_with_multiple_pairs
|
||||
assert_equal(
|
||||
{'x' => {'y' => [{'z' => '10', 'w' => 'a'}, {'z' => '20', 'w' => 'b'}]}},
|
||||
ActionController::AbstractRequest.parse_query_parameters('x[y][][z]=10&x[y][][w]=a&x[y][][z]=20&x[y][][w]=b')
|
||||
)
|
||||
end
|
||||
|
||||
def test_query_string_with_nil
|
||||
assert_equal(
|
||||
{ "action" => "create_customer", "full_name" => ''},
|
||||
ActionController::AbstractRequest.parse_query_parameters(@query_string_with_empty)
|
||||
)
|
||||
end
|
||||
|
||||
def test_query_string_with_array
|
||||
assert_equal(
|
||||
{ "action" => "create_customer", "selected" => ["1", "2", "3"]},
|
||||
ActionController::AbstractRequest.parse_query_parameters(@query_string_with_array)
|
||||
)
|
||||
end
|
||||
|
||||
def test_query_string_with_amps
|
||||
assert_equal(
|
||||
{ "action" => "create_customer", "name" => "Don't & Does"},
|
||||
ActionController::AbstractRequest.parse_query_parameters(@query_string_with_amps)
|
||||
)
|
||||
end
|
||||
|
||||
def test_query_string_with_many_equal
|
||||
assert_equal(
|
||||
{ "action" => "create_customer", "full_name" => "abc=def=ghi"},
|
||||
ActionController::AbstractRequest.parse_query_parameters(@query_string_with_many_equal)
|
||||
)
|
||||
end
|
||||
|
||||
def test_query_string_without_equal
|
||||
assert_equal(
|
||||
{ "action" => nil },
|
||||
ActionController::AbstractRequest.parse_query_parameters(@query_string_without_equal)
|
||||
)
|
||||
end
|
||||
|
||||
def test_query_string_with_empty_key
|
||||
assert_equal(
|
||||
{ "action" => "create_customer", "full_name" => "David Heinemeier Hansson" },
|
||||
ActionController::AbstractRequest.parse_query_parameters(@query_string_with_empty_key)
|
||||
)
|
||||
end
|
||||
|
||||
def test_query_string_with_many_ampersands
|
||||
assert_equal(
|
||||
{ "action" => "create_customer", "full_name" => "David Heinemeier Hansson"},
|
||||
ActionController::AbstractRequest.parse_query_parameters(@query_string_with_many_ampersands)
|
||||
)
|
||||
end
|
||||
|
||||
def test_unbalanced_query_string_with_array
|
||||
assert_equal(
|
||||
{'location' => ["1", "2"], 'age_group' => ["2"]},
|
||||
ActionController::AbstractRequest.parse_query_parameters("location[]=1&location[]=2&age_group[]=2")
|
||||
)
|
||||
assert_equal(
|
||||
{'location' => ["1", "2"], 'age_group' => ["2"]},
|
||||
ActionController::AbstractRequest.parse_request_parameters({'location[]' => ["1", "2"],
|
||||
'age_group[]' => ["2"]})
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
def test_request_hash_parsing
|
||||
query = {
|
||||
"note[viewers][viewer][][type]" => ["User", "Group"],
|
||||
"note[viewers][viewer][][id]" => ["1", "2"]
|
||||
}
|
||||
|
||||
expected = { "note" => { "viewers"=>{"viewer"=>[{ "id"=>"1", "type"=>"User"}, {"type"=>"Group", "id"=>"2"} ]} } }
|
||||
|
||||
assert_equal(expected, ActionController::AbstractRequest.parse_request_parameters(query))
|
||||
end
|
||||
|
||||
|
||||
def test_parse_params
|
||||
input = {
|
||||
"customers[boston][first][name]" => [ "David" ],
|
||||
"customers[boston][first][url]" => [ "http://David" ],
|
||||
"customers[boston][second][name]" => [ "Allan" ],
|
||||
"customers[boston][second][url]" => [ "http://Allan" ],
|
||||
"something_else" => [ "blah" ],
|
||||
"something_nil" => [ nil ],
|
||||
"something_empty" => [ "" ],
|
||||
"products[first]" => [ "Apple Computer" ],
|
||||
"products[second]" => [ "Pc" ],
|
||||
"" => [ 'Save' ]
|
||||
}
|
||||
|
||||
expected_output = {
|
||||
"customers" => {
|
||||
"boston" => {
|
||||
"first" => {
|
||||
"name" => "David",
|
||||
"url" => "http://David"
|
||||
},
|
||||
"second" => {
|
||||
"name" => "Allan",
|
||||
"url" => "http://Allan"
|
||||
}
|
||||
}
|
||||
},
|
||||
"something_else" => "blah",
|
||||
"something_empty" => "",
|
||||
"something_nil" => "",
|
||||
"products" => {
|
||||
"first" => "Apple Computer",
|
||||
"second" => "Pc"
|
||||
}
|
||||
}
|
||||
|
||||
assert_equal expected_output, ActionController::AbstractRequest.parse_request_parameters(input)
|
||||
end
|
||||
|
||||
UploadedStringIO = ActionController::UploadedStringIO
|
||||
class MockUpload < UploadedStringIO
|
||||
def initialize(content_type, original_path, *args)
|
||||
self.content_type = content_type
|
||||
self.original_path = original_path
|
||||
super *args
|
||||
end
|
||||
end
|
||||
|
||||
def test_parse_params_from_multipart_upload
|
||||
file = MockUpload.new('img/jpeg', 'foo.jpg')
|
||||
ie_file = MockUpload.new('img/jpeg', 'c:\\Documents and Settings\\foo\\Desktop\\bar.jpg')
|
||||
non_file_text_part = MockUpload.new('text/plain', '', 'abc')
|
||||
|
||||
input = {
|
||||
"something" => [ UploadedStringIO.new("") ],
|
||||
"array_of_stringios" => [[ UploadedStringIO.new("One"), UploadedStringIO.new("Two") ]],
|
||||
"mixed_types_array" => [[ UploadedStringIO.new("Three"), "NotStringIO" ]],
|
||||
"mixed_types_as_checkboxes[strings][nested]" => [[ file, "String", UploadedStringIO.new("StringIO")]],
|
||||
"ie_mixed_types_as_checkboxes[strings][nested]" => [[ ie_file, "String", UploadedStringIO.new("StringIO")]],
|
||||
"products[string]" => [ UploadedStringIO.new("Apple Computer") ],
|
||||
"products[file]" => [ file ],
|
||||
"ie_products[string]" => [ UploadedStringIO.new("Microsoft") ],
|
||||
"ie_products[file]" => [ ie_file ],
|
||||
"text_part" => [non_file_text_part]
|
||||
}
|
||||
|
||||
expected_output = {
|
||||
"something" => "",
|
||||
"array_of_stringios" => ["One", "Two"],
|
||||
"mixed_types_array" => [ "Three", "NotStringIO" ],
|
||||
"mixed_types_as_checkboxes" => {
|
||||
"strings" => {
|
||||
"nested" => [ file, "String", "StringIO" ]
|
||||
},
|
||||
},
|
||||
"ie_mixed_types_as_checkboxes" => {
|
||||
"strings" => {
|
||||
"nested" => [ ie_file, "String", "StringIO" ]
|
||||
},
|
||||
},
|
||||
"products" => {
|
||||
"string" => "Apple Computer",
|
||||
"file" => file
|
||||
},
|
||||
"ie_products" => {
|
||||
"string" => "Microsoft",
|
||||
"file" => ie_file
|
||||
},
|
||||
"text_part" => "abc"
|
||||
}
|
||||
|
||||
params = ActionController::AbstractRequest.parse_request_parameters(input)
|
||||
assert_equal expected_output, params
|
||||
|
||||
# Lone filenames are preserved.
|
||||
assert_equal 'foo.jpg', params['mixed_types_as_checkboxes']['strings']['nested'].first.original_filename
|
||||
assert_equal 'foo.jpg', params['products']['file'].original_filename
|
||||
|
||||
# But full Windows paths are reduced to their basename.
|
||||
assert_equal 'bar.jpg', params['ie_mixed_types_as_checkboxes']['strings']['nested'].first.original_filename
|
||||
assert_equal 'bar.jpg', params['ie_products']['file'].original_filename
|
||||
end
|
||||
|
||||
def test_parse_params_with_file
|
||||
input = {
|
||||
"customers[boston][first][name]" => [ "David" ],
|
||||
"something_else" => [ "blah" ],
|
||||
"logo" => [ File.new(File.dirname(__FILE__) + "/cgi_test.rb").path ]
|
||||
}
|
||||
|
||||
expected_output = {
|
||||
"customers" => {
|
||||
"boston" => {
|
||||
"first" => {
|
||||
"name" => "David"
|
||||
}
|
||||
}
|
||||
},
|
||||
"something_else" => "blah",
|
||||
"logo" => File.new(File.dirname(__FILE__) + "/cgi_test.rb").path,
|
||||
}
|
||||
|
||||
assert_equal expected_output, ActionController::AbstractRequest.parse_request_parameters(input)
|
||||
end
|
||||
|
||||
def test_parse_params_with_array
|
||||
input = { "selected[]" => [ "1", "2", "3" ] }
|
||||
|
||||
expected_output = { "selected" => [ "1", "2", "3" ] }
|
||||
|
||||
assert_equal expected_output, ActionController::AbstractRequest.parse_request_parameters(input)
|
||||
end
|
||||
|
||||
def test_parse_params_with_non_alphanumeric_name
|
||||
input = { "a/b[c]" => %w(d) }
|
||||
expected = { "a/b" => { "c" => "d" }}
|
||||
assert_equal expected, ActionController::AbstractRequest.parse_request_parameters(input)
|
||||
end
|
||||
|
||||
def test_parse_params_with_single_brackets_in_middle
|
||||
input = { "a/b[c]d" => %w(e) }
|
||||
expected = { "a/b" => {} }
|
||||
assert_equal expected, ActionController::AbstractRequest.parse_request_parameters(input)
|
||||
end
|
||||
|
||||
def test_parse_params_with_separated_brackets
|
||||
input = { "a/b@[c]d[e]" => %w(f) }
|
||||
expected = { "a/b@" => { }}
|
||||
assert_equal expected, ActionController::AbstractRequest.parse_request_parameters(input)
|
||||
end
|
||||
|
||||
def test_parse_params_with_separated_brackets_and_array
|
||||
input = { "a/b@[c]d[e][]" => %w(f) }
|
||||
expected = { "a/b@" => { }}
|
||||
assert_equal expected , ActionController::AbstractRequest.parse_request_parameters(input)
|
||||
end
|
||||
|
||||
def test_parse_params_with_unmatched_brackets_and_array
|
||||
input = { "a/b@[c][d[e][]" => %w(f) }
|
||||
expected = { "a/b@" => { "c" => { }}}
|
||||
assert_equal expected, ActionController::AbstractRequest.parse_request_parameters(input)
|
||||
end
|
||||
|
||||
def test_parse_params_with_nil_key
|
||||
input = { nil => nil, "test2" => %w(value1) }
|
||||
expected = { "test2" => "value1" }
|
||||
assert_equal expected, ActionController::AbstractRequest.parse_request_parameters(input)
|
||||
end
|
||||
|
||||
def test_parse_params_with_array_prefix_and_hashes
|
||||
input = { "a[][b][c]" => %w(d) }
|
||||
expected = {"a" => [{"b" => {"c" => "d"}}]}
|
||||
assert_equal expected, ActionController::AbstractRequest.parse_request_parameters(input)
|
||||
end
|
||||
|
||||
def test_parse_params_with_complex_nesting
|
||||
input = { "a[][b][c][][d][]" => %w(e) }
|
||||
expected = {"a" => [{"b" => {"c" => [{"d" => ["e"]}]}}]}
|
||||
assert_equal expected, ActionController::AbstractRequest.parse_request_parameters(input)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class MultipartRequestParameterParsingTest < Test::Unit::TestCase
|
||||
FIXTURE_PATH = File.dirname(__FILE__) + '/../fixtures/multipart'
|
||||
|
||||
def test_single_parameter
|
||||
params = process('single_parameter')
|
||||
assert_equal({ 'foo' => 'bar' }, params)
|
||||
end
|
||||
|
||||
def test_bracketed_param
|
||||
assert_equal({ 'foo' => { 'baz' => 'bar'}}, process('bracketed_param'))
|
||||
end
|
||||
|
||||
def test_text_file
|
||||
params = process('text_file')
|
||||
assert_equal %w(file foo), params.keys.sort
|
||||
assert_equal 'bar', params['foo']
|
||||
|
||||
file = params['file']
|
||||
assert_kind_of StringIO, file
|
||||
assert_equal 'file.txt', file.original_filename
|
||||
assert_equal "text/plain", file.content_type
|
||||
assert_equal 'contents', file.read
|
||||
end
|
||||
|
||||
def test_boundary_problem_file
|
||||
params = process('boundary_problem_file')
|
||||
assert_equal %w(file foo), params.keys.sort
|
||||
|
||||
file = params['file']
|
||||
foo = params['foo']
|
||||
|
||||
if RUBY_VERSION > '1.9'
|
||||
assert_kind_of File, file
|
||||
else
|
||||
assert_kind_of Tempfile, file
|
||||
end
|
||||
|
||||
assert_equal 'file.txt', file.original_filename
|
||||
assert_equal "text/plain", file.content_type
|
||||
|
||||
assert_equal 'bar', foo
|
||||
end
|
||||
|
||||
def test_large_text_file
|
||||
params = process('large_text_file')
|
||||
assert_equal %w(file foo), params.keys.sort
|
||||
assert_equal 'bar', params['foo']
|
||||
|
||||
file = params['file']
|
||||
if RUBY_VERSION > '1.9'
|
||||
assert_kind_of File, file
|
||||
else
|
||||
assert_kind_of Tempfile, file
|
||||
end
|
||||
assert_equal 'file.txt', file.original_filename
|
||||
assert_equal "text/plain", file.content_type
|
||||
assert ('a' * 20480) == file.read
|
||||
end
|
||||
|
||||
uses_mocha "test_no_rewind_stream" do
|
||||
def test_no_rewind_stream
|
||||
# Ensures that parse_multipart_form_parameters works with streams that cannot be rewound
|
||||
file = File.open(File.join(FIXTURE_PATH, 'large_text_file'), 'rb')
|
||||
file.expects(:rewind).raises(Errno::ESPIPE)
|
||||
params = ActionController::AbstractRequest.parse_multipart_form_parameters(file, 'AaB03x', file.stat.size, {})
|
||||
assert_not_equal 0, file.pos # file was not rewound after reading
|
||||
end
|
||||
end
|
||||
|
||||
def test_binary_file
|
||||
params = process('binary_file')
|
||||
assert_equal %w(file flowers foo), params.keys.sort
|
||||
assert_equal 'bar', params['foo']
|
||||
|
||||
file = params['file']
|
||||
assert_kind_of StringIO, file
|
||||
assert_equal 'file.csv', file.original_filename
|
||||
assert_nil file.content_type
|
||||
assert_equal 'contents', file.read
|
||||
|
||||
file = params['flowers']
|
||||
assert_kind_of StringIO, file
|
||||
assert_equal 'flowers.jpg', file.original_filename
|
||||
assert_equal "image/jpeg", file.content_type
|
||||
assert_equal 19512, file.size
|
||||
#assert_equal File.read(File.dirname(__FILE__) + '/../../../activerecord/test/fixtures/flowers.jpg'), file.read
|
||||
end
|
||||
|
||||
def test_mixed_files
|
||||
params = process('mixed_files')
|
||||
assert_equal %w(files foo), params.keys.sort
|
||||
assert_equal 'bar', params['foo']
|
||||
|
||||
# Ruby CGI doesn't handle multipart/mixed for us.
|
||||
files = params['files']
|
||||
assert_kind_of String, files
|
||||
files.force_encoding('ASCII-8BIT') if files.respond_to?(:force_encoding)
|
||||
assert_equal 19756, files.size
|
||||
end
|
||||
|
||||
private
|
||||
def process(name)
|
||||
File.open(File.join(FIXTURE_PATH, name), 'rb') do |file|
|
||||
params = ActionController::AbstractRequest.parse_multipart_form_parameters(file, 'AaB03x', file.stat.size, {})
|
||||
assert_equal 0, file.pos # file was rewound after reading
|
||||
params
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class XmlParamsParsingTest < Test::Unit::TestCase
|
||||
def test_hash_params
|
||||
person = parse_body("<person><name>David</name></person>")[:person]
|
||||
assert_kind_of Hash, person
|
||||
assert_equal 'David', person['name']
|
||||
end
|
||||
|
||||
def test_single_file
|
||||
person = parse_body("<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar></person>")
|
||||
|
||||
assert_equal "image/jpg", person['person']['avatar'].content_type
|
||||
assert_equal "me.jpg", person['person']['avatar'].original_filename
|
||||
assert_equal "ABC", person['person']['avatar'].read
|
||||
end
|
||||
|
||||
def test_multiple_files
|
||||
person = parse_body(<<-end_body)
|
||||
<person>
|
||||
<name>David</name>
|
||||
<avatars>
|
||||
<avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar>
|
||||
<avatar type='file' name='you.gif' content_type='image/gif'>#{ActiveSupport::Base64.encode64('DEF')}</avatar>
|
||||
</avatars>
|
||||
</person>
|
||||
end_body
|
||||
|
||||
assert_equal "image/jpg", person['person']['avatars']['avatar'].first.content_type
|
||||
assert_equal "me.jpg", person['person']['avatars']['avatar'].first.original_filename
|
||||
assert_equal "ABC", person['person']['avatars']['avatar'].first.read
|
||||
|
||||
assert_equal "image/gif", person['person']['avatars']['avatar'].last.content_type
|
||||
assert_equal "you.gif", person['person']['avatars']['avatar'].last.original_filename
|
||||
assert_equal "DEF", person['person']['avatars']['avatar'].last.read
|
||||
end
|
||||
|
||||
private
|
||||
def parse_body(body)
|
||||
env = { 'CONTENT_TYPE' => 'application/xml',
|
||||
'CONTENT_LENGTH' => body.size.to_s }
|
||||
cgi = ActionController::Integration::Session::StubCGI.new(env, body)
|
||||
ActionController::CgiRequest.new(cgi).request_parameters
|
||||
end
|
||||
end
|
||||
|
||||
class LegacyXmlParamsParsingTest < XmlParamsParsingTest
|
||||
private
|
||||
def parse_body(body)
|
||||
env = { 'HTTP_X_POST_DATA_FORMAT' => 'xml',
|
||||
'CONTENT_LENGTH' => body.size.to_s }
|
||||
cgi = ActionController::Integration::Session::StubCGI.new(env, body)
|
||||
ActionController::CgiRequest.new(cgi).request_parameters
|
||||
end
|
||||
end
|
||||
|
||||
class JsonParamsParsingTest < Test::Unit::TestCase
|
||||
def test_hash_params
|
||||
person = parse_body({:person => {:name => "David"}}.to_json)[:person]
|
||||
assert_kind_of Hash, person
|
||||
assert_equal 'David', person['name']
|
||||
end
|
||||
|
||||
private
|
||||
def parse_body(body)
|
||||
env = { 'CONTENT_TYPE' => 'application/json',
|
||||
'CONTENT_LENGTH' => body.size.to_s }
|
||||
cgi = ActionController::Integration::Session::StubCGI.new(env, body)
|
||||
ActionController::CgiRequest.new(cgi).request_parameters
|
||||
end
|
||||
end
|
|
@ -1,517 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
uses_mocha 'rescue' do
|
||||
|
||||
class RescueController < ActionController::Base
|
||||
class NotAuthorized < StandardError
|
||||
end
|
||||
class NotAuthorizedToRescueAsString < StandardError
|
||||
end
|
||||
|
||||
class RecordInvalid < StandardError
|
||||
end
|
||||
class RecordInvalidToRescueAsString < StandardError
|
||||
end
|
||||
|
||||
class NotAllowed < StandardError
|
||||
end
|
||||
class NotAllowedToRescueAsString < StandardError
|
||||
end
|
||||
|
||||
class InvalidRequest < StandardError
|
||||
end
|
||||
class InvalidRequestToRescueAsString < StandardError
|
||||
end
|
||||
|
||||
class BadGateway < StandardError
|
||||
end
|
||||
class BadGatewayToRescueAsString < StandardError
|
||||
end
|
||||
|
||||
class ResourceUnavailable < StandardError
|
||||
end
|
||||
class ResourceUnavailableToRescueAsString < StandardError
|
||||
end
|
||||
|
||||
# We use a fully-qualified name in some strings, and a relative constant
|
||||
# name in some other to test correct handling of both cases.
|
||||
|
||||
rescue_from NotAuthorized, :with => :deny_access
|
||||
rescue_from 'RescueController::NotAuthorizedToRescueAsString', :with => :deny_access
|
||||
|
||||
rescue_from RecordInvalid, :with => :show_errors
|
||||
rescue_from 'RescueController::RecordInvalidToRescueAsString', :with => :show_errors
|
||||
|
||||
rescue_from NotAllowed, :with => proc { head :forbidden }
|
||||
rescue_from 'RescueController::NotAllowedToRescueAsString', :with => proc { head :forbidden }
|
||||
|
||||
rescue_from InvalidRequest, :with => proc { |exception| render :text => exception.message }
|
||||
rescue_from 'InvalidRequestToRescueAsString', :with => proc { |exception| render :text => exception.message }
|
||||
|
||||
rescue_from BadGateway do
|
||||
head :status => 502
|
||||
end
|
||||
rescue_from 'BadGatewayToRescueAsString' do
|
||||
head :status => 502
|
||||
end
|
||||
|
||||
rescue_from ResourceUnavailable do |exception|
|
||||
render :text => exception.message
|
||||
end
|
||||
rescue_from 'ResourceUnavailableToRescueAsString' do |exception|
|
||||
render :text => exception.message
|
||||
end
|
||||
|
||||
def raises
|
||||
render :text => 'already rendered'
|
||||
raise "don't panic!"
|
||||
end
|
||||
|
||||
def method_not_allowed
|
||||
raise ActionController::MethodNotAllowed.new(:get, :head, :put)
|
||||
end
|
||||
|
||||
def not_implemented
|
||||
raise ActionController::NotImplemented.new(:get, :put)
|
||||
end
|
||||
|
||||
def not_authorized
|
||||
raise NotAuthorized
|
||||
end
|
||||
def not_authorized_raise_as_string
|
||||
raise NotAuthorizedToRescueAsString
|
||||
end
|
||||
|
||||
def not_allowed
|
||||
raise NotAllowed
|
||||
end
|
||||
def not_allowed_raise_as_string
|
||||
raise NotAllowedToRescueAsString
|
||||
end
|
||||
|
||||
def invalid_request
|
||||
raise InvalidRequest
|
||||
end
|
||||
def invalid_request_raise_as_string
|
||||
raise InvalidRequestToRescueAsString
|
||||
end
|
||||
|
||||
def record_invalid
|
||||
raise RecordInvalid
|
||||
end
|
||||
def record_invalid_raise_as_string
|
||||
raise RecordInvalidToRescueAsString
|
||||
end
|
||||
|
||||
def bad_gateway
|
||||
raise BadGateway
|
||||
end
|
||||
def bad_gateway_raise_as_string
|
||||
raise BadGatewayToRescueAsString
|
||||
end
|
||||
|
||||
def resource_unavailable
|
||||
raise ResourceUnavailable
|
||||
end
|
||||
def resource_unavailable_raise_as_string
|
||||
raise ResourceUnavailableToRescueAsString
|
||||
end
|
||||
|
||||
def missing_template
|
||||
end
|
||||
|
||||
protected
|
||||
def deny_access
|
||||
head :forbidden
|
||||
end
|
||||
|
||||
def show_errors(exception)
|
||||
head :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
class RescueTest < Test::Unit::TestCase
|
||||
FIXTURE_PUBLIC = "#{File.dirname(__FILE__)}/../fixtures".freeze
|
||||
|
||||
def setup
|
||||
@controller = RescueController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
|
||||
RescueController.consider_all_requests_local = true
|
||||
@request.remote_addr = '1.2.3.4'
|
||||
@request.host = 'example.com'
|
||||
|
||||
begin
|
||||
raise 'foo'
|
||||
rescue => @exception
|
||||
end
|
||||
end
|
||||
|
||||
def test_rescue_action_locally_if_all_requests_local
|
||||
@controller.expects(:local_request?).never
|
||||
@controller.expects(:rescue_action_locally).with(@exception)
|
||||
@controller.expects(:rescue_action_in_public).never
|
||||
|
||||
with_all_requests_local do
|
||||
@controller.send :rescue_action, @exception
|
||||
end
|
||||
end
|
||||
|
||||
def test_rescue_action_locally_if_remote_addr_is_localhost
|
||||
@controller.expects(:local_request?).returns(true)
|
||||
@controller.expects(:rescue_action_locally).with(@exception)
|
||||
@controller.expects(:rescue_action_in_public).never
|
||||
|
||||
with_all_requests_local false do
|
||||
@controller.send :rescue_action, @exception
|
||||
end
|
||||
end
|
||||
|
||||
def test_rescue_action_in_public_otherwise
|
||||
@controller.expects(:local_request?).returns(false)
|
||||
@controller.expects(:rescue_action_locally).never
|
||||
@controller.expects(:rescue_action_in_public).with(@exception)
|
||||
|
||||
with_all_requests_local false do
|
||||
@controller.send :rescue_action, @exception
|
||||
end
|
||||
end
|
||||
|
||||
def test_rescue_action_in_public_with_error_file
|
||||
with_rails_root FIXTURE_PUBLIC do
|
||||
with_all_requests_local false do
|
||||
get :raises
|
||||
end
|
||||
end
|
||||
|
||||
assert_response :internal_server_error
|
||||
body = File.read("#{FIXTURE_PUBLIC}/public/500.html")
|
||||
assert_equal body, @response.body
|
||||
end
|
||||
|
||||
def test_rescue_action_in_public_without_error_file
|
||||
with_rails_root '/tmp' do
|
||||
with_all_requests_local false do
|
||||
get :raises
|
||||
end
|
||||
end
|
||||
|
||||
assert_response :internal_server_error
|
||||
assert_equal ' ', @response.body
|
||||
end
|
||||
|
||||
def test_rescue_unknown_action_in_public_with_error_file
|
||||
with_rails_root FIXTURE_PUBLIC do
|
||||
with_all_requests_local false do
|
||||
get :foobar_doesnt_exist
|
||||
end
|
||||
end
|
||||
|
||||
assert_response :not_found
|
||||
body = File.read("#{FIXTURE_PUBLIC}/public/404.html")
|
||||
assert_equal body, @response.body
|
||||
end
|
||||
|
||||
def test_rescue_unknown_action_in_public_without_error_file
|
||||
with_rails_root '/tmp' do
|
||||
with_all_requests_local false do
|
||||
get :foobar_doesnt_exist
|
||||
end
|
||||
end
|
||||
|
||||
assert_response :not_found
|
||||
assert_equal ' ', @response.body
|
||||
end
|
||||
|
||||
def test_rescue_missing_template_in_public
|
||||
with_rails_root FIXTURE_PUBLIC do
|
||||
with_all_requests_local true do
|
||||
get :missing_template
|
||||
end
|
||||
end
|
||||
|
||||
assert_response :internal_server_error
|
||||
assert @response.body.include?('missing_template'), "Response should include the template name."
|
||||
end
|
||||
|
||||
def test_rescue_action_locally
|
||||
get :raises
|
||||
assert_response :internal_server_error
|
||||
assert_template 'diagnostics.erb'
|
||||
assert @response.body.include?('RescueController#raises'), "Response should include controller and action."
|
||||
assert @response.body.include?("don't panic"), "Response should include exception message."
|
||||
end
|
||||
|
||||
def test_local_request_when_remote_addr_is_localhost
|
||||
@controller.expects(:request).returns(@request).at_least_once
|
||||
with_remote_addr '127.0.0.1' do
|
||||
assert @controller.send(:local_request?)
|
||||
end
|
||||
end
|
||||
|
||||
def test_local_request_when_remote_addr_isnt_locahost
|
||||
@controller.expects(:request).returns(@request)
|
||||
with_remote_addr '1.2.3.4' do
|
||||
assert !@controller.send(:local_request?)
|
||||
end
|
||||
end
|
||||
|
||||
def test_rescue_responses
|
||||
responses = ActionController::Base.rescue_responses
|
||||
|
||||
assert_equal ActionController::Rescue::DEFAULT_RESCUE_RESPONSE, responses.default
|
||||
assert_equal ActionController::Rescue::DEFAULT_RESCUE_RESPONSE, responses[Exception.new]
|
||||
|
||||
assert_equal :not_found, responses[ActionController::RoutingError.name]
|
||||
assert_equal :not_found, responses[ActionController::UnknownAction.name]
|
||||
assert_equal :not_found, responses['ActiveRecord::RecordNotFound']
|
||||
assert_equal :conflict, responses['ActiveRecord::StaleObjectError']
|
||||
assert_equal :unprocessable_entity, responses['ActiveRecord::RecordInvalid']
|
||||
assert_equal :unprocessable_entity, responses['ActiveRecord::RecordNotSaved']
|
||||
assert_equal :method_not_allowed, responses['ActionController::MethodNotAllowed']
|
||||
assert_equal :not_implemented, responses['ActionController::NotImplemented']
|
||||
end
|
||||
|
||||
def test_rescue_templates
|
||||
templates = ActionController::Base.rescue_templates
|
||||
|
||||
assert_equal ActionController::Rescue::DEFAULT_RESCUE_TEMPLATE, templates.default
|
||||
assert_equal ActionController::Rescue::DEFAULT_RESCUE_TEMPLATE, templates[Exception.new]
|
||||
|
||||
assert_equal 'missing_template', templates[ActionView::MissingTemplate.name]
|
||||
assert_equal 'routing_error', templates[ActionController::RoutingError.name]
|
||||
assert_equal 'unknown_action', templates[ActionController::UnknownAction.name]
|
||||
assert_equal 'template_error', templates[ActionView::TemplateError.name]
|
||||
end
|
||||
|
||||
def test_clean_backtrace
|
||||
with_rails_root nil do
|
||||
# No action if RAILS_ROOT isn't set.
|
||||
cleaned = @controller.send(:clean_backtrace, @exception)
|
||||
assert_equal @exception.backtrace, cleaned
|
||||
end
|
||||
|
||||
with_rails_root Dir.pwd do
|
||||
# RAILS_ROOT is removed from backtrace.
|
||||
cleaned = @controller.send(:clean_backtrace, @exception)
|
||||
expected = @exception.backtrace.map { |line| line.sub(RAILS_ROOT, '') }
|
||||
assert_equal expected, cleaned
|
||||
|
||||
# No action if backtrace is nil.
|
||||
assert_nil @controller.send(:clean_backtrace, Exception.new)
|
||||
end
|
||||
end
|
||||
|
||||
def test_not_implemented
|
||||
with_all_requests_local false do
|
||||
with_rails_public_path(".") do
|
||||
head :not_implemented
|
||||
end
|
||||
end
|
||||
assert_response :not_implemented
|
||||
assert_equal "GET, PUT", @response.headers['Allow']
|
||||
end
|
||||
|
||||
def test_method_not_allowed
|
||||
with_all_requests_local false do
|
||||
with_rails_public_path(".") do
|
||||
get :method_not_allowed
|
||||
end
|
||||
end
|
||||
assert_response :method_not_allowed
|
||||
assert_equal "GET, HEAD, PUT", @response.headers['Allow']
|
||||
end
|
||||
|
||||
def test_rescue_handler
|
||||
get :not_authorized
|
||||
assert_response :forbidden
|
||||
end
|
||||
def test_rescue_handler_string
|
||||
get :not_authorized_raise_as_string
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
def test_rescue_handler_with_argument
|
||||
@controller.expects(:show_errors).once.with { |e| e.is_a?(Exception) }
|
||||
get :record_invalid
|
||||
end
|
||||
def test_rescue_handler_with_argument_as_string
|
||||
@controller.expects(:show_errors).once.with { |e| e.is_a?(Exception) }
|
||||
get :record_invalid_raise_as_string
|
||||
end
|
||||
|
||||
def test_proc_rescue_handler
|
||||
get :not_allowed
|
||||
assert_response :forbidden
|
||||
end
|
||||
def test_proc_rescue_handler_as_string
|
||||
get :not_allowed_raise_as_string
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
def test_proc_rescue_handle_with_argument
|
||||
get :invalid_request
|
||||
assert_equal "RescueController::InvalidRequest", @response.body
|
||||
end
|
||||
def test_proc_rescue_handle_with_argument_as_string
|
||||
get :invalid_request_raise_as_string
|
||||
assert_equal "RescueController::InvalidRequestToRescueAsString", @response.body
|
||||
end
|
||||
|
||||
def test_block_rescue_handler
|
||||
get :bad_gateway
|
||||
assert_response 502
|
||||
end
|
||||
def test_block_rescue_handler_as_string
|
||||
get :bad_gateway_raise_as_string
|
||||
assert_response 502
|
||||
end
|
||||
|
||||
def test_block_rescue_handler_with_argument
|
||||
get :resource_unavailable
|
||||
assert_equal "RescueController::ResourceUnavailable", @response.body
|
||||
end
|
||||
|
||||
def test_block_rescue_handler_with_argument_as_string
|
||||
get :resource_unavailable_raise_as_string
|
||||
assert_equal "RescueController::ResourceUnavailableToRescueAsString", @response.body
|
||||
end
|
||||
|
||||
|
||||
protected
|
||||
def with_all_requests_local(local = true)
|
||||
old_local, ActionController::Base.consider_all_requests_local =
|
||||
ActionController::Base.consider_all_requests_local, local
|
||||
yield
|
||||
ensure
|
||||
ActionController::Base.consider_all_requests_local = old_local
|
||||
end
|
||||
|
||||
def with_remote_addr(addr)
|
||||
old_remote_addr, @request.remote_addr = @request.remote_addr, addr
|
||||
yield
|
||||
ensure
|
||||
@request.remote_addr = old_remote_addr
|
||||
end
|
||||
|
||||
def with_rails_public_path(rails_root)
|
||||
old_rails = Object.const_get(:Rails) rescue nil
|
||||
mod = Object.const_set(:Rails, Module.new)
|
||||
(class << mod; self; end).instance_eval do
|
||||
define_method(:public_path) { "#{rails_root}/public" }
|
||||
end
|
||||
yield
|
||||
ensure
|
||||
Object.module_eval { remove_const(:Rails) } if defined?(Rails)
|
||||
Object.const_set(:Rails, old_rails) if old_rails
|
||||
end
|
||||
|
||||
def with_rails_root(path = nil,&block)
|
||||
old_rails_root = RAILS_ROOT if defined?(RAILS_ROOT)
|
||||
if path
|
||||
silence_warnings { Object.const_set(:RAILS_ROOT, path) }
|
||||
else
|
||||
Object.remove_const(:RAILS_ROOT) rescue nil
|
||||
end
|
||||
|
||||
with_rails_public_path(path, &block)
|
||||
|
||||
ensure
|
||||
if old_rails_root
|
||||
silence_warnings { Object.const_set(:RAILS_ROOT, old_rails_root) }
|
||||
else
|
||||
Object.remove_const(:RAILS_ROOT) rescue nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class ExceptionInheritanceRescueController < ActionController::Base
|
||||
|
||||
class ParentException < StandardError
|
||||
end
|
||||
|
||||
class ChildException < ParentException
|
||||
end
|
||||
|
||||
class GrandchildException < ChildException
|
||||
end
|
||||
|
||||
rescue_from ChildException, :with => lambda { head :ok }
|
||||
rescue_from ParentException, :with => lambda { head :created }
|
||||
rescue_from GrandchildException, :with => lambda { head :no_content }
|
||||
|
||||
def raise_parent_exception
|
||||
raise ParentException
|
||||
end
|
||||
|
||||
def raise_child_exception
|
||||
raise ChildException
|
||||
end
|
||||
|
||||
def raise_grandchild_exception
|
||||
raise GrandchildException
|
||||
end
|
||||
end
|
||||
|
||||
class ExceptionInheritanceRescueTest < Test::Unit::TestCase
|
||||
|
||||
def setup
|
||||
@controller = ExceptionInheritanceRescueController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
def test_bottom_first
|
||||
get :raise_grandchild_exception
|
||||
assert_response :no_content
|
||||
end
|
||||
|
||||
def test_inheritance_works
|
||||
get :raise_child_exception
|
||||
assert_response :created
|
||||
end
|
||||
end
|
||||
|
||||
class ControllerInheritanceRescueController < ExceptionInheritanceRescueController
|
||||
class FirstExceptionInChildController < StandardError
|
||||
end
|
||||
|
||||
class SecondExceptionInChildController < StandardError
|
||||
end
|
||||
|
||||
rescue_from FirstExceptionInChildController, 'SecondExceptionInChildController', :with => lambda { head :gone }
|
||||
|
||||
def raise_first_exception_in_child_controller
|
||||
raise FirstExceptionInChildController
|
||||
end
|
||||
|
||||
def raise_second_exception_in_child_controller
|
||||
raise SecondExceptionInChildController
|
||||
end
|
||||
end
|
||||
|
||||
class ControllerInheritanceRescueControllerTest < Test::Unit::TestCase
|
||||
|
||||
def setup
|
||||
@controller = ControllerInheritanceRescueController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
def test_first_exception_in_child_controller
|
||||
get :raise_first_exception_in_child_controller
|
||||
assert_response :gone
|
||||
end
|
||||
|
||||
def test_second_exception_in_child_controller
|
||||
get :raise_second_exception_in_child_controller
|
||||
assert_response :gone
|
||||
end
|
||||
|
||||
def test_exception_in_parent_controller
|
||||
get :raise_parent_exception
|
||||
assert_response :created
|
||||
end
|
||||
end
|
||||
end # uses_mocha
|
|
@ -1,873 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class ResourcesController < ActionController::Base
|
||||
def index() render :nothing => true end
|
||||
alias_method :show, :index
|
||||
def rescue_action(e) raise e end
|
||||
end
|
||||
|
||||
class ThreadsController < ResourcesController; end
|
||||
class MessagesController < ResourcesController; end
|
||||
class CommentsController < ResourcesController; end
|
||||
class AuthorsController < ResourcesController; end
|
||||
class LogosController < ResourcesController; end
|
||||
|
||||
class AccountsController < ResourcesController; end
|
||||
class AdminController < ResourcesController; end
|
||||
|
||||
module Backoffice
|
||||
class ProductsController < ResourcesController; end
|
||||
class TagsController < ResourcesController; end
|
||||
class ManufacturersController < ResourcesController; end
|
||||
class ImagesController < ResourcesController; end
|
||||
|
||||
module Admin
|
||||
class ProductsController < ResourcesController; end
|
||||
class ImagesController < ResourcesController; end
|
||||
end
|
||||
end
|
||||
|
||||
class ResourcesTest < Test::Unit::TestCase
|
||||
# The assertions in these tests are incompatible with the hash method
|
||||
# optimisation. This could indicate user level problems
|
||||
def setup
|
||||
ActionController::Base.optimise_named_routes = false
|
||||
end
|
||||
|
||||
def teardown
|
||||
ActionController::Base.optimise_named_routes = true
|
||||
end
|
||||
|
||||
def test_should_arrange_actions
|
||||
resource = ActionController::Resources::Resource.new(:messages,
|
||||
:collection => { :rss => :get, :reorder => :post, :csv => :post },
|
||||
:member => { :rss => :get, :atom => :get, :upload => :post, :fix => :post },
|
||||
:new => { :preview => :get, :draft => :get })
|
||||
|
||||
assert_resource_methods [:rss], resource, :collection, :get
|
||||
assert_resource_methods [:csv, :reorder], resource, :collection, :post
|
||||
assert_resource_methods [:edit, :rss, :atom], resource, :member, :get
|
||||
assert_resource_methods [:upload, :fix], resource, :member, :post
|
||||
assert_resource_methods [:new, :preview, :draft], resource, :new, :get
|
||||
end
|
||||
|
||||
def test_should_resource_controller_name_equal_resource_name_by_default
|
||||
resource = ActionController::Resources::Resource.new(:messages, {})
|
||||
assert_equal 'messages', resource.controller
|
||||
end
|
||||
|
||||
def test_should_resource_controller_name_equal_controller_option
|
||||
resource = ActionController::Resources::Resource.new(:messages, :controller => 'posts')
|
||||
assert_equal 'posts', resource.controller
|
||||
end
|
||||
|
||||
def test_should_all_singleton_paths_be_the_same
|
||||
[ :path, :nesting_path_prefix, :member_path ].each do |method|
|
||||
resource = ActionController::Resources::SingletonResource.new(:messages, :path_prefix => 'admin')
|
||||
assert_equal 'admin/messages', resource.send(method)
|
||||
end
|
||||
end
|
||||
|
||||
def test_default_restful_routes
|
||||
with_restful_routing :messages do
|
||||
assert_simply_restful_for :messages
|
||||
end
|
||||
end
|
||||
|
||||
def test_override_paths_for_default_restful_actions
|
||||
resource = ActionController::Resources::Resource.new(:messages,
|
||||
:path_names => {:new => 'nuevo', :edit => 'editar'})
|
||||
assert_equal resource.new_path, "#{resource.path}/nuevo"
|
||||
end
|
||||
|
||||
def test_multiple_default_restful_routes
|
||||
with_restful_routing :messages, :comments do
|
||||
assert_simply_restful_for :messages
|
||||
assert_simply_restful_for :comments
|
||||
end
|
||||
end
|
||||
|
||||
def test_with_custom_conditions
|
||||
with_restful_routing :messages, :conditions => { :subdomain => 'app' } do
|
||||
assert_equal 'app', ActionController::Routing::Routes.named_routes.routes[:messages].conditions[:subdomain]
|
||||
end
|
||||
end
|
||||
|
||||
def test_irregular_id_with_no_requirements_should_raise_error
|
||||
expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
|
||||
|
||||
with_restful_routing :messages do
|
||||
assert_raises(ActionController::RoutingError) do
|
||||
assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_irregular_id_with_requirements_should_pass
|
||||
expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
|
||||
|
||||
with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}) do
|
||||
assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
|
||||
end
|
||||
end
|
||||
|
||||
def test_with_path_prefix_requirements
|
||||
expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
|
||||
with_restful_routing :messages, :path_prefix => '/thread/:thread_id', :requirements => {:thread_id => /[0-9]\.[0-9]\.[0-9]/} do
|
||||
assert_recognizes(expected_options, :path => 'thread/1.1.1/messages/1', :method => :get)
|
||||
end
|
||||
end
|
||||
|
||||
def test_with_path_prefix
|
||||
with_restful_routing :messages, :path_prefix => '/thread/:thread_id' do
|
||||
assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
|
||||
end
|
||||
end
|
||||
|
||||
def test_multiple_with_path_prefix
|
||||
with_restful_routing :messages, :comments, :path_prefix => '/thread/:thread_id' do
|
||||
assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
|
||||
assert_simply_restful_for :comments, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
|
||||
end
|
||||
end
|
||||
|
||||
def test_with_name_prefix
|
||||
with_restful_routing :messages, :name_prefix => 'post_' do
|
||||
assert_simply_restful_for :messages, :name_prefix => 'post_'
|
||||
end
|
||||
end
|
||||
|
||||
def test_with_collection_actions
|
||||
actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
|
||||
|
||||
with_restful_routing :messages, :collection => actions do
|
||||
assert_restful_routes_for :messages do |options|
|
||||
actions.each do |action, method|
|
||||
assert_recognizes(options.merge(:action => action), :path => "/messages/#{action}", :method => method)
|
||||
end
|
||||
end
|
||||
|
||||
assert_restful_named_routes_for :messages do |options|
|
||||
actions.keys.each do |action|
|
||||
assert_named_route "/messages/#{action}", "#{action}_messages_path", :action => action
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_with_collection_actions_and_name_prefix
|
||||
actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
|
||||
|
||||
with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions do
|
||||
assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
|
||||
actions.each do |action, method|
|
||||
assert_recognizes(options.merge(:action => action), :path => "/threads/1/messages/#{action}", :method => method)
|
||||
end
|
||||
end
|
||||
|
||||
assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
|
||||
actions.keys.each do |action|
|
||||
assert_named_route "/threads/1/messages/#{action}", "#{action}_thread_messages_path", :action => action
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_with_collection_action_and_name_prefix_and_formatted
|
||||
actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
|
||||
|
||||
with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions do
|
||||
assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
|
||||
actions.each do |action, method|
|
||||
assert_recognizes(options.merge(:action => action, :format => 'xml'), :path => "/threads/1/messages/#{action}.xml", :method => method)
|
||||
end
|
||||
end
|
||||
|
||||
assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
|
||||
actions.keys.each do |action|
|
||||
assert_named_route "/threads/1/messages/#{action}.xml", "formatted_#{action}_thread_messages_path", :action => action, :format => 'xml'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_with_member_action
|
||||
[:put, :post].each do |method|
|
||||
with_restful_routing :messages, :member => { :mark => method } do
|
||||
mark_options = {:action => 'mark', :id => '1'}
|
||||
mark_path = "/messages/1/mark"
|
||||
assert_restful_routes_for :messages do |options|
|
||||
assert_recognizes(options.merge(mark_options), :path => mark_path, :method => method)
|
||||
end
|
||||
|
||||
assert_restful_named_routes_for :messages do |options|
|
||||
assert_named_route mark_path, :mark_message_path, mark_options
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_member_when_override_paths_for_default_restful_actions_with
|
||||
[:put, :post].each do |method|
|
||||
with_restful_routing :messages, :member => { :mark => method }, :path_names => {:new => 'nuevo'} do
|
||||
mark_options = {:action => 'mark', :id => '1', :controller => "messages"}
|
||||
mark_path = "/messages/1/mark"
|
||||
|
||||
assert_restful_routes_for :messages, :path_names => {:new => 'nuevo'} do |options|
|
||||
assert_recognizes(options.merge(mark_options), :path => mark_path, :method => method)
|
||||
end
|
||||
|
||||
assert_restful_named_routes_for :messages, :path_names => {:new => 'nuevo'} do |options|
|
||||
assert_named_route mark_path, :mark_message_path, mark_options
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_member_when_changed_default_restful_actions_and_path_names_not_specified
|
||||
default_path_names = ActionController::Base.resources_path_names
|
||||
ActionController::Base.resources_path_names = {:new => 'nuevo', :edit => 'editar'}
|
||||
|
||||
with_restful_routing :messages do
|
||||
new_options = { :action => 'new', :controller => 'messages' }
|
||||
new_path = "/messages/nuevo"
|
||||
edit_options = { :action => 'edit', :id => '1', :controller => 'messages' }
|
||||
edit_path = "/messages/1/editar"
|
||||
|
||||
assert_restful_routes_for :messages do |options|
|
||||
assert_recognizes(options.merge(new_options), :path => new_path, :method => :get)
|
||||
end
|
||||
|
||||
assert_restful_routes_for :messages do |options|
|
||||
assert_recognizes(options.merge(edit_options), :path => edit_path, :method => :get)
|
||||
end
|
||||
end
|
||||
ensure
|
||||
ActionController::Base.resources_path_names = default_path_names
|
||||
end
|
||||
|
||||
def test_with_two_member_actions_with_same_method
|
||||
[:put, :post].each do |method|
|
||||
with_restful_routing :messages, :member => { :mark => method, :unmark => method } do
|
||||
%w(mark unmark).each do |action|
|
||||
action_options = {:action => action, :id => '1'}
|
||||
action_path = "/messages/1/#{action}"
|
||||
assert_restful_routes_for :messages do |options|
|
||||
assert_recognizes(options.merge(action_options), :path => action_path, :method => method)
|
||||
end
|
||||
|
||||
assert_restful_named_routes_for :messages do |options|
|
||||
assert_named_route action_path, "#{action}_message_path".to_sym, action_options
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_with_new_action
|
||||
with_restful_routing :messages, :new => { :preview => :post } do
|
||||
preview_options = {:action => 'preview'}
|
||||
preview_path = "/messages/new/preview"
|
||||
assert_restful_routes_for :messages do |options|
|
||||
assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
|
||||
end
|
||||
|
||||
assert_restful_named_routes_for :messages do |options|
|
||||
assert_named_route preview_path, :preview_new_message_path, preview_options
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_with_new_action_with_name_prefix
|
||||
with_restful_routing :messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
|
||||
preview_options = {:action => 'preview', :thread_id => '1'}
|
||||
preview_path = "/threads/1/messages/new/preview"
|
||||
assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
|
||||
assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
|
||||
end
|
||||
|
||||
assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
|
||||
assert_named_route preview_path, :preview_new_thread_message_path, preview_options
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_with_formatted_new_action_with_name_prefix
|
||||
with_restful_routing :messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
|
||||
preview_options = {:action => 'preview', :thread_id => '1', :format => 'xml'}
|
||||
preview_path = "/threads/1/messages/new/preview.xml"
|
||||
assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
|
||||
assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
|
||||
end
|
||||
|
||||
assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
|
||||
assert_named_route preview_path, :formatted_preview_new_thread_message_path, preview_options
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_override_new_method
|
||||
with_restful_routing :messages do
|
||||
assert_restful_routes_for :messages do |options|
|
||||
assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
|
||||
assert_raises(ActionController::MethodNotAllowed) do
|
||||
ActionController::Routing::Routes.recognize_path("/messages/new", :method => :post)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
with_restful_routing :messages, :new => { :new => :any } do
|
||||
assert_restful_routes_for :messages do |options|
|
||||
assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :post)
|
||||
assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_nested_restful_routes
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.resources :threads do |map|
|
||||
map.resources :messages do |map|
|
||||
map.resources :comments
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_simply_restful_for :threads
|
||||
assert_simply_restful_for :messages,
|
||||
:name_prefix => 'thread_',
|
||||
:path_prefix => 'threads/1/',
|
||||
:options => { :thread_id => '1' }
|
||||
assert_simply_restful_for :comments,
|
||||
:name_prefix => 'thread_message_',
|
||||
:path_prefix => 'threads/1/messages/2/',
|
||||
:options => { :thread_id => '1', :message_id => '2' }
|
||||
end
|
||||
end
|
||||
|
||||
def test_nested_restful_routes_with_overwritten_defaults
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.resources :threads do |map|
|
||||
map.resources :messages, :name_prefix => nil do |map|
|
||||
map.resources :comments, :name_prefix => nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_simply_restful_for :threads
|
||||
assert_simply_restful_for :messages,
|
||||
:path_prefix => 'threads/1/',
|
||||
:options => { :thread_id => '1' }
|
||||
assert_simply_restful_for :comments,
|
||||
:path_prefix => 'threads/1/messages/2/',
|
||||
:options => { :thread_id => '1', :message_id => '2' }
|
||||
end
|
||||
end
|
||||
|
||||
def test_restful_routes_dont_generate_duplicates
|
||||
with_restful_routing :messages do
|
||||
routes = ActionController::Routing::Routes.routes
|
||||
routes.each do |route|
|
||||
routes.each do |r|
|
||||
next if route === r # skip the comparison instance
|
||||
assert distinct_routes?(route, r), "Duplicate Route: #{route}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_create_singleton_resource_routes
|
||||
with_singleton_resources :account do
|
||||
assert_singleton_restful_for :account
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_create_multiple_singleton_resource_routes
|
||||
with_singleton_resources :account, :logo do
|
||||
assert_singleton_restful_for :account
|
||||
assert_singleton_restful_for :logo
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_create_nested_singleton_resource_routes
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.resource :admin, :controller => 'admin' do |admin|
|
||||
admin.resource :account
|
||||
end
|
||||
end
|
||||
|
||||
assert_singleton_restful_for :admin, :controller => 'admin'
|
||||
assert_singleton_restful_for :account, :name_prefix => "admin_", :path_prefix => 'admin/'
|
||||
end
|
||||
end
|
||||
|
||||
def test_resource_has_many_should_become_nested_resources
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.resources :messages, :has_many => [ :comments, :authors ]
|
||||
end
|
||||
|
||||
assert_simply_restful_for :messages
|
||||
assert_simply_restful_for :comments, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
|
||||
assert_simply_restful_for :authors, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
|
||||
end
|
||||
end
|
||||
|
||||
def test_resource_has_one_should_become_nested_resources
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.resources :messages, :has_one => :logo
|
||||
end
|
||||
|
||||
assert_simply_restful_for :messages
|
||||
assert_singleton_restful_for :logo, :name_prefix => 'message_', :path_prefix => 'messages/1/', :options => { :message_id => '1' }
|
||||
end
|
||||
end
|
||||
|
||||
def test_singleton_resource_with_member_action
|
||||
[:put, :post].each do |method|
|
||||
with_singleton_resources :account, :member => { :reset => method } do
|
||||
reset_options = {:action => 'reset'}
|
||||
reset_path = "/account/reset"
|
||||
assert_singleton_routes_for :account do |options|
|
||||
assert_recognizes(options.merge(reset_options), :path => reset_path, :method => method)
|
||||
end
|
||||
|
||||
assert_singleton_named_routes_for :account do |options|
|
||||
assert_named_route reset_path, :reset_account_path, reset_options
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_singleton_resource_with_two_member_actions_with_same_method
|
||||
[:put, :post].each do |method|
|
||||
with_singleton_resources :account, :member => { :reset => method, :disable => method } do
|
||||
%w(reset disable).each do |action|
|
||||
action_options = {:action => action}
|
||||
action_path = "/account/#{action}"
|
||||
assert_singleton_routes_for :account do |options|
|
||||
assert_recognizes(options.merge(action_options), :path => action_path, :method => method)
|
||||
end
|
||||
|
||||
assert_singleton_named_routes_for :account do |options|
|
||||
assert_named_route action_path, "#{action}_account_path".to_sym, action_options
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_nest_resources_in_singleton_resource
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.resource :account do |account|
|
||||
account.resources :messages
|
||||
end
|
||||
end
|
||||
|
||||
assert_singleton_restful_for :account
|
||||
assert_simply_restful_for :messages, :name_prefix => "account_", :path_prefix => 'account/'
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_nest_resources_in_singleton_resource_with_path_prefix
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.resource(:account, :path_prefix => ':site_id') do |account|
|
||||
account.resources :messages
|
||||
end
|
||||
end
|
||||
|
||||
assert_singleton_restful_for :account, :path_prefix => '7/', :options => { :site_id => '7' }
|
||||
assert_simply_restful_for :messages, :name_prefix => "account_", :path_prefix => '7/account/', :options => { :site_id => '7' }
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_nest_singleton_resource_in_resources
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.resources :threads do |thread|
|
||||
thread.resource :admin, :controller => 'admin'
|
||||
end
|
||||
end
|
||||
|
||||
assert_simply_restful_for :threads
|
||||
assert_singleton_restful_for :admin, :controller => 'admin', :name_prefix => 'thread_', :path_prefix => 'threads/5/', :options => { :thread_id => '5' }
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_not_allow_delete_or_put_on_collection_path
|
||||
controller_name = :messages
|
||||
with_restful_routing controller_name do
|
||||
options = { :controller => controller_name.to_s }
|
||||
collection_path = "/#{controller_name}"
|
||||
|
||||
assert_raises(ActionController::MethodNotAllowed) do
|
||||
assert_recognizes(options.merge(:action => 'update'), :path => collection_path, :method => :put)
|
||||
end
|
||||
|
||||
assert_raises(ActionController::MethodNotAllowed) do
|
||||
assert_recognizes(options.merge(:action => 'destroy'), :path => collection_path, :method => :delete)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_resource_action_separator
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
|
||||
map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
|
||||
end
|
||||
|
||||
action_separator = ActionController::Base.resource_action_separator
|
||||
|
||||
assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
|
||||
assert_named_route "/threads/1/messages#{action_separator}search", "search_thread_messages_path", {}
|
||||
assert_named_route "/threads/1/messages/new", "new_thread_message_path", {}
|
||||
assert_named_route "/threads/1/messages/new#{action_separator}preview", "preview_new_thread_message_path", {}
|
||||
assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/'
|
||||
assert_named_route "/admin/account#{action_separator}login", "login_admin_account_path", {}
|
||||
assert_named_route "/admin/account/new", "new_admin_account_path", {}
|
||||
assert_named_route "/admin/account/new#{action_separator}preview", "preview_new_admin_account_path", {}
|
||||
end
|
||||
end
|
||||
|
||||
def test_new_style_named_routes_for_resource
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
|
||||
end
|
||||
assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
|
||||
assert_named_route "/threads/1/messages/search", "search_thread_messages_path", {}
|
||||
assert_named_route "/threads/1/messages/new", "new_thread_message_path", {}
|
||||
assert_named_route "/threads/1/messages/new/preview", "preview_new_thread_message_path", {}
|
||||
end
|
||||
end
|
||||
|
||||
def test_new_style_named_routes_for_singleton_resource
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
|
||||
end
|
||||
assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/'
|
||||
assert_named_route "/admin/account/login", "login_admin_account_path", {}
|
||||
assert_named_route "/admin/account/new", "new_admin_account_path", {}
|
||||
assert_named_route "/admin/account/new/preview", "preview_new_admin_account_path", {}
|
||||
end
|
||||
end
|
||||
|
||||
def test_resources_in_namespace
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.namespace :backoffice do |backoffice|
|
||||
backoffice.resources :products
|
||||
end
|
||||
end
|
||||
|
||||
assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
|
||||
end
|
||||
end
|
||||
|
||||
def test_resource_has_many_in_namespace
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.namespace :backoffice do |backoffice|
|
||||
backoffice.resources :products, :has_many => :tags
|
||||
end
|
||||
end
|
||||
|
||||
assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
|
||||
assert_simply_restful_for :tags, :controller => "backoffice/tags", :name_prefix => "backoffice_product_", :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
|
||||
end
|
||||
end
|
||||
|
||||
def test_resource_has_one_in_namespace
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.namespace :backoffice do |backoffice|
|
||||
backoffice.resources :products, :has_one => :manufacturer
|
||||
end
|
||||
end
|
||||
|
||||
assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
|
||||
assert_singleton_restful_for :manufacturer, :controller => "backoffice/manufacturers", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
|
||||
end
|
||||
end
|
||||
|
||||
def test_resources_in_nested_namespace
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.namespace :backoffice do |backoffice|
|
||||
backoffice.namespace :admin do |admin|
|
||||
admin.resources :products
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_simply_restful_for :products, :controller => "backoffice/admin/products", :name_prefix => 'backoffice_admin_', :path_prefix => 'backoffice/admin/'
|
||||
end
|
||||
end
|
||||
|
||||
def test_resources_using_namespace
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.resources :products, :namespace => "backoffice/"
|
||||
end
|
||||
|
||||
assert_simply_restful_for :products, :controller => "backoffice/products"
|
||||
end
|
||||
end
|
||||
|
||||
def test_nested_resources_using_namespace
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.namespace :backoffice do |backoffice|
|
||||
backoffice.resources :products do |products|
|
||||
products.resources :images
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_simply_restful_for :images, :controller => "backoffice/images", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => {:product_id => '1'}
|
||||
end
|
||||
end
|
||||
|
||||
def test_nested_resources_in_nested_namespace
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.namespace :backoffice do |backoffice|
|
||||
backoffice.namespace :admin do |admin|
|
||||
admin.resources :products do |products|
|
||||
products.resources :images
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_simply_restful_for :images, :controller => "backoffice/admin/images", :name_prefix => 'backoffice_admin_product_', :path_prefix => 'backoffice/admin/products/1/', :options => {:product_id => '1'}
|
||||
end
|
||||
end
|
||||
|
||||
def test_with_path_segment
|
||||
with_restful_routing :messages, :as => 'reviews' do
|
||||
assert_simply_restful_for :messages, :as => 'reviews'
|
||||
end
|
||||
end
|
||||
|
||||
def test_multiple_with_path_segment_and_controller
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
map.resources :products do |products|
|
||||
products.resources :product_reviews, :as => 'reviews', :controller => 'messages'
|
||||
end
|
||||
map.resources :tutors do |tutors|
|
||||
tutors.resources :tutor_reviews, :as => 'reviews', :controller => 'comments'
|
||||
end
|
||||
end
|
||||
|
||||
assert_simply_restful_for :product_reviews, :controller=>'messages', :as => 'reviews', :name_prefix => 'product_', :path_prefix => 'products/1/', :options => {:product_id => '1'}
|
||||
assert_simply_restful_for :tutor_reviews,:controller=>'comments', :as => 'reviews', :name_prefix => 'tutor_', :path_prefix => 'tutors/1/', :options => {:tutor_id => '1'}
|
||||
end
|
||||
end
|
||||
|
||||
def test_with_path_segment_path_prefix_requirements
|
||||
expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
|
||||
with_restful_routing :messages, :as => 'comments',:path_prefix => '/thread/:thread_id', :requirements => { :thread_id => /[0-9]\.[0-9]\.[0-9]/ } do
|
||||
assert_recognizes(expected_options, :path => 'thread/1.1.1/comments/1', :method => :get)
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
def with_restful_routing(*args)
|
||||
with_routing do |set|
|
||||
set.draw { |map| map.resources(*args) }
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
||||
def with_singleton_resources(*args)
|
||||
with_routing do |set|
|
||||
set.draw { |map| map.resource(*args) }
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
||||
# runs assert_restful_routes_for and assert_restful_named_routes for on the controller_name and options, without passing a block.
|
||||
def assert_simply_restful_for(controller_name, options = {})
|
||||
assert_restful_routes_for controller_name, options
|
||||
assert_restful_named_routes_for controller_name, nil, options
|
||||
end
|
||||
|
||||
def assert_singleton_restful_for(singleton_name, options = {})
|
||||
assert_singleton_routes_for singleton_name, options
|
||||
assert_singleton_named_routes_for singleton_name, options
|
||||
end
|
||||
|
||||
def assert_restful_routes_for(controller_name, options = {})
|
||||
options[:options] ||= {}
|
||||
options[:options][:controller] = options[:controller] || controller_name.to_s
|
||||
|
||||
new_action = ActionController::Base.resources_path_names[:new] || "new"
|
||||
edit_action = ActionController::Base.resources_path_names[:edit] || "edit"
|
||||
if options[:path_names]
|
||||
new_action = options[:path_names][:new] if options[:path_names][:new]
|
||||
edit_action = options[:path_names][:edit] if options[:path_names][:edit]
|
||||
end
|
||||
|
||||
collection_path = "/#{options[:path_prefix]}#{options[:as] || controller_name}"
|
||||
member_path = "#{collection_path}/1"
|
||||
new_path = "#{collection_path}/#{new_action}"
|
||||
edit_member_path = "#{member_path}/#{edit_action}"
|
||||
formatted_edit_member_path = "#{member_path}/#{edit_action}.xml"
|
||||
|
||||
with_options(options[:options]) do |controller|
|
||||
controller.assert_routing collection_path, :action => 'index'
|
||||
controller.assert_routing new_path, :action => 'new'
|
||||
controller.assert_routing member_path, :action => 'show', :id => '1'
|
||||
controller.assert_routing edit_member_path, :action => 'edit', :id => '1'
|
||||
controller.assert_routing "#{collection_path}.xml", :action => 'index', :format => 'xml'
|
||||
controller.assert_routing "#{new_path}.xml", :action => 'new', :format => 'xml'
|
||||
controller.assert_routing "#{member_path}.xml", :action => 'show', :id => '1', :format => 'xml'
|
||||
controller.assert_routing formatted_edit_member_path, :action => 'edit', :id => '1', :format => 'xml'
|
||||
end
|
||||
|
||||
assert_recognizes(options[:options].merge(:action => 'index'), :path => collection_path, :method => :get)
|
||||
assert_recognizes(options[:options].merge(:action => 'new'), :path => new_path, :method => :get)
|
||||
assert_recognizes(options[:options].merge(:action => 'create'), :path => collection_path, :method => :post)
|
||||
assert_recognizes(options[:options].merge(:action => 'show', :id => '1'), :path => member_path, :method => :get)
|
||||
assert_recognizes(options[:options].merge(:action => 'edit', :id => '1'), :path => edit_member_path, :method => :get)
|
||||
assert_recognizes(options[:options].merge(:action => 'update', :id => '1'), :path => member_path, :method => :put)
|
||||
assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1'), :path => member_path, :method => :delete)
|
||||
|
||||
assert_recognizes(options[:options].merge(:action => 'index', :format => 'xml'), :path => "#{collection_path}.xml", :method => :get)
|
||||
assert_recognizes(options[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
|
||||
assert_recognizes(options[:options].merge(:action => 'create', :format => 'xml'), :path => "#{collection_path}.xml", :method => :post)
|
||||
assert_recognizes(options[:options].merge(:action => 'show', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :get)
|
||||
assert_recognizes(options[:options].merge(:action => 'edit', :id => '1', :format => 'xml'), :path => formatted_edit_member_path, :method => :get)
|
||||
assert_recognizes(options[:options].merge(:action => 'update', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :put)
|
||||
assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :delete)
|
||||
|
||||
yield options[:options] if block_given?
|
||||
end
|
||||
|
||||
# test named routes like foo_path and foos_path map to the correct options.
|
||||
def assert_restful_named_routes_for(controller_name, singular_name = nil, options = {})
|
||||
if singular_name.is_a?(Hash)
|
||||
options = singular_name
|
||||
singular_name = nil
|
||||
end
|
||||
singular_name ||= controller_name.to_s.singularize
|
||||
|
||||
options[:options] ||= {}
|
||||
options[:options][:controller] = options[:controller] || controller_name.to_s
|
||||
|
||||
@controller = "#{options[:options][:controller].camelize}Controller".constantize.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
get :index, options[:options]
|
||||
options[:options].delete :action
|
||||
|
||||
full_prefix = "/#{options[:path_prefix]}#{options[:as] || controller_name}"
|
||||
name_prefix = options[:name_prefix]
|
||||
|
||||
new_action = "new"
|
||||
edit_action = "edit"
|
||||
if options[:path_names]
|
||||
new_action = options[:path_names][:new] || "new"
|
||||
edit_action = options[:path_names][:edit] || "edit"
|
||||
end
|
||||
|
||||
assert_named_route "#{full_prefix}", "#{name_prefix}#{controller_name}_path", options[:options]
|
||||
assert_named_route "#{full_prefix}.xml", "formatted_#{name_prefix}#{controller_name}_path", options[:options].merge( :format => 'xml')
|
||||
assert_named_route "#{full_prefix}/1", "#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1')
|
||||
assert_named_route "#{full_prefix}/1.xml", "formatted_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
|
||||
|
||||
assert_named_route "#{full_prefix}/#{new_action}", "new_#{name_prefix}#{singular_name}_path", options[:options]
|
||||
assert_named_route "#{full_prefix}/#{new_action}.xml", "formatted_new_#{name_prefix}#{singular_name}_path", options[:options].merge( :format => 'xml')
|
||||
assert_named_route "#{full_prefix}/1/#{edit_action}", "edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1')
|
||||
assert_named_route "#{full_prefix}/1/#{edit_action}.xml", "formatted_edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
|
||||
|
||||
yield options[:options] if block_given?
|
||||
end
|
||||
|
||||
def assert_singleton_routes_for(singleton_name, options = {})
|
||||
options[:options] ||= {}
|
||||
options[:options][:controller] = options[:controller] || singleton_name.to_s.pluralize
|
||||
|
||||
full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
|
||||
new_path = "#{full_path}/new"
|
||||
edit_path = "#{full_path}/edit"
|
||||
formatted_edit_path = "#{full_path}/edit.xml"
|
||||
|
||||
with_options options[:options] do |controller|
|
||||
controller.assert_routing full_path, :action => 'show'
|
||||
controller.assert_routing new_path, :action => 'new'
|
||||
controller.assert_routing edit_path, :action => 'edit'
|
||||
controller.assert_routing "#{full_path}.xml", :action => 'show', :format => 'xml'
|
||||
controller.assert_routing "#{new_path}.xml", :action => 'new', :format => 'xml'
|
||||
controller.assert_routing formatted_edit_path, :action => 'edit', :format => 'xml'
|
||||
end
|
||||
|
||||
assert_recognizes(options[:options].merge(:action => 'show'), :path => full_path, :method => :get)
|
||||
assert_recognizes(options[:options].merge(:action => 'new'), :path => new_path, :method => :get)
|
||||
assert_recognizes(options[:options].merge(:action => 'edit'), :path => edit_path, :method => :get)
|
||||
assert_recognizes(options[:options].merge(:action => 'create'), :path => full_path, :method => :post)
|
||||
assert_recognizes(options[:options].merge(:action => 'update'), :path => full_path, :method => :put)
|
||||
assert_recognizes(options[:options].merge(:action => 'destroy'), :path => full_path, :method => :delete)
|
||||
|
||||
assert_recognizes(options[:options].merge(:action => 'show', :format => 'xml'), :path => "#{full_path}.xml", :method => :get)
|
||||
assert_recognizes(options[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
|
||||
assert_recognizes(options[:options].merge(:action => 'edit', :format => 'xml'), :path => formatted_edit_path, :method => :get)
|
||||
assert_recognizes(options[:options].merge(:action => 'create', :format => 'xml'), :path => "#{full_path}.xml", :method => :post)
|
||||
assert_recognizes(options[:options].merge(:action => 'update', :format => 'xml'), :path => "#{full_path}.xml", :method => :put)
|
||||
assert_recognizes(options[:options].merge(:action => 'destroy', :format => 'xml'), :path => "#{full_path}.xml", :method => :delete)
|
||||
|
||||
yield options[:options] if block_given?
|
||||
end
|
||||
|
||||
def assert_singleton_named_routes_for(singleton_name, options = {})
|
||||
(options[:options] ||= {})[:controller] ||= singleton_name.to_s.pluralize
|
||||
@controller = "#{options[:options][:controller].camelize}Controller".constantize.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
get :show, options[:options]
|
||||
options[:options].delete :action
|
||||
|
||||
full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
|
||||
name_prefix = options[:name_prefix]
|
||||
|
||||
assert_named_route "#{full_path}", "#{name_prefix}#{singleton_name}_path", options[:options]
|
||||
assert_named_route "#{full_path}.xml", "formatted_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
|
||||
|
||||
assert_named_route "#{full_path}/new", "new_#{name_prefix}#{singleton_name}_path", options[:options]
|
||||
assert_named_route "#{full_path}/new.xml", "formatted_new_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
|
||||
assert_named_route "#{full_path}/edit", "edit_#{name_prefix}#{singleton_name}_path", options[:options]
|
||||
assert_named_route "#{full_path}/edit.xml", "formatted_edit_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
|
||||
end
|
||||
|
||||
def assert_named_route(expected, route, options)
|
||||
actual = @controller.send(route, options) rescue $!.class.name
|
||||
assert_equal expected, actual, "Error on route: #{route}(#{options.inspect})"
|
||||
end
|
||||
|
||||
def assert_resource_methods(expected, resource, action_method, method)
|
||||
assert_equal expected.length, resource.send("#{action_method}_methods")[method].size, "#{resource.send("#{action_method}_methods")[method].inspect}"
|
||||
expected.each do |action|
|
||||
assert resource.send("#{action_method}_methods")[method].include?(action),
|
||||
"#{method} not in #{action_method} methods: #{resource.send("#{action_method}_methods")[method].inspect}"
|
||||
end
|
||||
end
|
||||
|
||||
def distinct_routes? (r1, r2)
|
||||
if r1.conditions == r2.conditions and r1.requirements == r2.requirements then
|
||||
if r1.segments.collect(&:to_s) == r2.segments.collect(&:to_s) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
true
|
||||
end
|
||||
end
|
File diff suppressed because it is too large
Load Diff
|
@ -1,628 +0,0 @@
|
|||
#--
|
||||
# Copyright (c) 2006 Assaf Arkin (http://labnotes.org)
|
||||
# Under MIT and/or CC By license.
|
||||
#++
|
||||
|
||||
require 'abstract_unit'
|
||||
require 'controller/fake_controllers'
|
||||
|
||||
class SelectorTest < Test::Unit::TestCase
|
||||
#
|
||||
# Basic selector: element, id, class, attributes.
|
||||
#
|
||||
|
||||
def test_element
|
||||
parse(%Q{<div id="1"></div><p></p><div id="2"></div>})
|
||||
# Match element by name.
|
||||
select("div")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal "2", @matches[1].attributes["id"]
|
||||
# Not case sensitive.
|
||||
select("DIV")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal "2", @matches[1].attributes["id"]
|
||||
# Universal match (all elements).
|
||||
select("*")
|
||||
assert_equal 3, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal nil, @matches[1].attributes["id"]
|
||||
assert_equal "2", @matches[2].attributes["id"]
|
||||
end
|
||||
|
||||
|
||||
def test_identifier
|
||||
parse(%Q{<div id="1"></div><p></p><div id="2"></div>})
|
||||
# Match element by ID.
|
||||
select("div#1")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
# Match element by ID, substitute value.
|
||||
select("div#?", 2)
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
# Element name does not match ID.
|
||||
select("p#?", 2)
|
||||
assert_equal 0, @matches.size
|
||||
# Use regular expression.
|
||||
select("#?", /\d/)
|
||||
assert_equal 2, @matches.size
|
||||
end
|
||||
|
||||
|
||||
def test_class_name
|
||||
parse(%Q{<div id="1" class=" foo "></div><p id="2" class=" foo bar "></p><div id="3" class="bar"></div>})
|
||||
# Match element with specified class.
|
||||
select("div.foo")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
# Match any element with specified class.
|
||||
select("*.foo")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal "2", @matches[1].attributes["id"]
|
||||
# Match elements with other class.
|
||||
select("*.bar")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
assert_equal "3", @matches[1].attributes["id"]
|
||||
# Match only element with both class names.
|
||||
select("*.bar.foo")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
end
|
||||
|
||||
|
||||
def test_attribute
|
||||
parse(%Q{<div id="1"></div><p id="2" title="" bar="foo"></p><div id="3" title="foo"></div>})
|
||||
# Match element with attribute.
|
||||
select("div[title]")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "3", @matches[0].attributes["id"]
|
||||
# Match any element with attribute.
|
||||
select("*[title]")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
assert_equal "3", @matches[1].attributes["id"]
|
||||
# Match element with attribute value.
|
||||
select("*[title=foo]")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "3", @matches[0].attributes["id"]
|
||||
# Match element with attribute and attribute value.
|
||||
select("[bar=foo][title]")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
# Not case sensitive.
|
||||
select("[BAR=foo][TiTle]")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
end
|
||||
|
||||
|
||||
def test_attribute_quoted
|
||||
parse(%Q{<div id="1" title="foo"></div><div id="2" title="bar"></div><div id="3" title=" bar "></div>})
|
||||
# Match without quotes.
|
||||
select("[title = bar]")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
# Match with single quotes.
|
||||
select("[title = 'bar' ]")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
# Match with double quotes.
|
||||
select("[title = \"bar\" ]")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
# Match with spaces.
|
||||
select("[title = \" bar \" ]")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "3", @matches[0].attributes["id"]
|
||||
end
|
||||
|
||||
|
||||
def test_attribute_equality
|
||||
parse(%Q{<div id="1" title="foo bar"></div><div id="2" title="barbaz"></div>})
|
||||
# Match (fail) complete value.
|
||||
select("[title=bar]")
|
||||
assert_equal 0, @matches.size
|
||||
# Match space-separate word.
|
||||
select("[title~=foo]")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
select("[title~=bar]")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
# Match beginning of value.
|
||||
select("[title^=ba]")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
# Match end of value.
|
||||
select("[title$=ar]")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
# Match text in value.
|
||||
select("[title*=bar]")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal "2", @matches[1].attributes["id"]
|
||||
# Match first space separated word.
|
||||
select("[title|=foo]")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
select("[title|=bar]")
|
||||
assert_equal 0, @matches.size
|
||||
end
|
||||
|
||||
|
||||
#
|
||||
# Selector composition: groups, sibling, children
|
||||
#
|
||||
|
||||
|
||||
def test_selector_group
|
||||
parse(%Q{<h1 id="1"></h1><h2 id="2"></h2><h3 id="3"></h3>})
|
||||
# Simple group selector.
|
||||
select("h1,h3")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal "3", @matches[1].attributes["id"]
|
||||
select("h1 , h3")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal "3", @matches[1].attributes["id"]
|
||||
# Complex group selector.
|
||||
parse(%Q{<h1 id="1"><a href="foo"></a></h1><h2 id="2"><a href="bar"></a></h2><h3 id="2"><a href="baz"></a></h3>})
|
||||
select("h1 a, h3 a")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "foo", @matches[0].attributes["href"]
|
||||
assert_equal "baz", @matches[1].attributes["href"]
|
||||
# And now for the three selector challenge.
|
||||
parse(%Q{<h1 id="1"><a href="foo"></a></h1><h2 id="2"><a href="bar"></a></h2><h3 id="2"><a href="baz"></a></h3>})
|
||||
select("h1 a, h2 a, h3 a")
|
||||
assert_equal 3, @matches.size
|
||||
assert_equal "foo", @matches[0].attributes["href"]
|
||||
assert_equal "bar", @matches[1].attributes["href"]
|
||||
assert_equal "baz", @matches[2].attributes["href"]
|
||||
end
|
||||
|
||||
|
||||
def test_sibling_selector
|
||||
parse(%Q{<h1 id="1"></h1><h2 id="2"></h2><h3 id="3"></h3>})
|
||||
# Test next sibling.
|
||||
select("h1+*")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
select("h1+h2")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
select("h1+h3")
|
||||
assert_equal 0, @matches.size
|
||||
select("*+h3")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "3", @matches[0].attributes["id"]
|
||||
# Test any sibling.
|
||||
select("h1~*")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
assert_equal "3", @matches[1].attributes["id"]
|
||||
select("h2~*")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "3", @matches[0].attributes["id"]
|
||||
end
|
||||
|
||||
|
||||
def test_children_selector
|
||||
parse(%Q{<div><p id="1"><span id="2"></span></p></div><div><p id="3"><span id="4" class="foo"></span></p></div>})
|
||||
# Test child selector.
|
||||
select("div>p")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal "3", @matches[1].attributes["id"]
|
||||
select("div>span")
|
||||
assert_equal 0, @matches.size
|
||||
select("div>p#3")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "3", @matches[0].attributes["id"]
|
||||
select("div>p>span")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
assert_equal "4", @matches[1].attributes["id"]
|
||||
# Test descendant selector.
|
||||
select("div p")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal "3", @matches[1].attributes["id"]
|
||||
select("div span")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
assert_equal "4", @matches[1].attributes["id"]
|
||||
select("div *#3")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "3", @matches[0].attributes["id"]
|
||||
select("div *#4")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "4", @matches[0].attributes["id"]
|
||||
# This is here because it failed before when whitespaces
|
||||
# were not properly stripped.
|
||||
select("div .foo")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "4", @matches[0].attributes["id"]
|
||||
end
|
||||
|
||||
|
||||
#
|
||||
# Pseudo selectors: root, nth-child, empty, content, etc
|
||||
#
|
||||
|
||||
|
||||
def test_root_selector
|
||||
parse(%Q{<div id="1"><div id="2"></div></div>})
|
||||
# Can only find element if it's root.
|
||||
select(":root")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
select("#1:root")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
select("#2:root")
|
||||
assert_equal 0, @matches.size
|
||||
# Opposite for nth-child.
|
||||
select("#1:nth-child(1)")
|
||||
assert_equal 0, @matches.size
|
||||
end
|
||||
|
||||
|
||||
def test_nth_child_odd_even
|
||||
parse(%Q{<table><tr id="1"></tr><tr id="2"></tr><tr id="3"></tr><tr id="4"></tr></table>})
|
||||
# Test odd nth children.
|
||||
select("tr:nth-child(odd)")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal "3", @matches[1].attributes["id"]
|
||||
# Test even nth children.
|
||||
select("tr:nth-child(even)")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
assert_equal "4", @matches[1].attributes["id"]
|
||||
end
|
||||
|
||||
|
||||
def test_nth_child_a_is_zero
|
||||
parse(%Q{<table><tr id="1"></tr><tr id="2"></tr><tr id="3"></tr><tr id="4"></tr></table>})
|
||||
# Test the third child.
|
||||
select("tr:nth-child(0n+3)")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "3", @matches[0].attributes["id"]
|
||||
# Same but an can be omitted when zero.
|
||||
select("tr:nth-child(3)")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "3", @matches[0].attributes["id"]
|
||||
# Second element (but not every second element).
|
||||
select("tr:nth-child(0n+2)")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
# Before first and past last returns nothing.:
|
||||
assert_raises(ArgumentError) { select("tr:nth-child(-1)") }
|
||||
select("tr:nth-child(0)")
|
||||
assert_equal 0, @matches.size
|
||||
select("tr:nth-child(5)")
|
||||
assert_equal 0, @matches.size
|
||||
end
|
||||
|
||||
|
||||
def test_nth_child_a_is_one
|
||||
parse(%Q{<table><tr id="1"></tr><tr id="2"></tr><tr id="3"></tr><tr id="4"></tr></table>})
|
||||
# a is group of one, pick every element in group.
|
||||
select("tr:nth-child(1n+0)")
|
||||
assert_equal 4, @matches.size
|
||||
# Same but a can be omitted when one.
|
||||
select("tr:nth-child(n+0)")
|
||||
assert_equal 4, @matches.size
|
||||
# Same but b can be omitted when zero.
|
||||
select("tr:nth-child(n)")
|
||||
assert_equal 4, @matches.size
|
||||
end
|
||||
|
||||
|
||||
def test_nth_child_b_is_zero
|
||||
parse(%Q{<table><tr id="1"></tr><tr id="2"></tr><tr id="3"></tr><tr id="4"></tr></table>})
|
||||
# If b is zero, pick the n-th element (here each one).
|
||||
select("tr:nth-child(n+0)")
|
||||
assert_equal 4, @matches.size
|
||||
# If b is zero, pick the n-th element (here every second).
|
||||
select("tr:nth-child(2n+0)")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal "3", @matches[1].attributes["id"]
|
||||
# If a and b are both zero, no element selected.
|
||||
select("tr:nth-child(0n+0)")
|
||||
assert_equal 0, @matches.size
|
||||
select("tr:nth-child(0)")
|
||||
assert_equal 0, @matches.size
|
||||
end
|
||||
|
||||
|
||||
def test_nth_child_a_is_negative
|
||||
parse(%Q{<table><tr id="1"></tr><tr id="2"></tr><tr id="3"></tr><tr id="4"></tr></table>})
|
||||
# Since a is -1, picks the first three elements.
|
||||
select("tr:nth-child(-n+3)")
|
||||
assert_equal 3, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal "2", @matches[1].attributes["id"]
|
||||
assert_equal "3", @matches[2].attributes["id"]
|
||||
# Since a is -2, picks the first in every second of first four elements.
|
||||
select("tr:nth-child(-2n+3)")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal "3", @matches[1].attributes["id"]
|
||||
# Since a is -2, picks the first in every second of first three elements.
|
||||
select("tr:nth-child(-2n+2)")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
end
|
||||
|
||||
|
||||
def test_nth_child_b_is_negative
|
||||
parse(%Q{<table><tr id="1"></tr><tr id="2"></tr><tr id="3"></tr><tr id="4"></tr></table>})
|
||||
# Select last of four.
|
||||
select("tr:nth-child(4n-1)")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "4", @matches[0].attributes["id"]
|
||||
# Select first of four.
|
||||
select("tr:nth-child(4n-4)")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
# Select last of every second.
|
||||
select("tr:nth-child(2n-1)")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
assert_equal "4", @matches[1].attributes["id"]
|
||||
# Select nothing since an+b always < 0
|
||||
select("tr:nth-child(-1n-1)")
|
||||
assert_equal 0, @matches.size
|
||||
end
|
||||
|
||||
|
||||
def test_nth_child_substitution_values
|
||||
parse(%Q{<table><tr id="1"></tr><tr id="2"></tr><tr id="3"></tr><tr id="4"></tr></table>})
|
||||
# Test with ?n?.
|
||||
select("tr:nth-child(?n?)", 2, 1)
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal "3", @matches[1].attributes["id"]
|
||||
select("tr:nth-child(?n?)", 2, 2)
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
assert_equal "4", @matches[1].attributes["id"]
|
||||
select("tr:nth-child(?n?)", 4, 2)
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
# Test with ? (b only).
|
||||
select("tr:nth-child(?)", 3)
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "3", @matches[0].attributes["id"]
|
||||
select("tr:nth-child(?)", 5)
|
||||
assert_equal 0, @matches.size
|
||||
end
|
||||
|
||||
|
||||
def test_nth_last_child
|
||||
parse(%Q{<table><tr id="1"></tr><tr id="2"></tr><tr id="3"></tr><tr id="4"></tr></table>})
|
||||
# Last two elements.
|
||||
select("tr:nth-last-child(-n+2)")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "3", @matches[0].attributes["id"]
|
||||
assert_equal "4", @matches[1].attributes["id"]
|
||||
# All old elements counting from last one.
|
||||
select("tr:nth-last-child(odd)")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
assert_equal "4", @matches[1].attributes["id"]
|
||||
end
|
||||
|
||||
|
||||
def test_nth_of_type
|
||||
parse(%Q{<table><thead></thead><tr id="1"></tr><tr id="2"></tr><tr id="3"></tr><tr id="4"></tr></table>})
|
||||
# First two elements.
|
||||
select("tr:nth-of-type(-n+2)")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal "2", @matches[1].attributes["id"]
|
||||
# All old elements counting from last one.
|
||||
select("tr:nth-last-of-type(odd)")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
assert_equal "4", @matches[1].attributes["id"]
|
||||
end
|
||||
|
||||
|
||||
def test_first_and_last
|
||||
parse(%Q{<table><thead></thead><tr id="1"></tr><tr id="2"></tr><tr id="3"></tr><tr id="4"></tr></table>})
|
||||
# First child.
|
||||
select("tr:first-child")
|
||||
assert_equal 0, @matches.size
|
||||
select(":first-child")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "thead", @matches[0].name
|
||||
# First of type.
|
||||
select("tr:first-of-type")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
select("thead:first-of-type")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "thead", @matches[0].name
|
||||
select("div:first-of-type")
|
||||
assert_equal 0, @matches.size
|
||||
# Last child.
|
||||
select("tr:last-child")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "4", @matches[0].attributes["id"]
|
||||
# Last of type.
|
||||
select("tr:last-of-type")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "4", @matches[0].attributes["id"]
|
||||
select("thead:last-of-type")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "thead", @matches[0].name
|
||||
select("div:last-of-type")
|
||||
assert_equal 0, @matches.size
|
||||
end
|
||||
|
||||
|
||||
def test_first_and_last
|
||||
# Only child.
|
||||
parse(%Q{<table><tr></tr></table>})
|
||||
select("table:only-child")
|
||||
assert_equal 0, @matches.size
|
||||
select("tr:only-child")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "tr", @matches[0].name
|
||||
parse(%Q{<table><tr></tr><tr></tr></table>})
|
||||
select("tr:only-child")
|
||||
assert_equal 0, @matches.size
|
||||
# Only of type.
|
||||
parse(%Q{<table><thead></thead><tr></tr><tr></tr></table>})
|
||||
select("thead:only-of-type")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "thead", @matches[0].name
|
||||
select("td:only-of-type")
|
||||
assert_equal 0, @matches.size
|
||||
end
|
||||
|
||||
|
||||
def test_empty
|
||||
parse(%Q{<table><tr></tr></table>})
|
||||
select("table:empty")
|
||||
assert_equal 0, @matches.size
|
||||
select("tr:empty")
|
||||
assert_equal 1, @matches.size
|
||||
parse(%Q{<div> </div>})
|
||||
select("div:empty")
|
||||
assert_equal 1, @matches.size
|
||||
end
|
||||
|
||||
|
||||
def test_content
|
||||
parse(%Q{<div> </div>})
|
||||
select("div:content()")
|
||||
assert_equal 1, @matches.size
|
||||
parse(%Q{<div>something </div>})
|
||||
select("div:content()")
|
||||
assert_equal 0, @matches.size
|
||||
select("div:content(something)")
|
||||
assert_equal 1, @matches.size
|
||||
select("div:content( 'something' )")
|
||||
assert_equal 1, @matches.size
|
||||
select("div:content( \"something\" )")
|
||||
assert_equal 1, @matches.size
|
||||
select("div:content(?)", "something")
|
||||
assert_equal 1, @matches.size
|
||||
select("div:content(?)", /something/)
|
||||
assert_equal 1, @matches.size
|
||||
end
|
||||
|
||||
|
||||
#
|
||||
# Test negation.
|
||||
#
|
||||
|
||||
|
||||
def test_element_negation
|
||||
parse(%Q{<p></p><div></div>})
|
||||
select("*")
|
||||
assert_equal 2, @matches.size
|
||||
select("*:not(p)")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "div", @matches[0].name
|
||||
select("*:not(div)")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "p", @matches[0].name
|
||||
select("*:not(span)")
|
||||
assert_equal 2, @matches.size
|
||||
end
|
||||
|
||||
|
||||
def test_id_negation
|
||||
parse(%Q{<p id="1"></p><p id="2"></p>})
|
||||
select("p")
|
||||
assert_equal 2, @matches.size
|
||||
select(":not(#1)")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
select(":not(#2)")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
end
|
||||
|
||||
|
||||
def test_class_name_negation
|
||||
parse(%Q{<p class="foo"></p><p class="bar"></p>})
|
||||
select("p")
|
||||
assert_equal 2, @matches.size
|
||||
select(":not(.foo)")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "bar", @matches[0].attributes["class"]
|
||||
select(":not(.bar)")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "foo", @matches[0].attributes["class"]
|
||||
end
|
||||
|
||||
|
||||
def test_attribute_negation
|
||||
parse(%Q{<p title="foo"></p><p title="bar"></p>})
|
||||
select("p")
|
||||
assert_equal 2, @matches.size
|
||||
select(":not([title=foo])")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "bar", @matches[0].attributes["title"]
|
||||
select(":not([title=bar])")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "foo", @matches[0].attributes["title"]
|
||||
end
|
||||
|
||||
|
||||
def test_pseudo_class_negation
|
||||
parse(%Q{<div><p id="1"></p><p id="2"></p></div>})
|
||||
select("p")
|
||||
assert_equal 2, @matches.size
|
||||
select("p:not(:first-child)")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
select("p:not(:nth-child(2))")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
end
|
||||
|
||||
|
||||
def test_negation_details
|
||||
parse(%Q{<p id="1"></p><p id="2"></p><p id="3"></p>})
|
||||
assert_raises(ArgumentError) { select(":not(") }
|
||||
assert_raises(ArgumentError) { select(":not(:not())") }
|
||||
select("p:not(#1):not(#3)")
|
||||
assert_equal 1, @matches.size
|
||||
assert_equal "2", @matches[0].attributes["id"]
|
||||
end
|
||||
|
||||
|
||||
def test_select_from_element
|
||||
parse(%Q{<div><p id="1"></p><p id="2"></p></div>})
|
||||
select("div")
|
||||
@matches = @matches[0].select("p")
|
||||
assert_equal 2, @matches.size
|
||||
assert_equal "1", @matches[0].attributes["id"]
|
||||
assert_equal "2", @matches[1].attributes["id"]
|
||||
end
|
||||
|
||||
|
||||
protected
|
||||
|
||||
def parse(html)
|
||||
@html = HTML::Document.new(html).root
|
||||
end
|
||||
|
||||
def select(*selector)
|
||||
@matches = HTML.selector(*selector).select(@html)
|
||||
end
|
||||
|
||||
end
|
|
@ -1,138 +0,0 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
|
||||
module TestFileUtils
|
||||
def file_name() File.basename(__FILE__) end
|
||||
def file_path() File.expand_path(__FILE__) end
|
||||
def file_data() File.open(file_path, 'rb') { |f| f.read } end
|
||||
end
|
||||
|
||||
|
||||
class SendFileController < ActionController::Base
|
||||
include TestFileUtils
|
||||
layout "layouts/standard" # to make sure layouts don't interfere
|
||||
|
||||
attr_writer :options
|
||||
def options() @options ||= {} end
|
||||
|
||||
def file() send_file(file_path, options) end
|
||||
def data() send_data(file_data, options) end
|
||||
|
||||
def rescue_action(e) raise end
|
||||
end
|
||||
|
||||
SendFileController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
|
||||
class SendFileTest < Test::Unit::TestCase
|
||||
include TestFileUtils
|
||||
|
||||
Mime::Type.register "image/png", :png unless defined? Mime::PNG
|
||||
|
||||
def setup
|
||||
@controller = SendFileController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
def test_file_nostream
|
||||
@controller.options = { :stream => false }
|
||||
response = nil
|
||||
assert_nothing_raised { response = process('file') }
|
||||
assert_not_nil response
|
||||
assert_kind_of String, response.body
|
||||
assert_equal file_data, response.body
|
||||
end
|
||||
|
||||
def test_file_stream
|
||||
response = nil
|
||||
assert_nothing_raised { response = process('file') }
|
||||
assert_not_nil response
|
||||
assert_kind_of Proc, response.body
|
||||
|
||||
require 'stringio'
|
||||
output = StringIO.new
|
||||
output.binmode
|
||||
assert_nothing_raised { response.body.call(response, output) }
|
||||
assert_equal file_data, output.string
|
||||
end
|
||||
|
||||
def test_file_url_based_filename
|
||||
@controller.options = { :url_based_filename => true }
|
||||
response = nil
|
||||
assert_nothing_raised { response = process('file') }
|
||||
assert_not_nil response
|
||||
assert_equal "attachment", response.headers["Content-Disposition"]
|
||||
end
|
||||
|
||||
def test_x_sendfile_header
|
||||
@controller.options = { :x_sendfile => true }
|
||||
|
||||
response = nil
|
||||
assert_nothing_raised { response = process('file') }
|
||||
assert_not_nil response
|
||||
|
||||
assert_equal @controller.file_path, response.headers['X-Sendfile']
|
||||
assert response.body.blank?
|
||||
end
|
||||
|
||||
def test_data
|
||||
response = nil
|
||||
assert_nothing_raised { response = process('data') }
|
||||
assert_not_nil response
|
||||
|
||||
assert_kind_of String, response.body
|
||||
assert_equal file_data, response.body
|
||||
end
|
||||
|
||||
def test_headers_after_send_shouldnt_include_charset
|
||||
response = process('data')
|
||||
assert_equal "application/octet-stream", response.content_type
|
||||
|
||||
response = process('file')
|
||||
assert_equal "application/octet-stream", response.content_type
|
||||
end
|
||||
|
||||
# Test that send_file_headers! is setting the correct HTTP headers.
|
||||
def test_send_file_headers!
|
||||
options = {
|
||||
:length => 1,
|
||||
:type => Mime::PNG,
|
||||
:disposition => 'disposition',
|
||||
:filename => 'filename'
|
||||
}
|
||||
|
||||
# Do it a few times: the resulting headers should be identical
|
||||
# no matter how many times you send with the same options.
|
||||
# Test resolving Ticket #458.
|
||||
@controller.headers = {}
|
||||
@controller.send(:send_file_headers!, options)
|
||||
@controller.send(:send_file_headers!, options)
|
||||
@controller.send(:send_file_headers!, options)
|
||||
|
||||
h = @controller.headers
|
||||
assert_equal 1, h['Content-Length']
|
||||
assert_equal 'image/png', h['Content-Type']
|
||||
assert_equal 'disposition; filename="filename"', h['Content-Disposition']
|
||||
assert_equal 'binary', h['Content-Transfer-Encoding']
|
||||
|
||||
# test overriding Cache-Control: no-cache header to fix IE open/save dialog
|
||||
@controller.headers = { 'Cache-Control' => 'no-cache' }
|
||||
@controller.send(:send_file_headers!, options)
|
||||
h = @controller.headers
|
||||
assert_equal 'private', h['Cache-Control']
|
||||
end
|
||||
|
||||
%w(file data).each do |method|
|
||||
define_method "test_send_#{method}_status" do
|
||||
@controller.options = { :stream => false, :status => 500 }
|
||||
assert_nothing_raised { assert_not_nil process(method) }
|
||||
assert_equal '500 Internal Server Error', @response.headers['Status']
|
||||
end
|
||||
|
||||
define_method "test_default_send_#{method}_status" do
|
||||
@controller.options = { :stream => false }
|
||||
assert_nothing_raised { assert_not_nil process(method) }
|
||||
assert_equal ActionController::Base::DEFAULT_RENDER_STATUS_CODE, @response.headers['Status']
|
||||
end
|
||||
end
|
||||
end
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue