package products import ( "fmt" "net/http" "net/url" "testing" "git.t5de.dev/t5de/t5de-proxy/internal/proxy" ) func TestGuard(t *testing.T) { h, _ := newHarness(t) local := h.p.byProductID("blue-coat").local g := Guard{Products: h.p} check := func(rawURL, body string, refused bool) { t.Helper() u, _ := url.Parse(rawURL) req, _ := http.NewRequest("POST", rawURL, nil) x := &proxy.Exchange{URL: u, Request: req, Body: []byte(body)} g.Intercept(x) if (x.Response != nil) != refused { t.Errorf("%s %s: refused %v", rawURL, body, x.Response != nil) } } check("https://client-dynamic.imvu.com/api/new.php", fmt.Sprintf(`{"pids": [80, %d.0]}`, local), true) check(fmt.Sprintf("https://api.imvu.com/product/product-%d", local), "", true) check(fmt.Sprintf("https://client-dynamic.imvu.com/api/x.php?ids=1%%2C%d", local), "", true) check("https://client-dynamic.imvu.com/api/new.php", `{"pids": [80, 2000000777]}`, false) // not given out check("https://client-dynamic.imvu.com/api/new.php", fmt.Sprintf(`{"n": "1%d"}`, local), false) check(fmt.Sprintf("https://hub.example/%d", local), "", false) // only IMVU's hosts }