Skip to content

Commit b69dc8d

Browse files
committed
test: test with properly-formatted URLs
1 parent 5c566fa commit b69dc8d

File tree

5 files changed

+49
-19
lines changed

5 files changed

+49
-19
lines changed

test/fixtures/known-assets.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ const path = require('path');
33

44
const md5 = require('js-md5');
55

6+
/**
7+
* @typedef {object} KnownAsset
8+
* @property {Buffer} content - The content of the asset.
9+
* @property {string} hash - The MD5 hash of the asset content.
10+
*/
11+
12+
/**
13+
* @typedef {{[id: string]: KnownAsset}} KnownAssetCollection
14+
*/
15+
616
const projects = [
717
'117504922'
818
];
@@ -15,6 +25,11 @@ const assets = [
1525
'fe5e3566965f9de793beeffce377d054.jpg'
1626
];
1727

28+
/**
29+
* Load a file from disk, then return its content and hash.
30+
* @param {string} filename - The file to load
31+
* @returns {KnownAsset} The loaded asset
32+
*/
1833
const loadSomething = filename => {
1934
const fullPath = path.join(__dirname, 'assets', filename);
2035
const content = fs.readFileSync(fullPath);
@@ -25,6 +40,11 @@ const loadSomething = filename => {
2540
};
2641
};
2742

43+
/**
44+
* Load a project from disk, ensure it's valid JSON, then return its content and hash.
45+
* @param {string} id - The project ID
46+
* @returns {KnownAsset} The loaded project asset
47+
*/
2848
const loadProject = id => {
2949
const filename = `${id}.json`;
3050
const result = loadSomething(filename);
@@ -35,6 +55,11 @@ const loadProject = id => {
3555
return result;
3656
};
3757

58+
/**
59+
* Load an asset from disk, ensuring its hash matches its filename.
60+
* @param {string} filename - The file to load
61+
* @returns {KnownAsset} The loaded asset
62+
*/
3863
const loadAsset = filename => {
3964
const result = loadSomething(filename);
4065

@@ -46,15 +71,18 @@ const loadAsset = filename => {
4671
return result;
4772
};
4873

74+
/**
75+
* @type {KnownAssetCollection}
76+
*/
4977
const knownAssets = Object.assign({},
5078
projects.reduce((bag, id) => {
5179
bag[id] = loadProject(id);
5280
return bag;
53-
}, {}),
81+
}, /** @type {KnownAssetCollection} */ ({})),
5482
assets.reduce((bag, filename) => {
5583
bag[filename] = loadAsset(filename);
5684
return bag;
57-
}, {})
85+
}, /** @type {KnownAssetCollection} */ ({}))
5886
);
5987

6088
module.exports = knownAssets;

