-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
118 lines (105 loc) · 3.21 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
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
package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
srvConfig "github.com/CHESSComputing/golib/config"
docdb "github.com/CHESSComputing/golib/docdb"
"github.com/CHESSComputing/golib/lexicon"
server "github.com/CHESSComputing/golib/server"
utils "github.com/CHESSComputing/golib/utils"
"github.com/gin-gonic/gin"
)
// helper function to initialize MetaData for tests
func initMetaData() {
srvConfig.Init()
log.SetFlags(0)
log.SetFlags(log.LstdFlags | log.Lshortfile)
// current directory is a <pwd>/test
_, err := os.Getwd()
if err != nil {
log.Fatal("unable to get current working dir")
}
// load Lexicon patterns
lexPatterns, err := lexicon.LoadPatterns(srvConfig.Config.CHESSMetaData.LexiconFile)
if err != nil {
log.Fatal(err)
}
lexicon.LexiconPatterns = lexPatterns
// init docdb
metaDB, err = docdb.InitializeDocDB(srvConfig.Config.CHESSMetaData.MongoDB.DBUri)
if err != nil {
log.Fatal(err)
}
// init Verbose
Verbose = srvConfig.Config.CHESSMetaData.WebServer.Verbose
// initialize schema manager
_smgr = SchemaManager{}
for _, fname := range srvConfig.Config.CHESSMetaData.SchemaFiles {
_, err := _smgr.Load(fname)
if err != nil {
log.Fatalf("unable to load %s error %v", fname, err)
}
_beamlines = append(_beamlines, utils.FileName(fname))
}
log.Println("Schema", _smgr.String())
}
var router *gin.Engine
func initServer() {
if router == nil {
// we need to initialize meta data only once since it calls srvConfig.Init()
initMetaData()
}
if router == nil {
routes := []server.Route{
server.Route{Method: "GET", Path: "/:did", Handler: RecordHandler, Authorized: false},
server.Route{Method: "PUT", Path: "/", Handler: DataHandler, Authorized: false},
server.Route{Method: "POST", Path: "/", Handler: DataHandler, Authorized: false},
server.Route{Method: "DELETE", Path: "/:did", Handler: DeleteHandler, Authorized: false},
}
router = server.Router(routes, nil, "static", srvConfig.Config.CHESSMetaData.WebServer)
}
}
// helper function to print any struct in formatted way
func logStruct(t *testing.T, msg string, data any) {
body, err := json.MarshalIndent(data, "", " ")
if err != nil {
t.Logf("%s\n%+v\n", msg, data)
return
}
t.Logf("%s\n%s\n", msg, string(body))
}
// helper function to create http test response recorder
// for given HTTP Method, endPoint, reader and web handler
func responseRecorder(t *testing.T, v TestCase) *httptest.ResponseRecorder {
// read data from the inpit
data, err := json.Marshal(v.Input)
if err != nil {
t.Fatal(err.Error())
}
reader := bytes.NewReader(data)
if v.Verbose > 0 {
t.Logf("submit method=%s endpoint=%s url=%s input=%v output=%v code=%v fail=%v data=%s", v.Method, v.Endpoint, v.Url, v.Input, v.Output, v.Code, v.Fail, string(data))
}
// setup HTTP request
req, err := http.NewRequest(v.Method, v.Url, reader)
if err != nil {
t.Fatal(err)
}
req.Header.Add("Accept", "application/json")
if v.Method == "POST" {
req.Header.Set("Content-Type", "application/json")
}
// create response recorder
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
if v.Verbose > 1 {
logStruct(t, "HTTP request", req)
logStruct(t, "HTTP response", rr)
}
return rr
}