github.com/avenga/couper@v1.12.2/config/runtime/server/options_test.go (about) 1 package server_test 2 3 import ( 4 "reflect" 5 "strings" 6 "testing" 7 8 "github.com/avenga/couper/config" 9 "github.com/avenga/couper/config/runtime/server" 10 "github.com/avenga/couper/errors" 11 ) 12 13 func TestServer_NewServerOptions_NoConfig(t *testing.T) { 14 options, err := server.NewServerOptions(nil, nil) 15 if err != nil { 16 t.Errorf("Unexpected error given: %#v", err) 17 } 18 19 exp := &server.Options{ 20 APIErrTpls: map[*config.API]*errors.Template(nil), 21 ServerErrTpl: errors.DefaultHTML, 22 APIBasePaths: map[*config.API]string(nil), 23 SrvBasePath: "", 24 ServerName: "", 25 } 26 if !reflect.DeepEqual(options, exp) { 27 t.Errorf("want\n%#v\ngot\n%#v", exp, options) 28 } 29 } 30 31 func TestServer_NewServerOptions_EmptyConfig(t *testing.T) { 32 conf := &config.Server{} 33 34 options, err := server.NewServerOptions(conf, nil) 35 if err != nil { 36 t.Errorf("Unexpected error given: %#v", err) 37 } 38 39 exp := &server.Options{ 40 APIErrTpls: map[*config.API]*errors.Template(nil), 41 FilesErrTpls: []*errors.Template{}, 42 ServerErrTpl: errors.DefaultHTML, 43 APIBasePaths: map[*config.API]string(nil), 44 FilesBasePaths: []string{}, 45 SrvBasePath: "/", 46 ServerName: "", 47 } 48 if !reflect.DeepEqual(options, exp) { 49 t.Errorf("want\n%#v\ngot\n%#v", exp, options) 50 } 51 } 52 53 func TestServer_NewServerOptions_ConfigWithPaths(t *testing.T) { 54 api1 := &config.API{ 55 BasePath: "/api/v1", 56 } 57 api2 := &config.API{ 58 BasePath: "/api/v2", 59 } 60 61 abps := make(map[*config.API]string) 62 abps[api1] = "/server/api/v1" 63 abps[api2] = "/server/api/v2" 64 65 aets := make(map[*config.API]*errors.Template) 66 aets[api1] = errors.DefaultJSON 67 aets[api2] = errors.DefaultJSON 68 69 conf := &config.Server{ 70 BasePath: "/server", 71 Name: "ServerName", 72 73 Files: []*config.Files{{ 74 BasePath: "/files", 75 }}, 76 SPAs: []*config.Spa{{ 77 BasePath: "/spa", 78 }}, 79 APIs: config.APIs{ 80 api1, api2, 81 }, 82 } 83 84 options, err := server.NewServerOptions(conf, nil) 85 if err != nil { 86 t.Errorf("Unexpected error given: %#v", err) 87 } 88 89 exp := &server.Options{ 90 APIErrTpls: aets, 91 FilesErrTpls: []*errors.Template{errors.DefaultHTML}, 92 ServerErrTpl: errors.DefaultHTML, 93 APIBasePaths: abps, 94 FilesBasePaths: []string{"/server/files"}, 95 SPABasePaths: []string{"/server/spa"}, 96 SrvBasePath: "/server", 97 ServerName: "ServerName", 98 } 99 if !reflect.DeepEqual(options, exp) { 100 t.Errorf("want\n%#v\ngot\n%#v", exp, options) 101 } 102 } 103 104 func TestServer_NewServerOptions_MissingErrTplFile(t *testing.T) { 105 for _, testcase := range []*config.Server{{ 106 ErrorFile: "not-there", 107 }, { 108 Files: []*config.Files{{ 109 ErrorFile: "not-there", 110 }}, 111 }, { 112 APIs: config.APIs{ 113 {ErrorFile: "not-there"}, 114 }, 115 }, 116 } { 117 _, err := server.NewServerOptions(testcase, nil) 118 if err == nil || !strings.Contains(err.Error(), "no such file or directory") { 119 t.Errorf("Unexpected error given: %#v", err) 120 } 121 } 122 } 123 124 func TestServer_NewServerOptions_ConfigWithErrTpl_Valid(t *testing.T) { 125 conf := &config.Server{ 126 ErrorFile: "testdata/error.file", 127 Files: []*config.Files{{ 128 ErrorFile: "testdata/error.file", 129 }}, 130 APIs: config.APIs{ 131 {ErrorFile: "testdata/error.file"}, 132 }, 133 } 134 135 _, err := server.NewServerOptions(conf, nil) 136 if err != nil { 137 t.Error("Unexpected error given") 138 } 139 }