-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
59 lines (49 loc) · 1.15 KB
/
main_test.go
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
package main
/*
* License: GPL-3.0-or-later
* Authors:
* Mateus Melchiades <matbme@duck.com>
* Copyright: 2023
*/
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"testing"
)
var testDBPath string = "test.db"
func TestMain(m *testing.M) {
_, err := exec.LookPath("sqlite3")
if err != nil {
panic("sqlite3 binary not found.")
}
err = exec.Command(
"sh",
"-c",
fmt.Sprintf("sqlite3 %s \"create table auth(ID INTEGER, name, pass TEXT, PRIMARY KEY(ID)); insert into auth values(1, 'admin', 'admin');\"", testDBPath),
).Run()
if err != nil {
os.Remove(testDBPath)
panic("error setting up test db: " + err.Error())
}
status := m.Run()
os.Remove(testDBPath)
os.Exit(status)
}
func TestServer(t *testing.T) {
router, err := setupRouter(testDBPath)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/status", nil)
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("/status request returned non-OK status '%d'", w.Code)
}
if w.Body.String() != "{\"status\":\"ok\"}" {
t.Fatalf("/status request returned unexpected body '%s'", w.Body.String())
}
}