github.com/blend/go-sdk@v1.20220411.3/web/option_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 "context" 12 "io" 13 "log" 14 "net" 15 "testing" 16 "time" 17 18 "github.com/blend/go-sdk/assert" 19 "github.com/blend/go-sdk/logger" 20 "github.com/blend/go-sdk/webutil" 21 ) 22 23 func TestOptConfig(t *testing.T) { 24 assert := assert.New(t) 25 26 cfg := Config{ 27 DefaultHeaders: map[string]string{"X-Debug": "debug-value"}, 28 CookieName: "FOOBAR", 29 } 30 31 var app App 32 assert.Nil(OptConfig(cfg)(&app)) 33 assert.Equal("FOOBAR", app.Auth.CookieDefaults.Name) 34 assert.NotEmpty(app.BaseHeaders) 35 assert.Equal([]string{"debug-value"}, app.BaseHeaders["X-Debug"]) 36 assert.Equal([]string{PackageName}, app.BaseHeaders[webutil.HeaderServer]) 37 } 38 39 func TestOptBindAddr(t *testing.T) { 40 assert := assert.New(t) 41 42 var app App 43 assert.Nil(OptBindAddr(":9999")(&app)) 44 assert.Equal(":9999", app.Config.BindAddr) 45 } 46 47 func TestOptPort(t *testing.T) { 48 assert := assert.New(t) 49 50 var app App 51 assert.Nil(OptPort(9999)(&app)) 52 assert.Equal(":9999", app.Config.BindAddr) 53 assert.Equal(9999, app.Config.Port) 54 } 55 56 func TestOptBaseContext(t *testing.T) { 57 t.Parallel() 58 it := assert.New(t) 59 60 ctx := context.TODO() 61 opt := OptBaseContext(ctx) 62 a := App{} 63 err := opt(&a) 64 it.Nil(err) 65 root := a.BaseContext(nil) 66 it.ReferenceEqual(ctx, root) 67 } 68 69 func TestOptBaseContextFunc(t *testing.T) { 70 t.Parallel() 71 it := assert.New(t) 72 73 ctx := context.TODO() 74 bc := func(_ net.Listener) context.Context { 75 return ctx 76 } 77 opt := OptBaseContextFunc(bc) 78 a := App{} 79 err := opt(&a) 80 it.Nil(err) 81 root := a.BaseContext(nil) 82 it.ReferenceEqual(ctx, root) 83 } 84 85 func TestOptLog(t *testing.T) { 86 assert := assert.New(t) 87 88 var app App 89 assert.Nil(OptLog(logger.None())(&app)) 90 assert.NotNil(app.Log) 91 } 92 93 func TestOptServerOptions(t *testing.T) { 94 assert := assert.New(t) 95 96 baseline, baselineErr := New() 97 assert.Nil(baselineErr) 98 assert.NotNil(baseline) 99 assert.NotNil(baseline.Server) 100 assert.Nil(baseline.Server.ErrorLog) 101 102 app, err := New( 103 OptBindAddr("127.0.0.1:0"), 104 OptServerOptions( 105 webutil.OptHTTPServerErrorLog(log.New(io.Discard, "", log.LstdFlags)), 106 ), 107 ) 108 assert.Nil(err) 109 assert.NotNil(app.Server.ErrorLog) 110 111 go func() { _ = app.Start() }() 112 <-app.NotifyStarted() 113 defer func() { _ = app.Stop() }() 114 115 assert.NotNil(app.Server.ErrorLog) 116 } 117 118 func TestOptReadTimeout(t *testing.T) { 119 assert := assert.New(t) 120 121 var app App 122 assert.Zero(app.Config.ReadTimeout) 123 assert.Nil(OptReadTimeout(time.Second)(&app)) 124 assert.Equal(time.Second, app.Config.ReadTimeout) 125 } 126 127 func TestOptReadHeaderTimeout(t *testing.T) { 128 assert := assert.New(t) 129 130 var app App 131 assert.Zero(app.Config.ReadHeaderTimeout) 132 assert.Nil(OptReadHeaderTimeout(time.Second)(&app)) 133 assert.Equal(time.Second, app.Config.ReadHeaderTimeout) 134 } 135 136 func TestOptWriteTimeout(t *testing.T) { 137 assert := assert.New(t) 138 139 var app App 140 assert.Zero(app.Config.WriteTimeout) 141 assert.Nil(OptWriteTimeout(time.Second)(&app)) 142 assert.Equal(time.Second, app.Config.WriteTimeout) 143 } 144 145 func TestOptIdleTimeout(t *testing.T) { 146 assert := assert.New(t) 147 148 var app App 149 assert.Zero(app.Config.IdleTimeout) 150 assert.Nil(OptIdleTimeout(time.Second)(&app)) 151 assert.Equal(time.Second, app.Config.IdleTimeout) 152 } 153 154 func TestOptMaxHeaderBytes(t *testing.T) { 155 assert := assert.New(t) 156 157 var app App 158 assert.Zero(app.Config.MaxHeaderBytes) 159 assert.Nil(OptMaxHeaderBytes(100)(&app)) 160 assert.Equal(100, app.Config.MaxHeaderBytes) 161 } 162 163 func TestOptBaseURL(t *testing.T) { 164 assert := assert.New(t) 165 166 var app App 167 assert.Empty(app.Config.BaseURL) 168 assert.Nil(OptBaseURL("https://example.local")(&app)) 169 assert.Equal("https://example.local", app.Config.BaseURL) 170 }