diff --git a/.test-coverage b/.test-coverage index 2142c44..06fa03d 100755 --- a/.test-coverage +++ b/.test-coverage @@ -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 diff --git a/http/good_show_test.go b/http/good_show_test.go index d6e499b..1ef9dc9 100644 --- a/http/good_show_test.go +++ b/http/good_show_test.go @@ -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() + +} diff --git a/test/testrest.go b/test/testrest.go index 053325e..9d34edc 100644 --- a/test/testrest.go +++ b/test/testrest.go @@ -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