Skip to content

Instantly share code, notes, and snippets.

@pete-otaqui
Created February 5, 2010 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pete-otaqui/295755 to your computer and use it in GitHub Desktop.
Save pete-otaqui/295755 to your computer and use it in GitHub Desktop.
Sinatra "Response Fetcher" which loads text files of HTTP responses and reconstructs them
HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Etag: "3f80f-1b6-3e1cb03b"
Accept-Ranges: bytes
Content-Length: 438
Connection: close
Content-Type: text/html; charset=UTF-8
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Home</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</body>
</html>
# About:
# Fetches plain text files of complete http responses
# and reconstructs them into the header and body parts
#
# Usage:
# require 'sinatra'
# require 'sinatra/responsefetcher'
# get "/" do
# fetchresponse('responses/home_200.txt')
# end
require 'sinatra/base'
module Sinatra
module ResponseFetcherHelper
def fetchresponse(filename)
response = {}
code = '404'
head = {}
body = ''
file = File.open(filename)
done = false
file.each_line do |line|
line.strip!
if done == false then
unless line.size == 0 then
#puts "HEADER .... "+line
unless line =~ /:/ then
codeparts = line.split(' ')
code = codeparts[1]
else
headparts = line.split(':')
head[headparts[0]] = headparts[1]
end
else
done = true
end
else
body += line
end
end
status code
headers head
return body
end
end
helpers ResponseFetcherHelper
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment