Skip to content

Commit

Permalink
Handle better realm url input parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
borisyankov committed May 11, 2017
1 parent 4786626 commit 507839a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/start/RealmScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import styles from '../common/styles';
import { Screen, ErrorMsg, ZulipButton, Input } from '../common';
import { getAuthBackends } from '../api';
import config from '../config';
import { fixRealmUrl } from '../utils/url';

type Props = {
realm: ?string,
Expand Down Expand Up @@ -36,8 +37,10 @@ class RealmScreen extends React.Component {
realm = `https://${realm}`;
}

realm = realm.replace(/\/$/, '');

this.setState({
realm,
realm: fixRealmUrl(realm),
progress: true,
error: undefined,
});
Expand Down
15 changes: 15 additions & 0 deletions src/utils/__tests__/url-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
isSpecialLink,
getNarrowFromLink,
getMessageIdFromLink,
fixRealmUrl,
} from '../url';

describe('getFullUrl', () => {
Expand Down Expand Up @@ -294,3 +295,17 @@ describe('getMessageIdFromLink', () => {
).toBe(1);
});
});

describe('fixRealmUrl', () => {
test('when a realm url is missing a protocol, prepend https', () => {
expect(fixRealmUrl('example.com')).toEqual('https://example.com');
});

test('when a realm url has a trailing "/" remove it', () => {
expect(fixRealmUrl('https://example.com/')).toEqual('https://example.com');
});

test('when input url is correct, do not change it', () => {
expect(fixRealmUrl('https://example.com')).toEqual('https://example.com');
});
});
11 changes: 11 additions & 0 deletions src/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,14 @@ const getResourceNoAuth = (uri: string) => ({

export const getResource = (uri: string, auth: Auth) =>
(isUrlOnRealm(uri, auth.realm) ? getResourceWithAuth(uri, auth) : getResourceNoAuth(uri));

export const fixRealmUrl = (url: string) => {
// Automatically prepend 'https://' if the user does not enter a protocol
if (url.search(/\b(http|https):\/\//) === -1) {
url = `https://${url}`;
}

url = url.replace(/\/$/, '');

return url;
};

0 comments on commit 507839a

Please sign in to comment.