package controllers import ( "net/http/httptest" "testing" "github.com/gofiber/fiber/v2" ) /* The console and the customer app share handlers, and a few of those handlers owe the two callers different answers. This is the switch that tells them apart, so it is worth a test of its own: get it wrong in the permissive direction and shoppers are offered empty shelves again; get it wrong in the strict direction and a merchant's restocking screen goes blank, hiding the work they came to do. */ func pathSays(t *testing.T, path string, want bool) { t.Helper() app := fiber.New() got := false app.Get("/*", func(c *fiber.Ctx) error { got = isAppRequest(c) return nil }) if _, err := app.Test(httptest.NewRequest("GET", path, nil)); err != nil { t.Fatalf("Test(%q): %v", path, err) } if got != want { t.Errorf("isAppRequest(%q) = %v, want %v", path, got, want) } } func TestTheAppBaseIsRecognised(t *testing.T) { pathSays(t, "/live/api/v1/mob/products/getallproducts", true) } func TestTheConsoleBaseIsNotTreatedAsTheApp(t *testing.T) { // The console must keep seeing empty shelves — restocking them is the whole // point of that screen. pathSays(t, "/live/api/v1/web/products/getallproducts", false) } func TestAKeywordContainingMobDoesNotImpersonateTheApp(t *testing.T) { // Matched with its slashes, so a search for "mobil" or a shop called // "Mobius" cannot flip a console request into an app one and empty the // merchant's screen. pathSays(t, "/live/api/v1/web/products/getallproducts?keyword=mobile", false) pathSays(t, "/live/api/v1/web/tenants/search?keyword=mob", false) }