首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >存根图像url并返回图像

存根图像url并返回图像
EN

Stack Overflow用户
提问于 2012-07-27 21:30:48
回答 1查看 1.9K关注 0票数 2

如何存根调用url (例如http://www.example.com/images/123.png )并返回名为123.png的图像?

我使用的是Rails 3.2,Carrierwave。我试过Fakeweb,但有点迷惑。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-22 11:27:11

经过几个小时的研究,事实证明很简单:

代码语言:javascript
复制
FakeWeb.register_uri(:get, string_or_regxp_of_uri,
        body: SupportFiles.uploaded_file("square.jpg"),
        content_type: 'image/jpg')

我的问题更棘手:

我正在测试FB头像,我得到了whitelst扩展

以上代码将不起作用,因为缺少扩展名

(FB头像网址:https://graph.facebook.com/123/picture)

但真实的FB头像将重定向到CDN或扩展名为

因此,您需要添加另一个存根:

代码语言:javascript
复制
# Register a fake remote image
fake_avatar_uri = "https://graph.facebook.com/fake_avatar.jpg"
# Redirect to a fake uri
FakeWeb.register_uri(:get, %r|https://graph\.facebook\.com/|,
  status: ["301", "Moved Permanently"],
  location: fake_avatar_uri)
# Feed fake image for the fake uri
FakeWeb.register_uri(:get, fake_avatar_uri,
  body: SupportFiles.uploaded_file("square.jpg"),
  content_type: 'image/jpg')

SupportFiles模块(不是我自己写的:P):

代码语言:javascript
复制
require 'rack/test/uploaded_file'

module SupportFiles
  extend ActiveSupport::Concern

  included do
    let(:an_image) do
      open_file("square.jpg")
    end
  end


  def open_file(filename)
    File.open(support_file_path(filename))
  end

  # idea stolen from ActionDispatch::TestProcess#fixture_file_upload
  def uploaded_file(filename)
    Rack::Test::UploadedFile.new(support_file_path(filename))
  end
  module_function :uploaded_file

  protected

  def support_file_path(filename)
    Rails.root.join("spec/support/files", filename)
  end
  module_function :support_file_path

end
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11688777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档