MCPcopy Index your code
hub / github.com/coder/coder / startFakeAgentAPI

Function startFakeAgentAPI

cli/task_test.go:525–573  ·  view source on GitHub ↗

startFakeAgentAPI starts an HTTP server that implements the AgentAPI endpoints. handlers is a map of path -> handler function.

(t *testing.T, handlers map[string]http.HandlerFunc)

Source from the content-addressed store, hash-verified

523// startFakeAgentAPI starts an HTTP server that implements the AgentAPI endpoints.
524// handlers is a map of path -> handler function.
525func startFakeAgentAPI(t *testing.T, handlers map[string]http.HandlerFunc) *fakeAgentAPI {
526 t.Helper()
527
528 fake := &fakeAgentAPI{
529 t: t,
530 handlers: handlers,
531 called: make(map[string]bool),
532 }
533
534 mux := http.NewServeMux()
535
536 // Register all provided handlers with call tracking
537 for path, handler := range handlers {
538 mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
539 fake.mu.Lock()
540 fake.called[path] = true
541 fake.mu.Unlock()
542 handler(w, r)
543 })
544 }
545
546 knownEndpoints := []string{"/status", "/messages", "/message"}
547 for _, endpoint := range knownEndpoints {
548 if handlers[endpoint] == nil {
549 endpoint := endpoint // capture loop variable
550 mux.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) {
551 t.Fatalf("unexpected call to %s %s - no handler defined", r.Method, endpoint)
552 })
553 }
554 }
555 // Default handler for unknown endpoints should cause the test to fail.
556 mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
557 t.Fatalf("unexpected call to %s %s - no handler defined", r.Method, r.URL.Path)
558 })
559
560 fake.server = httptest.NewServer(mux)
561
562 // Register cleanup to check that all defined handlers were called
563 t.Cleanup(func() {
564 fake.server.Close()
565 fake.mu.Lock()
566 for path := range handlers {
567 if !fake.called[path] {
568 t.Errorf("handler for %s was defined but never called", path)
569 }
570 }
571 })
572 return fake
573}
574
575func (f *fakeAgentAPI) URL() string {
576 return f.server.URL

Callers 2

Test_TasksFunction · 0.85
setupCLITaskTestFunction · 0.85

Calls 8

HelperMethod · 0.65
CleanupMethod · 0.65
CloseMethod · 0.65
handlerFunction · 0.50
LockMethod · 0.45
UnlockMethod · 0.45
FatalfMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected