Fixed pathname encoding and keep-alive

This commit is contained in:
Gregory Schier 2016-10-11 09:57:06 -07:00
parent 3d5408bfc3
commit 741278a05b
3 changed files with 18 additions and 14 deletions

View File

@ -17,6 +17,7 @@ describe('buildRequestConfig()', () => {
expect(config).toEqual({
body: '',
followAllRedirects: true,
forever: true,
gzip: true,
headers: {host: ''},
maxRedirects: 20,
@ -37,7 +38,7 @@ describe('buildRequestConfig()', () => {
parameters: [{name: 'foo bar', value: 'hello&world'}],
method: 'POST',
body: 'foo=bar',
url: 'http://foo.com:3332/★/foo%20bar?bar=baz',
url: 'http://foo.com:3332/★/hi@gmail.com/foo%20bar?bar=baz',
authentication: {
username: 'user',
password: 'pass'
@ -49,6 +50,7 @@ describe('buildRequestConfig()', () => {
expect(config).toEqual({
body: 'foo=bar',
followAllRedirects: true,
forever: true,
gzip: true,
headers: {
'Content-Type': 'application/json',
@ -61,7 +63,8 @@ describe('buildRequestConfig()', () => {
rejectUnauthorized: true,
time: true,
timeout: 0,
url: 'http://foo.com:3332/%E2%98%85/foo%20bar?bar=baz&foo%20bar=hello%26world'
url: 'http://foo.com:3332/%E2%98%85/hi%40gmail.com/' +
'foo%20bar?bar=baz&foo%20bar=hello%26world'
})
})
});

View File

@ -106,7 +106,7 @@ describe('prepareUrlForSending()', () => {
it('encodes pathname mixed encoding', () => {
const url = util.prepareUrlForSending('https://google.com/foo bar baz%20qux/100%/foo%25');
expect(url).toBe('https://google.com/foo%20bar%20baz%20qux/100%25/foo%2525');
expect(url).toBe('https://google.com/foo%20bar%20baz%20qux/100%25/foo%25');
});
it('leaves already encoded pathname', () => {

View File

@ -60,30 +60,30 @@ export function flexibleEncodeComponent (str) {
// Sometimes spaces screw things up because of url.parse
str = str.replace(/%20/g, ' ');
let decodedPathname;
let decoded;
try {
decodedPathname = decodeURIComponent(str);
decoded = decodeURIComponent(str);
} catch (e) {
// Malformed (probably not encoded) so assume it's decoded already
decodedPathname = str;
decoded = str;
}
return encodeURIComponent(decodedPathname);
return encodeURIComponent(decoded);
}
export function flexibleEncode (str) {
// Sometimes spaces screw things up because of url.parse
str = str.replace(/%20/g, ' ');
let decodedPathname;
let decoded;
try {
decodedPathname = decodeURI(str);
decoded = decodeURI(str);
} catch (e) {
// Malformed (probably not encoded) so assume it's decoded already
decodedPathname = str;
decoded = str;
}
return encodeURI(decodedPathname);
return encodeURI(decoded);
}
export function prepareUrlForSending (url) {
@ -96,9 +96,10 @@ export function prepareUrlForSending (url) {
// 1. Pathname //
// ~~~~~~~~~~~ //
parsedUrl.pathname = flexibleEncode(
parsedUrl.pathname || ''
);
if (parsedUrl.pathname) {
const segments = parsedUrl.pathname.split('/');
parsedUrl.pathname = segments.map(flexibleEncodeComponent).join('/');
}
// ~~~~~~~~~~~~~~ //
// 2. Querystring //