Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test Angular 2 headers being sent by service using Http module?

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?

like image 334
chitty Avatar asked Feb 15 '17 23:02

chitty


Video Answer


1 Answers

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')).toEqu‌al('application/json‌​');         

      let options = new ResponseOptions({
         body: JSON.stringify({ data: 'returned' })
      });
      connection.mockRespond(new Response(options));
 });
like image 182
Amanda Avatar answered Oct 13 '22 01:10

Amanda