-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_test.go
More file actions
100 lines (83 loc) · 2.74 KB
/
export_test.go
File metadata and controls
100 lines (83 loc) · 2.74 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package api
import "reflect"
// Test-only exports for internal functions.
var (
HasParamTags = hasParamTags
HasFormTags = hasFormTags
HasBodyField = hasBodyField
HasRawRequest = hasRawRequest
TagOptions = tagOptions
TagContains = tagContains
TypeToSchema = typeToSchema
StructToSchema = structToSchema
JSONFieldName = jsonFieldName
ApplyConstraintTags = applyConstraintTags
ErrorResponseSchema = errorResponseSchema
ErrorSchemaName = errorSchemaName
ValidateConstraints = validateConstraints
GenerateOperationID = generateOperationID
WriteEvent = writeEvent
)
// BuildResponseDescriptor exposes the internal descriptor builder to tests,
// wrapped so the external test package can inspect it without importing
// unexported types.
func BuildResponseDescriptor(t reflect.Type) (*ResponseDescriptor, error) {
d, err := buildResponseDescriptor(t)
if err != nil {
return nil, err
}
return &ResponseDescriptor{desc: d}, nil
}
// Exported body kind constants for tests. BodyKindNone is a test-only
// sentinel meaning "no Body field on the response type."
const (
BodyKindNone BodyKind = -1
BodyKindCodec = bodyKindCodec
BodyKindReader = bodyKindReader
BodyKindChan = bodyKindChan
)
// BodyKind is the exported name for bodyKind in tests.
type BodyKind = bodyKind
// ResponseDescriptor is the exported wrapper for tests.
type ResponseDescriptor struct {
desc *responseDescriptor
}
// HasStatus reports whether the descriptor tracks a status field.
func (r *ResponseDescriptor) HasStatus() bool { return r.desc.status != nil }
// HeaderNames returns the ordered header names the descriptor emits.
func (r *ResponseDescriptor) HeaderNames() []string {
out := make([]string, len(r.desc.headers))
for i, h := range r.desc.headers {
out[i] = h.name
}
return out
}
// CookieNames returns the ordered cookie names the descriptor emits.
func (r *ResponseDescriptor) CookieNames() []string {
out := make([]string, len(r.desc.cookies))
for i, c := range r.desc.cookies {
out[i] = c.name
}
return out
}
// BodyKind returns the body emission kind, or BodyKindNone if no Body field.
func (r *ResponseDescriptor) BodyKind() BodyKind {
if r.desc.body == nil {
return BodyKindNone
}
return r.desc.body.kind
}
// TestSchemaRegistry wraps schemaRegistry for external tests.
type TestSchemaRegistry struct {
reg *schemaRegistry
Defs map[string]JSONSchema
}
// NewSchemaRegistry creates a TestSchemaRegistry for testing.
func NewSchemaRegistry() *TestSchemaRegistry {
r := newSchemaRegistry()
return &TestSchemaRegistry{reg: r, Defs: r.defs}
}
// TypeToSchema delegates to the internal registry.
func (t *TestSchemaRegistry) TypeToSchema(typ reflect.Type) JSONSchema {
return t.reg.typeToSchema(typ)
}