Skip to content

Instantly share code, notes, and snippets.

@stevepm
Last active August 29, 2015 14:01
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 stevepm/4562475bd1f4d57e7f9f to your computer and use it in GitHub Desktop.
Save stevepm/4562475bd1f4d57e7f9f to your computer and use it in GitHub Desktop.
HttpResponse
require 'fileutils'
class HttpResponse
def initialize(text)
@text = text
@headers = @text.split("\n\n").first
@body = @text.split("\n\n")[1]
end
def headers
lines = @headers.split("\n")
lines.shift
new_lines = lines.map do |line|
line.split(/[:\s]/, 2)
end
headers = new_lines.flatten.map do |line|
line.strip
end
header_hash = {}
Hash[*headers].each do |k, v|
header_hash[k.downcase.gsub('-','_').to_sym] = v
end
header_hash
end
def body
<<-BODY
#{@body}
BODY
end
def status_code
status_line = @headers.split("\n").shift
status_line.split(/[\s]/,2)[1].gsub(/[\D]/, '').to_i
end
def response_json
if headers[:content_type].include?("json")
JSON.parse(@body)
else
nil
end
end
def save_html!
path = File.join(File.dirname(__FILE__), '../tmp/')
if headers[:content_type].include?("text/html")
unless File.directory?(path)
FileUtils.mkdir_p(path)
end
File.open "#{path}body.html", "w+" do |file|
file.write(@body)
end
`open "#{path}body.html"`
end
end
end
require 'json'
require 'spec_helper'
require_relative '../../lib/http_response'
describe "http_response" do
let(:text) {
<<-INPUT
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Tue, 06 May 2014 02:17:16 GMT
Content-Type: text/html
Last-Modified: Sun, 27 Apr 2014 04:03:41 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Content-Encoding: gzip
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8" />
<meta name="description" content="should i test private methods?" />
<meta name="keywords" content="test,private,methods,oo,object,oriented,tdd" />
<title>Should I Test Private Methods?</title>
</head>
<body>
<div style='font-size: 96px; font-weight: bold; text-align: center; padding-top: 200px; font-family: Verdana, Helvetica, sans-serif'>NO</div>
<!-- Every time you consider testing a private method, your code is telling you that you haven't allocated responsibilities well. Are you listening to it? -->
</body>
</html>
INPUT
}
let(:response) { HttpResponse.new(text) }
let(:json) {
<<-INPUT
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 06 May 2014 02:15:51 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST
{"coord":{"lon":-0.13,"lat":51.51},"sys":{"message":0.0346,"country":"GB","sunrise":1399350122,"sunset":1399404728},"weather":[{"id":721,"main":"Haze","description":"haze","icon":"50n"},{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"base":"cmc stations","main":{"temp":286.24,"humidity":83,"pressure":995,"temp_min":284.82,"temp_max":287.59},"wind":{"speed":3.77,"deg":220.5},"clouds":{"all":88},"dt":1399339749,"id":2643743,"name":"London","cod":200}
INPUT
}
it 'returns the headers' do
expect(response.headers).to eq({:server=>"nginx/1.4.6 (Ubuntu)", :date=>"Tue, 06 May 2014 02:17:16 GMT", :content_type=>"text/html", :last_modified=>"Sun, 27 Apr 2014 04:03:41 GMT", :transfer_encoding=>"chunked", :connection=>"keep-alive", :content_encoding=>"gzip"})
expect(response.headers).to_not include({"HTTP/1.1" => "200 OK"})
end
it 'returns the body' do
body = <<-INPUT
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8" />
<meta name="description" content="should i test private methods?" />
<meta name="keywords" content="test,private,methods,oo,object,oriented,tdd" />
<title>Should I Test Private Methods?</title>
</head>
<body>
<div style='font-size: 96px; font-weight: bold; text-align: center; padding-top: 200px; font-family: Verdana, Helvetica, sans-serif'>NO</div>
<!-- Every time you consider testing a private method, your code is telling you that you haven't allocated responsibilities well. Are you listening to it? -->
</body>
</html>
INPUT
expect(response.body.strip).to eq(body.strip)
end
it 'returns the status code' do
expect(response.status_code).to eq(200)
end
it 'returns a ruby hash if it is json' do
expect(response.response_json).to eq(nil)
json_response = HttpResponse.new(json)
expect(json_response.response_json).to eq(JSON.parse('{"coord":{"lon":-0.13,"lat":51.51},"sys":{"message":0.0346,"country":"GB","sunrise":1399350122,"sunset":1399404728},"weather":[{"id":721,"main":"Haze","description":"haze","icon":"50n"},{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"base":"cmc stations","main":{"temp":286.24,"humidity":83,"pressure":995,"temp_min":284.82,"temp_max":287.59},"wind":{"speed":3.77,"deg":220.5},"clouds":{"all":88},"dt":1399339749,"id":2643743,"name":"London","cod":200}'))
end
it 'writes the html to a file and opens it in a browser' do
response.save_html!
path = File.join(File.dirname(__FILE__), '../../tmp/')
expect(File.exists?("#{path}body.html")).to eq(true)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment