@@ -2,9 +2,11 @@ package param_test
22
33import (
44 "context"
5+ "errors"
56 "flag"
67 "go-http-server/param"
78 "path/filepath"
9+ "reflect"
810 "testing"
911
1012 "github.com/urfave/cli/v2"
@@ -20,9 +22,12 @@ func TestContextToParams(t *testing.T) {
2022 e_directory := "example dir"
2123 e_cache_control_max_age := int64 (2048 )
2224 e_spa := true
23- e_ignore_cache_control_paths := "example path1"
25+ e_ignore_cache_control_paths := [] string { "example path1" , "example path2" }
2426 e_cache := true
2527 e_cache_buffer := 64
28+ e_logger := true
29+ e_log_pretty := true
30+ e_no_compress := []string {".map" , ".zip" }
2631
2732 f .String ("address" , e_adress , "" )
2833 f .Int ("port" , e_port , "" )
@@ -32,9 +37,12 @@ func TestContextToParams(t *testing.T) {
3237 f .String ("directory" , e_directory , "" )
3338 f .Int64 ("cache-max-age" , e_cache_control_max_age , "" )
3439 f .Bool ("spa" , e_spa , "" )
35- f .String ( "ignore-cache-control-paths" , e_ignore_cache_control_paths , "" )
40+ f .Var ( cli . NewStringSlice ( e_ignore_cache_control_paths ... ), "ignore-cache-control-paths" , "" )
3641 f .Bool ("cache" , e_cache , "" )
3742 f .Int ("cache-buffer" , e_cache_buffer , "" )
43+ f .Bool ("logger" , e_logger , "" )
44+ f .Bool ("log-pretty" , e_log_pretty , "" )
45+ f .Var (cli .NewStringSlice (e_no_compress ... ), "no-compress" , "" )
3846
3947 ctx := cli .NewContext (nil , f , nil )
4048 ctx .Context = context .WithValue (context .Background (), "key" , "val" )
@@ -77,11 +85,9 @@ func TestContextToParams(t *testing.T) {
7785 t .Errorf ("Got %t, expected %t" , params .SpaMode , e_spa )
7886 }
7987
80- //TODO
81- // fmt.Println(params.IgnoreCacheControlPaths)
82- // if params.IgnoreCacheControlPaths[0] != e_ignore_cache_control_paths {
83- // t.Errorf("Got %s, expected %s", params.IgnoreCacheControlPaths, e_ignore_cache_control_paths)
84- // }
88+ if ! reflect .DeepEqual (params .IgnoreCacheControlPaths , e_ignore_cache_control_paths ) {
89+ t .Errorf ("Got %v, expected %v" , params .IgnoreCacheControlPaths , e_ignore_cache_control_paths )
90+ }
8591
8692 if params .CacheEnabled != e_cache {
8793 t .Errorf ("Got %t, expected %t" , params .CacheEnabled , e_cache )
@@ -90,4 +96,38 @@ func TestContextToParams(t *testing.T) {
9096 if params .CacheBuffer != e_cache_buffer {
9197 t .Errorf ("Got %d, expected %d" , params .CacheBuffer , e_cache_buffer )
9298 }
99+
100+ if params .Logger != e_logger {
101+ t .Errorf ("Got %t, expected %t" , params .Logger , e_logger )
102+ }
103+
104+ if params .LogPretty != e_log_pretty {
105+ t .Errorf ("Got %t, expected %t" , params .LogPretty , e_log_pretty )
106+ }
107+
108+ if ! reflect .DeepEqual (params .NoCompress , e_no_compress ) {
109+ t .Errorf ("Got %v, expected %v" , params .NoCompress , e_no_compress )
110+ }
111+ }
112+
113+ func TestContextToParamsWithAbsError (t * testing.T ) {
114+ f := flag .NewFlagSet ("a" , flag .ContinueOnError )
115+ f .String ("directory" , "example" , "" )
116+
117+ ctx := cli .NewContext (nil , f , nil )
118+ ctx .Context = context .WithValue (context .Background (), "key" , "val" )
119+
120+ expectedErr := errors .New ("abs failed" )
121+ params , err := param .ContextToParamsWithAbs (ctx , func (_ string ) (string , error ) {
122+ return "" , expectedErr
123+ })
124+ if err == nil {
125+ t .Fatalf ("Expected error, got nil" )
126+ }
127+ if ! errors .Is (err , expectedErr ) {
128+ t .Fatalf ("Expected error %v, got %v" , expectedErr , err )
129+ }
130+ if params != nil {
131+ t .Fatalf ("Expected params to be nil on error, got %v" , params )
132+ }
93133}
0 commit comments