2017-04-28 10:27:36 +02:00
|
|
|
// A little lib to easy create everything for running virtual api
|
2017-03-25 16:09:17 +01:00
|
|
|
package test
|
|
|
|
|
2017-04-26 15:40:01 +02:00
|
|
|
// Request an easy manager to test REST-API
|
2017-03-25 16:09:17 +01:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2017-03-30 19:05:33 +02:00
|
|
|
"github.com/genofire/hs_master-kss-monolith/lib/database"
|
2017-03-25 16:09:17 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
goji "goji.io"
|
|
|
|
)
|
|
|
|
|
2017-04-07 11:32:49 +02:00
|
|
|
var srv *http.Server
|
|
|
|
|
2017-04-28 10:27:36 +02:00
|
|
|
//initialisieren an API test api
|
2017-03-25 16:09:17 +01:00
|
|
|
func Init(t *testing.T) (assertion *assert.Assertions, router *goji.Mux) {
|
|
|
|
assertion = assert.New(t)
|
2017-03-30 19:05:33 +02:00
|
|
|
database.Open(database.Config{
|
|
|
|
Type: "sqlite3",
|
2017-03-31 11:06:40 +02:00
|
|
|
Logging: true,
|
2017-03-30 19:05:33 +02:00
|
|
|
Connection: ":memory:",
|
|
|
|
})
|
2017-03-25 16:09:17 +01:00
|
|
|
router = goji.NewMux()
|
2017-04-07 11:32:49 +02:00
|
|
|
|
|
|
|
apirouter := http.FileServer(http.Dir("../webroot"))
|
|
|
|
srv = &http.Server{
|
|
|
|
Addr: ":8080",
|
|
|
|
Handler: apirouter,
|
|
|
|
}
|
|
|
|
go srv.ListenAndServe()
|
2017-03-25 16:09:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-28 10:27:36 +02:00
|
|
|
// close just the static webserver (with test files of other microservice)
|
2017-04-07 11:32:49 +02:00
|
|
|
func CloseServer() {
|
|
|
|
srv.Close()
|
|
|
|
}
|
|
|
|
|
2017-04-28 10:27:36 +02:00
|
|
|
// close everything
|
2017-03-30 19:05:33 +02:00
|
|
|
func Close() {
|
|
|
|
database.Close()
|
2017-04-07 11:32:49 +02:00
|
|
|
srv.Close()
|
2017-03-30 19:05:33 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 10:27:36 +02:00
|
|
|
// handle a test client session with cookies
|
2017-03-25 16:09:17 +01:00
|
|
|
type Request struct {
|
|
|
|
req *http.Request
|
|
|
|
cookies []*http.Cookie
|
|
|
|
router *goji.Mux
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSession to get a new easy manager
|
|
|
|
func NewSession(router *goji.Mux) *Request {
|
|
|
|
return &Request{router: router}
|
|
|
|
}
|
|
|
|
|
2017-04-28 10:27:36 +02:00
|
|
|
// send request to router and recieve the api answer
|
2017-03-25 16:09:17 +01:00
|
|
|
func (r *Request) JSONRequest(method string, url string, body interface{}) (jsonResult interface{}, res *http.Response) {
|
|
|
|
jsonObj, _ := json.Marshal(body)
|
|
|
|
req, _ := http.NewRequest(method, url, bytes.NewReader(jsonObj))
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
for _, c := range r.cookies {
|
|
|
|
req.AddCookie(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r.router.ServeHTTP(w, req)
|
|
|
|
res = w.Result()
|
|
|
|
cookies := res.Cookies()
|
|
|
|
if len(cookies) > 0 {
|
|
|
|
r.cookies = cookies
|
|
|
|
}
|
|
|
|
json.NewDecoder(w.Body).Decode(&jsonResult)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-29 18:26:36 +02:00
|
|
|
// login the current session
|
|
|
|
func (r *Request) Login() {
|
|
|
|
r.cookies = nil
|
|
|
|
r.cookies = append(r.cookies, &http.Cookie{Name: "session", Value: "testsessionkey"})
|
|
|
|
}
|
|
|
|
|
|
|
|
// logout the current session
|
|
|
|
func (r *Request) Logout() {
|
|
|
|
r.cookies = nil
|
|
|
|
r.cookies = append(r.cookies, &http.Cookie{Name: "session", Value: "trashkey"})
|
|
|
|
}
|
|
|
|
|
2017-04-28 10:27:36 +02:00
|
|
|
// clean the current session
|
2017-03-25 16:09:17 +01:00
|
|
|
func (r *Request) Clean() {
|
|
|
|
r.cookies = nil
|
|
|
|
}
|