-
-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from spiral/feature/arguments
Feature/arguments
- Loading branch information
Showing
8 changed files
with
181 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"errors" | ||
) | ||
|
||
const contextKey = "psr:attributes" | ||
|
||
type attrs map[string]interface{} | ||
|
||
// InitAttributes returns request with new context and attribute bag. | ||
func InitAttributes(r *http.Request) *http.Request { | ||
return r.WithContext(context.WithValue(r.Context(), contextKey, attrs{})) | ||
} | ||
|
||
// AllAttributes returns all context attributes. | ||
func AllAttributes(r *http.Request) map[string]interface{} { | ||
v := r.Context().Value(contextKey) | ||
if v == nil { | ||
return attrs{} | ||
} | ||
|
||
return v.(attrs) | ||
} | ||
|
||
// Get gets the value from request context. It replaces any existing | ||
// values. | ||
func GetAttribute(r *http.Request, key string) interface{} { | ||
v := r.Context().Value(contextKey) | ||
if v == nil { | ||
return nil | ||
} | ||
|
||
return v.(attrs).Get(key) | ||
} | ||
|
||
// Set sets the key to value. It replaces any existing | ||
// values. Context specific. | ||
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. | ||
func (v attrs) Get(key string) interface{} { | ||
if v == nil { | ||
return "" | ||
} | ||
|
||
return v[key] | ||
} | ||
|
||
// Set sets the key to value. It replaces any existing | ||
// values. | ||
func (v attrs) Set(key string, value interface{}) { | ||
v[key] = value | ||
} | ||
|
||
// Del deletes the value associated with key. | ||
func (v attrs) Del(key string) { | ||
delete(v, key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package http | ||
|
||
import ( | ||
"testing" | ||
"net/http" | ||
"github.com/stretchr/testify/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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters