This repository was archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup_test.go
92 lines (81 loc) · 2.53 KB
/
setup_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
// Goro
//
// Created by Yakka
// http://theyakka.com
//
// Copyright (c) 2019 Yakka LLC.
// All rights reserved.
// See the LICENSE file for licensing details and requirements.
package goro_test
import (
"github.com/theyakka/goro"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
)
var wasHit = false
var router *goro.Router
var sum = 0
func TestMain(m *testing.M) {
router = goro.NewRouter()
chainHandlers := []goro.ChainHandler{chainHandler1, chainHandler2, chainHandler3}
haltHandlers := []goro.ChainHandler{chainHandler1, chainHandler2, testHaltHandler, chainHandler3}
chainErrorHandlers := []goro.ChainHandler{chainHandler1, chainHandler2, testErrorChainHandler, chainHandler3}
router.SetErrorHandler(777, goro.ContextHandlerFunc(chainCustomErrorHandler))
router.SetStringVariable("color", "blue")
// router tests
router.GET("/").HandleFunc(testHandler)
router.GET("/users/:id").HandleFunc(testParamsHandler)
router.GET("/users/:id/action/:action").HandleFunc(testParamsHandler)
router.GET("/colors/$color").HandleFunc(testHandler)
// route groups
apiGroup := router.Group("/api")
v1Group := apiGroup.Group("/v1")
v1Group.GET("/").HandleFunc(testHandler)
v1Group.POST("/").HandleFunc(testHandler)
v1Group.GET("/stats").HandleFunc(testHandler)
apiDocsGroup := v1Group.Group("/docs")
apiDocsGroup.GET("/stats").HandleFunc(testHandler)
// chain tests
router.GET("/chain/simple").HandleFunc(router.HC(chainHandlers...).Call())
router.GET("/chain/then").HandleFunc(router.HC(chainHandlers...).Then(testThenHandler))
router.GET("/chain/halt").HandleFunc(router.HC(haltHandlers...).Call())
router.GET("/chain/error").HandleFunc(router.HC(chainErrorHandlers...).Then(testThenHandler))
if printDebug {
router.PrintRoutes()
}
os.Exit(m.Run())
}
func resetState() {
wasHit = false
sum = 0
}
func expectHitResult(t *testing.T, handler http.Handler, method string, path string) {
Debug("Requesting", path, "...")
execMockRequest(handler, method, path)
if !wasHit {
t.Error("Expected", path, "to be HIT but it wasn't")
}
resetState()
}
func expectNotHitResult(t *testing.T, handler http.Handler, method string, path string) {
Debug("Requesting", path, "...")
execMockRequest(handler, method, path)
if wasHit {
t.Error("Expected", path, "to be NOT HIT but it was")
}
resetState()
}
func execMockRequest(handler http.Handler, method string, url string) {
req, _ := http.NewRequest(method, url, nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
}
func Debug(v ...interface{}) {
if !printDebug {
return
}
log.Println(v...)
}