Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// emit last progress events
tickSeconds(10);
fakeEvents.next({
type: HttpEventType.UploadProgress,
loaded: 10,
total: 10
});
expect(component.progress).toBe(10 / 30);
expect(component.status).toBe('processing');
tickSeconds(15);
expect(component.progress).toBe(25 / 30);
// emit response and complete
fakeEvents.next(new HttpResponse());
fakeEvents.complete();
expect(component.progress).toBe(1);
expect(component.status).toBe('done');
// let the fake event observable a chance to realize that the processing is done
tickSeconds(1);
})
);
firstName: testUser.firstName,
lastName: testUser.lastName,
token: 'fake-jwt-token'
};
return of(new HttpResponse({ status: 200, body }));
} else {
// else return 400 bad request
return throwError({ error: { message: 'Username or password is incorrect' } });
}
}
// get users
if (request.url.endsWith('/users') && request.method === 'GET') {
// check for fake auth token in header and return users if valid, this security is implemented server side in a real application
if (request.headers.get('Authorization') === 'Bearer fake-jwt-token') {
return of(new HttpResponse({ status: 200, body: [testUser] }));
} else {
// return 401 not authorised if token is null or invalid
return throwError({ error: { message: 'Unauthorised' } });
}
}
// pass through any requests not handled above
return next.handle(request);
}))
const testUser: any = {
username: 'valid',
password: 'valid'
};
const body = JSON.parse(request.body);
if (body.username === testUser.username && body.password === testUser.password) {
return observableOf(
new HttpResponse({
status: 200,
body: { token: 'fake-jwt-token' }
})
);
} else {
return observableOf(new HttpResponse({ status: 401 }));
}
}
return next.handle(request);
}
}
return Observable.create((observer: any) => {
const itemToRemove = this.internalFoodList.find(x => x.id === id);
const indexToRemove = this.internalFoodList.indexOf(itemToRemove);
this.internalFoodList.splice(indexToRemove, 1);
observer.next(
new HttpResponse({
status: 204
})
);
observer.complete();
});
}
registerHandle(newUser: StorageUser, update = false) {
const duplicateUser = this.users.find(user => user.email === newUser.email);
if (duplicateUser && !update) {
return throwError({ error: { message: 'Account with email "' + newUser.email + '" already exists' } });
}
if (update && duplicateUser) {
Object.assign(duplicateUser, newUser);
} else {
this.users.push(newUser);
}
localStorage.setItem('users', JSON.stringify(this.users));
const body = this.getUserJWT(newUser);
return of(new HttpResponse({ status: 200, body: this.generateToken(body) }));
}
public handle(req: HttpRequest): Observable> {
let subject = new BehaviorSubject(new HttpResponse());
return subject.asObservable();
}
}
}));
} else if (this.state === SERVER_STATE.UNAUTHORIZED) {
return of(new HttpResponse({
status: 401,
body: { error: 'Unauthorized'},
}));
} else if (this.state === SERVER_STATE.ERROR) {
return of(new HttpResponse({
status: 500,
body: { error: 'Internal server error'},
}));
}
switch (method) {
case RequestMethod.Get:
if (Number.isInteger(id)) {
return of(new HttpResponse({
body: this.rangers.find(ranger => ranger.id === id),
status: 200,
}));
} else {
return of(new HttpResponse({
body: this.rangers.map(ranger => ({ id: ranger.id, name: ranger.name })),
status: 200,
}));
}
case RequestMethod.Post:
body.id = this.rangers.length;
this.rangers.push(body);
return of(new HttpResponse({
body,
status: 200,
}));
function ok(body?) {
return of(new HttpResponse({ status: 200, body }))
}