Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
Add url parsing utility (#8)
Browse files Browse the repository at this point in the history
* Add url utils

* Fix test and add to list

* Fixes based on comments

* Fix to use parens around the license
  • Loading branch information
draffensperger committed Feb 8, 2019
1 parent 3082b32 commit 061665b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
50 changes: 50 additions & 0 deletions packages/opencensus-web-core/src/common/url-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright 2019, OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Interface to represent the components of a parsed URL.
* The field comments below reference this sample URL:
* https://example.com:3000/pathname/?search=test#hash
*/
export interface ParsedUrl {
/** Protocol of the URL including the colon. E.g. https: */
readonly protocol: string;
/** Host name only (no port). E.g. example.com */
readonly hostname: string;
/**
* Port number if explicitly specified (else empty string). E.g. 3000
* This is a string type to match the type of the field in the document <a>
* tag that is used to implement this.
*/
readonly port: string;
/** Host portion of the URL including port. E.g. example.com:3000 */
readonly host: string;
/** Origin with protocol, hostname and port. E.g. https://example.com:3000 */
readonly origin: string;
/** Request URL path. E.g. /pathname/ */
readonly pathname: string;
/** Portion of the URL after and including the `#` character. E.g. #hash */
readonly hash: string;
/** Query string before the `#` char including the `?`. E.g. ?search=test */
readonly search: string;
}

/** Parses the given URL into components. */
export function parseUrl(url: string): ParsedUrl {
const aTag = document.createElement('a');
aTag.href = url;
return aTag;
}
1 change: 1 addition & 0 deletions packages/opencensus-web-core/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ import './test-span';
import './test-time-util';
import './test-tracer';
import './test-tracing';
import './test-url-util';
47 changes: 47 additions & 0 deletions packages/opencensus-web-core/test/test-url-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2019, OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {parseUrl} from '../src/common/url-util';

describe('parseUrl', () => {
it('parses a URL into its various components', () => {
const parsedUrl =
parseUrl('https://example.com:3000/pathname/?search=test#hash');

expect(parsedUrl.protocol).toBe('https:');
expect(parsedUrl.hostname).toBe('example.com');
expect(parsedUrl.port).toBe('3000');
expect(parsedUrl.host).toBe('example.com:3000');
expect(parsedUrl.origin).toBe('https://example.com:3000');
expect(parsedUrl.pathname).toBe('/pathname/');
expect(parsedUrl.hash).toBe('#hash');
expect(parsedUrl.search).toBe('?search=test');
});

it('correctly parases a url without a port', () => {
const parsedUrl =
parseUrl('http://example.com/path1/path2?search=test&format=json');

expect(parsedUrl.protocol).toBe('http:');
expect(parsedUrl.hostname).toBe('example.com');
expect(parsedUrl.port).toBe('');
expect(parsedUrl.host).toBe('example.com');
expect(parsedUrl.origin).toBe('http://example.com');
expect(parsedUrl.pathname).toBe('/path1/path2');
expect(parsedUrl.hash).toBe('');
expect(parsedUrl.search).toBe('?search=test&format=json');
});
});

0 comments on commit 061665b

Please sign in to comment.