summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-07-07 19:52:46 -0700
committerWolfy-J <[email protected]>2018-07-07 19:52:46 -0700
commitb819c830c407fb4c8bec21c3a3891a8313db7a8e (patch)
treef69e8e5885f7feabb11b94a9b2b87182f6034d75 /service
parent806b89a1ac24cf6d173f697c9261eed5efa68c3f (diff)
more tests
Diffstat (limited to 'service')
-rw-r--r--service/http/attributes.go14
-rw-r--r--service/http/attributes_test.go67
2 files changed, 77 insertions, 4 deletions
diff --git a/service/http/attributes.go b/service/http/attributes.go
index fe069adf..3db538db 100644
--- a/service/http/attributes.go
+++ b/service/http/attributes.go
@@ -3,6 +3,7 @@ package http
import (
"context"
"net/http"
+ "github.com/go-errors/errors"
)
const contextKey = "psr:attributes"
@@ -18,7 +19,7 @@ func InitAttributes(r *http.Request) *http.Request {
func AllAttributes(r *http.Request) map[string]interface{} {
v := r.Context().Value(contextKey)
if v == nil {
- return nil
+ return attrs{}
}
return v.(attrs)
@@ -29,7 +30,7 @@ func AllAttributes(r *http.Request) map[string]interface{} {
func GetAttribute(r *http.Request, key string) interface{} {
v := r.Context().Value(contextKey)
if v == nil {
- return ""
+ return nil
}
return v.(attrs).Get(key)
@@ -37,9 +38,14 @@ func GetAttribute(r *http.Request, key string) interface{} {
// Set sets the key to value. It replaces any existing
// values. Context specific.
-func SetAttribute(r *http.Request, key string, value interface{}) {
+func SetAttribute(r *http.Request, key string, value interface{}) error {
v := r.Context().Value(contextKey)
+ if v == nil {
+ return errors.New("unable to find psr:attributes context value")
+ }
+
v.(attrs).Set(key, value)
+ return nil
}
// Get gets the value associated with the given key.
@@ -60,4 +66,4 @@ func (v attrs) Set(key string, value interface{}) {
// Del deletes the value associated with key.
func (v attrs) Del(key string) {
delete(v, key)
-} \ No newline at end of file
+}
diff --git a/service/http/attributes_test.go b/service/http/attributes_test.go
new file mode 100644
index 00000000..d58d8879
--- /dev/null
+++ b/service/http/attributes_test.go
@@ -0,0 +1,67 @@
+package http
+
+import (
+ "testing"
+ "net/http"
+ "github.com/magiconair/properties/assert"
+)
+
+func TestAllAttributes(t *testing.T) {
+ r := &http.Request{}
+ r = InitAttributes(r)
+
+ SetAttribute(r, "key", "value")
+
+ assert.Equal(t, AllAttributes(r), map[string]interface{}{
+ "key": "value",
+ })
+}
+
+func TestAllAttributesNone(t *testing.T) {
+ r := &http.Request{}
+ r = InitAttributes(r)
+
+ assert.Equal(t, AllAttributes(r), map[string]interface{}{})
+}
+
+func TestAllAttributesNone2(t *testing.T) {
+ r := &http.Request{}
+
+ assert.Equal(t, AllAttributes(r), map[string]interface{}{})
+}
+
+func TestGetAttribute(t *testing.T) {
+ r := &http.Request{}
+ r = InitAttributes(r)
+
+ SetAttribute(r, "key", "value")
+ assert.Equal(t, GetAttribute(r, "key"), "value")
+}
+
+func TestGetAttributeNone(t *testing.T) {
+ r := &http.Request{}
+ r = InitAttributes(r)
+
+ assert.Equal(t, GetAttribute(r, "key"), nil)
+}
+
+func TestGetAttributeNone2(t *testing.T) {
+ r := &http.Request{}
+
+ assert.Equal(t, GetAttribute(r, "key"), nil)
+}
+
+func TestSetAttribute(t *testing.T) {
+ r := &http.Request{}
+ r = InitAttributes(r)
+
+ SetAttribute(r, "key", "value")
+ assert.Equal(t, GetAttribute(r, "key"), "value")
+}
+
+func TestSetAttributeNone(t *testing.T) {
+ r := &http.Request{}
+
+ SetAttribute(r, "key", "value")
+ assert.Equal(t, GetAttribute(r, "key"), nil)
+} \ No newline at end of file