diff --git a/tests/controllers/get_license_test.go b/tests/controllers/get_license_test.go index 0568e69f..e77fb197 100644 --- a/tests/controllers/get_license_test.go +++ b/tests/controllers/get_license_test.go @@ -1,33 +1,15 @@ package test import ( - "net/http" - "net/http/httptest" "testing" - "runtime" - "path/filepath" _ "github.com/deluan/gosonic/routers" - - "github.com/astaxie/beego" - . "github.com/smartystreets/goconvey/convey" + . "github.com/deluan/gosonic/tests" "encoding/xml" - "fmt" - "github.com/deluan/gosonic/tests" + . "github.com/smartystreets/goconvey/convey" ) -func init() { - _, file, _, _ := runtime.Caller(1) - appPath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, "../.." + string(filepath.Separator)))) - beego.TestBeegoInit(appPath) -} - -// TestGet is a sample to run an endpoint test func TestGetLicense(t *testing.T) { - r, _ := http.NewRequest("GET", test.AddParams("/rest/getLicense.view"), nil) - w := httptest.NewRecorder() - beego.BeeApp.Handlers.ServeHTTP(w, r) - - beego.Trace("testing", "TestGetLicense", fmt.Sprintf("Code[%d]\n%s", w.Code, w.Body.String())) + _, w := Get(AddParams("/rest/getLicense.view"), "TestGetLicense") Convey("Subject: GetLicense Endpoint\n", t, func() { Convey("Status code should be 200", func() { diff --git a/tests/controllers/get_music_folders_test.go b/tests/controllers/get_music_folders_test.go index 89d70624..1aee7bd5 100644 --- a/tests/controllers/get_music_folders_test.go +++ b/tests/controllers/get_music_folders_test.go @@ -1,31 +1,15 @@ package test import ( - "net/http" - "net/http/httptest" "testing" - "runtime" - "path/filepath" _ "github.com/deluan/gosonic/routers" + . "github.com/deluan/gosonic/tests" - "github.com/astaxie/beego" . "github.com/smartystreets/goconvey/convey" - "fmt" ) -func init() { - _, file, _, _ := runtime.Caller(1) - appPath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, "../.." + string(filepath.Separator)))) - beego.TestBeegoInit(appPath) -} - -// TestGet is a sample to run an endpoint test func TestGetMusicFolders(t *testing.T) { - r, _ := http.NewRequest("GET", "/rest/getMusicFolders.view", nil) - w := httptest.NewRecorder() - beego.BeeApp.Handlers.ServeHTTP(w, r) - - beego.Trace("testing", "TestGetMusicFolders", fmt.Sprintf("Code[%d]\n%s", w.Code, w.Body.String())) + _, w := Get(AddParams("/rest/getMusicFolders.view"), "TestGetMusicFolders") Convey("Subject: GetMusicFolders Endpoint\n", t, func() { Convey("Status code should be 200", func() { diff --git a/tests/controllers/ping_test.go b/tests/controllers/ping_test.go index 0ca26ca5..d8c0e382 100644 --- a/tests/controllers/ping_test.go +++ b/tests/controllers/ping_test.go @@ -1,32 +1,16 @@ package test import ( - "net/http" - "net/http/httptest" "testing" - "runtime" "encoding/xml" - "path/filepath" _ "github.com/deluan/gosonic/routers" - "github.com/astaxie/beego" . "github.com/smartystreets/goconvey/convey" - "fmt" "github.com/deluan/gosonic/controllers/responses" - "github.com/deluan/gosonic/tests" + . "github.com/deluan/gosonic/tests" ) -func init() { - _, file, _, _ := runtime.Caller(1) - appPath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, "../.." + string(filepath.Separator)))) - beego.TestBeegoInit(appPath) -} - func TestPing(t *testing.T) { - r, _ := http.NewRequest("GET", test.AddParams("/rest/ping.view"), nil) - w := httptest.NewRecorder() - beego.BeeApp.Handlers.ServeHTTP(w, r) - - beego.Trace("testing", "TestPing", fmt.Sprintf("\nUrl: %s\n\nCode[%d]\n%s", r.URL, w.Code, w.Body.String())) + _, w := Get(AddParams("/rest/ping.view"), "TestPing") Convey("Subject: Ping Endpoint\n", t, func() { Convey("Status code should be 200", func() { diff --git a/tests/controllers/validation_test.go b/tests/controllers/validation_test.go index 76f3ce40..87ab4fdd 100644 --- a/tests/controllers/validation_test.go +++ b/tests/controllers/validation_test.go @@ -1,31 +1,16 @@ package test import ( - "net/http" - "net/http/httptest" "testing" - "runtime" "encoding/xml" - "path/filepath" _ "github.com/deluan/gosonic/routers" - "github.com/astaxie/beego" + . "github.com/deluan/gosonic/tests" . "github.com/smartystreets/goconvey/convey" - "fmt" "github.com/deluan/gosonic/controllers/responses" ) -func init() { - _, file, _, _ := runtime.Caller(1) - appPath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, "../.." + string(filepath.Separator)))) - beego.TestBeegoInit(appPath) -} - func TestCheckParams(t *testing.T) { - r, _ := http.NewRequest("GET", "/rest/ping.view", nil) - w := httptest.NewRecorder() - beego.BeeApp.Handlers.ServeHTTP(w, r) - - beego.Trace("testing", "TestCheckParams", fmt.Sprintf("\nUrl: %s\n\nCode[%d]\n%s", r.URL, w.Code, w.Body.String())) + _, w := Get("/rest/ping.view", "TestCheckParams") Convey("Subject: Validation\n", t, func() { Convey("Status code should be 200", func() { @@ -43,11 +28,7 @@ func TestCheckParams(t *testing.T) { } func TestAuthentication(t *testing.T) { - r, _ := http.NewRequest("GET", "/rest/ping.view?u=INVALID&p=INVALID&c=test&v=1.0.0", nil) - w := httptest.NewRecorder() - beego.BeeApp.Handlers.ServeHTTP(w, r) - - beego.Trace("testing", "TestCheckParams", fmt.Sprintf("\nUrl: %s\n\nCode[%d]\n%s", r.URL, w.Code, w.Body.String())) + _, w := Get("/rest/ping.view?u=INVALID&p=INVALID&c=test&v=1.0.0", "TestAuthentication") Convey("Subject: Validation\n", t, func() { Convey("Status code should be 200", func() { diff --git a/tests/test_helper.go b/tests/test_helper.go index 0c2c3dfe..a088ada5 100644 --- a/tests/test_helper.go +++ b/tests/test_helper.go @@ -1,6 +1,13 @@ package test -import "fmt" +import ( + "fmt" + "github.com/astaxie/beego" + "net/http/httptest" +"net/http" + "path/filepath" + "runtime" +) const ( testUser = "deluan" @@ -9,8 +16,23 @@ const ( testVersion = "1.0.0" ) +func init() { + _, file, _, _ := runtime.Caller(1) + appPath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator)))) + beego.TestBeegoInit(appPath) +} + func AddParams(url string) string { return fmt.Sprintf("%s?u=%s&p=%s&c=%s&v=%s", url, testUser, testPassword, testClient, testVersion) } +func Get(url string, methodName string) (*http.Request, *httptest.ResponseRecorder) { + r, _ := http.NewRequest("GET", url, nil) + w := httptest.NewRecorder() + beego.BeeApp.Handlers.ServeHTTP(w, r) + + beego.Trace("testing", methodName, fmt.Sprintf("\nUrl: %s\n\nCode[%d]\n%s", r.URL, w.Code, w.Body.String())) + + return r, w +}