-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathschema_loader_test.go
More file actions
55 lines (45 loc) · 2.17 KB
/
Copy pathschema_loader_test.go
File metadata and controls
55 lines (45 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package validate
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-openapi/spec"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag/loading"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)
// TestSchemaValidator_WithPathLoader verifies that a document loader injected through
// WithPathLoader is the one used to resolve a remote $ref when building a schema validator — the
// hook a caller uses to confine loading when validating input from an untrusted source.
func TestSchemaValidator_WithPathLoader(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{"type":"string"}`))
}))
defer srv.Close()
var calls int
loader := func(pth string, opts ...loading.Option) (json.RawMessage, error) {
calls++
return loading.LoadFromFileOrHTTP(pth, opts...)
}
schema := &spec.Schema{SchemaProps: spec.SchemaProps{Ref: spec.MustCreateRef(srv.URL + "/schema.json")}}
v := NewSchemaValidator(schema, nil, "", strfmt.Default, WithPathLoader(loader))
require.NotNil(t, v)
assert.TrueT(t, calls > 0, "expected the injected loader to resolve the remote $ref")
}
// TestSpecValidator_WithPathLoader verifies that WithPathLoader passed to the spec validator
// constructors reaches the schema options consumed by both the schema validation and the $ref
// resolution ($ref resolveRef) performed during spec validation.
func TestSpecValidator_WithPathLoader(t *testing.T) {
loader := func(string, ...loading.Option) (json.RawMessage, error) { return nil, nil }
s := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default, WithPathLoader(loader))
require.NotNil(t, s.schemaOptions)
require.NotNil(t, s.schemaOptions.pathLoaderWithOptions,
"WithPathLoader must reach the spec validator's schema options (used by resolveRef and schema validation)")
// built-in defaults are still applied alongside the injected option
assert.TrueT(t, s.schemaOptions.EnableObjectArrayTypeCheck)
assert.TrueT(t, s.schemaOptions.recycleValidators)
}