Merge pull request #1956 from weaveworks/1953-fix-query-string

Prevent querystrings from starting with &
This commit is contained in:
Mike Lang
2016-10-27 14:29:16 -07:00
committed by GitHub
2 changed files with 19 additions and 2 deletions

View File

@@ -1,5 +1,7 @@
jest.dontMock('../web-api-utils');
import {OrderedMap as makeOrderedMap} from 'immutable';
describe('WebApiUtils', () => {
const WebApiUtils = require('../web-api-utils');
@@ -22,4 +24,19 @@ describe('WebApiUtils', () => {
expect(basePath('/')).toBe('');
});
});
describe('buildOptionsQuery', () => {
const buildOptionsQuery = WebApiUtils.buildOptionsQuery;
it('should handle empty options', () => {
expect(buildOptionsQuery(makeOrderedMap({}))).toBe('');
});
it('should combine multiple options', () => {
expect(buildOptionsQuery(makeOrderedMap([
['foo', 2],
['bar', 4]
]))).toBe('foo=2&bar=4');
});
});
});

View File

@@ -25,9 +25,9 @@ let controlErrorTimer = 0;
let createWebsocketAt = 0;
let firstMessageOnWebsocketAt = 0;
function buildOptionsQuery(options) {
export function buildOptionsQuery(options) {
if (options) {
return options.reduce((query, value, param) => `${query}&${param}=${value}`, '');
return options.map((value, param) => `${param}=${value}`).join('&');
}
return '';
}