mirror of
https://github.com/labstack/echo.git
synced 2026-03-12 15:46:17 +02:00
71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
// SPDX-License-Identifier: MIT
|
|
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
|
|
|
|
package echo
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestContextGetOK(t *testing.T) {
|
|
c := NewContext(nil, nil)
|
|
|
|
c.Set("key", int64(123))
|
|
|
|
v, err := ContextGet[int64](c, "key")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(123), v)
|
|
}
|
|
|
|
func TestContextGetNonExistentKey(t *testing.T) {
|
|
c := NewContext(nil, nil)
|
|
|
|
c.Set("key", int64(123))
|
|
|
|
v, err := ContextGet[int64](c, "nope")
|
|
assert.ErrorIs(t, err, ErrNonExistentKey)
|
|
assert.Equal(t, int64(0), v)
|
|
}
|
|
|
|
func TestContextGetInvalidCast(t *testing.T) {
|
|
c := NewContext(nil, nil)
|
|
|
|
c.Set("key", int64(123))
|
|
|
|
v, err := ContextGet[bool](c, "key")
|
|
assert.ErrorIs(t, err, ErrInvalidKeyType)
|
|
assert.Equal(t, false, v)
|
|
}
|
|
|
|
func TestContextGetOrOK(t *testing.T) {
|
|
c := NewContext(nil, nil)
|
|
|
|
c.Set("key", int64(123))
|
|
|
|
v, err := ContextGetOr[int64](c, "key", 999)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(123), v)
|
|
}
|
|
|
|
func TestContextGetOrNonExistentKey(t *testing.T) {
|
|
c := NewContext(nil, nil)
|
|
|
|
c.Set("key", int64(123))
|
|
|
|
v, err := ContextGetOr[int64](c, "nope", 999)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(999), v)
|
|
}
|
|
|
|
func TestContextGetOrInvalidCast(t *testing.T) {
|
|
c := NewContext(nil, nil)
|
|
|
|
c.Set("key", int64(123))
|
|
|
|
v, err := ContextGetOr[float32](c, "key", float32(999))
|
|
assert.ErrorIs(t, err, ErrInvalidKeyType)
|
|
assert.Equal(t, float32(0), v)
|
|
}
|