diff --git a/test/utils.test.js b/test/utils.test.js index 0d84bc480..33acd9844 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -1,5 +1,5 @@ const assert = require("assert"); -const { getFromObjectCaseInsensitive } = require("../lib/build/utils"); +const { getFromObjectCaseInsensitive, getTopLevelDomainForSameSiteResolution } = require("../lib/build/utils"); describe("SuperTokens utils test", () => { it("Test getFromObjectCaseInsensitive", () => { @@ -18,3 +18,39 @@ describe("SuperTokens utils test", () => { assert.equal(getFromObjectCaseInsensitive("authoriZation", testObj), "test"); }); }); + +describe("getTopLevelDomainForSameSiteResolution test", () => { + it('should return "localhost" for localhost URLs', () => { + assert.equal(getTopLevelDomainForSameSiteResolution("http://localhost:3000"), "localhost"); + assert.equal(getTopLevelDomainForSameSiteResolution("https://localhost"), "localhost"); + }); + + it('should return "localhost" for localhost.org URLs', () => { + assert.equal(getTopLevelDomainForSameSiteResolution("http://localhost.org"), "localhost"); + assert.equal(getTopLevelDomainForSameSiteResolution("https://localhost.org/test-path"), "localhost"); + }); + + it('should return "localhost" for IP addresses', () => { + assert.equal(getTopLevelDomainForSameSiteResolution("http://127.0.0.1"), "localhost"); + assert.equal(getTopLevelDomainForSameSiteResolution("https://192.168.1.1"), "localhost"); + }); + + it("should return the correct domain for normal URLs", () => { + assert.equal(getTopLevelDomainForSameSiteResolution("https://www.example.com"), "example.com"); + assert.equal(getTopLevelDomainForSameSiteResolution("http://sub.domain.co.uk"), "domain.co.uk"); + }); + + it("should handle .amazonaws.com domains correctly", () => { + assert.equal(getTopLevelDomainForSameSiteResolution("https://my-instance.amazonaws.com"), "amazonaws.com"); + }); + + it("should handle .local domains correctly", () => { + assert.equal(getTopLevelDomainForSameSiteResolution("http://myserver.local"), "myserver.local"); + }); + + it("should throw an error for invalid domains", () => { + assert.throws(() => { + getTopLevelDomainForSameSiteResolution("http://invalid..com"); + }, Error); + }); +});