ry / caching-service

dedicated caching service

caching-service / test.rb
100644 128 lines (99 sloc) 3.113 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
require 'net/http'
require 'test/unit'
 
HOST = "0.0.0.0"
PORT = "8000"
 
def parse_http_response(response)
  response.sub!(%r{HTTP/(\d.\d) (\d\d\d) .*}, "")
  version = $1
  code = $2.to_i
  header_string, body = response.split("\r\n\r\n")
  headers = {}
  header_string.each_line do |line|
    field, value = line.split ':', 2
    next unless value
    headers[field] = value.chomp
  end
  headers["HTTP_VERSION"] = version
 
  [code, headers, body]
end
 
def build_request(method, path, headers, body="")
  header_string = "#{method} #{path} HTTP/1.0\r\n"
  headers["Content-Length"] ||= body.length
  headers.each_pair do |k,v|
    header_string << "#{k}: #{v}\r\n"
  end
  request = "#{header_string}\r\n#{body}"
end
 
def post(path, headers={}, body="")
  socket = TCPSocket.new(HOST, PORT)
  request = build_request("POST", path, headers, body)
 
  written = 0
  while(written < request.length)
    written += socket.write(request)
  end
 
  response = socket.read(1024*4)
  parse_http_response(response)
end
 
def get(path, headers={})
  socket = TCPSocket.new(HOST, PORT)
  request = build_request("GET", path, headers)
 
  written = 0
  while(written < request.length)
    written += socket.write(request)
  end
 
  response = socket.read(1024*4)
  parse_http_response(response)
end
 
 
 
class TestSimple < Test::Unit::TestCase
  def setup
    n = File.dirname(__FILE__) + "/start.sh"
    @t = Thread.new { %x{sh #{n}} }
    sleep 1
  end
 
  def teardown
    @t.kill
    %x{killall beam}
  end
 
  def test_get_empty_db
    gstatus, gheaders, gbody = get("/test")
    assert_equal 404, gstatus
  end
 
  def test_simple_post
    headers = { "Content-Type" => "text/plain", "X-Cache-Identifiers" => "123 321" }
    pstatus, pheaders, pbody = post("/test", headers, "Hello World")
    assert_equal 200, pstatus
 
    gstatus, gheaders, gbody = get("/test")
    assert_equal 200, gstatus
    assert_equal "Hello World", gbody
  end
 
  def test_double_insert
    headers = { "Content-Type" => "text/plain", "X-Cache-Identifiers" => "123 321" }
    pstatus, _, _ = post("/test", headers, "Hello World")
    assert_equal 200, pstatus
 
    headers = { "Content-Type" => "text/plain", "X-Cache-Identifiers" => "123 321" }
    pstatus, _, _ = post("/test", headers, "Hello World")
    assert_equal 200, pstatus
 
    gstatus, gheaders, gbody = get("/test")
    assert_equal 200, gstatus
    assert_equal "Hello World", gbody
  end
 
  def test_expire
    path = "/something/blah"
 
    headers = { "Content-Type" => "text/plain", "X-Cache-Identifiers" => "123 321" }
    pstatus, _, _ = post(path, headers, "Hello World")
    assert_equal 200, pstatus
 
    gstatus, _, gbody = get(path)
    assert_equal 200, gstatus
    assert_equal "Hello World", gbody
 
    pstatus, _, _ = post("/_expire?identifiers=123")
    assert_equal 200, gstatus
 
    gstatus, _, _ = get(path)
    assert_equal 404, gstatus
 
    # try to repost now
    pstatus, _, _ = post(path, headers, "Hello World")
    assert_equal 200, pstatus
 
    gstatus, _, gbody = get(path)
    assert_equal 200, gstatus
    assert_equal "Hello World", gbody
  end
 
end