rspec - Mocking or Stubbing mtime for File::Stat -


my object gets file on http.

it so, using if-modified-since header. if files has not been modified since time in header not modified response returned , file should not fetched&written. so:

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') |f|       f.write res.body     end if res.is_a?(net::httpsuccess)   end   #... end 

i want test both cases (in both cases, file on disk exists). so, i'd need stub either in net::http, or need stub file.stat return mtime sure online resource return new or not-modified-since.

should stub (or mock) net::http? , if so, what?

or should stub mtime return date far in past or far in future enforce or suppress not-modified header?


edit: diving deeper matter, learned i.ytimg.com-domain not support these headers. i'll need solve inspecing json youtube api. however, problem "what , how mock when testing if-modified-since-headers" still stands.

here how conclude domain not support this:

$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 

there no "last-modified" there. illustrated call, exaple.com, support if-modified-since headers.

$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 


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -