go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/web/config_test.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package web 9 10 import ( 11 "context" 12 "net/http" 13 "testing" 14 "time" 15 16 . "go.charczuk.com/sdk/assert" 17 "go.charczuk.com/sdk/configutil" 18 ) 19 20 func Test_Config_Resolve_setAddrFromEnv(t *testing.T) { 21 ctx := configutil.WithEnvVars(context.Background(), map[string]string{ 22 "BIND_ADDR": "127.0.0.1:8080", 23 "PORT": "8080", 24 }) 25 26 var cfg Config 27 err := cfg.Resolve(ctx) 28 ItsNil(t, err) 29 ItsEqual(t, "127.0.0.1:8080", cfg.Addr) 30 } 31 32 func Test_Config_Resolve_setAddrFromPort(t *testing.T) { 33 ctx := configutil.WithEnvVars(context.Background(), map[string]string{ 34 "PORT": "8080", 35 }) 36 37 var cfg Config 38 err := cfg.Resolve(ctx) 39 ItsNil(t, err) 40 ItsEqual(t, ":8080", cfg.Addr) 41 } 42 43 func Test_Config_ApplyTo(t *testing.T) { 44 cfg := Config{ 45 BaseURL: "https://example.com", 46 ShutdownGracePeriod: 30 * time.Second, 47 Addr: ":9000", 48 MaxHeaderBytes: 32 * 1024, 49 ReadTimeout: 5 * time.Second, 50 ReadHeaderTimeout: 2 * time.Second, 51 WriteTimeout: 10 * time.Second, 52 IdleTimeout: 45 * time.Second, 53 Headers: map[string]string{ 54 "Foo": "foo-value", 55 "Bar": "bar-value", 56 }, 57 } 58 a := new(App) 59 a.Headers = http.Header{ 60 "Moo": {"moo-value"}, 61 } 62 cfg.ApplyTo(a) 63 64 ItsEqual(t, "https://example.com", a.BaseURL) 65 ItsEqual(t, 30*time.Second, a.ShutdownGracePeriod) 66 ItsEqual(t, ":9000", a.Addr) 67 ItsEqual(t, 32*1024, a.MaxHeaderBytes) 68 ItsEqual(t, 5*time.Second, a.ReadTimeout) 69 ItsEqual(t, 2*time.Second, a.ReadHeaderTimeout) 70 ItsEqual(t, 10*time.Second, a.WriteTimeout) 71 ItsEqual(t, 45*time.Second, a.IdleTimeout) 72 }