forked from go-aah/aah
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc_test.go
103 lines (83 loc) · 2.28 KB
/
misc_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
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// aahframework.org/aah source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package aah
import (
"fmt"
"net/http"
"path/filepath"
"testing"
"aahframework.org/ahttp.v0"
"aahframework.org/ainsp.v0"
"aahframework.org/config.v0"
"aahframework.org/essentials.v0"
"aahframework.org/log.v0"
"aahframework.org/test.v0/assert"
)
func TestLogInitRelativeFilePath(t *testing.T) {
logPath := filepath.Join(testdataBaseDir(), "sample-test-app.log")
defer ess.DeleteFiles(logPath)
// Relative path file
a := newApp()
cfg, _ := config.ParseString(`log {
receiver = "file"
file = "sample-test-app.log"
}`)
a.cfg = cfg
err := a.initLog()
assert.Nil(t, err)
a.AddLoggerHook("myapphook", func(e log.Entry) {
t.Logf("%v", e)
})
}
func TestLogInitNoFilePath(t *testing.T) {
// No file input - auto location
logPath := filepath.Join(testdataBaseDir(), "wepapp1.log")
defer ess.DeleteFiles(logPath)
// Relative path file
a := newApp()
cfg, _ := config.ParseString(`log {
receiver = "file"
}`)
a.cfg = cfg
err := a.initLog()
assert.Nil(t, err)
a.AddLoggerHook("myapphook", func(e log.Entry) {
t.Logf("%v", e)
})
}
func TestAccessLogInitAbsPath(t *testing.T) {
logPath := filepath.Join(testdataBaseDir(), "sample-test-access.log")
defer ess.DeleteFiles(logPath)
a := newApp()
cfg, _ := config.ParseString(fmt.Sprintf(`server {
access_log {
file = "%s"
}
}`, filepath.ToSlash(logPath)))
a.cfg = cfg
err := a.initAccessLog()
assert.Nil(t, err)
}
type testErrorController1 struct {
}
func (tec *testErrorController1) HandleError(err *Error) bool {
log.Info("I have handled it at controller level")
return true
}
func TestErrorCallControllerHandler(t *testing.T) {
req, err := http.NewRequest(ahttp.MethodGet, "http://localhost:8080", nil)
assert.Nil(t, err)
ctx := &Context{
Req: ahttp.AcquireRequest(req),
controller: &ainsp.Target{FqName: "testErrorController1"},
target: &testErrorController1{},
}
l, err := log.New(config.NewEmpty())
assert.Nil(t, err)
ctx.logger = l
ctx.Reply().ContentType("application/json")
ctx.Reply().BadRequest().Error(newError(nil, http.StatusBadRequest))
em := new(errorManager)
em.Handle(ctx)
}