-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswagger_internal_test.go
More file actions
75 lines (65 loc) · 1.64 KB
/
swagger_internal_test.go
File metadata and controls
75 lines (65 loc) · 1.64 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
// SPDX-License-Identifier: EUPL-1.2
package api
import (
"encoding/json"
"testing"
"github.com/gin-gonic/gin"
)
type swaggerSnapshotGroup struct {
name string
basePath string
descs []RouteDescription
}
func (g *swaggerSnapshotGroup) Name() string { return g.name }
func (g *swaggerSnapshotGroup) BasePath() string { return g.basePath }
func (g *swaggerSnapshotGroup) RegisterRoutes(_ *gin.RouterGroup) {}
func (g *swaggerSnapshotGroup) Describe() []RouteDescription {
return g.descs
}
func TestSwaggerSpec_ReadDoc_Good_SnapshotsGroups(t *testing.T) {
original := &swaggerSnapshotGroup{
name: "first",
basePath: "/first",
descs: []RouteDescription{
{
Method: "GET",
Path: "/ping",
Summary: "First group",
Response: map[string]any{
"type": "string",
},
},
},
}
replacement := &swaggerSnapshotGroup{
name: "second",
basePath: "/second",
descs: []RouteDescription{
{
Method: "GET",
Path: "/pong",
Summary: "Second group",
Response: map[string]any{
"type": "string",
},
},
},
}
groups := []RouteGroup{original}
spec := newSwaggerSpec(&SpecBuilder{
Title: "Test",
Version: "1.0.0",
}, groups)
groups[0] = replacement
var doc map[string]any
if err := json.Unmarshal([]byte(spec.ReadDoc()), &doc); err != nil {
t.Fatalf("invalid JSON: %v", err)
}
paths := doc["paths"].(map[string]any)
if _, ok := paths["/first/ping"]; !ok {
t.Fatal("expected original group path to remain in the spec")
}
if _, ok := paths["/second/pong"]; ok {
t.Fatal("did not expect mutated group path to leak into the spec")
}
}