From 100d5889b61d1a9f8498d7140248394cd230263d Mon Sep 17 00:00:00 2001 From: Niko Filippidis <11477309+filikos@users.noreply.github.com> Date: Mon, 30 Aug 2021 18:12:02 +0200 Subject: [PATCH 1/3] Added SetParam shortcut for e2e tests, added SetParam test --- context.go | 9 +++++++++ context_test.go | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/context.go b/context.go index 56c20f2c61..44ebd3ab42 100644 --- a/context.go +++ b/context.go @@ -383,6 +383,15 @@ func (c *Context) Param(key string) string { return c.Params.ByName(key) } +// SetParam adds param to context an +// replaces path param key with given value for e2e testing purposes +// Example Route: "/user/:id" +// SetParam("id", 1) +// Result: "/user/1" +func (c *Context) SetParam(key, value string) { + c.Params = append(c.Params, Param{Key: key,Value: value}) +} + // Query returns the keyed url query value if it exists, // otherwise it returns an empty string `("")`. // It is shortcut for `c.Request.URL.Query().Get(key)` diff --git a/context_test.go b/context_test.go index 176eaae6b6..85e14a2b39 100644 --- a/context_test.go +++ b/context_test.go @@ -2150,3 +2150,14 @@ func TestContextWithFallbackValueFromRequestContext(t *testing.T) { }) } } + +func TestContextSetParam(t *testing.T) { + c := &Context{} + id := "id" + value := "1" + c.SetParam(id, value) + + v, ok := c.Params.Get(id) + assert.Equal(t, ok, true) + assert.Equal(t, value, v) +} From 961f4951f43e8874409208f84e1d9dfd530fc96c Mon Sep 17 00:00:00 2001 From: Niko Filippidis <11477309+filikos@users.noreply.github.com> Date: Wed, 1 Sep 2021 13:35:13 +0200 Subject: [PATCH 2/3] Adjusted naming and formatting --- context.go | 8 ++++---- context_test.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/context.go b/context.go index 44ebd3ab42..9a146f4e99 100644 --- a/context.go +++ b/context.go @@ -383,13 +383,13 @@ func (c *Context) Param(key string) string { return c.Params.ByName(key) } -// SetParam adds param to context an +// AddParam adds param to context an // replaces path param key with given value for e2e testing purposes // Example Route: "/user/:id" -// SetParam("id", 1) +// AddParam("id", 1) // Result: "/user/1" -func (c *Context) SetParam(key, value string) { - c.Params = append(c.Params, Param{Key: key,Value: value}) +func (c *Context) AddParam(key, value string) { + c.Params = append(c.Params, Param{Key: key, Value: value}) } // Query returns the keyed url query value if it exists, diff --git a/context_test.go b/context_test.go index 85e14a2b39..45fba4fb57 100644 --- a/context_test.go +++ b/context_test.go @@ -2151,11 +2151,11 @@ func TestContextWithFallbackValueFromRequestContext(t *testing.T) { } } -func TestContextSetParam(t *testing.T) { +func TestContextAddParam(t *testing.T) { c := &Context{} id := "id" value := "1" - c.SetParam(id, value) + c.AddParam(id, value) v, ok := c.Params.Get(id) assert.Equal(t, ok, true) From 783438c8e2d5ea1a4e9f40e62ac73f579b232b36 Mon Sep 17 00:00:00 2001 From: Niko Filippidis <11477309+filikos@users.noreply.github.com> Date: Wed, 1 Sep 2021 13:43:54 +0200 Subject: [PATCH 3/3] fixed typo --- context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context.go b/context.go index 9a146f4e99..62849488b2 100644 --- a/context.go +++ b/context.go @@ -383,7 +383,7 @@ func (c *Context) Param(key string) string { return c.Params.ByName(key) } -// AddParam adds param to context an +// AddParam adds param to context and // replaces path param key with given value for e2e testing purposes // Example Route: "/user/:id" // AddParam("id", 1)