-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use proper file extension based on content-type (#36)
- Loading branch information
1 parent
2ac020f
commit f5fbade
Showing
12 changed files
with
584 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
src/migrations/cache-fs/1-txt-to-proper-file-type.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
|
||
import { RequestRepositoryFile } from '../../infrastructure/repository'; | ||
import { Request, Response } from '../../domain/entity'; | ||
import { moveTxtToProperFileTypeMigration } from './1-txt-to-proper-file-type'; | ||
|
||
const MEMENTO_CACHE_DIR = path.join(__dirname, '../../../.memento-test-cache'); | ||
const OUTPUT_DIRECTORY = `${MEMENTO_CACHE_DIR}/https___pokeapi-co_api_v2`; | ||
let requestRepository: RequestRepositoryFile; | ||
|
||
function getOutputFilePath(fileName: string) { | ||
return path.join(OUTPUT_DIRECTORY, fileName); | ||
} | ||
|
||
afterAll(() => { | ||
fs.removeSync(MEMENTO_CACHE_DIR); | ||
}); | ||
|
||
beforeEach(() => { | ||
fs.removeSync(MEMENTO_CACHE_DIR); | ||
|
||
requestRepository = new RequestRepositoryFile({ | ||
targetUrl: 'https://pokeapi.co/api/v2', | ||
cacheDirectory: MEMENTO_CACHE_DIR, | ||
}); | ||
}); | ||
|
||
describe('text/plain requests migration', () => { | ||
beforeEach(async () => { | ||
const request = new Request('GET', '/text', {}, ''); | ||
const response = new Response( | ||
200, | ||
{ | ||
'content-type': 'text/plain', | ||
}, | ||
Buffer.from('Ok'), | ||
0 | ||
); | ||
|
||
await requestRepository.persistResponseForRequest(request, response); | ||
}); | ||
|
||
it('should not move the body file', async () => { | ||
// When | ||
await moveTxtToProperFileTypeMigration({ | ||
targetUrl: 'https://pokeapi.co/api/v2', | ||
cacheDirectory: MEMENTO_CACHE_DIR, | ||
requestRepository, | ||
}); | ||
|
||
// Then | ||
const olfFileStillExists = await fs.pathExists( | ||
getOutputFilePath( | ||
'get__text-74121b3875b9d4d16c7e9dfd80bd90ff50da5d86/body.txt' | ||
) | ||
); | ||
|
||
// Assert that fold files have been moved to new files. | ||
expect(olfFileStillExists).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('application/json requests migration', () => { | ||
beforeEach(async () => { | ||
const request = new Request('GET', '/json', {}, ''); | ||
const response = new Response( | ||
200, | ||
{ | ||
'content-type': 'application/json', | ||
}, | ||
Buffer.from('{"name": "John Doe"}'), | ||
0 | ||
); | ||
|
||
await requestRepository.persistResponseForRequest(request, response); | ||
|
||
await fs.move( | ||
getOutputFilePath( | ||
'get__json-728ad90473b5366f44ebc49a57da2c5df837d040/body.json' | ||
), | ||
getOutputFilePath( | ||
'get__json-728ad90473b5366f44ebc49a57da2c5df837d040/body.txt' | ||
) | ||
); | ||
}); | ||
|
||
it('should move the body file to body.json', async () => { | ||
// When | ||
await moveTxtToProperFileTypeMigration({ | ||
targetUrl: 'https://pokeapi.co/api/v2', | ||
cacheDirectory: MEMENTO_CACHE_DIR, | ||
requestRepository, | ||
}); | ||
|
||
// Then | ||
const oldFileExists = await fs.pathExists( | ||
getOutputFilePath( | ||
'get__json-728ad90473b5366f44ebc49a57da2c5df837d040/body.txt' | ||
) | ||
); | ||
const newFileExists = await fs.pathExists( | ||
getOutputFilePath( | ||
'get__json-728ad90473b5366f44ebc49a57da2c5df837d040/body.json' | ||
) | ||
); | ||
|
||
// Assert that fold files have been moved to new files. | ||
expect(oldFileExists).toBe(false); | ||
expect(newFileExists).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('application/octet-stream request migration', () => { | ||
beforeEach(async () => { | ||
const request = new Request('GET', '/octet-stream', {}, ''); | ||
const response = new Response( | ||
200, | ||
{ | ||
'content-type': 'application/octet-stream', | ||
}, | ||
Buffer.from('something'), | ||
0 | ||
); | ||
|
||
await requestRepository.persistResponseForRequest(request, response); | ||
|
||
await fs.move( | ||
getOutputFilePath( | ||
'get__octet-stream-d1f560b23c4db2f2a2e0dfc1bfa32592d4370cb5/body' | ||
), | ||
getOutputFilePath( | ||
'get__octet-stream-d1f560b23c4db2f2a2e0dfc1bfa32592d4370cb5/body.txt' | ||
) | ||
); | ||
}); | ||
|
||
it('should move the body file to body', async () => { | ||
// When | ||
await moveTxtToProperFileTypeMigration({ | ||
targetUrl: 'https://pokeapi.co/api/v2', | ||
cacheDirectory: MEMENTO_CACHE_DIR, | ||
requestRepository, | ||
}); | ||
|
||
// Then | ||
const oldFileExists = await fs.pathExists( | ||
getOutputFilePath( | ||
'get__octet-stream-d1f560b23c4db2f2a2e0dfc1bfa32592d4370cb5/body.txt' | ||
) | ||
); | ||
const newFileExists = await fs.pathExists( | ||
getOutputFilePath( | ||
'get__octet-stream-d1f560b23c4db2f2a2e0dfc1bfa32592d4370cb5/body' | ||
) | ||
); | ||
// Assert that fold files have been moved to new files. | ||
expect(oldFileExists).toBe(false); | ||
expect(newFileExists).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('requests respecting the new format', () => { | ||
beforeEach(async () => { | ||
const request = new Request('GET', '/new-format', {}, ''); | ||
const response = new Response( | ||
200, | ||
{ | ||
'content-type': 'application.xml', | ||
}, | ||
Buffer.from('<text>something</text>'), | ||
0 | ||
); | ||
|
||
await requestRepository.persistResponseForRequest(request, response); | ||
}); | ||
|
||
it('should not move the body file', async () => { | ||
// When | ||
await moveTxtToProperFileTypeMigration({ | ||
targetUrl: 'https://pokeapi.co/api/v2', | ||
cacheDirectory: MEMENTO_CACHE_DIR, | ||
requestRepository, | ||
}); | ||
|
||
// Then | ||
const oldFileExists = await fs.pathExists( | ||
getOutputFilePath( | ||
'get__new-format-bb4032a14daf7b56f8f9f4312ce139543bc9a281' | ||
) | ||
); | ||
|
||
expect(oldFileExists).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import path from 'path'; | ||
import fs from 'fs-extra'; | ||
|
||
import { RequestRepository } from '../../domain/repository'; | ||
import { getFileExtension, getRequestDirectory } from '../../utils/path'; | ||
|
||
export interface Dependencies { | ||
targetUrl: string; | ||
cacheDirectory: string; | ||
requestRepository: RequestRepository; | ||
} | ||
|
||
export async function moveTxtToProperFileTypeMigration({ | ||
targetUrl, | ||
cacheDirectory, | ||
requestRepository, | ||
}: Dependencies) { | ||
const allRequests = await requestRepository.getAllRequests(); | ||
|
||
for (const request of allRequests) { | ||
const requestDirectory = getRequestDirectory( | ||
cacheDirectory, | ||
targetUrl, | ||
request | ||
); | ||
const metadatafile = await fs.readJson( | ||
path.join(requestDirectory, 'metadata.json') | ||
); | ||
|
||
const contentType = metadatafile.responseHeaders['content-type']; | ||
const newFileExtension = getFileExtension(contentType); | ||
|
||
const oldBodyFile = path.join(requestDirectory, 'body.txt'); | ||
const newBodyFile = path.join(requestDirectory, `body${newFileExtension}`); | ||
|
||
if (oldBodyFile !== newBodyFile) { | ||
const hasOldBodyFile = await fs.pathExists(oldBodyFile); | ||
|
||
if (hasOldBodyFile) { | ||
await fs.move(oldBodyFile, newBodyFile); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.