fixign tests.

This commit is contained in:
Jason Kulatunga 2022-10-29 13:30:20 -07:00
parent 2ca6f6ad96
commit 3973c4e003
3 changed files with 35 additions and 16 deletions

View File

@ -77,13 +77,8 @@ export class PouchdbRepository implements IDatabaseRepository {
constructor(couchDbEndpointBase: string, localPouchDb?: PouchDB.Database) {
// couchDbEndpointBase could be a relative or absolute path.
//if its absolute, we should pass it in, as-is
if (couchDbEndpointBase.indexOf('http://') === 0 || couchDbEndpointBase.indexOf('https://') === 0){
//absolute
this.remotePouchEndpoint = couchDbEndpointBase
} else {
//relative, we need to retrive the absolutePath from base
this.remotePouchEndpoint = GetEndpointAbsolutePath(globalThis.location, couchDbEndpointBase)
}
this.remotePouchEndpoint = GetEndpointAbsolutePath(globalThis.location, couchDbEndpointBase)
//setup PouchDB Plugins
//https://pouchdb.com/guides/mango-queries.html

View File

@ -4,6 +4,22 @@ describe('PouchdbRepository', () => {
describe('GetEndpointAbsolutePath', () => {
describe('with absolute http path', () => {
it('should return absolute path', async () => {
let currentUrl = new URL("http://www.example.com/")
const absoluteUrl = GetEndpointAbsolutePath(currentUrl, 'http://www.example2.com/my/test/path')
expect(absoluteUrl).toEqual('http://www.example2.com/my/test/path');
});
})
describe('with absolute https path', () => {
it('should return absolute path', async () => {
let currentUrl = new URL("http://www.example.com/")
const absoluteUrl = GetEndpointAbsolutePath(currentUrl, 'https://www.example2.com/my/test/path')
expect(absoluteUrl).toEqual('https://www.example2.com/my/test/path');
});
})
describe('with no subpath and no /web/', () => {
it('should return absolute path', async () => {
let currentUrl = new URL("http://www.example.com/")

View File

@ -5,16 +5,24 @@
//Fasten may be served behind a reverse proxy with a subpath, so lets try to find that component if it exists.
// if no subpath is found, just use the current url information to generate a path
export function GetEndpointAbsolutePath(currentUrl: {pathname: string, protocol: string, host: string}, relativePath: string): string {
//no `/web` path to strip out, lets just use the relative path
let absolutePath = relativePath
if(currentUrl.pathname.includes('/web')){
// probably running locally, and *may* include a subpath
let subPath = currentUrl.pathname.split('/web').slice(0, 1)[0]
if(subPath != "/"){
//subpath, so we need to update the absolutePath with the subpath before adding the relative path to the end
absolutePath = subPath + relativePath
//if its absolute, we should pass it in, as-is
if (relativePath.indexOf('http://') === 0 || relativePath.indexOf('https://') === 0){
//absolute
return relativePath
} else {
//relative, we need to retrive the absolutePath from base
//no `/web` path to strip out, lets just use the relative path
let absolutePath = relativePath
if(currentUrl.pathname.includes('/web')){
// probably running locally, and *may* include a subpath
let subPath = currentUrl.pathname.split('/web').slice(0, 1)[0]
if(subPath != "/"){
//subpath, so we need to update the absolutePath with the subpath before adding the relative path to the end
absolutePath = subPath + relativePath
}
}
return `${currentUrl.protocol}//${currentUrl.host}${absolutePath}`
}
return `${currentUrl.protocol}//${currentUrl.host}${absolutePath}`
}