-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers_test.go
70 lines (55 loc) · 1.58 KB
/
handlers_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
package main
import (
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
"gopkg.in/ory-am/dockertest.v2"
"labix.org/v2/mgo"
"time"
"github.com/gin-gonic/gin"
)
func doRequest(engine *gin.Engine, method, path string) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
req, _ := http.NewRequest(method, path, nil)
engine.ServeHTTP(w, req)
return w
}
func TestRoutes(t *testing.T) {
// Set up database
var db *mgo.Session
c, err := dockertest.ConnectToMongoDB(15, time.Millisecond*500, func(url string) bool {
// Check if the docker image responds
var err error
db, err = mgo.Dial(url)
if err != nil {
return false
}
// Check if the docker image responds to ping
return db.Ping() == nil
})
if err != nil {
// log.Fatalf("Could not connect to database: %s", err)
}
// Close DB connection and kill container when we're done. It'll now be ready.
defer db.Close()
defer c.KillRemove()
// Done setting up database
// Set up Gin
engine := gin.Default()
setupServer(db)
AddRoutes("1", engine)
// Done setting up Gin
var w *httptest.ResponseRecorder
// Assert HTTP 200 is returned when loading all templates view
w = doRequest(engine, "GET", "/api/v1/template/")
assert.Equal(t, 200, w.Code)
// Assert HTTP 404 if loading non-existing template
w = doRequest(engine, "GET", "/api/v1/template/nonexisting-template")
assert.Equal(t, 404, w.Code)
// Load default templates
loadDefaultTemplates("templates/")
// Load existing article
w = doRequest(engine, "GET", "/api/v1/template/artikkel")
assert.Equal(t, 200, w.Code)
}