test/fixtures/mockFetch.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const successText = 'successful response';
3737
* @returns {Promise<MockFetchResponse>} A promise for a Response-like object. Does not fully implement Response.
3838
*/
3939
const mockFetch = (resource, options) => {
40-
/** @type MockFetchResponse */
40+
/** @type {MockFetchResponse} */
4141
const results = {
4242
ok: false,
4343
status: 0
@@ -46,13 +46,15 @@ const mockFetch = (resource, options) => {
4646
options.mockFetchTestData.headers = new Headers(options.headers);
4747
options.mockFetchTestData.headersCount = Array.from(options.mockFetchTestData.headers).length;
4848
}
49-
const assetInfo = knownAssets[resource];
49+
const request = new Request(resource, options);
50+
const path = new URL(request.url).pathname.slice(1); // remove leading '/'
51+
const assetInfo = knownAssets[path];
5052
if (assetInfo) {
5153
results.ok = true;
5254
results.status = 200;
5355
results.arrayBuffer = () => Promise.resolve(assetInfo.content);
5456
} else {
55-
switch (resource) {
57+
switch (path) {
5658
case '200':
5759
results.ok = true;
5860
results.status = 200;

test/integration/download-known-assets.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ const addWebStores = storage => {
7575
// in the real world they would generate proper URIs
7676
storage.addWebStore(
7777
[storage.AssetType.Project],
78-
asset => asset.assetId,
78+
asset => `http://example.com/${asset.assetId}`,
7979
null, null);
8080
storage.addWebStore(
8181
[storage.AssetType.ImageVector, storage.AssetType.ImageBitmap, storage.AssetType.Sound],
82-
asset => `${asset.assetId}.${asset.dataFormat}`,
82+
asset => `http://example.com/${asset.assetId}.${asset.dataFormat}`,
8383
null, null
8484
);
8585
};

test/unit/fetch-tool.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const {FetchTool} = require('../../src/FetchTool');
88
test('send success returns response.text()', async () => {
99
const tool = new FetchTool();
1010

11-
const result = await tool.send({url: '200'});
11+
const result = await tool.send({url: 'http://example.com/200'});
1212
expect(result).toBe(mockFetch.successText);
1313
});
1414

@@ -18,7 +18,7 @@ test('send failure returns response.status', async () => {
1818
const catcher = jest.fn();
1919

2020
try {
21-
await tool.send({url: '500'});
21+
await tool.send({url: 'http://example.com/500'});
2222
} catch (e) {
2323
catcher(e);
2424
}
@@ -32,14 +32,14 @@ test('get success returns Uint8Array.body(response.arrayBuffer())', async () =>
3232

3333
const tool = new FetchTool();
3434

35-
const result = await tool.get({url: '200'});
35+
const result = await tool.get({url: 'http://example.com/200'});
3636
expect(decoder.decode(result)).toBe(mockFetch.successText);
3737
});
3838

3939
test('get with 404 response returns null data', async () => {
4040
const tool = new FetchTool();
4141

42-
const result = await tool.get({url: '404'});
42+
const result = await tool.get({url: 'http://example.com/404'});
4343
expect(result).toBeNull();
4444
});
4545

@@ -48,7 +48,7 @@ test('get failure returns response.status', async () => {
4848
const catcher = jest.fn();
4949

5050
try {
51-
await tool.get({url: '500'});
51+
await tool.get({url: 'http://example.com/500'});
5252
} catch (e) {
5353
catcher(e);
5454
}

test/unit/metadata.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test('get without metadata', async () => {
1818
const tool = new FetchTool();
1919

2020
const mockFetchTestData = {};
21-
const result = await tool.get({url: '200', mockFetchTestData});
21+
const result = await tool.get({url: 'http://example.com/200', mockFetchTestData});
2222

2323
expect(result).toBeInstanceOf(Uint8Array);
2424
expect(mockFetchTestData.headers).toBeTruthy();
@@ -36,7 +36,7 @@ test('get with metadata', async () => {
3636
setMetadata(RequestMetadata.RunId, 5678);
3737

3838
const mockFetchTestData = {};
39-
const result = await tool.get({url: '200', mockFetchTestData});
39+
const result = await tool.get({url: 'http://example.com/200', mockFetchTestData});
4040

4141
expect(result).toBeInstanceOf(Uint8Array);
4242
expect(mockFetchTestData.headers).toBeTruthy();
@@ -50,7 +50,7 @@ test('send without metadata', async () => {
5050
const tool = new FetchTool();
5151

5252
const mockFetchTestData = {};
53-
const result = await tool.send({url: '200', mockFetchTestData});
53+
const result = await tool.send({url: 'http://example.com/200', mockFetchTestData});
5454

5555
expect(typeof result).toBe('string');
5656
expect(mockFetchTestData.headers).toBeTruthy();
@@ -68,7 +68,7 @@ test('send with metadata', async () => {
6868
setMetadata(RequestMetadata.RunId, 8765);
6969

7070
const mockFetchTestData = {};
71-
const result = await tool.send({url: '200', mockFetchTestData});
71+
const result = await tool.send({url: 'http://example.com/200', mockFetchTestData});
7272

7373
expect(typeof result).toBe('string');
7474
expect(mockFetchTestData.headers).toBeTruthy();
@@ -90,7 +90,7 @@ test('selectively delete metadata', async () => {
9090

9191
const mockFetchTestData = {};
9292

93-
const result1 = await tool.send({url: '200', mockFetchTestData});
93+
const result1 = await tool.send({url: 'http://example.com/200', mockFetchTestData});
9494
expect(typeof result1).toBe('string');
9595
expect(mockFetchTestData.headers).toBeTruthy();
9696

@@ -101,7 +101,7 @@ test('selectively delete metadata', async () => {
101101
// remove the Project ID from metadata
102102
unsetMetadata(RequestMetadata.ProjectId);
103103

104-
const result2 = await tool.send({url: '200', mockFetchTestData});
104+
const result2 = await tool.send({url: 'http://example.com/200', mockFetchTestData});
105105
expect(typeof result2).toBe('string');
106106
expect(mockFetchTestData.headers).toBeTruthy();
107107

@@ -121,7 +121,7 @@ test('metadata has case-insensitive keys', async () => {
121121
const tool = new FetchTool();
122122

123123
const mockFetchTestData = {};
124-
await tool.get({url: '200', mockFetchTestData});
124+
await tool.get({url: 'http://example.com/200', mockFetchTestData});
125125

126126
expect(mockFetchTestData.headers).toBeTruthy();
127127
expect(mockFetchTestData.headersCount).toBe(1);

0 commit comments

Comments
 (0)