- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的 Rails 应用程序中使用 Paperclip
来附加 images
。我在模型中将 content_type
的验证声明为
validates_attachment :image,
:content_type => { :content_type => ["image/jpg", "image/gif", "image/png"] }
我有两个例子,一个是有效图片,另一个是无效图片
对于无效图像,我只是将 .txt
文件重命名为 .png
it "Image is valid" do
image = File.new("#{Rails.root}/spec/support/right.png")
expect(FactoryGirl.build(:pin, image: image)).to be_valid
end
it "Image is invalid" do
image = File.new("#{Rails.root}/spec/support/wrong.png")
expect(FactoryGirl.build(:pin, image: image)).to have(1).errors_on(:image_content_type)
end
我希望我的两个示例都能成功运行。但是,我的第二个例子失败了。对于 wrong.png
的 content_type,我没有收到任何错误。
我认为 Paperclip 的 content_type
验证实际上会检查上传文件的文件格式(二进制数据编码)。但似乎在这里,它只是检查文件扩展名。此验证是否仅检查上传文件的扩展名?
我可能在这里遗漏了一些东西(配置?)。 Paperclip 中是否还有其他可用的验证来实现此目的?还是在这种情况下我应该选择自定义验证器?
最佳答案
Paperclip 于 2014 年 2 月 21 日
发布的最新版本 4.1.1
中解决了此问题。
it "Image is valid" do
image = File.new("#{Rails.root}/spec/support/right.png")
expect(FactoryGirl.build(:pin, image: image)).to be_valid
end
it "Image is invalid" do
image = File.new("#{Rails.root}/spec/support/wrong.png")
expect(FactoryGirl.build(:pin, image: image)).to have(1).errors_on(:image_content_type)
end
经过一些研究发现,当我上传一张无效图片
时,
例如:欺骗(重命名)wrong.txt
文件为wrong.png
并上传。
在 Paperclip 的先前版本中,wrong.png
以出色的表现通过了 content_type
验证,没有给出任何错误,因为 Paperclip
仅用于 检查上传文件的扩展名
,而不是其中的内容。
然而,在 Paperclip 4.1.1
的当前版本中,相同的欺骗 wrong.png
未通过验证并在 View 中抛出以下错误:
Image has an extension that does not match its contents
在调查服务器日志条目后,我发现了以下内容:
Command :: file -b --mime-type '/var/folders/tg/8sxl1vss4fb0sqtcrv3lzcfm0000gn/T/a7f21d0002b0d9d91eb158d702cd930320140317-531-swkmb8' [paperclip] Content Type Spoof: Filename wrong.png (["image/png"]), content type discovered from file command: text/plain. See documentation to allow this combination.
在这里,您可以看到 Paperclip 实际上检查了说明 text/plain
的上传文件的内容,并且还错误地指出了 Content Type Spoof
。
希望我的发现能帮助其他人了解 Paperclip 的内容类型
验证如何随着时间的推移而得到改进。
关于ruby-on-rails - 回形针 + RSpec : content_type validation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21610920/
我想知道 Rspec 中的命令式和声明式步骤是什么。 这是 Rspec 书中的示例代码: Scenario: transfer money (declarative) Given I have $10
我正在尝试使用 Travis 设置 CI。但是我遇到了在 Travis 上失败但在本地没有的测试,甚至提供了相同的种子。 我认为种子运行相同,但现在我不确定,并想弄清楚它是否存在,所以我现在在哪里看。
RSpec 的文档提到了 --bisect option ,当运行时提供最小的复制,例如 rspec ./spec/calculator_10_spec.rb[1:1] ./spec/calculat
我有一些昂贵的测试设置,仅对我的规范中的少数示例是必需的,如果需要,它只需要运行一次。因为它很慢,所以我试图避免将它放在 before(:each) block 中,但 before(:all) 似乎
在我的 Gemfile 中 gem 'rspec-rails', '~> 3.3' 我发现,errors_on 匹配器移动到了另一个 gem:https://github.com/rspec/rspe
如果我运行此命令“rspec ./spec/requests/api/v1/password_reset_request_spec.rb”,此文件中的所有测试都会通过。 但是,当我运行“rspec”时
我花了 4 天时间尝试正确安装 Ruby on Rails,但我遇到了一个大问题。我第一次使用Linux(对我来说更舒服),但遇到问题后,我使用了Windows。令我惊讶的是,我在 Windows 上
我在这样的验证中测试了很多坏字符串: ['0', '3:a', 'xx:11', '-1', '-3:00', 'h', 'h2', 'h2h', 'm', 'm10', 'm10m', '2hm',
我的测试套件中有一部分运行着一堆非常慢的导入器。这些测试不需要经常运行(除非实际上正在处理导入器)所以我使用 Rspec 标签将它们分开:http://relishapp.com/rspec/rspe
Rspec 尝试在运行任何规范文件或整个测试套件结束时运行 Test::Unit 测试。它仍然没关系,因为我没有任何测试单元测试文件,但它尝试传递给 rspec 的命令行选项,因为它们将传递给 Tes
我有一段代码要测试: class User :destroy end 请举例测试关联代码的RSpec代码。 最佳答案 对我来说,这个变体效果很好: describe User do it { s
我正在尝试在 rails Controller 规范中重用一些通用代码。我对管理员用户和普通用户有不同的上下文。但是,对于特定操作,大部分行为是相同的,因此我尝试将这种常见行为提取到辅助函数中: de
我有两个类OneClass和 AnotherClass : class OneClass def initialize(*args) @another_member = AnotherCl
我有一个看起来像这样的模型: class Gist def self.create(options) post_response = Faraday.post do |request|
我是 RSPEC 的新手。我编写了一个名为 result_spec.rb 的 RSPEC 代码,如下所示: describe '#grouped_scores' do subject { result
我对字符串类进行了如下扩展: class String def last_character self[-1] end end 我将 string.rb 文件放在 lib 中,如下所示
我是 RSpec 的新手,我在其中编写了一个测试场景: my_object.should_not be_valid 它工作正常。但是,我想测试模型的特定属性是否无效。这样的 RSpec 行为是现成的吗
我正在尝试测试邮政编码属性的长度以确保其长度为 5 个字符。现在我正在测试以确保它不是空白,然后是 4 个字符太短,6 个字符太长。 有没有办法测试它是否正好是 5 个字符?到目前为止,我在网上或 r
我将rspec与电子邮件规范gem一起使用。我正在尝试做: last_delivery = ActionMailer::Base.deliveries.last last_delivery.body.
我创建了一个新的 rails 应用程序,并按照此处的 rspec-rails 安装说明进行操作 - https://github.com/rspec/rspec-rails然后我在我的 app/lib
我是一名优秀的程序员,十分优秀!