...

Source file src/github.com/aws/smithy-go/middleware/stack_values_test.go

Documentation: github.com/aws/smithy-go/middleware

     1  package middleware
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  )
     7  
     8  func TestStackValues(t *testing.T) {
     9  	ctx := context.Background()
    10  
    11  	// Ensure empty stack values don't return something
    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  	// Add a stack values, ensure not polluting previous context.
    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  	// Clear the stack values ensure new context doesn't have any stack values.
    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