genofire/hs_monolith
genofire
/
hs_monolith
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.
hs_monolith/test/testrest.go

130 lines
3.0 KiB
Go
Raw Normal View History

2017-05-15 10:22:24 +02:00
// Package that contains a lib to easily create everything for running a virtual api and test the microservice
2017-03-25 16:09:17 +01:00
package test
2017-05-03 08:07:45 +02:00
// Import an easy manager to test the REST-API
2017-03-25 16:09:17 +01:00
import (
"bytes"
"encoding/json"
"errors"
2017-05-19 11:02:19 +02:00
"fmt"
2017-03-25 16:09:17 +01:00
"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-05-03 08:07:45 +02:00
// Pointer to the server
2017-05-18 00:34:26 +02:00
var mock *MockTransport
2017-05-18 00:34:26 +02:00
type MockTransport struct {
Handler http.Handler
running bool
}
2017-05-18 00:34:26 +02:00
func (t *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if !t.running {
return nil, errors.New("mock a error")
}
w := httptest.NewRecorder()
2017-05-18 00:34:26 +02:00
t.Handler.ServeHTTP(w, req)
return w.Result(), nil
}
2017-05-18 00:34:26 +02:00
func (t *MockTransport) Start() {
t.running = true
}
func (t *MockTransport) Close() {
t.running = false
}
2017-05-19 11:02:19 +02:00
var lastTestDB int
2017-05-03 08:07:45 +02:00
// Function to initialize a test api (with test files of depending microservice)
2017-03-25 16:09:17 +01:00
func Init(t *testing.T) (assertion *assert.Assertions, router *goji.Mux) {
assertion = assert.New(t)
2017-05-19 11:02:19 +02:00
lastTestDB++
//database.Close()
2017-03-30 19:05:33 +02:00
database.Open(database.Config{
Type: "sqlite3",
Logging: true,
2017-05-19 11:02:19 +02:00
Connection: fmt.Sprintf("file:database%s?mode=memory", lastTestDB),
2017-03-30 19:05:33 +02:00
})
2017-03-25 16:09:17 +01:00
router = goji.NewMux()
mockBackend := http.FileServer(http.Dir("../webroot"))
2017-05-18 00:34:26 +02:00
mock = &MockTransport{Handler: mockBackend, running: true}
http.DefaultClient.Transport = mock
2017-03-25 16:09:17 +01:00
return
}
2017-05-03 08:07:45 +02:00
// Function to close the static webserver
func CloseServer() {
2017-05-18 00:34:26 +02:00
mock.Close()
}
2017-05-03 08:07:45 +02:00
// Function to close and stop the whole microservice
2017-03-30 19:05:33 +02:00
func Close() {
database.Close()
2017-05-18 00:34:26 +02:00
mock.Close()
2017-03-30 19:05:33 +02:00
}
2017-05-03 08:07:45 +02:00
// Handle a test session with cookies
2017-03-25 16:09:17 +01:00
type Request struct {
req *http.Request
cookies []*http.Cookie
router *goji.Mux
Header map[string]string
2017-03-25 16:09:17 +01:00
}
2017-05-15 10:22:24 +02:00
// Function to create a NewSession with the easy manager
2017-03-25 16:09:17 +01:00
func NewSession(router *goji.Mux) *Request {
return &Request{router: router, Header: make(map[string]string)}
2017-03-25 16:09:17 +01:00
}
2017-05-05 11:22:43 +02:00
// Function to send a request to the router and receive the api's 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")
if len(r.Header) > 0 {
for k, h := range r.Header {
req.Header.Set(k, h)
}
}
2017-03-25 16:09:17 +01:00
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-05-03 08:07:45 +02:00
// Function to log the current session
2017-04-29 18:26:36 +02:00
func (r *Request) Login() {
r.cookies = nil
r.cookies = append(r.cookies, &http.Cookie{Name: "session", Value: "testsessionkey"})
}
2017-05-03 08:07:45 +02:00
// Function to logout/quit the current session
2017-04-29 18:26:36 +02:00
func (r *Request) Logout() {
r.cookies = nil
r.cookies = append(r.cookies, &http.Cookie{Name: "session", Value: "trashkey"})
}
2017-05-03 08:07:45 +02:00
// Function to clean the current session
2017-03-25 16:09:17 +01:00
func (r *Request) Clean() {
r.cookies = nil
}