0
votes

Problem

The Observable.of convert arguments to an observable sequence means it can be used as a mock response to another Observable which is subscribe. But now, It seems Observable.of does not work when its called from an interceptor because methods which are subscribing to it does not execute

Expected behavior:

When Observable.of is called with the given data, the HttpClient should return the data to the method which is already subscribing to it

Demo

Live Demo (open DevTools console to read logs)

There is a service (mock.service.ts) which makes a mock HTTP get request using HttpClient, the request arrives at mock.interceptor.ts and it want's to answer with the mock data using Observable.of (means wants to not let the request to goes over the network), the mock.service has a subscriber in app.component which is waiting for the response of the HttpClient. No data arrives in subscriber, nor error.

Any idea about how to implement interceptor that works with Observable.of?

2

2 Answers

3
votes

Try to change the return statement in interceptor intercept method to:

return Observable.of( new HttpResponse<any>( { body: mockData }))

Live Demo

1
votes

Observable.of is depricated use Observable.create instead

return Observable.create((ob) => {
      ob.next(new HttpResponse<any>({body: data}));
});