When testing an Angular 2 service that makes http GET, POST, PUT, etc. requests, is there a way to test and verify the headers that are being sent?
Yes! If you use the Angular2 MockBackend module inside your unit tests, you can subscribe to connections and check your headers inside there. For example:
var mockBackend = TestBed.get(MockBackend);
mockBackend.connections.subscribe((connection: MockConnection) => {
expect(connection.request.headers.get('Content-Type')).toEqual('application/json');
let options = new ResponseOptions({
body: JSON.stringify({ data: 'returned' })
});
connection.mockRespond(new Response(options));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With