forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unify base path in HttpService (elastic#38237)
* unify modifyUrl on client and server * create BasePath as a separate entity on server * use BasePath class in http server * use BasePath a separate entity on client * use BasePath class on Http service on the client * switch client code to the new api * improve setver http service mocks * address comments #1 * address comments #2 * update docs * add comment why we define own typings
- Loading branch information
Showing
38 changed files
with
521 additions
and
525 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
docs/development/core/public/kibana-plugin-public.httpservicebase.basepath.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-public](./kibana-plugin-public.md) > [HttpServiceBase](./kibana-plugin-public.httpservicebase.md) > [basePath](./kibana-plugin-public.httpservicebase.basepath.md) | ||
|
||
## HttpServiceBase.basePath property | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
basePath: { | ||
get: () => string; | ||
prepend: (url: string) => string; | ||
remove: (url: string) => string; | ||
}; | ||
``` |
15 changes: 0 additions & 15 deletions
15
docs/development/core/public/kibana-plugin-public.httpservicebase.getbasepath.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
...development/core/public/kibana-plugin-public.httpservicebase.prependbasepath.md
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
.../development/core/public/kibana-plugin-public.httpservicebase.removebasepath.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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 { BasePath } from './base_path_service'; | ||
|
||
describe('BasePath', () => { | ||
describe('#get()', () => { | ||
it('returns an empty string if no basePath not provided', () => { | ||
expect(new BasePath().get()).toBe(''); | ||
}); | ||
|
||
it('returns basePath value if provided', () => { | ||
expect(new BasePath('/foo').get()).toBe('/foo'); | ||
}); | ||
|
||
describe('#prepend()', () => { | ||
it('adds the base path to the path if it is relative and starts with a slash', () => { | ||
const basePath = new BasePath('/foo/bar'); | ||
|
||
expect(basePath.prepend('/a/b')).toBe('/foo/bar/a/b'); | ||
}); | ||
|
||
it('leaves the query string and hash of path unchanged', () => { | ||
const basePath = new BasePath('/foo/bar'); | ||
|
||
expect(basePath.prepend('/a/b?x=y#c/d/e')).toBe('/foo/bar/a/b?x=y#c/d/e'); | ||
}); | ||
|
||
it('returns the path unchanged if it does not start with a slash', () => { | ||
const basePath = new BasePath('/foo/bar'); | ||
|
||
expect(basePath.prepend('a/b')).toBe('a/b'); | ||
}); | ||
|
||
it('returns the path unchanged it it has a hostname', () => { | ||
const basePath = new BasePath('/foo/bar'); | ||
|
||
expect(basePath.prepend('http://localhost:5601/a/b')).toBe('http://localhost:5601/a/b'); | ||
}); | ||
}); | ||
|
||
describe('#remove()', () => { | ||
it('removes the basePath if relative path starts with it', () => { | ||
const basePath = new BasePath('/foo/bar'); | ||
|
||
expect(basePath.remove('/foo/bar/a/b')).toBe('/a/b'); | ||
}); | ||
|
||
it('leaves query string and hash intact', () => { | ||
const basePath = new BasePath('/foo/bar'); | ||
|
||
expect(basePath.remove('/foo/bar/a/b?c=y#1234')).toBe('/a/b?c=y#1234'); | ||
}); | ||
|
||
it('ignores urls with hostnames', () => { | ||
const basePath = new BasePath('/foo/bar'); | ||
|
||
expect(basePath.remove('http://localhost:5601/foo/bar/a/b')).toBe( | ||
'http://localhost:5601/foo/bar/a/b' | ||
); | ||
}); | ||
|
||
it('returns slash if path is just basePath', () => { | ||
const basePath = new BasePath('/foo/bar'); | ||
|
||
expect(basePath.remove('/foo/bar')).toBe('/'); | ||
}); | ||
|
||
it('returns full path if basePath is not its own segment', () => { | ||
const basePath = new BasePath('/foo/bar'); | ||
|
||
expect(basePath.remove('/foo/barhop')).toBe('/foo/barhop'); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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. | ||
*/ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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 { modifyUrl } from '../../utils'; | ||
|
||
export class BasePath { | ||
constructor(private readonly basePath: string = '') {} | ||
|
||
public get = () => { | ||
return this.basePath; | ||
}; | ||
|
||
public prepend = (path: string): string => { | ||
if (!this.basePath) return path; | ||
return modifyUrl(path, parts => { | ||
if (!parts.hostname && parts.pathname && parts.pathname.startsWith('/')) { | ||
parts.pathname = `${this.basePath}${parts.pathname}`; | ||
} | ||
}); | ||
}; | ||
|
||
public remove = (path: string): string => { | ||
if (!this.basePath) { | ||
return path; | ||
} | ||
|
||
if (path === this.basePath) { | ||
return '/'; | ||
} | ||
|
||
if (path.startsWith(`${this.basePath}/`)) { | ||
return path.slice(this.basePath.length); | ||
} | ||
|
||
return path; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.