mirror of
https://github.com/kataras/iris.git
synced 2026-01-09 13:05:56 +00:00
add hCaptcha middleware and example and memstore json struct tags
Former-commit-id: 31e4f68429e354c9130ebcf3829683a6d52e4492
This commit is contained in:
@@ -21,9 +21,9 @@ type (
|
||||
}
|
||||
// Entry is the entry of the context storage Store - .Values()
|
||||
Entry struct {
|
||||
Key string
|
||||
ValueRaw interface{}
|
||||
immutable bool // if true then it can't change by its caller.
|
||||
Key string `json:"key" msgpack:"key" yaml:"Key" toml:"Value"`
|
||||
ValueRaw interface{} `json:"value" msgpack:"value" yaml:"Value" toml:"Value"`
|
||||
immutable bool // if true then it can't change by its caller.
|
||||
}
|
||||
|
||||
// Store is a collection of key-value entries with immutability capabilities.
|
||||
|
||||
@@ -2,11 +2,14 @@
|
||||
package memstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type myTestObject struct {
|
||||
name string
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func TestMuttable(t *testing.T) {
|
||||
@@ -15,10 +18,10 @@ func TestMuttable(t *testing.T) {
|
||||
// slice
|
||||
p.Set("slice", []myTestObject{{"value 1"}, {"value 2"}})
|
||||
v := p.Get("slice").([]myTestObject)
|
||||
v[0].name = "modified"
|
||||
v[0].Name = "modified"
|
||||
|
||||
vv := p.Get("slice").([]myTestObject)
|
||||
if vv[0].name != "modified" {
|
||||
if vv[0].Name != "modified" {
|
||||
t.Fatalf("expected slice to be muttable but caller was not able to change its value")
|
||||
}
|
||||
|
||||
@@ -29,7 +32,7 @@ func TestMuttable(t *testing.T) {
|
||||
vMap["key 1"] = myTestObject{"modified"}
|
||||
|
||||
vvMap := p.Get("map").(map[string]myTestObject)
|
||||
if vvMap["key 1"].name != "modified" {
|
||||
if vvMap["key 1"].Name != "modified" {
|
||||
t.Fatalf("expected map to be muttable but caller was not able to change its value")
|
||||
}
|
||||
|
||||
@@ -38,10 +41,10 @@ func TestMuttable(t *testing.T) {
|
||||
// we expect pointer here, as we set it.
|
||||
vObjP := p.Get("objp").(*myTestObject)
|
||||
|
||||
vObjP.name = "modified"
|
||||
vObjP.Name = "modified"
|
||||
|
||||
vvObjP := p.Get("objp").(*myTestObject)
|
||||
if vvObjP.name != "modified" {
|
||||
if vvObjP.Name != "modified" {
|
||||
t.Fatalf("expected objp to be muttable but caller was able to change its value")
|
||||
}
|
||||
}
|
||||
@@ -52,10 +55,10 @@ func TestImmutable(t *testing.T) {
|
||||
// slice
|
||||
p.SetImmutable("slice", []myTestObject{{"value 1"}, {"value 2"}})
|
||||
v := p.Get("slice").([]myTestObject)
|
||||
v[0].name = "modified"
|
||||
v[0].Name = "modified"
|
||||
|
||||
vv := p.Get("slice").([]myTestObject)
|
||||
if vv[0].name == "modified" {
|
||||
if vv[0].Name == "modified" {
|
||||
t.Fatalf("expected slice to be immutable but caller was able to change its value")
|
||||
}
|
||||
|
||||
@@ -65,17 +68,17 @@ func TestImmutable(t *testing.T) {
|
||||
vMap["key 1"] = myTestObject{"modified"}
|
||||
|
||||
vvMap := p.Get("map").(map[string]myTestObject)
|
||||
if vvMap["key 1"].name == "modified" {
|
||||
if vvMap["key 1"].Name == "modified" {
|
||||
t.Fatalf("expected map to be immutable but caller was able to change its value")
|
||||
}
|
||||
|
||||
// object value, it's immutable at all cases.
|
||||
p.SetImmutable("obj", myTestObject{"value"})
|
||||
vObj := p.Get("obj").(myTestObject)
|
||||
vObj.name = "modified"
|
||||
vObj.Name = "modified"
|
||||
|
||||
vvObj := p.Get("obj").(myTestObject)
|
||||
if vvObj.name == "modified" {
|
||||
if vvObj.Name == "modified" {
|
||||
t.Fatalf("expected obj to be immutable but caller was able to change its value")
|
||||
}
|
||||
|
||||
@@ -85,10 +88,10 @@ func TestImmutable(t *testing.T) {
|
||||
// so it can't be changed by-design
|
||||
vObjP := p.Get("objp").(myTestObject)
|
||||
|
||||
vObjP.name = "modified"
|
||||
vObjP.Name = "modified"
|
||||
|
||||
vvObjP := p.Get("objp").(myTestObject)
|
||||
if vvObjP.name == "modified" {
|
||||
if vvObjP.Name == "modified" {
|
||||
t.Fatalf("expected objp to be immutable but caller was able to change its value")
|
||||
}
|
||||
}
|
||||
@@ -100,13 +103,13 @@ func TestImmutableSetOnlyWithSetImmutable(t *testing.T) {
|
||||
|
||||
p.Set("objp", &myTestObject{"modified"})
|
||||
vObjP := p.Get("objp").(myTestObject)
|
||||
if vObjP.name == "modified" {
|
||||
if vObjP.Name == "modified" {
|
||||
t.Fatalf("caller should not be able to change the immutable entry with a simple `Set`")
|
||||
}
|
||||
|
||||
p.SetImmutable("objp", &myTestObject{"value with SetImmutable"})
|
||||
vvObjP := p.Get("objp").(myTestObject)
|
||||
if vvObjP.name != "value with SetImmutable" {
|
||||
if vvObjP.Name != "value with SetImmutable" {
|
||||
t.Fatalf("caller should be able to change the immutable entry with a `SetImmutable`")
|
||||
}
|
||||
}
|
||||
@@ -119,3 +122,46 @@ func TestGetInt64Default(t *testing.T) {
|
||||
t.Fatalf("unexpected value of %d", v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSON(t *testing.T) {
|
||||
var p Store
|
||||
|
||||
p.Set("key1", "value1")
|
||||
p.Set("key2", 2)
|
||||
p.Set("key3", myTestObject{Name: "makis"})
|
||||
|
||||
b, err := json.Marshal(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expectedJSON := []byte(`[{"key":"key1","value":"value1"},{"key":"key2","value":2},{"key":"key3","value":{"name":"makis"}}]`)
|
||||
|
||||
if !bytes.Equal(b, expectedJSON) {
|
||||
t.Fatalf("expected: %s but got: %s", string(expectedJSON), string(b))
|
||||
}
|
||||
|
||||
var newStore Store
|
||||
if err = json.Unmarshal(b, &newStore); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i, v := range newStore {
|
||||
expected, got := p.Get(v.Key), v.ValueRaw
|
||||
|
||||
if ex, g := fmt.Sprintf("%v", expected), fmt.Sprintf("%v", got); ex != g {
|
||||
if _, isMap := got.(map[string]interface{}); isMap {
|
||||
// was struct but converted into map (as expected).
|
||||
b1, _ := json.Marshal(expected)
|
||||
b2, _ := json.Marshal(got)
|
||||
|
||||
if !bytes.Equal(b1, b2) {
|
||||
t.Fatalf("[%d] JSON expected: %s but got: %s", i, string(b1), string(b2))
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
t.Fatalf("[%d] expected: %s but got: %s", i, ex, g)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user