This repository was archived by the owner on Aug 20, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcurl_test.go
More file actions
164 lines (141 loc) · 3.91 KB
/
Copy pathcurl_test.go
File metadata and controls
164 lines (141 loc) · 3.91 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package mcptools
import (
"context"
"encoding/json"
"os/exec"
"testing"
"github.com/shaharia-lab/goai"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestNewCurl(t *testing.T) {
tests := []struct {
name string
config CurlConfig
expectedBlocked []string
}{
{
name: "Empty blocked methods",
config: CurlConfig{
BlockedMethods: []string{},
},
expectedBlocked: []string{},
},
{
name: "Multiple blocked methods",
config: CurlConfig{
BlockedMethods: []string{"POST", "delete", "PUT"},
},
expectedBlocked: []string{"POST", "DELETE", "PUT"},
},
{
name: "Case normalization",
config: CurlConfig{
BlockedMethods: []string{"post", "Delete", "PUT"},
},
expectedBlocked: []string{"POST", "DELETE", "PUT"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockLogger := new(MockLogger)
mockLogger.On("WithFields", mock.Anything).Return(mockLogger)
curl := NewCurl(mockLogger, tt.config)
assert.NotNil(t, curl)
assert.Equal(t, len(tt.expectedBlocked), len(curl.blockedMethods))
for i, method := range curl.blockedMethods {
assert.Equal(t, tt.expectedBlocked[i], method)
}
})
}
}
func TestCurl_isMethodBlocked(t *testing.T) {
tests := []struct {
name string
blockedMethods []string
testMethod string
expected bool
}{
{
name: "Blocked method - exact match",
blockedMethods: []string{"POST", "DELETE"},
testMethod: "POST",
expected: true,
},
{
name: "Blocked method - case insensitive",
blockedMethods: []string{"POST", "DELETE"},
testMethod: "post",
expected: true,
},
{
name: "Not blocked method",
blockedMethods: []string{"POST", "DELETE"},
testMethod: "GET",
expected: false,
},
{
name: "Empty blocked list",
blockedMethods: []string{},
testMethod: "POST",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockLogger := new(MockLogger)
curl := NewCurl(mockLogger, CurlConfig{BlockedMethods: tt.blockedMethods})
result := curl.isMethodBlocked(tt.testMethod)
assert.Equal(t, tt.expected, result)
})
}
}
// MockCommandExecutor implements CommandExecutor for testing
type MockCommandExecutor struct {
mock.Mock
}
func (m *MockCommandExecutor) ExecuteCommand(ctx context.Context, cmd *exec.Cmd) ([]byte, error) {
args := m.Called(ctx, cmd)
return args.Get(0).([]byte), args.Error(1)
}
// curl_test.go
func TestCurl_CurlAllInOneTool(t *testing.T) {
mockLogger := new(MockLogger)
mockExecutor := new(MockCommandExecutor)
// Set up mock expectations for logger
mockLogger.On("WithFields", mock.Anything).Return(mockLogger)
mockLogger.On("Info", mock.Anything).Return()
// Set up mock expectations for executor
mockExecutor.On("ExecuteCommand", mock.Anything, mock.Anything).Return(
[]byte("mock response"), nil,
)
curl := NewCurl(mockLogger, CurlConfig{BlockedMethods: []string{"DELETE"}})
curl.cmdExecutor = mockExecutor
tool := curl.CurlAllInOneTool()
// Test tool metadata
assert.Equal(t, CurlToolName, tool.Name)
assert.NotEmpty(t, tool.Description)
// Validate input schema
var schema map[string]interface{}
err := json.Unmarshal(tool.InputSchema, &schema)
assert.NoError(t, err)
assert.Equal(t, "object", schema["type"])
// Test handler with valid input
validInput := map[string]interface{}{
"url": "https://api.example.com",
"method": "GET",
}
inputJSON, _ := json.Marshal(validInput)
ctx := context.Background()
result, err := tool.Handler(ctx, goai.CallToolParams{
Name: CurlToolName,
Arguments: inputJSON,
})
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, []goai.ToolResultContent{
{Type: "text", Text: "mock response"},
}, result.Content)
mockLogger.AssertExpectations(t)
mockExecutor.AssertExpectations(t)
}