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

81 lines
2.0 KiB
Go
Raw Permalink Normal View History

2016-10-07 15:13:31 +02:00
package test
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
2016-10-12 12:44:56 +02:00
"os"
2016-10-12 22:28:10 +02:00
"testing"
2016-10-07 15:13:31 +02:00
"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-12 22:28:10 +02:00
libsession "dev.sum7.eu/sum7/warehost/lib/session"
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 22:28:10 +02:00
libconfig.ReadConfigFile(os.Getenv("GOPATH") + "/src/dev.sum7.eu/sum7/warehost/cmd/warehost/config.yml.example")
2016-10-12 12:44:56 +02:00
log.NewLogger(libconfig.Data.Log.Path)
2016-10-07 15:13:31 +02:00
// Session mgmt
2016-10-12 22:28:10 +02:00
libsession.Init()
go libsession.Stop()
2016-10-07 15:13:31 +02:00
// Database
2016-10-12 22:28:10 +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))
2016-12-19 12:24:18 +01:00
req.Header.Set("Content-Type", "application/json")
2016-10-07 15:13:31 +02:00
for _, c := range r.cookies {
req.AddCookie(c)
}
w := httptest.NewRecorder()
r.router.ServeHTTP(w, req)
res = w.Result()
2016-10-12 22:28:10 +02:00
cookies := res.Cookies()
if len(cookies) > 0 {
r.cookies = cookies
}
2016-10-07 15:13:31 +02:00
json.NewDecoder(w.Body).Decode(&jsonResult)
return
}
2016-12-19 12:24:18 +01:00
// Clean to clean the current session
2016-10-12 22:28:10 +02:00
func (r *Request) Clean() {
2016-10-07 15:13:31 +02:00
r.cookies = nil
}