...
1 package middleware
2
3 import (
4 "context"
5 "testing"
6 )
7
8 func TestStackValues(t *testing.T) {
9 ctx := context.Background()
10
11
12 if v := GetStackValue(ctx, "some key"); v != nil {
13 t.Fatalf("expect not-existing key to be nil, got %T, %v", v, v)
14 }
15
16
17 ctx2 := WithStackValue(ctx, "some key", "foo")
18 ctx2 = WithStackValue(ctx2, "some other key", "bar")
19 if v := GetStackValue(ctx, "some key"); v != nil {
20 t.Fatalf("expect not-existing key to be nil, got %T, %v", v, v)
21 }
22 if v, ok := GetStackValue(ctx2, "some key").(string); !ok || v != "foo" {
23 t.Fatalf("expect key to be present")
24 }
25 if v, ok := GetStackValue(ctx2, "some other key").(string); !ok || v != "bar" {
26 t.Fatalf("expect key to be present")
27 }
28
29
30 ctx3 := ClearStackValues(ctx2)
31 if v, ok := GetStackValue(ctx2, "some key").(string); !ok || v != "foo" {
32 t.Fatalf("expect key to be present")
33 }
34 if v := GetStackValue(ctx3, "some key"); v != nil {
35 t.Fatalf("expect not-existing key to be nil, got %T, %v", v, v)
36 }
37 if v := GetStackValue(ctx3, "some other key"); v != nil {
38 t.Fatalf("expect not-existing key to be nil, got %T, %v", v, v)
39 }
40 }
41
View as plain text