Skip to content

Instantly share code, notes, and snippets.

@vrinek
Created May 25, 2012 14:04
Show Gist options
  • Save vrinek/2788312 to your computer and use it in GitHub Desktop.
Save vrinek/2788312 to your computer and use it in GitHub Desktop.
# -*- encoding : utf-8 -*-
require "spec_helper"
require "set"
# Reads requests from test/fixtures/logs/requests.log and builds examples that
# test if the requests are routable.
#
# The syntax it understands is (same as `haproxy.log`):
# {www.skroutz.gr} "POST /shops/add_review/984?some=other"
#
# Remember that all PUT and DELETE methods are logged as POST in the logs prior
# to hitting the rails app. They will fail these tests.
#
# @note To disable these tests just leave requests.log empty. Remember to never
# commit it unless it is empty.
#
# @note To populate requests.log, use something like:
# `grep /shops/ ~/path/to.logs.txt | shuf -n 1000 > test/fixtures/logs/requests.log`
describe "routing" do
# We expect requests.log to not be HUGE, else we fill fill up the memory
# pretty quick. This script was tested with ~10000 lines in the requests.log
# and performed fine.
file = File.open('test/fixtures/logs/requests.log')
lines = file.read.split("\n")
method_paths = Set.new
lines.each do |line|
domain, method, path, _ =
*line.scan(/^\{([^\}]+)\} "(\w+) ([^\?]+)(\?[^"]*)?"/).flatten
# Skip known public folders and files
next if path =~ %r{^/(images|stylesheets|sitemaps)/}i
next if path =~ %r{^/(crossdomain.xml|favicon.ico|robots\.txt)$}
# Skip stupid PHP exploit attempts
next if path =~ %r{\.php$}
# Fix path prefices
path = "/apiv2" + path if domain == 'apiv2.skroutz.gr' && path !~ /^\/apiv2/
unless method_paths.include?(method+path)
it "recognizes #{line}" do
{method.downcase => path}.should be_routable
end
method_paths << method+path
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment