github.com/blend/go-sdk@v1.20220411.3/web/ctx_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 "bytes" 12 "io" 13 "net/url" 14 "testing" 15 "time" 16 17 "github.com/blend/go-sdk/assert" 18 "github.com/blend/go-sdk/uuid" 19 "github.com/blend/go-sdk/webutil" 20 ) 21 22 func TestCtxGetState(t *testing.T) { 23 assert := assert.New(t) 24 25 context := NewCtx(nil, nil) 26 context.WithStateValue("foo", "bar") 27 assert.Equal("bar", context.StateValue("foo")) 28 } 29 30 func TestCtxParamQuery(t *testing.T) { 31 assert := assert.New(t) 32 33 context := MockCtx("GET", "/", OptCtxQueryValue("foo", "bar")) 34 param, err := context.Param("foo") 35 assert.Nil(err) 36 assert.Equal("bar", param) 37 38 param, err = context.QueryValue("foo") 39 assert.Nil(err) 40 assert.Equal("bar", param) 41 } 42 43 func TestCtxParamHeader(t *testing.T) { 44 assert := assert.New(t) 45 46 context := MockCtx("GET", "/", OptCtxHeaderValue("foo", "bar")) 47 param, err := context.Param("foo") 48 assert.Nil(err) 49 assert.Equal("bar", param) 50 51 param, err = context.HeaderValue("foo") 52 assert.Nil(err) 53 assert.Equal("bar", param) 54 } 55 56 func TestCtxParamForm(t *testing.T) { 57 assert := assert.New(t) 58 59 context := MockCtx("GET", "/", OptCtxPostFormValue("foo", "bar")) 60 param, err := context.Param("foo") 61 assert.Nil(err) 62 assert.Equal("bar", param) 63 64 param, err = context.FormValue("foo") 65 assert.Nil(err) 66 assert.Equal("bar", param) 67 } 68 69 func TestCtxParamCookie(t *testing.T) { 70 assert := assert.New(t) 71 72 context := MockCtx("GET", "/", OptCtxCookieValue("foo", "bar")) 73 param, err := context.Param("foo") 74 assert.Nil(err) 75 assert.Equal("bar", param) 76 } 77 78 func TestCtxPostBodyAsString(t *testing.T) { 79 assert := assert.New(t) 80 81 context := MockCtx("GET", "/", OptCtxBodyBytes([]byte("test payload"))) 82 body, err := context.PostBodyAsString() 83 assert.Nil(err) 84 assert.Equal("test payload", body) 85 86 context = MockCtx("GET", "/") 87 body, err = context.PostBodyAsString() 88 assert.Nil(err) 89 assert.Empty(body) 90 } 91 92 func TestCtxPostBodyAsJSON(t *testing.T) { 93 assert := assert.New(t) 94 95 context := MockCtx("GET", "/", OptCtxBodyBytes([]byte(`{"test":"test payload"}`))) 96 97 var contents map[string]interface{} 98 err := context.PostBodyAsJSON(&contents) 99 assert.Nil(err) 100 assert.Equal("test payload", contents["test"]) 101 102 context = MockCtx("GET", "/") 103 assert.Nil(err) 104 contents = make(map[string]interface{}) 105 err = context.PostBodyAsJSON(&contents) 106 assert.NotNil(err) 107 } 108 109 type postXMLTest string 110 111 func TestCtxPostBodyAsXML(t *testing.T) { 112 assert := assert.New(t) 113 114 context := MockCtx("GET", "/", OptCtxBodyBytes([]byte(`<postXMLTest>test payload</postXMLTest>`))) 115 116 var contents postXMLTest 117 err := context.PostBodyAsXML(&contents) 118 assert.Nil(err) 119 assert.Equal("test payload", string(contents)) 120 } 121 122 func TestCtxPostedFiles(t *testing.T) { 123 assert := assert.New(t) 124 125 context := MockCtx("GET", "/") 126 postedFiles, err := webutil.PostedFiles(context.Request) 127 assert.NotNil(err, "we expect this to fail if the body isn't a multipart form") 128 assert.Empty(postedFiles) 129 130 context = MockCtx("GET", "/", OptCtxPostedFiles(webutil.PostedFile{ 131 Key: "file", 132 FileName: "test.txt", 133 Contents: []byte("this is only a test"), 134 })) 135 136 postedFiles, err = webutil.PostedFiles(context.Request) 137 assert.Nil(err) 138 assert.NotEmpty(postedFiles) 139 assert.Equal("file", postedFiles[0].Key) 140 assert.Equal("test.txt", postedFiles[0].FileName) 141 assert.Equal("this is only a test", string(postedFiles[0].Contents)) 142 } 143 144 func TestCtxRouteParam(t *testing.T) { 145 assert := assert.New(t) 146 147 context := MockCtx("GET", "/", OptCtxRouteParamValue("foo", "bar")) 148 value, err := context.RouteParam("foo") 149 assert.Nil(err) 150 assert.Equal("bar", value) 151 } 152 153 func TestCtxSession(t *testing.T) { 154 assert := assert.New(t) 155 156 session := NewSession("test user", NewSessionID()) 157 ctx := MockCtx("GET", "/", OptCtxSession(session)) 158 assert.Equal(ctx.Session, session) 159 } 160 161 func TestCtxExtendCookie(t *testing.T) { 162 assert := assert.New(t) 163 164 ctx := MockCtx("GET", "/", OptCtxCookieValue("foo", "bar")) 165 ctx.ExtendCookie("foo", "/", 0, 0, 1) 166 167 cookies := ReadSetCookies(ctx.Response.Header()) 168 assert.NotEmpty(cookies) 169 cookie := cookies[0] 170 assert.False(cookie.Expires.IsZero()) 171 } 172 173 func TestCtxExtendCookieByDuration(t *testing.T) { 174 assert := assert.New(t) 175 176 ctx := MockCtx("GET", "/", OptCtxCookieValue("foo", "bar")) 177 ctx.ExtendCookieByDuration("foo", "/", time.Hour) 178 179 cookies := ReadSetCookies(ctx.Response.Header()) 180 assert.NotEmpty(cookies) 181 cookie := cookies[0] 182 assert.False(cookie.Expires.IsZero()) 183 } 184 185 type PostFormTest struct { 186 ID string `postForm:"id"` 187 Name string `postForm:"Name"` 188 Cost float64 `postForm:"notCost"` 189 Excluded string 190 } 191 192 func TestCtxPostBodyAsForm(t *testing.T) { 193 assert := assert.New(t) 194 195 formValues := url.Values{ 196 "id": []string{uuid.V4().String()}, 197 "Name": []string{"foobar"}, 198 "notCost": []string{"3.14", "6.28"}, 199 "Excluded": []string{"bad"}, 200 } 201 postBody := []byte(formValues.Encode()) 202 203 ctx := MockCtx("POST", "/") 204 ctx.Request.Header.Set(webutil.HeaderContentType, webutil.ContentTypeApplicationFormEncoded) 205 ctx.Request.Body = io.NopCloser(bytes.NewReader(postBody)) 206 207 var p PostFormTest 208 assert.Nil(ctx.PostBodyAsForm(&p)) 209 assert.Equal(formValues["id"][0], p.ID) 210 assert.Equal(formValues["Name"][0], p.Name) 211 assert.Equal(3.14, p.Cost) 212 assert.Empty(p.Excluded) 213 }