github.com/blend/go-sdk@v1.20240719.1/web/timeout_test.go (about) 1 /* 2 3 Copyright (c) 2024 - 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 "net/http/httptest" 13 "testing" 14 "time" 15 16 "github.com/blend/go-sdk/assert" 17 ) 18 19 func TestTimeout(t *testing.T) { 20 for _, tc := range []struct { 21 Name string 22 Timeout time.Duration 23 Action Action 24 Status int 25 }{ 26 { 27 Name: "panic", 28 Timeout: time.Minute, 29 Action: func(_ *Ctx) Result { 30 panic("test") 31 }, 32 Status: http.StatusInternalServerError, 33 }, 34 { 35 Name: "long action", 36 Timeout: time.Microsecond, 37 Action: func(r *Ctx) Result { 38 <-r.Context().Done() 39 return NoContent 40 }, 41 Status: http.StatusServiceUnavailable, 42 }, 43 { 44 Name: "short action", 45 Timeout: time.Minute, 46 Action: func(_ *Ctx) Result { 47 return NoContent 48 }, 49 Status: http.StatusNoContent, 50 }, 51 } { 52 t.Run(tc.Name, func(t *testing.T) { 53 assert := assert.New(t) 54 55 app := MustNew( 56 OptBindAddr(DefaultMockBindAddr), 57 OptUse(WithTimeout(tc.Timeout)), 58 ) 59 app.GET("/endpoint", tc.Action) 60 61 ts := httptest.NewServer(app) 62 defer ts.Close() 63 64 res, err := http.Get(ts.URL + "/endpoint") 65 assert.Nil(err) 66 assert.Nil(res.Body.Close()) 67 assert.Equal(tc.Status, res.StatusCode) 68 }) 69 } 70 }