Fixes context, and adds more info to it
This commit is contained in:
@@ -34,3 +34,14 @@ func Get(url string, testCase string) (*http.Request, *httptest.ResponseRecorder
|
|||||||
|
|
||||||
return r, w
|
return r, w
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetWithHeader(url string, header, value, testCase string) (*http.Request, *httptest.ResponseRecorder) {
|
||||||
|
r, _ := http.NewRequest("GET", url, nil)
|
||||||
|
r.Header.Add(header, value)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
beego.BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
|
|
||||||
|
beego.Debug("testing", testCase, fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%s", r.URL, w.Code, w.Body.String()))
|
||||||
|
|
||||||
|
return r, w
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -13,10 +12,7 @@ import (
|
|||||||
"github.com/cloudsonic/sonic-server/utils"
|
"github.com/cloudsonic/sonic-server/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BaseAPIController struct {
|
type BaseAPIController struct{ beego.Controller }
|
||||||
beego.Controller
|
|
||||||
context context.Context
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *BaseAPIController) NewEmpty() responses.Subsonic {
|
func (c *BaseAPIController) NewEmpty() responses.Subsonic {
|
||||||
return responses.Subsonic{Status: "ok", Version: beego.AppConfig.String("apiVersion")}
|
return responses.Subsonic{Status: "ok", Version: beego.AppConfig.String("apiVersion")}
|
||||||
|
|||||||
+8
-9
@@ -27,18 +27,12 @@ func Validate(controller BaseAPIController) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getData(c BaseAPIController, name string) string {
|
|
||||||
if v, ok := c.Ctx.Input.GetData(name).(string); ok {
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func addNewContext(c BaseAPIController) {
|
func addNewContext(c BaseAPIController) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
id := getData(c, "requestId")
|
id := c.Ctx.Input.GetData("requestId")
|
||||||
c.context = context.WithValue(ctx, "requestId", id)
|
ctx = context.WithValue(ctx, "requestId", id)
|
||||||
|
c.Ctx.Input.SetData("context", ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkParameters(c BaseAPIController) {
|
func checkParameters(c BaseAPIController) {
|
||||||
@@ -54,6 +48,11 @@ func checkParameters(c BaseAPIController) {
|
|||||||
if c.GetString("p") == "" && (c.GetString("s") == "" || c.GetString("t") == "") {
|
if c.GetString("p") == "" && (c.GetString("s") == "" || c.GetString("t") == "") {
|
||||||
logWarn(c, "Missing authentication information")
|
logWarn(c, "Missing authentication information")
|
||||||
}
|
}
|
||||||
|
ctx := c.Ctx.Input.GetData("context").(context.Context)
|
||||||
|
ctx = context.WithValue(ctx, "user", c.GetString("u"))
|
||||||
|
ctx = context.WithValue(ctx, "client", c.GetString("c"))
|
||||||
|
ctx = context.WithValue(ctx, "version", c.GetString("v"))
|
||||||
|
c.Ctx.Input.SetData("context", ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func authenticate(c BaseAPIController) {
|
func authenticate(c BaseAPIController) {
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/astaxie/beego"
|
||||||
|
"github.com/cloudsonic/sonic-server/api"
|
||||||
"github.com/cloudsonic/sonic-server/api/responses"
|
"github.com/cloudsonic/sonic-server/api/responses"
|
||||||
"github.com/cloudsonic/sonic-server/tests"
|
"github.com/cloudsonic/sonic-server/tests"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
@@ -74,3 +78,39 @@ func TestAuthentication(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type mockController struct {
|
||||||
|
api.BaseAPIController
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *mockController) Get() {
|
||||||
|
actualContext = c.Ctx.Input.GetData("context").(context.Context)
|
||||||
|
c.Ctx.WriteString("OK")
|
||||||
|
}
|
||||||
|
|
||||||
|
var actualContext context.Context
|
||||||
|
|
||||||
|
func TestContext(t *testing.T) {
|
||||||
|
tests.Init(t, false)
|
||||||
|
beego.Router("/rest/mocktest", &mockController{})
|
||||||
|
|
||||||
|
Convey("Subject: Context", t, func() {
|
||||||
|
_, w := GetWithHeader("/rest/mocktest?u=deluan&p=wordpass&c=testClient&v=1.0.0", "X-Request-Id", "123123", "TestContext")
|
||||||
|
Convey("The status should be 'OK'", func() {
|
||||||
|
resp := string(w.Body.Bytes())
|
||||||
|
So(resp, ShouldEqual, "OK")
|
||||||
|
})
|
||||||
|
Convey("user should be set", func() {
|
||||||
|
So(actualContext.Value("user"), ShouldEqual, "deluan")
|
||||||
|
})
|
||||||
|
Convey("client should be set", func() {
|
||||||
|
So(actualContext.Value("client"), ShouldEqual, "testClient")
|
||||||
|
})
|
||||||
|
Convey("version should be set", func() {
|
||||||
|
So(actualContext.Value("version"), ShouldEqual, "1.0.0")
|
||||||
|
})
|
||||||
|
Convey("context should be set", func() {
|
||||||
|
So(actualContext.Value("requestId"), ShouldEqual, "123123")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -75,7 +75,6 @@ func initFilters() {
|
|||||||
if id == "" {
|
if id == "" {
|
||||||
id = uuid.NewV4().String()
|
id = uuid.NewV4().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Input.SetData("requestId", id)
|
ctx.Input.SetData("requestId", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user