Skip to content

Commit

Permalink
refactor: rename URLCrypto class to URLTransformer for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTraveylan committed Jan 16, 2025
1 parent ff78fd1 commit 9f9e843
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/url-transformer.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createHash } from "crypto"

export class URLCrypto {
export class URLTransformer {
private static readonly SEPARATOR = "-"

static toBase64Url(url: string): string {
Expand Down
46 changes: 23 additions & 23 deletions lib/url-transformer.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { URLCrypto } from "./url-transformer.service"
import { URLTransformer } from "./url-transformer.service"

describe("URLCrypto", () => {
// Tests d'encodage
describe("toBase64Url", () => {
test("devrait encoder une URL valide", () => {
const url = "https://www.alextraveylan.fr/fr"
const encoded = URLCrypto.toBase64Url(url)
const encoded = URLTransformer.toBase64Url(url)

expect(encoded).toBeDefined()
expect(encoded).toContain("-")
Expand All @@ -16,25 +16,25 @@ describe("URLCrypto", () => {
const invalidUrl = "not-a-url"

expect(() => {
URLCrypto.toBase64Url(invalidUrl)
URLTransformer.toBase64Url(invalidUrl)
}).toThrow("URL invalide")
})

test("devrait gérer les URLs avec des caractères spéciaux", () => {
const urlWithSpecialChars = "https://example.com/path?param=value&special=@#$"
const encoded = URLCrypto.toBase64Url(urlWithSpecialChars)
const encoded = URLTransformer.toBase64Url(urlWithSpecialChars)

expect(encoded).toBeDefined()
expect(URLCrypto.fromBase64Url(encoded)).toBe(urlWithSpecialChars)
expect(URLTransformer.fromBase64Url(encoded)).toBe(urlWithSpecialChars)
})
})

// Tests de décodage
describe("fromBase64Url", () => {
test("devrait décoder une string encodée valide", () => {
const originalUrl = "https://www.alextraveylan.fr/fr"
const encoded = URLCrypto.toBase64Url(originalUrl)
const decoded = URLCrypto.fromBase64Url(encoded)
const encoded = URLTransformer.toBase64Url(originalUrl)
const decoded = URLTransformer.fromBase64Url(encoded)

expect(decoded).toBe(originalUrl)
})
Expand All @@ -43,16 +43,16 @@ describe("URLCrypto", () => {
const invalidEncoded = "invalid-encoded-string"

expect(() => {
URLCrypto.fromBase64Url(invalidEncoded)
URLTransformer.fromBase64Url(invalidEncoded)
}).toThrow("Décodage impossible")
})

test("devrait rejeter une string avec un hash incorrect", () => {
const encoded = URLCrypto.toBase64Url("https://www.alextraveylan.fr/fr")
const encoded = URLTransformer.toBase64Url("https://www.alextraveylan.fr/fr")
const corruptedEncoded = encoded.slice(0, -1) + "X"

expect(() => {
URLCrypto.fromBase64Url(corruptedEncoded)
URLTransformer.fromBase64Url(corruptedEncoded)
}).toThrow("Décodage impossible")
})
})
Expand All @@ -61,38 +61,38 @@ describe("URLCrypto", () => {
describe("isValid", () => {
test("devrait retourner true pour une string encodée valide", () => {
const url = "https://www.alextraveylan.fr/fr"
const encoded = URLCrypto.toBase64Url(url)
const encoded = URLTransformer.toBase64Url(url)

expect(URLCrypto.isValid(encoded)).toBe(true)
expect(URLTransformer.isValid(encoded)).toBe(true)
})

test("devrait retourner false pour une string encodée invalide", () => {
expect(URLCrypto.isValid("invalid-string")).toBe(false)
expect(URLTransformer.isValid("invalid-string")).toBe(false)
})
})

// Tests de cas limites
describe("edge cases", () => {
test("devrait gérer les URLs très longues", () => {
const longUrl = "https://example.com/" + "a".repeat(1000)
const encoded = URLCrypto.toBase64Url(longUrl)
const decoded = URLCrypto.fromBase64Url(encoded)
const encoded = URLTransformer.toBase64Url(longUrl)
const decoded = URLTransformer.fromBase64Url(encoded)

expect(decoded).toBe(longUrl)
})

test("devrait gérer les URLs avec des fragments", () => {
const urlWithFragment = "https://example.com/page#section"
const encoded = URLCrypto.toBase64Url(urlWithFragment)
const decoded = URLCrypto.fromBase64Url(encoded)
const encoded = URLTransformer.toBase64Url(urlWithFragment)
const decoded = URLTransformer.fromBase64Url(encoded)

expect(decoded).toBe(urlWithFragment)
})

test("devrait gérer les URLs avec des query params complexes", () => {
const urlWithParams = "https://example.com/search?q=test&filter[]=1&filter[]=2"
const encoded = URLCrypto.toBase64Url(urlWithParams)
const decoded = URLCrypto.fromBase64Url(encoded)
const encoded = URLTransformer.toBase64Url(urlWithParams)
const decoded = URLTransformer.fromBase64Url(encoded)

expect(decoded).toBe(urlWithParams)
})
Expand All @@ -102,10 +102,10 @@ describe("URLCrypto", () => {
describe("round-trip", () => {
test("devrait préserver l'URL après encodage/décodage multiple", () => {
const originalUrl = "https://www.alextraveylan.fr/fr"
const encoded1 = URLCrypto.toBase64Url(originalUrl)
const decoded1 = URLCrypto.fromBase64Url(encoded1)
const encoded2 = URLCrypto.toBase64Url(decoded1)
const decoded2 = URLCrypto.fromBase64Url(encoded2)
const encoded1 = URLTransformer.toBase64Url(originalUrl)
const decoded1 = URLTransformer.fromBase64Url(encoded1)
const encoded2 = URLTransformer.toBase64Url(decoded1)
const decoded2 = URLTransformer.fromBase64Url(encoded2)

expect(decoded2).toBe(originalUrl)
})
Expand Down

0 comments on commit 9f9e843

Please sign in to comment.