MCPcopy Index your code
hub / github.com/tailscale/tailscale / mockLocalAPI

Function mockLocalAPI

client/web/web_test.go:1450–1492  ·  view source on GitHub ↗

mockLocalAPI constructs a test localapi handler that can be used to simulate localapi responses without a functioning tailnet. self accepts a function that resolves to a self node status, so that tests may swap out the /localapi/v0/status response as desired.

(t *testing.T, whoIs map[string]*apitype.WhoIsResponse, self func() *ipnstate.PeerStatus, prefs func() *ipn.Prefs, metricCapture func(string))

Source from the content-addressed store, hash-verified

1448// so that tests may swap out the /localapi/v0/status response
1449// as desired.
1450func mockLocalAPI(t *testing.T, whoIs map[string]*apitype.WhoIsResponse, self func() *ipnstate.PeerStatus, prefs func() *ipn.Prefs, metricCapture func(string)) *http.Server {
1451 return &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1452 switch r.URL.Path {
1453 case "/localapi/v0/whois":
1454 addr := r.URL.Query().Get("addr")
1455 if addr == "" {
1456 t.Fatalf("/whois call missing \"addr\" query")
1457 }
1458 if node := whoIs[addr]; node != nil {
1459 writeJSON(w, &node)
1460 return
1461 }
1462 http.Error(w, "not a node", http.StatusUnauthorized)
1463 return
1464 case "/localapi/v0/status":
1465 writeJSON(w, ipnstate.Status{Self: self()})
1466 return
1467 case "/localapi/v0/prefs":
1468 writeJSON(w, prefs())
1469 return
1470 case "/localapi/v0/upload-client-metrics":
1471 type metricName struct {
1472 Name string `json:"name"`
1473 }
1474
1475 var metricNames []metricName
1476 if err := json.NewDecoder(r.Body).Decode(&metricNames); err != nil {
1477 http.Error(w, "invalid JSON body", http.StatusBadRequest)
1478 return
1479 }
1480 if metricCapture != nil && len(metricNames) > 0 {
1481 metricCapture(metricNames[0].Name)
1482 }
1483 writeJSON(w, struct{}{})
1484 return
1485 case "/localapi/v0/logout":
1486 fmt.Fprintf(w, "success")
1487 return
1488 default:
1489 t.Fatalf("unhandled localapi test endpoint %q, add to localapi handler func in test", r.URL.Path)
1490 }
1491 })}
1492}
1493
1494func mockNewAuthURL(_ context.Context, src tailcfg.NodeID) (*tailcfg.WebClientAuthResponse, error) {
1495 // Create new dummy auth URL.

Callers 6

TestServeAPIFunction · 0.85
TestAuthorizeRequestFunction · 0.85
TestServeAuthFunction · 0.85
TestRequireTailscaleIPFunction · 0.85

Calls 7

writeJSONFunction · 0.70
GetMethod · 0.65
FatalfMethod · 0.65
ErrorMethod · 0.65
prefsStruct · 0.50
QueryMethod · 0.45
DecodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…