Skip to content

🎧 Your Personal Streaming Service

Refactored responses, implemented getLicense

Deluan1a3f370ea6e6
diff --git a/conf/app.conf b/conf/app.conf
index 153370877..f1f43e961 100644
--- a/conf/app.conf
+++ b/conf/app.conf
@@ -3,6 +3,8 @@ httpport = 8080
runmode = dev
autorender = false
copyrequestbody = true
-enableadmin = true
-apiversion = 1.0.0
\ No newline at end of file
+apiversion = 1.0.0
+
+[dev]
+enableadmin = true
diff --git a/controllers/get_license.go b/controllers/get_license.go
new file mode 100644
index 000000000..95eb938ba
--- /dev/null
+++ b/controllers/get_license.go
@@ -0,0 +1,19 @@
+package controllers
+
+import (
+ "github.com/astaxie/beego"
+ "encoding/xml"
+ "github.com/deluan/gosonic/responses"
+)
+
+type GetLicenseController struct{ beego.Controller }
+
+// @router /rest/getLicense.view [get]
+func (this *GetLicenseController) Get() {
+ response := responses.NewGetLicense(true)
+ xmlBody, _ := xml.Marshal(response)
+ this.Ctx.Output.Body([]byte(xml.Header + string(xmlBody)))
+}
+
+
+
diff --git a/controllers/ping.go b/controllers/ping.go
index 28ae973dc..2ab2765ee 100644
--- a/controllers/ping.go
+++ b/controllers/ping.go
@@ -3,19 +3,14 @@ package controllers
import (
"github.com/astaxie/beego"
"encoding/xml"
+ "github.com/deluan/gosonic/responses"
)
-type PingResponse struct {
- XMLName xml.Name `xml:"http://subsonic.org/restapi subsonic-response"`
- Status string `xml:"status,attr"`
- Version string `xml:"version,attr"`
-}
-
type PingController struct{ beego.Controller }
// @router /rest/ping.view [get]
func (this *PingController) Get() {
- response := &PingResponse{Status:"ok", Version: beego.AppConfig.String("apiversion")}
+ response := responses.NewSubsonic()
xmlBody, _ := xml.Marshal(response)
this.Ctx.Output.Body([]byte(xml.Header + string(xmlBody)))
}
diff --git a/responses/license.go b/responses/license.go
new file mode 100644
index 000000000..7b544517f
--- /dev/null
+++ b/responses/license.go
@@ -0,0 +1,17 @@
+package responses
+
+type valid struct {
+ Valid bool `xml:"valid,attr"`
+}
+
+type license struct {
+ Subsonic
+ Body valid `xml:"license"`
+}
+
+func NewGetLicense(valid bool) *license {
+ response := new(license)
+ response.Subsonic = NewSubsonic()
+ response.Body.Valid = valid
+ return response
+}
diff --git a/responses/subsonic.go b/responses/subsonic.go
new file mode 100644
index 000000000..84723ad6e
--- /dev/null
+++ b/responses/subsonic.go
@@ -0,0 +1,16 @@
+package responses
+
+import (
+ "encoding/xml"
+ "github.com/astaxie/beego"
+)
+
+type Subsonic struct {
+ XMLName xml.Name `xml:"http://subsonic.org/restapi subsonic-response"`
+ Status string `xml:"status,attr"`
+ Version string `xml:"version,attr"`
+}
+
+func NewSubsonic() Subsonic {
+ return Subsonic{Status: "ok", Version: beego.AppConfig.String("apiversion")}
+}
\ No newline at end of file
diff --git a/routers/router.go b/routers/router.go
index f2997e954..eff9faae1 100644
--- a/routers/router.go
+++ b/routers/router.go
@@ -14,5 +14,8 @@ import (
)
func init() {
- beego.Include(&controllers.PingController{})
+ beego.Include(
+ &controllers.PingController{},
+ &controllers.GetLicenseController{},
+ )
}
diff --git a/tests/controllers/get_license_test.go b/tests/controllers/get_license_test.go
new file mode 100644
index 000000000..c2cd35c54
--- /dev/null
+++ b/tests/controllers/get_license_test.go
@@ -0,0 +1,44 @@
+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"
+ "encoding/xml"
+ "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 TestGetLicense(t *testing.T) {
+ r, _ := http.NewRequest("GET", "/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()))
+
+ Convey("Subject: GetLicense Endpoint\n", t, func() {
+ Convey("Status code should be 200", func() {
+ So(w.Code, ShouldEqual, 200)
+ })
+ Convey("The license should always be valid", func() {
+ v := new(string)
+ err := xml.Unmarshal(w.Body.Bytes(), &v)
+ So(err, ShouldBeNil)
+ So(w.Body.String(), ShouldContainSubstring, `license valid="true"`)
+ })
+
+ })
+}
+
diff --git a/tests/controllers/ping_test.go b/tests/controllers/ping_test.go
index 8f82144e2..a33fe103f 100644
--- a/tests/controllers/ping_test.go
+++ b/tests/controllers/ping_test.go
@@ -5,19 +5,19 @@ import (
"net/http/httptest"
"testing"
"runtime"
+ "encoding/xml"
"path/filepath"
_ "github.com/deluan/gosonic/routers"
-
"github.com/astaxie/beego"
. "github.com/smartystreets/goconvey/convey"
- "github.com/deluan/gosonic/controllers"
-"encoding/xml"
+ "github.com/deluan/gosonic/responses"
+ "fmt"
)
func init() {
_, file, _, _ := runtime.Caller(1)
- apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, "../.." + string(filepath.Separator))))
- beego.TestBeegoInit(apppath)
+ appPath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, "../.." + string(filepath.Separator))))
+ beego.TestBeegoInit(appPath)
}
// TestGet is a sample to run an endpoint test
@@ -26,17 +26,17 @@ func TestPing(t *testing.T) {
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
- beego.Trace("testing", "TestPing", "Code[%d]\n%s", w.Code, w.Body.String())
+ beego.Trace("testing", "TestPing", fmt.Sprintf("Code[%d]\n%s", w.Code, w.Body.String()))
Convey("Subject: Ping Endpoint\n", t, func() {
- Convey("Status Code Should Be 200", func() {
+ Convey("Status code should be 200", func() {
So(w.Code, ShouldEqual, 200)
})
- Convey("The Result Should Not Be Empty", func() {
+ Convey("The result should not be empty", func() {
So(w.Body.Len(), ShouldBeGreaterThan, 0)
})
- Convey("The Result Should Be A Valid Ping Response", func() {
- v := controllers.PingResponse{}
+ Convey("The result should be a valid ping response", func() {
+ v := responses.Subsonic{}
xml.Unmarshal(w.Body.Bytes(), &v)
So(v.Status, ShouldEqual, "ok")
So(v.Version, ShouldEqual, "1.0.0")
diff --git a/tests/default_test.go b/tests/default_test.go
deleted file mode 100644
index de2075bfc..000000000
--- a/tests/default_test.go
+++ /dev/null
@@ -1,38 +0,0 @@
-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"
-)
-
-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 xTestGet(t *testing.T) {
- r, _ := http.NewRequest("GET", "/v1/object", nil)
- w := httptest.NewRecorder()
- beego.BeeApp.Handlers.ServeHTTP(w, r)
-
- beego.Trace("testing", "TestGet", "Code[%d]\n%s", w.Code, w.Body.String())
-
- Convey("Subject: Test Station Endpoint\n", t, func() {
- Convey("Status Code Should Be 200", func() {
- So(w.Code, ShouldEqual, 200)
- })
- Convey("The Result Should Not Be Empty", func() {
- So(w.Body.Len(), ShouldBeGreaterThan, 0)
- })
- })
-}
-