github.com/blend/go-sdk@v1.20220411.3/web/util_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package web 9 10 import ( 11 "net/http" 12 "testing" 13 14 "github.com/blend/go-sdk/assert" 15 "github.com/blend/go-sdk/webutil" 16 ) 17 18 func TestPathRedirectHandler(t *testing.T) { 19 assert := assert.New(t) 20 21 redirect := PathRedirectHandler("/foo") 22 23 newURL := redirect(NewCtx(nil, webutil.NewMockRequest("GET", "/notfoo"))) 24 assert.Equal("/foo", newURL.Path) 25 } 26 27 func TestBase64URL(t *testing.T) { 28 assert := assert.New(t) 29 bs := []byte("hello") 30 enc := Base64URLEncode(bs) 31 assert.NotEmpty(enc) 32 33 out, err := Base64URLDecode(enc) 34 assert.Nil(err) 35 assert.Equal(string(bs), string(out)) 36 } 37 38 func TestNewCookie(t *testing.T) { 39 assert := assert.New(t) 40 c := NewCookie("hello", "world") 41 assert.NotNil(c) 42 assert.Equal("hello", c.Name) 43 assert.Equal("world", c.Value) 44 } 45 46 func TestMergeHeaders(t *testing.T) { 47 assert := assert.New(t) 48 49 a := map[string][]string{ 50 "Foo": {"foo1a", "foo2a"}, 51 "Bar": {"bar1a", "bar2a"}, 52 } 53 54 b := map[string][]string{ 55 "Foo": {"foo1b", "foo2b", "foo3b"}, 56 "example-string": {"dog"}, 57 } 58 59 c := map[string][]string{ 60 "Bar": {"bar1c", "bar2c"}, 61 "Buzz": {"fuzz"}, 62 } 63 64 merged := MergeHeaders(a, b, c) 65 66 assert.Equal( 67 []string{"foo1a", "foo2a", "foo1b", "foo2b", "foo3b"}, 68 merged["Foo"], 69 ) 70 71 assert.Equal( 72 []string{"bar1a", "bar2a", "bar1c", "bar2c"}, 73 merged["Bar"], 74 ) 75 76 assert.Equal( 77 []string{"dog"}, 78 merged[http.CanonicalHeaderKey("example-string")], 79 ) 80 81 assert.Equal( 82 []string{"fuzz"}, 83 merged["Buzz"], 84 ) 85 }