diff --git a/__mocks__/https.js b/__mocks__/https.js new file mode 100644 index 0000000..debb071 --- /dev/null +++ b/__mocks__/https.js @@ -0,0 +1,35 @@ +import EventEmitter from 'events'; + +const https = jest.genMockFromModule('https'); + +const MockServerResponse = class extends EventEmitter {}; + +// create a mock response and data object +let mockData = null; + +/** + * Sets the mock response + * + * @param {String} data + */ +const __setMockResponse = (data) => { + mockData = data; +}; + +/** + * Mock function to override https.get + * + * @param {String} url + * @param {Function} callback + */ +const get = (url, callback) => { + const mockResponse = new MockServerResponse(); + process.nextTick(() => { mockResponse.emit('data', mockData); }); + process.nextTick(() => { mockResponse.emit('end'); }); + callback.call(null, mockResponse); +}; + +https.__setMockResponse = __setMockResponse; +https.get = get; + +module.exports = https; diff --git a/__tests__/api/index.js b/__tests__/api/index.js index 78daafe..acc92a1 100644 --- a/__tests__/api/index.js +++ b/__tests__/api/index.js @@ -15,6 +15,7 @@ jest.mock('conf'); jest.mock('download'); jest.mock('fs'); jest.mock('http'); +jest.mock('https'); jest.mock('npm-name'); jest.mock('rimraf'); @@ -168,7 +169,7 @@ describe('search', () => { }, ], }; - require('http').__setMockResponse(JSON.stringify(mockHttpResponse)); + require('https').__setMockResponse(JSON.stringify(mockHttpResponse)); expect(await api.search('foobar')).toContainEqual({ name: 'foobar-default-theme', desc: 'Foobar default theme', @@ -184,7 +185,7 @@ describe('search', () => { const mockHttpResponse = { results: null, }; - require('http').__setMockResponse(JSON.stringify(mockHttpResponse)); + require('https').__setMockResponse(JSON.stringify(mockHttpResponse)); try { await api.search('foobar'); } catch (err) { diff --git a/__tests__/utils/search.js b/__tests__/utils/search.js index 5e62663..5ae591d 100644 --- a/__tests__/utils/search.js +++ b/__tests__/utils/search.js @@ -1,6 +1,6 @@ import search from '../../src/utils/search'; -jest.mock('http'); +jest.mock('https'); describe('search', () => { it('should seach for packages', async () => { @@ -16,7 +16,7 @@ describe('search', () => { }, ], }; - require('http').__setMockResponse(JSON.stringify(mockHttpResponse)); + require('https').__setMockResponse(JSON.stringify(mockHttpResponse)); const q = 'foobar'; const mockResponse = [ diff --git a/src/errors/index.js b/src/errors/index.js index f296021..d924990 100644 --- a/src/errors/index.js +++ b/src/errors/index.js @@ -5,4 +5,5 @@ exports.ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND'; exports.ERR_MODULE_DISABLED = 'ERR_MODULE_DISABLED'; exports.ERR_MODULE_NOT_INSTALLED = 'ERR_MODULE_NOT_INSTALLED'; exports.ERR_MODULE_REMOVE_FAILED = 'ERR_MODULE_REMOVE_FAILED'; +exports.ERR_MODULE_SEARCH_FAILED = 'ERR_MODULE_SEARCH_FAILED'; exports.ERR_THEME_ALREADY_ACTIVE = 'ERR_THEME_ALREADY_ACTIVE'; diff --git a/src/utils/search.js b/src/utils/search.js index 08b4270..541160a 100644 --- a/src/utils/search.js +++ b/src/utils/search.js @@ -1,4 +1,4 @@ -const http = require('http'); +const https = require('https'); /** * Retrieves the API url @@ -20,7 +20,7 @@ const searchPackages = q => new Promise((resolve) => { let body = ''; const endpoint = getSearchUrl(q); // Retrieve the search results - return http.get(endpoint, (res) => { + return https.get(endpoint, (res) => { res.on('data', (chunk) => { body += chunk; });