genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[TASK] improve mock of other microservice

This commit is contained in:
Martin Geno 2017-05-18 00:29:49 +02:00
parent c05dad026a
commit 978f65eb7d
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
3 changed files with 78 additions and 14 deletions

View File

@ -11,7 +11,7 @@ FAIL=0
for dir in $(find . -maxdepth 10 -not -path './.git*' -not -path '*/_*' -type d);
do
if ls $dir/*.go &> /dev/null; then
go test -p 1 -v -covermode=count -coverprofile=profile.tmp $dir || FAIL=$?
go test -v -covermode=count -coverprofile=profile.tmp $dir || FAIL=$?
if [ -f profile.tmp ]
then
tail -n +2 < profile.tmp >> profile.cov

View File

@ -98,3 +98,58 @@ func TestGetGoodAvailable(t *testing.T) {
test.Close()
}
// Function to getGoodFreshness()
func TestGetGoodFreshness(t *testing.T) {
now := time.Now().Add(36 * time.Hour)
assertion, router := test.Init(t)
runtime.ProductURL = "http://localhost:8080/api-test/product/%d/"
BindAPI(router)
session := test.NewSession(router)
result, w := session.JSONRequest("GET", "/api/good/freshness/a", nil)
assertion.Equal(http.StatusNotAcceptable, w.StatusCode)
database.Write.Create(&models.Good{
ID: 3,
ProductID: -2,
})
result, w = session.JSONRequest("GET", "/api/good/freshness/3", nil)
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(false, result)
database.Write.Save(&models.Good{
ID: 3,
ProductID: -2,
FouledAt: &now,
})
result, w = session.JSONRequest("GET", "/api/good/freshness/3", nil)
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(true, result)
fouled := now.Add(-72 * time.Hour)
database.Write.Save(&models.Good{
ID: 3,
ProductID: -2,
FouledAt: &fouled,
})
result, w = session.JSONRequest("GET", "/api/good/freshness/3", nil)
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(false, result)
req, _ := http.NewRequest("GET", "/api/good/freshness/3", nil)
req.Header.Set("Content-Type", "image/svg+xml")
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
assertion.Equal(http.StatusOK, w.StatusCode)
result, w = session.JSONRequest("GET", "/api/good/freshness/7", nil)
assertion.Equal(http.StatusNotFound, w.StatusCode)
test.Close()
}

View File

@ -5,6 +5,7 @@ package test
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
@ -16,7 +17,20 @@ import (
)
// Pointer to the server
var srv *http.Server
var srv bool
type mockTransport struct {
handler http.Handler
}
func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if !srv {
return nil, errors.New("mock a error")
}
w := httptest.NewRecorder()
t.handler.ServeHTTP(w, req)
return w.Result(), nil
}
// Function to initialize a test api (with test files of depending microservice)
func Init(t *testing.T) (assertion *assert.Assertions, router *goji.Mux) {
@ -28,28 +42,23 @@ func Init(t *testing.T) (assertion *assert.Assertions, router *goji.Mux) {
})
router = goji.NewMux()
apirouter := http.FileServer(http.Dir("../webroot"))
srv = &http.Server{
Addr: ":8080",
Handler: apirouter,
}
go func() {
if err := srv.ListenAndServe(); err != nil {
panic(err)
}
}()
srv = true
mockBackend := http.FileServer(http.Dir("../webroot"))
http.DefaultClient.Transport = &mockTransport{handler: mockBackend}
return
}
// Function to close the static webserver
func CloseServer() {
srv.Close()
srv = false
}
// Function to close and stop the whole microservice
func Close() {
database.Close()
srv.Close()
srv = false
}
// Handle a test session with cookies