[TASK] improve mock of other microservice
This commit is contained in:
parent
c05dad026a
commit
978f65eb7d
|
@ -11,7 +11,7 @@ FAIL=0
|
||||||
for dir in $(find . -maxdepth 10 -not -path './.git*' -not -path '*/_*' -type d);
|
for dir in $(find . -maxdepth 10 -not -path './.git*' -not -path '*/_*' -type d);
|
||||||
do
|
do
|
||||||
if ls $dir/*.go &> /dev/null; then
|
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 ]
|
if [ -f profile.tmp ]
|
||||||
then
|
then
|
||||||
tail -n +2 < profile.tmp >> profile.cov
|
tail -n +2 < profile.tmp >> profile.cov
|
||||||
|
|
|
@ -98,3 +98,58 @@ func TestGetGoodAvailable(t *testing.T) {
|
||||||
test.Close()
|
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()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ package test
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -16,7 +17,20 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Pointer to the server
|
// 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)
|
// Function to initialize a test api (with test files of depending microservice)
|
||||||
func Init(t *testing.T) (assertion *assert.Assertions, router *goji.Mux) {
|
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()
|
router = goji.NewMux()
|
||||||
|
|
||||||
apirouter := http.FileServer(http.Dir("../webroot"))
|
srv = true
|
||||||
srv = &http.Server{
|
mockBackend := http.FileServer(http.Dir("../webroot"))
|
||||||
Addr: ":8080",
|
|
||||||
Handler: apirouter,
|
http.DefaultClient.Transport = &mockTransport{handler: mockBackend}
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
if err := srv.ListenAndServe(); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to close the static webserver
|
// Function to close the static webserver
|
||||||
func CloseServer() {
|
func CloseServer() {
|
||||||
srv.Close()
|
srv = false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to close and stop the whole microservice
|
// Function to close and stop the whole microservice
|
||||||
func Close() {
|
func Close() {
|
||||||
database.Close()
|
database.Close()
|
||||||
srv.Close()
|
srv = false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle a test session with cookies
|
// Handle a test session with cookies
|
||||||
|
|
Reference in New Issue