Skip to content

Commit 860bdaf

Browse files
committed
fix: enhance device redirection logic and add tests for CriOS user agent #274
1 parent 87a113f commit 860bdaf

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

server/middleware/1.redirect.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const SOCIAL_BOTS = [
1919
'whatsapp',
2020
]
2121

22+
const APPLE_DEVICE_UA_MARKERS = ['iphone', 'ipad', 'ipod', 'crios']
23+
2224
function isSocialBot(userAgent: string): boolean {
2325
const ua = userAgent.toLowerCase()
2426
return SOCIAL_BOTS.some(bot => ua.includes(bot))
@@ -34,7 +36,7 @@ function getDeviceRedirectUrl(userAgent: string, link: Link): string | null {
3436
return link.google
3537
}
3638

37-
if (link.apple && (ua.includes('iphone') || ua.includes('ipad') || ua.includes('ipod'))) {
39+
if (link.apple && APPLE_DEVICE_UA_MARKERS.some(marker => ua.includes(marker))) {
3840
return link.apple
3941
}
4042

tests/sink.spec.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
import { describe, expect, it } from 'vitest'
2-
import { fetch } from './utils'
2+
import { fetch, postJson } from './utils'
33

44
describe('/', () => {
55
it('returns 200 for homepage request', async () => {
66
const response = await fetch('/')
77
expect(response.status).toBe(200)
88
})
9+
10+
it('redirects CriOS user agent to apple URL', async () => {
11+
const slug = `crios-apple-${crypto.randomUUID()}`
12+
const apple = 'https://apps.apple.com/app/sink-test'
13+
14+
const createResponse = await postJson('/api/link/create', {
15+
url: 'https://example.com',
16+
slug,
17+
apple,
18+
})
19+
expect(createResponse.status).toBe(201)
20+
const createData = await createResponse.json() as { link: { apple?: string } }
21+
expect(createData.link.apple).toBe(apple)
22+
23+
const response = await fetch(`/${slug}`, {
24+
redirect: 'manual',
25+
headers: {
26+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/147 Version/11.1.1 Safari/605.1.15',
27+
},
28+
})
29+
30+
expect(response.status).toBe(301)
31+
expect(response.headers.get('Location')).toBe(apple)
32+
})
933
})

0 commit comments

Comments
 (0)