Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 模拟或截短文件::Stat的mtime_File_Rspec_Mocking_Net Http_If Modified Since - Fatal编程技术网

File 模拟或截短文件::Stat的mtime

File 模拟或截短文件::Stat的mtime,file,rspec,mocking,net-http,if-modified-since,File,Rspec,Mocking,Net Http,If Modified Since,我的对象通过HTTP获取文件 如果自标题修改,则使用。如果文件自标头中的时间起未被修改,则将返回未修改响应,并且不应获取和写入文件。像这样: class YouTube #... def parse uri = URI("http://i.ytimg.com/vi/#{@id}/0.jpg") req = Net::HTTP::Get.new(uri.request_uri) if File.exists? thumbname stat = File.

我的对象
通过HTTP获取文件

如果自
标题修改,则使用
。如果文件自标头中的时间起未被修改,则将返回未修改响应,并且不应获取和写入文件。像这样:

class YouTube
  #...
  def parse
    uri = URI("http://i.ytimg.com/vi/#{@id}/0.jpg")
    req = Net::HTTP::Get.new(uri.request_uri)
    if File.exists? thumbname
      stat = File.stat thumbname
      req['If-Modified-Since'] = stat.mtime.rfc2822
    end

    res = Net::HTTP.start(uri.hostname, uri.port) {|http|
      http.request(req)
    }

    File.open(thumbname, 'wb') do |f|
      f.write res.body
    end if res.is_a?(Net::HTTPSuccess)
  end
  #...
end
我想测试这两种情况(在这两种情况下,磁盘上都存在一个文件)。为此,我需要在
Net::HTTP
中存根某些内容,或者我需要存根File.stat以返回一个
mtime
,我确信在线资源将返回一个新的或未修改的

我应该存根(甚至模拟)
Net::HTTP
?如果是的话,是什么

或者我应该存根
mtime
以返回一个远在过去或远在未来的日期来强制或抑制未修改的标题


编辑:深入调查后,我了解到I.ytimg.com-domain不支持这些标题。所以我需要通过从YouTube API中插入JSON来解决这个问题。然而,“如果修改了自标头,那么在测试时模拟什么以及如何模拟”的问题仍然存在

以下是我如何得出该领域不支持这一点的结论:

$curl -I --header 'If-Modified-Since: Sun, 24 Mar 2013 17:33:29 +0100' -L http://i.ytimg.com/vi/D80QdsFWdcQ/0.jpg
HTTP/1.1 200 OK
Content-Type: image/jpeg
Date: Sun, 24 Mar 2013 16:31:10 GMT
Expires: Sun, 24 Mar 2013 22:31:10 GMT
X-Content-Type-Options: nosniff
Server: sffe
Content-Length: 13343
X-XSS-Protection: 1; mode=block
Cache-Control: public, max-age=21600
Age: 822
这里没有“上次修改”。如另一个调用Example.com所示,该调用不支持if-modified-since头

$curl -I --header 'If-Modified-Since: Sun, 24 Mar 2013 17:33:29 +0100' -L example.com
HTTP/1.0 302 Found
Location: http://www.iana.org/domains/example/
Server: BigIP
Connection: Keep-Alive
Content-Length: 0

HTTP/1.1 302 FOUND
Date: Sun, 24 Mar 2013 16:47:47 GMT
Server: Apache/2.2.3 (CentOS)
Location: http://www.iana.org/domains/example
Connection: close
Content-Type: text/html; charset=utf-8

HTTP/1.1 304 NOT MODIFIED
Date: Sun, 24 Mar 2013 16:47:47 GMT
Server: Apache/2.2.3 (CentOS)
Connection: close