Skip to content

Commit

Permalink
fix(map): remove SERVICE & REQUEST params from wms url
Browse files Browse the repository at this point in the history
If these params are given to OL for the WMS layer then they will most likely
cause an error. Added a url utility to remove them in a case-insensitive way.
  • Loading branch information
jahow committed Jul 31, 2023
1 parent ba4b049 commit c8349a1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
29 changes: 28 additions & 1 deletion libs/feature/record/src/lib/map-view/map-view.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class OpenLayersMapMock {
}
}

class InteractionsMock implements Collection<Interaction> {}
class InteractionsMock extends Collection<Interaction> {}

class mapManagerMock {
map = new OpenLayersMapMock()
Expand Down Expand Up @@ -743,6 +743,33 @@ describe('MapViewComponent', () => {
})
})
})

describe('WMS link containing REQUEST & SERVICE params', () => {
beforeEach(() => {
mdViewFacade.mapApiLinks$.next([
{
url: 'http://abcd.com/wxs/?request=GetCapabilities&a=2&Service=wms',
name: 'layer',
protocol: 'OGC:WMS',
type: MetadataLinkType.WMS,
},
])
mdViewFacade.geoDataLinks$.next([])
fixture.detectChanges()
})
it('emits a map context with WMS urls cleaned of unneeded parameters', () => {
expect(mapComponent.context).toEqual({
layers: [
{
url: 'http://abcd.com/wxs/?a=2',
name: 'layer',
type: 'wms',
},
],
view: expect.any(Object),
})
})
})
})

describe('prioritizePageScroll', () => {
Expand Down
5 changes: 3 additions & 2 deletions libs/feature/record/src/lib/map-view/map-view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
} from 'rxjs/operators'
import { MdViewFacade } from '../state/mdview.facade'
import { DataService } from '@geonetwork-ui/feature/dataviz'
import { removeSearchParams } from '@geonetwork-ui/util/shared'

@Component({
selector: 'gn-ui-map-view',
Expand Down Expand Up @@ -167,7 +168,7 @@ export class MapViewComponent implements OnInit, OnDestroy {
getLayerFromLink(link: MetadataLink): Observable<MapContextLayerModel> {
if (link.type === MetadataLinkType.WMS) {
return of({
url: link.url,
url: removeSearchParams(link.url, ['request', 'service']),
type: MapContextLayerTypeEnum.WMS,
name: link.name,
})
Expand All @@ -190,7 +191,7 @@ export class MapViewComponent implements OnInit, OnDestroy {
}))
)
}
return throwError('protocol not supported')
return throwError(() => 'protocol not supported')
}

selectLinkToDisplay(link: number) {
Expand Down
1 change: 1 addition & 0 deletions libs/util/shared/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './strip-html'
export * from './freeze'
export * from './geojson'
export * from './atomic-operations'
export * from './url'
14 changes: 14 additions & 0 deletions libs/util/shared/src/lib/utils/url.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { removeSearchParams } from './url'

describe('URL utils', () => {
describe('removeSearchParams', () => {
it('removes given search params in a case insensitive way', () => {
expect(
removeSearchParams(
'http://my.org/abc/?arg0=1234&arg1=aaa&Arg1=111&ARG2=&aRG3=fff&arg4=5678',
['ARG1', 'arg2', 'arg3']
)
).toEqual('http://my.org/abc/?arg0=1234&arg4=5678')
})
})
})
20 changes: 20 additions & 0 deletions libs/util/shared/src/lib/utils/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Removes the given search params from the URL completely; this is case-insensitive
* @param url
* @param searchParams
*/
export function removeSearchParams(
url: string,
searchParams: string[]
): string {
const toDelete = []
const urlObj = new URL(url, window.location.toString())
const keysLower = searchParams.map((p) => p.toLowerCase())
for (const param of urlObj.searchParams.keys()) {
if (keysLower.indexOf(param.toLowerCase()) > -1) {
toDelete.push(param)
}
}
toDelete.map((param) => urlObj.searchParams.delete(param))
return urlObj.toString()
}
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"importHelpers": true,
"target": "es2020",
"module": "esnext",
"lib": ["es2019", "dom"],
"lib": ["es2019", "dom", "dom.iterable"],
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"baseUrl": ".",
Expand Down

0 comments on commit c8349a1

Please sign in to comment.