-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathserver_test.go
112 lines (98 loc) · 2.05 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// +build !goci
package rest
import (
"testing"
)
const ServerAddr = "0.0.0.0:8080"
var closeChan = make(chan struct{})
type MyIntType int
type Struct struct {
Bool bool
Int int
Uint uint
Ignore int `json:"-"`
Float32 float32
Float64 float64
String string
SubStruct SubStruct
}
type SubStruct struct {
A MyIntType
B MyIntType
}
func NewStruct() *Struct {
return &Struct{
Bool: true,
Int: 1,
Uint: 2,
Ignore: 3,
Float32: 4,
Float64: 5,
String: "7",
SubStruct: SubStruct{
A: 8,
B: 9,
},
}
}
var RefStruct = Struct{
Bool: true,
Int: 1,
Uint: 2,
Ignore: 0, // default value instead of 3 because of json:"-"
Float32: 4,
Float64: 5,
String: "7",
SubStruct: SubStruct{
A: 8,
B: 9,
},
}
func TestStartServer(t *testing.T) {
go RunServer(ServerAddr, closeChan)
}
func TestHandleGET_struct(t *testing.T) {
path := "/struct.json"
HandleGET(path, func() *Struct {
return NewStruct()
})
var result Struct
err := GetJSONStrict("http://"+ServerAddr+path, &result)
if err != nil {
t.Error(err)
}
if result != RefStruct {
t.Errorf("GET %s: invalid result", ServerAddr+path)
}
}
func TestHandleGET_struct_error(t *testing.T) {
noerrorPath := "/struct_noerror.json"
HandleGET(noerrorPath, func() (*Struct, error) {
return NewStruct(), nil
})
var result Struct
err := GetJSONStrict("http://"+ServerAddr+noerrorPath, &result)
if err != nil {
t.Error(err)
}
if result != RefStruct {
t.Errorf("GET %s: invalid result", ServerAddr+noerrorPath)
}
// errorPath := "/struct_error.json"
// errorMessage := "Test Error"
// HandleGET(path, func() (*Struct, error) {
// return nil, errors.New(errorMessage)
// })
// response, err := http.Get("http://"+ServerAddr+errorPath)
// if err != nil {
// t.Error(err)
// }
// if response.Header.
// if result != RefStruct {
// t.Errorf("GET %s: invalid result", ServerAddr+errorPath)
// }
}
// TODO: needs much more testing, but see example for some working code
func TestStopServer(t *testing.T) {
closeChan <- struct{}{}
}