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

Replace request with got #76

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 5 additions & 5 deletions lib/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module.exports = {
},
// Yandex.Metrica - https://metrica.yandex.com
yandex(id, payload) {
const request = require('request');
const {CookieJar, Cookie} = require('tough-cookie');

const ts = new Date(Number.parseInt(id, 10))
.toISOString()
Expand All @@ -89,16 +89,16 @@ module.exports = {
const url = `https://mc.yandex.ru/watch/${this.trackingCode}`;

// Set custom cookie using tough-cookie
const _jar = request.jar();
const _jar = new CookieJar();
const cookieString = `name=yandexuid; value=${this.clientId}; path=/;`;
const cookie = request.cookie(cookieString);
const cookie = Cookie.parse(cookieString);
_jar.setCookie(cookie, url);

return {
url,
method: 'GET',
qs,
jar: _jar,
searchParams: qs,
cookieJar: _jar,
};
},
};
16 changes: 8 additions & 8 deletions lib/push.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const request = require('request');
const got = require('got');
const async = require('async');
const Insight = require('.');

Expand All @@ -19,14 +19,14 @@ process.on('message', message => {
const id = parts[0];
const payload = q[element];

request(insight._getRequestObj(id, payload), error => {
if (error) {
cb(error);
return;
}

try {
got(insight._getRequestObj(id, payload));
} catch (error) {
cb(error);
return;
} finally {
cb();
});
}
}, error => {
if (error) {
const q2 = config.get('queue') || {};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"inquirer": "^6.3.1",
"lodash.debounce": "^4.0.8",
"os-name": "^4.0.1",
"request": "^2.88.0",
"got": "^11.8.2",
"tough-cookie": "^4.0.0",
"uuid": "^8.3.2"
},
Expand Down
8 changes: 4 additions & 4 deletions test/providers-yandex-metrica.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ const insight = new Insight({
});

test('form valid request', async t => {
const request = require('request');
const got = require('got');

// Test querystrings
const requestObject = insight._getRequestObj(ts, pageviewPayload);
const _qs = requestObject.qs;
const _qs = requestObject.searchParams;

t.is(_qs['page-url'], `http://${pkg}.insight/test/path?version=${ver}`);
t.is(_qs['browser-info'], `i:20130824223344:z:0:t:${pageviewPayload.path}`);

// Test cookie
await request(requestObject);
await got(requestObject).catch(() => {});

// Cookie string looks like:
// [{"key":"name","value":"yandexuid",
// "extensions":["value=80579748502"],"path":"/","creation":...
const cookieClientId = requestObject.jar.getCookies(requestObject.url)[0].extensions[0].split('=')[1];
const cookieClientId = (await requestObject.cookieJar.getCookies(requestObject.url))[0].extensions[0].split('=')[1];
t.is(Number(cookieClientId), Number(insight.clientId));
});