8000 test: demo parseRequestUrl override that turns `/foo/heroes` into `/h… · angular/in-memory-web-api@028747d · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 028747d

Browse files
committed
test: demo parseRequestUrl override that turns /foo/heroes into /heroes
closes #139
1 parent 71b1894 commit 028747d

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/app/hero-in-mem-data-override.service.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,17 @@ export class HeroInMemDataOverrideService extends HeroInMemDataService {
7979
});
8080
}
8181

82-
// parseRequestUrl override that logs the result
83-
// a more interesting example would give special treatment to some URLs
84-
// while leaving the others for the default parser.
82+
// parseRequestUrl override
83+
// Do this to manipulate the request URL or the parsed result
84+
// into something your data store can handle.
85+
// This example turns a request for `/foo/heroes` into just `/heroes`.
86+
// It leaves other URLs untouched and forwards to the default parser.
87+
// It also logs the result of the default parser.
8588
parseRequestUrl(url: string, utils: RequestInfoUtilities): ParsedRequestUrl {
86-
const parsed = utils.parseRequestUrl(url);
87-
console.log('parseRequestUrl override:', parsed);
89+
const newUrl = url.replace(/\/foo\/heroes/, '/heroes');
90+
// console.log('newUrl', newUrl);
91+
const parsed = utils.parseRequestUrl(newUrl);
92+
console.log(`parseRequestUrl override of '${url}':`, parsed);
8893
return parsed;
8994
}
9095

src/in-mem/http-backend.service.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,18 @@ describe('Http Backend Service', () => {
272272
);
273273
}));
274274

275+
it('can translate `foo/heroes` to `heroes` via `parsedRequestUrl` override', async(() => {
276+
http.get('api/foo/heroes')
277+
.map(res => res.json().data as Hero[])
278+
.subscribe(
279+
heroes => {
280+
// console.log(heroes);
281+
expect(heroes.length).toBeGreaterThan(0, 'should have heroes');
282+
},
283+
failure
284+
);
285+
}));
286+
275287
it('can get villains', async(() => {
276288
http.get('api/villains')
277289
.map(res => res.json().data as Hero[])

src/in-mem/http-client-backend.service.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,18 @@ describe('HttpClient Backend Service', () => {
325325
);
326326
}));
327327

328+
it('can translate `foo/heroes` to `heroes` via `parsedRequestUrl` override', async(() => {
329+
http.get<Data>('api/foo/heroes')
330+
.map(data => data.data as Hero[])
331+
.subscribe(
332+
heroes => {
333+
// console.log(heroes);
334+
expect(heroes.length).toBeGreaterThan(0, 'should have heroes');
335+
},
336+
failure
337+
);
338+
}));
339+
328340
it('can get villains', async(() => {
329341
http.get<Data>('api/villains')
330342
.map(data => data.data as Hero[])

0 commit comments

Comments
 (0)
0