Skip to content

Commit

Permalink
Simple integration tests
Browse files Browse the repository at this point in the history
+ some fixes for rule handling and help page mapping.
  • Loading branch information
Ishan Oshadi Jayawardene committed Sep 4, 2012
1 parent 0c8d63e commit ccc91fc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
36 changes: 36 additions & 0 deletions inttest/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#! /bin/bash

set -e -u

readonly alecto=http://127.0.0.1:4567

# delete all
curl -X DELETE $alecto/rules/all | grep 'All rules deleted'

# add rule
curl -X POST --data-binary @- $alecto/rules <<EOF | grep 'Rule added/updated'
{
"number": 1,
"description": "rule 1",
"strings": [ "dead", "beef" ],
"response": "pass1"
}
EOF

# test that rule
curl -X POST --data-binary @- $alecto/test <<< 0xdeadbeef | grep pass1

# add another rule to work on the query string
curl -X POST --data-binary @- $alecto/rules <<EOF | grep 'Rule added/updated'
{
"number": 2,
"description": "Test query string matching",
"query_strings": [ "dead", "beef" ],
"response": "pass2"
}
EOF

curl -X POST "$alecto/test?dead&beef&" | grep pass2

# GET
curl "$alecto/test?dead&beef&" | grep pass2
29 changes: 23 additions & 6 deletions src/dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'request'
require 'samplerequests'
require 'mimeheaders'
require 'helpmap'

# This class takes care of dispatching to various code paths
# depending on the input, which must be an instance of
Expand Down Expand Up @@ -47,6 +48,22 @@ def Dispatcher.dispatch(request)
# and nothing is forbidden.
# http://en.wikipedia.org/wiki/Alamut_(1938_novel)
[ 200, nil, nil ]
elsif HelpMap.mappings.has_key?(path_info) and method == 'GET' then
# If this path_info should serve a help message from disk,
# do that...
begin
data = nil
open(HelpMap.mappings[path_info], 'r:UTF-8') do |help_f|
data = help_f.read
end
if data.nil? or data.length == 0 then
data = '(No data found)'
end
[ 200, MimeHeaders.text_html_utf8, data ]
rescue Exception => e
[ 500, MimeHeaders.text_plain_utf8, "Oops.. Something went wrong.\n" +
e.message + "\n" + e.backtrace.join("\n") + "\n" ]
end
elsif path_info.start_with?('/favicon.ico') then
[ 204, nil, nil ] # 204 == no content
elsif path_info.start_with?('/rules') then
Expand All @@ -63,12 +80,12 @@ def Dispatcher.dispatch(request)
Rules.instance.delete(string_remainder(path_info, '/rules/'))
else
[ 417, MimeHeaders.text_plain_utf8,
'Unsupported method; supported methods are GET, POST and DELETE' ]
"Unsupported method; supported methods are GET, POST and DELETE\n" ]
end
elsif path_info.start_with?('/sample_requests') then
if method != 'GET' then
# Method Not Allowed
[ 405, MimeHeaders.text_plain_utf8, 'Only GET is allowed on /sample_requests']
[ 405, MimeHeaders.text_plain_utf8, "Only GET is allowed on /sample_requests\n" ]
elsif path_info == '/sample_requests/allrefs' then
[ 200, MimeHeaders.application_json_utf8, SampleRequests.allrefs ]
elsif path_info.start_with?('/sample_requests/ref/') then
Expand All @@ -77,19 +94,19 @@ def Dispatcher.dispatch(request)
if not data.nil? then
[ 200, MimeHeaders.text_plain_utf8, data ]
else
[ 404, MimeHeaders.text_plain_utf8, "Not found" ]
[ 404, MimeHeaders.text_plain_utf8, "Not found\n" ]
end
else
# Not Acceptable
[ 406, MimeHeaders.text_plain_utf8, 'Bad path.' +
' Try GET on /sample_requests/allrefs or /sample_requests/ref/[id]' ]
[ 406, MimeHeaders.text_plain_utf8, "Bad path." +
" Try GET on /sample_requests/allrefs or /sample_requests/ref/[id]\n" ]
end
elsif method == 'POST' or method == 'GET' then
resp = Resolver.resolve(request)
[ resp.status, resp.headers, resp.body ]
else
[ 417, MimeHeaders.text_plain_utf8,
"No route matched the #{request.request_method}, to #{request.path_info}" ]
"No route matched the #{request.request_method}, to #{request.path_info}\n" ]
end

# return...
Expand Down
11 changes: 3 additions & 8 deletions src/rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,8 @@ def add_or_update_batch(str)
true
end

def add_or_update(str)
if str.nil? or str.length < 2 then
return [ 417, MimeHeaders.text_plain_utf8, "Invalid input." +
" A rule number (/rules/<integer>) and valid JSON body are required.\n" ]
end

json_hash = JSON.parse(str)
def add_or_update(rulespec_str)
json_hash = JSON.parse(rulespec_str)
rulespec = json_hash

# all that went well...
Expand Down Expand Up @@ -110,7 +105,7 @@ def delete(param)
@rules.clear
@rule_numbers = []
end
ret = [ 200, MimeHeaders.text_plain_utf8, " All rules deleted\n" ]
ret = [ 200, MimeHeaders.text_plain_utf8, "All rules deleted\n" ]
end

ret
Expand Down

0 comments on commit ccc91fc

Please sign in to comment.