MCPcopy Create free account
hub / github.com/chainreactors/EvilProxy / TestManagementPluginsRouteRegistered

Function TestManagementPluginsRouteRegistered

internal/api/server_test.go:236–293  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

234}
235
236func TestManagementPluginsRouteRegistered(t *testing.T) {
237 t.Setenv("MANAGEMENT_PASSWORD", "test-management-key")
238
239 server := newTestServer(t)
240 enabled := true
241 server.cfg.Plugins.Configs = map[string]proxyconfig.PluginInstanceConfig{
242 "sample": {Enabled: &enabled, Priority: 4},
243 }
244 if errWrite := os.WriteFile(server.configFilePath, []byte("{}\n"), 0o600); errWrite != nil {
245 t.Fatalf("failed to write config file: %v", errWrite)
246 }
247
248 req := httptest.NewRequest(http.MethodGet, "/v0/management/plugins", nil)
249 req.Header.Set("Authorization", "Bearer test-management-key")
250 rr := httptest.NewRecorder()
251 server.engine.ServeHTTP(rr, req)
252
253 if rr.Code != http.StatusOK {
254 t.Fatalf("status = %d, want %d body=%s", rr.Code, http.StatusOK, rr.Body.String())
255 }
256
257 var payload struct {
258 PluginsEnabled bool `json:"plugins_enabled"`
259 Plugins []any `json:"plugins"`
260 }
261 if errUnmarshal := json.Unmarshal(rr.Body.Bytes(), &payload); errUnmarshal != nil {
262 t.Fatalf("unmarshal response: %v body=%s", errUnmarshal, rr.Body.String())
263 }
264 if payload.Plugins == nil {
265 t.Fatalf("plugins field = nil, want array; body=%s", rr.Body.String())
266 }
267
268 req = httptest.NewRequest(http.MethodGet, "/v0/management/plugins/sample/config", nil)
269 req.Header.Set("Authorization", "Bearer test-management-key")
270 rr = httptest.NewRecorder()
271 server.engine.ServeHTTP(rr, req)
272 if rr.Code != http.StatusOK {
273 t.Fatalf("config status = %d, want %d body=%s", rr.Code, http.StatusOK, rr.Body.String())
274 }
275 var configPayload struct {
276 Enabled bool `json:"enabled"`
277 Priority int `json:"priority"`
278 }
279 if errUnmarshal := json.Unmarshal(rr.Body.Bytes(), &configPayload); errUnmarshal != nil {
280 t.Fatalf("unmarshal config response: %v body=%s", errUnmarshal, rr.Body.String())
281 }
282 if !configPayload.Enabled || configPayload.Priority != 4 {
283 t.Fatalf("plugin config = %#v, want enabled true priority 4", configPayload)
284 }
285
286 req = httptest.NewRequest(http.MethodDelete, "/v0/management/plugins/sample", nil)
287 req.Header.Set("Authorization", "Bearer test-management-key")
288 rr = httptest.NewRecorder()
289 server.engine.ServeHTTP(rr, req)
290 if rr.Code != http.StatusOK {
291 t.Fatalf("delete status = %d, want %d body=%s", rr.Code, http.StatusOK, rr.Body.String())
292 }
293}

Callers

nothing calls this directly

Calls 4

BytesMethod · 0.80
newTestServerFunction · 0.70
SetMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected