This repository was archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathapp_test.go
122 lines (100 loc) · 2.18 KB
/
app_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
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
package app
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/gernest/utron/config"
"github.com/gernest/utron/controller"
)
const notFoundMsg = "nothing"
func TestGetAbsPath(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Error(err)
}
// non existing
_, err = getAbsolutePath("nope")
if err == nil {
t.Error("expcted error got nil")
}
if !os.IsNotExist(err) {
t.Errorf("expcetd not exist got %v", err)
}
absPath := filepath.Join(wd, "fixtures")
// Relqtive
dir, err := getAbsolutePath("fixtures")
if err != nil {
t.Error(err)
}
if dir != absPath {
t.Errorf("expceted %s got %s", absPath, dir)
}
// Absolute
dir, err = getAbsolutePath(absPath)
if err != nil {
t.Error(err)
}
if dir != absPath {
t.Errorf("expceted %s got %s", absPath, dir)
}
}
type SimpleMVC struct {
controller.BaseController
}
func (s *SimpleMVC) Hello() {
s.Ctx.Data["Name"] = "gernest"
s.Ctx.Template = "index"
s.String(http.StatusOK)
}
func TestMVC(t *testing.T) {
app, err := NewMVC("fixtures/mvc")
if err != nil {
t.Skip(err)
}
app.AddController(controller.GetCtrlFunc(&SimpleMVC{}))
req, _ := http.NewRequest("GET", "/simplemvc/hello", nil)
w := httptest.NewRecorder()
app.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("expcted %d got %d", http.StatusOK, w.Code)
}
if !strings.Contains(w.Body.String(), "gernest") {
t.Errorf("expected %s to contain gernest", w.Body.String())
}
}
func TestApp(t *testing.T) {
app := NewApp()
// Set not found handler
err := app.SetNotFoundHandler(http.HandlerFunc(sampleDefault))
if err != nil {
t.Error(err)
}
// no router
app.Router = nil
err = app.SetNotFoundHandler(http.HandlerFunc(sampleDefault))
if err == nil {
t.Error("expected an error")
}
}
func sampleDefault(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(notFoundMsg))
}
func TestStaticServer(t *testing.T) {
c := &config.Config{}
_, ok, _ := StaticServer(c)
if ok {
t.Error("expected false")
}
c.StaticDir = "fixtures"
s, ok, _ := StaticServer(c)
if !ok {
t.Error("expected true")
}
expect := "/static/"
if s != expect {
t.Errorf("expected %s got %s", expect, s)
}
}