add mock for testing region pins with API (#4691)

we'll ensure that clients support redirection when making an API
to an unpinned region
This commit is contained in:
David Zhao
2026-07-19 21:12:35 -07:00
committed by GitHub
parent 366cadcd96
commit 3f59d0dd9e
4 changed files with 71 additions and 1 deletions
+29 -1
View File
@@ -112,7 +112,14 @@ func (h *mockHandler) handleRegions(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(cfg.RegionsStatus)
return
}
body, err := protojson.Marshal(h.regions)
regions := h.regions
if len(cfg.PinnedRegions) > 0 {
// A pinned project's discovery returns only its allowed regions, so the
// client redirects straight to one after a 451 — mirroring cloud filtering
// /settings/regions by the project's PinnedRegions.
regions = pinnedRegionSettings(h.regions, cfg.PinnedRegions)
}
body, err := protojson.Marshal(regions)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
@@ -127,6 +134,27 @@ func (h *mockHandler) handleTwirp(w http.ResponseWriter, r *http.Request) {
h.serveAPI(w, r)
}
// regionName is the advertised name of the region this listener represents.
func (h *mockHandler) regionName() string {
if h.regionIndex < len(h.regions.Regions) {
return h.regions.Regions[h.regionIndex].Region
}
return ""
}
// pinnedRegionSettings returns only the regions whose name appears in pinned,
// mirroring how cloud filters /settings/regions to a project's PinnedRegions.
// Names outside the region list are ignored.
func pinnedRegionSettings(all *livekit.RegionSettings, pinned []string) *livekit.RegionSettings {
out := &livekit.RegionSettings{}
for _, region := range all.Regions {
if slices.Contains(pinned, region.Region) {
out.Regions = append(out.Regions, region)
}
}
return out
}
func (h *mockHandler) shouldFail(cfg *mockConfig) bool {
return slices.Contains(cfg.FailRegions, h.regionIndex)
}