Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escaping names passed to Overpass #2074

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading