Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use https module for search #41

Merged
merged 1 commit into from
Oct 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions __mocks__/https.js
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 3 additions & 2 deletions __tests__/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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',
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/utils/search.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import search from '../../src/utils/search';

jest.mock('http');
jest.mock('https');

describe('search', () => {
it('should seach for packages', async () => {
Expand All @@ -16,7 +16,7 @@ describe('search', () => {
},
],
};
require('http').__setMockResponse(JSON.stringify(mockHttpResponse));
require('https').__setMockResponse(JSON.stringify(mockHttpResponse));

const q = 'foobar';
const mockResponse = [
Expand Down
1 change: 1 addition & 0 deletions src/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
4 changes: 2 additions & 2 deletions src/utils/search.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const http = require('http');
const https = require('https');

/**
* Retrieves the API url
Expand All @@ -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;
});
Expand Down