Skip to content

Commit

Permalink
fix(redirect-controller): proper handling of undefined values
Browse files Browse the repository at this point in the history
  • Loading branch information
yong-jie committed Jul 28, 2021
1 parent c12d491 commit 515b444
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
12 changes: 9 additions & 3 deletions src/server/modules/redirect/__tests__/RedirectController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,24 @@ const mockAnalyticsLogger: jest.Mocked<AnalyticsLoggerService> = {
}

// eslint-disable-next-line no-unused-vars
const writeShortlinkToCookie = jest.fn((_: string[] | null, __: string) => [])
const writeShortlinkToCookie = jest.fn(
(_: string[] | undefined, __: string) => [],
)

const cookieArrayReducerMockUnvisited: jest.Mocked<CookieArrayReducerService> =
{
// eslint-disable-next-line no-unused-vars
userHasVisitedShortlink: jest.fn((_: string[] | null, __: string) => false),
userHasVisitedShortlink: jest.fn(
(_: string[] | undefined, __: string) => false,
),
writeShortlinkToCookie,
}

const cookieArrayReducerMockVisited: jest.Mocked<CookieArrayReducerService> = {
// eslint-disable-next-line no-unused-vars
userHasVisitedShortlink: jest.fn((_: string[] | null, __: string) => true),
userHasVisitedShortlink: jest.fn(
(_: string[] | undefined, __: string) => true,
),
writeShortlinkToCookie,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ import { cookieSessionMaxSizeBytes } from '../../../config'
["userHasVisitedShortlink", "writeShortlinkToCookie"] }] */
@injectable()
export class CookieArrayReducerService {
userHasVisitedShortlink(cookie: string[] | null, shortUrl: string): boolean {
userHasVisitedShortlink(
cookie: string[] | undefined,
shortUrl: string,
): boolean {
if (!cookie) return false
return cookie.includes(shortUrl)
}

writeShortlinkToCookie(cookie: string[] | null, shortUrl: string): string[] {
writeShortlinkToCookie(
cookie: string[] | undefined,
shortUrl: string,
): string[] {
if (!cookie) return [shortUrl]
if (cookie.includes(shortUrl)) {
return _.without(cookie, shortUrl).concat(shortUrl)
Expand Down
4 changes: 2 additions & 2 deletions src/server/modules/redirect/services/RedirectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class RedirectService {

public redirectFor: (
shortUrl: string,
pastVisits: string[],
pastVisits: string[] | undefined,
userAgent: string,
referrer: string,
) => Promise<RedirectResult> = async (
Expand All @@ -59,7 +59,7 @@ export class RedirectService {
if (this.crawlerCheckService.isCrawler(userAgent)) {
return {
longUrl,
visitedUrls: pastVisits,
visitedUrls: pastVisits || [],
redirectType: RedirectType.Direct,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('CookieArrayReducerService', () => {

describe('userHasVisitedShortLink', () => {
it('is false on null cookie array', () => {
expect(service.userHasVisitedShortlink(null, shortUrl)).toBeFalsy()
expect(service.userHasVisitedShortlink(undefined, shortUrl)).toBeFalsy()
})

it('is false on empty cookie array', () => {
Expand All @@ -36,9 +36,9 @@ describe('CookieArrayReducerService', () => {

describe('writeShortlinkToCookie', () => {
it('returns array with shortUrl on null cookie', () => {
expect(service.writeShortlinkToCookie(null, shortUrl)).toStrictEqual([
shortUrl,
])
expect(service.writeShortlinkToCookie(undefined, shortUrl)).toStrictEqual(
[shortUrl],
)
})

it('moves shortUrl to the end of cookie array', () => {
Expand Down

0 comments on commit 515b444

Please sign in to comment.