Skip to content

Commit

Permalink
Use actual Http::Parser in tests, not mock.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgraham committed Sep 13, 2014
1 parent 0b67495 commit 32dcce4
Showing 1 changed file with 43 additions and 58 deletions.
101 changes: 43 additions & 58 deletions test/stream/http/request_test.rb
Expand Up @@ -9,14 +9,7 @@
before do
File.open(PASSWORD, 'w') {|f| f.puts '/etc/passwd contents' }
File.open(INDEX, 'w') {|f| f.puts 'index.html contents' }

@stream = MiniTest::Mock.new
@parser = MiniTest::Mock.new
@parser.expect(:headers, {'Content-Type' => 'text/html', 'Host' => 'wonderland.lit'})
@parser.expect(:http_method, 'GET')
@parser.expect(:request_path, '/blogs/12')
@parser.expect(:request_url, '/blogs/12?ok=true')
@parser.expect(:query_string, 'ok=true')
end

after do
Expand All @@ -26,21 +19,26 @@

describe 'initialize' do
it 'copies request info from parser' do
request = Vines::Stream::Http::Request.new(@stream, @parser, '<html></html>')
assert_equal request.headers, {'Content-Type' => 'text/html', 'Host' => 'wonderland.lit'}
headers = ['Host: wonderland.lit', 'Content-Type: text/html']
parser = parser('GET', '/blogs/12?ok=true', headers)
request = Vines::Stream::Http::Request.new(@stream, parser, '<html></html>')

assert_equal request.headers, {'Host' => 'wonderland.lit', 'Content-Type' => 'text/html'}
assert_equal request.method, 'GET'
assert_equal request.path, '/blogs/12'
assert_equal request.url, '/blogs/12?ok=true'
assert_equal request.query, 'ok=true'
assert_equal request.body, '<html></html>'
assert @stream.verify
assert @parser.verify
end
end

describe 'reply_with_file' do
it 'returns 404 file not found' do
request = Vines::Stream::Http::Request.new(@stream, @parser, '<html></html>')
headers = ['Host: wonderland.lit', 'Content-Type: text/html']
parser = parser('GET', '/blogs/12?ok=true', headers)
request = Vines::Stream::Http::Request.new(@stream, parser, '<html></html>')

headers = [
"HTTP/1.1 404 Not Found",
"Content-Length: 0"
Expand All @@ -50,17 +48,11 @@

request.reply_with_file(Dir.pwd)
assert @stream.verify
assert @parser.verify
end

it 'prevents directory traversal with 404 response' do
parser = MiniTest::Mock.new
parser.expect(:headers, {'Content-Type' => 'text/html', 'Host' => 'wonderland.lit'})
parser.expect(:http_method, 'GET')
parser.expect(:request_path, '/../passwords')
parser.expect(:request_url, '/../passwords')
parser.expect(:query_string, '')

headers = ['Host: wonderland.lit', 'Content-Type: text/html']
parser = parser('GET', '/../passwords', headers)
request = Vines::Stream::Http::Request.new(@stream, parser, '<html></html>')

headers = [
Expand All @@ -72,17 +64,11 @@

request.reply_with_file(Dir.pwd)
assert @stream.verify
assert parser.verify
end

it 'serves index.html for directory request' do
parser = MiniTest::Mock.new
parser.expect(:headers, {'Content-Type' => 'text/html', 'Host' => 'wonderland.lit'})
parser.expect(:http_method, 'GET')
parser.expect(:request_path, '/')
parser.expect(:request_url, '/?ok=true')
parser.expect(:query_string, 'ok=true')

headers = ['Host: wonderland.lit', 'Content-Type: text/html']
parser = parser('GET', '/?ok=true', headers)
request = Vines::Stream::Http::Request.new(@stream, parser, '<html></html>')

mtime = File.mtime(INDEX).utc.strftime('%a, %d %b %Y %H:%M:%S GMT')
Expand All @@ -98,17 +84,11 @@

request.reply_with_file(Dir.pwd)
assert @stream.verify
assert parser.verify
end

it 'redirects for missing trailing slash' do
parser = MiniTest::Mock.new
parser.expect(:headers, {'Content-Type' => 'text/html', 'Host' => 'wonderland.lit'})
parser.expect(:http_method, 'GET')
parser.expect(:request_path, '/http')
parser.expect(:request_url, '/http?ok=true')
parser.expect(:query_string, 'ok=true')

headers = ['Host: wonderland.lit', 'Content-Type: text/html']
parser = parser('GET', '/http?ok=true', headers)
request = Vines::Stream::Http::Request.new(@stream, parser, '<html></html>')

headers = [
Expand All @@ -121,23 +101,18 @@
# so the /http url above will work
request.reply_with_file(File.expand_path('../../', __FILE__))
assert @stream.verify
assert parser.verify
end
end

describe 'reply_to_options' do
it 'returns cors headers' do
parser = MiniTest::Mock.new
parser.expect(:headers, {
'Content-Type' => 'text/xml',
'Host' => 'wonderland.lit',
'Origin' => 'remote.wonderland.lit',
'Access-Control-Request-Headers' => 'Content-Type, Origin'})
parser.expect(:http_method, 'OPTIONS')
parser.expect(:request_path, '/xmpp')
parser.expect(:request_url, '/xmpp')
parser.expect(:query_string, '')

headers = [
'Content-Type: text/xml',
'Host: wonderland.lit',
'Origin: remote.wonderland.lit',
'Access-Control-Request-Headers: Content-Type, Origin'
]
parser = parser('OPTIONS', '/xmpp', headers)
request = Vines::Stream::Http::Request.new(@stream, parser, '')

headers = [
Expand All @@ -152,7 +127,6 @@
@stream.expect(:stream_write, nil, ["#{headers}\r\n\r\n"])
request.reply_to_options
assert @stream.verify
assert parser.verify
end
end

Expand All @@ -168,6 +142,21 @@

private

# Create a parser that has completed one valid HTTP request.
#
# method - The HTTP method String (e.g. 'GET', 'POST').
# url - The request URL String (e.g. '/blogs/12?ok=true').
# headers - The optional Array of request headers.
#
# Returns an Http::Parser.
def parser(method, url, headers = [])
head = ["#{method} #{url} HTTP/1.1"].concat(headers).join("\r\n")
body = '<html></html>'
Http::Parser.new.tap do |parser|
parser << "%s\r\n\r\n%s" % [head, body]
end
end

def reply_with_cookie(cookie)
config = Vines::Config.new do
host 'wonderland.lit' do
Expand All @@ -178,15 +167,12 @@ def reply_with_cookie(cookie)
end
end

parser = MiniTest::Mock.new
parser.expect(:headers, {
'Content-Type' => 'text/xml',
'Host' => 'wonderland.lit',
'Origin' => 'remote.wonderland.lit'})
parser.expect(:http_method, 'POST')
parser.expect(:request_path, '/xmpp')
parser.expect(:request_url, '/xmpp')
parser.expect(:query_string, '')
headers = [
'Content-Type: text/xml',
'Host: wonderland.lit',
'Origin: remote.wonderland.lit'
]
parser = parser('POST', '/xmpp', headers)

request = Vines::Stream::Http::Request.new(@stream, parser, '')
message = '<message>hello</message>'
Expand All @@ -204,6 +190,5 @@ def reply_with_cookie(cookie)
@stream.expect(:config, config)
request.reply(message, 'application/xml')
assert @stream.verify
assert parser.verify
end
end

0 comments on commit 32dcce4

Please sign in to comment.