Skip to content

Commit

Permalink
Escaping names passed to Overpass (#2074)
Browse files Browse the repository at this point in the history
* Escaping names passed to Overpass

Resolves #2073

* Add tests for relevant scenario

* Fix lint, improve coverage

---------

Co-authored-by: HarelM <[email protected]>
  • Loading branch information
zstadler and HarelM authored Nov 13, 2024
1 parent f03ad4d commit 8a41fd0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { inject, TestBed } from "@angular/core/testing";
import { provideHttpClient, withInterceptorsFromDi } from "@angular/common/http";
import { HttpTestingController, provideHttpClientTesting } from "@angular/common/http/testing";
import { OverpassTurboService } from "./overpass-turbo.service";

describe("OverpassTurboService", () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [],
providers: [
OverpassTurboService,
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting()
]
});
});

it("Should get a long way by name", inject([OverpassTurboService, HttpTestingController], async (service: OverpassTurboService, mockBackend: HttpTestingController) => {
// Arrange
const response = "<osm></osm>";
// Act
const promise = service.getLongWay("id", "name", false, false);

mockBackend.expectOne("https://overpass-api.de/api/interpreter").flush(response);
// Assert
const results = await promise;
expect(results.features.length).toBe(0);
}));

it("Should get a long mtb way by name", inject([OverpassTurboService, HttpTestingController], async (service: OverpassTurboService, mockBackend: HttpTestingController) => {
// Arrange
const response = "<osm></osm>";
// Act
const promise = service.getLongWay("id", "aaa", false, true);

mockBackend.expectOne(u => u.body.includes("mtb:name")).flush(response);
// Assert
const results = await promise;
expect(results.features.length).toBe(0);
}));

it("Should get a long waterway way by name", inject([OverpassTurboService, HttpTestingController], async (service: OverpassTurboService, mockBackend: HttpTestingController) => {
// Arrange
const response = "<osm></osm>";
// Act
const promise = service.getLongWay("id", "aaa", true, false);

mockBackend.expectOne(u => u.body.includes("waterway")).flush(response);
// Assert
const results = await promise;
expect(results.features.length).toBe(0);
}));

it("Should get a long way by name with '\"'", inject([OverpassTurboService, HttpTestingController], async (service: OverpassTurboService, mockBackend: HttpTestingController) => {
// Arrange
const response = "<osm></osm>";
// Act
const promise = service.getLongWay("id", "lalala\"", false, false);

mockBackend.expectOne(u => u.body.includes("lalala\\\"")).flush(response);
// Assert
const results = await promise;
expect(results.features.length).toBe(0);
}));

it("Should get a place by id", inject([OverpassTurboService, HttpTestingController], async (service: OverpassTurboService, mockBackend: HttpTestingController) => {
// Arrange
const response = "<osm></osm>";
// Act
const promise = service.getPlaceGeometry("42");

mockBackend.expectOne("https://overpass-api.de/api/interpreter").flush(response);
// Assert
const results = await promise;
expect(results.features.length).toBe(0);
}));
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ export class OverpassTurboService {
}
}

public async getLongWay(id: string, title: string, isWaterway: boolean, isMtbRoute: boolean): Promise<GeoJSON.FeatureCollection> {
public async getLongWay(id: string, name: string, isWaterway: boolean, isMtbRoute: boolean): Promise<GeoJSON.FeatureCollection> {
const quotedName = name.replace(/"/g, '\\"')
const query = `
way(${id});
complete
{
way(around:30)
[${isWaterway ? 'waterway' : 'highway'}]
["${isMtbRoute ? 'mtb:name' : 'name'}"="${title}"];
["${isMtbRoute ? 'mtb:name' : 'name'}"="${quotedName}"];
}
out geom;`;
return await this.getGeoJsonFromQuery(query);
Expand Down

0 comments on commit 8a41fd0

Please sign in to comment.