sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/test/main.go

79 lines
2.0 KiB
Go
Raw Normal View History

2016-10-07 15:13:31 +02:00
package test
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
2016-10-12 12:44:56 +02:00
"os"
2016-10-07 15:13:31 +02:00
"github.com/astaxie/session"
_ "github.com/astaxie/session/providers/memory"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/stretchr/testify/assert"
2016-10-11 20:16:24 +02:00
"goji.io"
2016-10-07 15:13:31 +02:00
2016-10-11 20:16:24 +02:00
libconfig "dev.sum7.eu/sum7/warehost/config"
libapi "dev.sum7.eu/sum7/warehost/lib/api"
log "dev.sum7.eu/sum7/warehost/lib/log"
2016-10-07 15:13:31 +02:00
)
//Init to initialisieren a API
2016-10-12 12:44:56 +02:00
func Init(t *testing.T) (assertion *assert.Assertions, dbconnection *gorm.DB, router *goji.Mux) {
2016-10-07 15:13:31 +02:00
assertion = assert.New(t)
2016-10-12 12:44:56 +02:00
libconfig.ReadConfigFile(os.Getenv("GOPATH") +"/src/dev.sum7.eu/sum7/warehost/cmd/warehost/config.yml.example")
log.NewLogger(libconfig.Data.Log.Path)
2016-10-07 15:13:31 +02:00
// Session mgmt
sessions, err := session.NewManager("memory", "session", 3600)
go sessions.GC()
assertion.NoError(err)
// Database
2016-10-12 12:44:56 +02:00
dbconnection, err = gorm.Open("postgres", libconfig.Data.Database)
2016-10-07 15:13:31 +02:00
assertion.NoError(err)
dbconnection.Callback().Create().Remove("gorm:update_time_stamp")
dbconnection.Callback().Update().Remove("gorm:update_time_stamp")
dbconnection.SingularTable(true)
2016-10-12 12:44:56 +02:00
dbconnection.LogMode(libconfig.Data.DatabaseDebug)
2016-10-07 15:13:31 +02:00
2016-10-11 20:16:24 +02:00
router = goji.NewMux()
2016-10-07 15:13:31 +02:00
return
}
// Request a easy manager to test REST-API
type Request struct {
req *http.Request
cookies []*http.Cookie
2016-10-11 20:16:24 +02:00
router *goji.Mux
2016-10-07 15:13:31 +02:00
}
// NewSession to get a new easy manager
2016-10-11 20:16:24 +02:00
func NewSession(router *goji.Mux) *Request {
2016-10-07 15:13:31 +02:00
return &Request{router: router}
}
// JSONRequest send request to router
func (r *Request) JSONRequest(method string, url string, body interface{}) (jsonResult libapi.JSONResult, res *http.Response) {
jsonObj, _ := json.Marshal(body)
req, _ := http.NewRequest(method, url, bytes.NewReader(jsonObj))
for _, c := range r.cookies {
req.AddCookie(c)
}
w := httptest.NewRecorder()
r.router.ServeHTTP(w, req)
res = w.Result()
r.cookies = res.Cookies()
json.NewDecoder(w.Body).Decode(&jsonResult)
return
}
// CleanSession to clean the current session
func (r *Request) CleanSession() {
r.cookies = nil
}