1#!/usr/bin/env lua
2
3-- Licensed to the Apache Software Foundation (ASF) under one or more
4-- contributor license agreements.  See the NOTICE file distributed with
5-- this work for additional information regarding copyright ownership.
6-- The ASF licenses this file to You under the Apache License, Version 2.0
7-- (the "License"); you may not use this file except in compliance with
8-- the License.  You may obtain a copy of the License at
9--
10-- http://www.apache.org/licenses/LICENSE-2.0
11--
12-- Unless required by applicable law or agreed to in writing, software
13-- distributed under the License is distributed on an "AS IS" BASIS,
14-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-- See the License for the specific language governing permissions and
16-- limitations under the License.
17
18local mu = require "moonunit" 
19local http = require "helpers"
20
21http.base_url = "http://localhost:8008"
22
23local test = mu.TestCase:new{}
24
25function test:document_root()
26  local b, c = http.get "/document_root.lua"
27  assert(200 == c, "expected status code 200, got " .. c)
28  assert(b:find("test"), "test not found in document root")
29end
30
31function test:basic_get()
32  local b, c = http.get "/basic"
33  assert(200 == c, "expected status code 200, got " .. c)
34  assert(b:find("hello Lua world"), "'hello Lua world' not found in response")
35end
36
37function test:quietly()
38  local b, c = http.get "/test_quietly"
39  assert(200 == c, "unexpected response code " .. c)
40  assert(b == 'hello!', "unexpected response body [" .. b .. "]")
41end
42
43function test.basic_post()
44  local b, c = http.post "/basic" "hello=7&hello=1"
45  assert(200 == c, "expected status code 200, got " .. c)
46  assert(b:find("complex:%s+hello: 7, 1\n"), "didn't find complex post parsing")
47  assert(b:find("simple:%s+hello: 7\n"), "didn't find simple post parsing")
48end
49
50function test.basic_post_alt()
51  local b, c = http.post("/test_foo", "hello=7&hello=1")
52  assert(201 == c, "expected status code 200, got " .. c)
53  assert(b:find("Handler FOO!"), "unexpected output!")
54end
55
56function test.post_with_table()
57  local b, c = http.post "/basic" { hello = "7" }
58  assert(200 == c, "expected status code 200, got " .. c)
59  assert(b:find("hello: 7"), "didn't get expected post data [" .. b .."]")
60  
61  b, c = http.post "/basic" { hello = "7", goodbye = "8" }
62  
63  assert(200 == c, "expected status code 200, got " .. c)
64  assert(b:find("hello: 7"), "didn't get expected post data [" .. b .."]")
65  assert(b:find("goodbye: 8"), "didn't get expected post data [" .. b .."]")
66end
67
68function test:simple_filter()
69  local b, c = http.get "/filter/simple"
70  assert(200 == c, "expected status code 200, got " .. c)
71end
72
73function test:request_attributes()
74  local r, c = http.get "/test_attributes?yes=no"
75  assert(201 == c, "expected status code 201, got " .. c)
76  
77  assert(r:find("status: 200\nstatus: 201"), "changing status code failed")
78  assert(r:find("method: GET"), "method wasn't reported correctly")
79  assert(r:find("protocol: HTTP/1.1"), "protocol reported incorrectly")
80  assert(r:find("assbackwards: false"), "assbackwards reported incorrectly")
81  assert(r:find("args: yes=no"), "args not reported correctly")
82end
83
84function test:map_regex()
85  local r, c = http.get "/test_regex"
86  assert(200 == c, "expected status code 200, got " .. c)
87  assert(r:find("matched in handle_regex"), "didn't find 'matched in handle_regex'")  
88end
89
90function test:map_regex2()
91  local r, c = http.get "/test_regex?a=8"
92  assert(200 == c, "expected status code 200, got " .. c)
93  assert(r:find("matched in handle_regex"), "didn't find 'matched in handle_regex'")  
94end
95
96function test:translate_name_hook()
97  local r, c = http.get "/translate-name"
98  assert(200 == c, "expected 200 got " .. c)
99  assert(r:find("please find me"), "didn't get expected static file :-(, instead got " .. r)
100end
101
102function test:translate_name_hook2()
103  local r, c = http.get "/translate-name2"
104  assert(200 == c, "expected 200 got " .. c)
105  assert(r:find("please find me"), "didn't get expected static file :-(, instead got " .. r)
106end
107
108function test:server_version()
109  local r, c = http.get "/test_serverversion"
110  assert(200 == c)
111  assert(r:find("Apache/2"), "version isn't Apache/2, but is " .. r)
112end
113
114function test:fixups_hook()
115  local r, c = http.get "/test_fixupstest"
116  assert(201 == c, "incorrect status code returned, expected 201 got " .. c)
117  assert(r:find("status is 201"), "handler sees incorrect status")
118end
119
120function test:simple()
121    local r, c = http.get "/simple.lua"
122    assert(200 == c, "incorrect status code returned, expected 200 got " .. c)
123    assert(r:find("Hi"), "Didn't find 'Hi'")
124end
125
126test:run()
127