From 4c9315e98454df6591199671f2949c9d1dd07f33 Mon Sep 17 00:00:00 2001 From: kevin Date: Sat, 31 Oct 2020 22:10:11 +0800 Subject: [PATCH] add more tests --- rest/internal/context/params.go | 2 +- rest/internal/context/params_test.go | 32 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 rest/internal/context/params_test.go diff --git a/rest/internal/context/params.go b/rest/internal/context/params.go index b25e0e6b..4890689d 100644 --- a/rest/internal/context/params.go +++ b/rest/internal/context/params.go @@ -23,5 +23,5 @@ func WithPathVars(r *http.Request, params map[string]string) *http.Request { type contextKey string func (c contextKey) String() string { - return "rest/internal/context context key" + string(c) + return "rest/internal/context key: " + string(c) } diff --git a/rest/internal/context/params_test.go b/rest/internal/context/params_test.go new file mode 100644 index 00000000..c26ffd40 --- /dev/null +++ b/rest/internal/context/params_test.go @@ -0,0 +1,32 @@ +package context + +import ( + "context" + "net/http" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestVars(t *testing.T) { + expect := map[string]string{ + "a": "1", + "b": "2", + } + r, err := http.NewRequest(http.MethodGet, "/", nil) + assert.Nil(t, err) + r = r.WithContext(context.WithValue(context.Background(), pathVars, expect)) + assert.EqualValues(t, expect, Vars(r)) +} + +func TestVarsNil(t *testing.T) { + r, err := http.NewRequest(http.MethodGet, "/", nil) + assert.Nil(t, err) + assert.Nil(t, Vars(r)) +} + +func TestContextKey(t *testing.T) { + ck := contextKey("hello") + assert.True(t, strings.Contains(ck.String(), "hello")) +}