-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver_test.go
96 lines (72 loc) · 2.6 KB
/
server_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
package gosrv
import (
"testing"
"time"
)
func TestNew(t *testing.T) {
s := New()
testAssertEqual(t, "dev", s.Env)
testAssertEqual(t, "", s.Addr)
}
func TestNewForceProd(t *testing.T) {
oldForceProd := ForceProdEnv
defer func(){ ForceProdEnv = oldForceProd }()
ForceProdEnv = true
s := New()
testAssertEqual(t, "prod", s.Env)
testAssertEqual(t, "", s.Addr)
}
func TestNewFromConfig(t *testing.T) {
s, err := NewFromConfig("testdata/server.cfg")
if err != nil { t.Fatal( err ) }
testAssertEqual(t, "dev", s.Env)
testAssertEqual(t, DefaultPidFile, s.PidFile)
testAssertEqual(t, 1 * time.Second, s.ReadTimeout)
testAssertEqual(t, 500 * time.Millisecond, s.WriteTimeout)
testAssertEqual(t, 1024, s.MaxHeaderBytes)
testAssertEqual(t, "", s.CertFile)
testAssertEqual(t, "", s.KeyFile)
val, err := s.Config.String("customConfig")
if err != nil { t.Fatal( err ) }
testAssertEqual(t, "customValue", val)
}
func TestNewFromConfigEnv(t *testing.T) {
s, err := NewFromConfig("testdata/server.cfg", "prod")
if err != nil { t.Fatal( err ) }
testAssertEqual(t, "prod", s.Env)
testAssertEqual(t, "path/to/server.pid", s.PidFile)
testAssertEqual(t, 1 * time.Second, s.ReadTimeout)
testAssertEqual(t, 1 * time.Second, s.WriteTimeout)
testAssertEqual(t, 1024, s.MaxHeaderBytes)
testAssertEqual(t, "path/to/server.cert", s.CertFile)
testAssertEqual(t, "path/to/server.key", s.KeyFile)
l, _ := s.Logger.(*httpLogger)
testAssertEqual(t, "(02/01/2006 15:04:05)", l.timeFormat)
logFmt := "$RemoteAddr - $RemoteUser $Time \"$Request\" $Status $BodyBytes"
testAssertEqual(t, logFmt, l.logFormat)
val, err := s.Config.String("customConfig")
if err != nil { t.Fatal( err ) }
testAssertEqual(t, "customValue", val)
}
func TestNewFromFlag(t *testing.T) {
s, err := NewFromFlag(
"test","-a",":7000","-pid","path/to/server.pid","-e","stage")
if err != nil { t.Fatal( err ) }
testAssertEqual(t, "stage", s.Env)
testAssertEqual(t, "stage", s.Config.Env)
testAssertEqual(t, ":7000", s.Addr)
testAssertEqual(t, "path/to/server.pid", s.PidFile)
}
func TestNewFromFlagAndConfig(t *testing.T) {
s, err := NewFromFlag(
"test","-a",":7000","-c","testdata/server.cfg","-e","prod")
if err != nil { t.Fatal( err ) }
testAssertEqual(t, "prod", s.Env)
testAssertEqual(t, "prod", s.Config.Env)
testAssertEqual(t, ":7000", s.Addr)
testAssertEqual(t, 1024, s.MaxHeaderBytes)
testAssertEqual(t, 1 * time.Second, s.WriteTimeout)
val, err := s.Config.String("customConfig")
if err != nil { t.Fatal( err ) }
testAssertEqual(t, "customValue", val)
}