543 - update: to use updated config structure
This commit is contained in:
@@ -18,21 +18,26 @@ func (vp schemav2ValidatorProvider) New(ctx context.Context, config map[string]s
|
||||
return nil, nil, errors.New("context cannot be nil")
|
||||
}
|
||||
|
||||
url, ok := config["url"]
|
||||
if !ok || url == "" {
|
||||
return nil, nil, errors.New("url not configured")
|
||||
}
|
||||
typeVal, hasType := config["type"]
|
||||
locVal, hasLoc := config["location"]
|
||||
|
||||
cacheTTL := 3600
|
||||
if ttlStr, ok := config["cacheTTL"]; ok {
|
||||
if ttl, err := strconv.Atoi(ttlStr); err == nil && ttl > 0 {
|
||||
cacheTTL = ttl
|
||||
}
|
||||
if !hasType || typeVal == "" {
|
||||
return nil, nil, errors.New("type not configured")
|
||||
}
|
||||
if !hasLoc || locVal == "" {
|
||||
return nil, nil, errors.New("location not configured")
|
||||
}
|
||||
|
||||
cfg := &schemav2validator.Config{
|
||||
URL: url,
|
||||
CacheTTL: cacheTTL,
|
||||
Type: typeVal,
|
||||
Location: locVal,
|
||||
CacheTTL: 3600,
|
||||
}
|
||||
|
||||
if ttlStr, ok := config["cacheTTL"]; ok {
|
||||
if ttl, err := strconv.Atoi(ttlStr); err == nil && ttl > 0 {
|
||||
cfg.CacheTTL = ttl
|
||||
}
|
||||
}
|
||||
|
||||
return schemav2validator.New(ctx, cfg)
|
||||
|
||||
@@ -48,39 +48,64 @@ func TestProvider_New(t *testing.T) {
|
||||
errMsg: "context cannot be nil",
|
||||
},
|
||||
{
|
||||
name: "missing url",
|
||||
name: "missing type",
|
||||
ctx: context.Background(),
|
||||
config: map[string]string{},
|
||||
config: map[string]string{"location": server.URL},
|
||||
wantErr: true,
|
||||
errMsg: "url not configured",
|
||||
errMsg: "type not configured",
|
||||
},
|
||||
{
|
||||
name: "empty url",
|
||||
name: "missing location",
|
||||
ctx: context.Background(),
|
||||
config: map[string]string{"url": ""},
|
||||
config: map[string]string{"type": "url"},
|
||||
wantErr: true,
|
||||
errMsg: "url not configured",
|
||||
errMsg: "location not configured",
|
||||
},
|
||||
{
|
||||
name: "empty type",
|
||||
ctx: context.Background(),
|
||||
config: map[string]string{"type": "", "location": server.URL},
|
||||
wantErr: true,
|
||||
errMsg: "type not configured",
|
||||
},
|
||||
{
|
||||
name: "empty location",
|
||||
ctx: context.Background(),
|
||||
config: map[string]string{"type": "url", "location": ""},
|
||||
wantErr: true,
|
||||
errMsg: "location not configured",
|
||||
},
|
||||
{
|
||||
name: "valid config with default TTL",
|
||||
ctx: context.Background(),
|
||||
config: map[string]string{"url": server.URL},
|
||||
config: map[string]string{"type": "url", "location": server.URL},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "valid config with custom TTL",
|
||||
ctx: context.Background(),
|
||||
config: map[string]string{
|
||||
"url": server.URL,
|
||||
"type": "url",
|
||||
"location": server.URL,
|
||||
"cacheTTL": "7200",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "valid file type",
|
||||
ctx: context.Background(),
|
||||
config: map[string]string{
|
||||
"type": "file",
|
||||
"location": "/tmp/spec.yaml",
|
||||
},
|
||||
wantErr: true, // file doesn't exist
|
||||
},
|
||||
{
|
||||
name: "invalid TTL falls back to default",
|
||||
ctx: context.Background(),
|
||||
config: map[string]string{
|
||||
"url": server.URL,
|
||||
"type": "url",
|
||||
"location": server.URL,
|
||||
"cacheTTL": "invalid",
|
||||
},
|
||||
wantErr: false,
|
||||
@@ -89,7 +114,8 @@ func TestProvider_New(t *testing.T) {
|
||||
name: "negative TTL falls back to default",
|
||||
ctx: context.Background(),
|
||||
config: map[string]string{
|
||||
"url": server.URL,
|
||||
"type": "url",
|
||||
"location": server.URL,
|
||||
"cacheTTL": "-100",
|
||||
},
|
||||
wantErr: false,
|
||||
@@ -98,7 +124,8 @@ func TestProvider_New(t *testing.T) {
|
||||
name: "zero TTL falls back to default",
|
||||
ctx: context.Background(),
|
||||
config: map[string]string{
|
||||
"url": server.URL,
|
||||
"type": "url",
|
||||
"location": server.URL,
|
||||
"cacheTTL": "0",
|
||||
},
|
||||
wantErr: false,
|
||||
|
||||
Reference in New Issue
Block a user