-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathendpoint_request_test.go
More file actions
91 lines (80 loc) · 2.34 KB
/
Copy pathendpoint_request_test.go
File metadata and controls
91 lines (80 loc) · 2.34 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
package envoy_mcp_openapi_processor
import (
"testing"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewEndpointRequest_BuildsRequestFromArguments(t *testing.T) {
endpoint := endpoint{
Method: "post",
PathTemplate: "/users/{id}",
ContentType: "application/json",
Parameters: map[string]any{
"id": map[string]any{
"in": openapi3.ParameterInPath,
"required": true,
},
"status": map[string]any{
"in": openapi3.ParameterInQuery,
},
"role": map[string]any{
"in": openapi3.ParameterInQuery,
},
"x-trace-id": map[string]any{
"in": openapi3.ParameterInHeader,
},
},
}
request, err := newEndpointRequest(endpoint, map[string]any{
"id": "123",
"role": []any{"admin", "editor"},
"status": "active",
"x-trace-id": "trace-1",
})
require.Nil(t, err)
require.NotNil(t, request)
assert.Equal(t, "/users/123?role=admin&role=editor&status=active", request.fullPath())
assert.Equal(t, "*/*", request.extraHeaders["accept"])
assert.Equal(t, "application/json", request.extraHeaders["content-type"])
assert.Equal(t, "trace-1", request.extraHeaders["x-trace-id"])
}
func TestNewEndpointRequest_ReturnsErrorForMissingRequiredArgument(t *testing.T) {
endpoint := endpoint{
PathTemplate: "/users/{id}",
Parameters: map[string]any{
"id": map[string]any{
"in": openapi3.ParameterInPath,
"required": true,
},
},
}
request, err := newEndpointRequest(endpoint, map[string]any{})
assert.Nil(t, request)
require.NotNil(t, err)
assert.Equal(t, openapi3.ParameterInPath, err.paramIn)
assert.Equal(t, "id", err.paramName)
assert.Equal(t, "required but missing", err.reason)
}
func TestNewEndpointRequest_ReturnsErrorForNestedJSONHeaderArgument(t *testing.T) {
endpoint := endpoint{
PathTemplate: "/users",
Parameters: map[string]any{
"x-metadata": map[string]any{
"in": openapi3.ParameterInHeader,
},
},
}
request, err := newEndpointRequest(endpoint, map[string]any{
"x-metadata": map[string]any{
"nested": map[string]any{
"property": "test",
},
},
})
assert.Nil(t, request)
require.NotNil(t, err)
assert.Equal(t, openapi3.ParameterInHeader, err.paramIn)
assert.Equal(t, "x-metadata", err.paramName)
assert.Equal(t, "must be a scalar value", err.reason)
}