lundi 25 mars 2019

Testing add multiple headers interceptor

I am trying to Test an interceptor service like this:

import { Injectable } from '@angular/core';
import { HttpInterceptor } from '@angular/common/http';

@Injectable()

export class ChacheCtrlInterceptor implements HttpInterceptor {

    constructor() { }

    intercept(req, next) {
        let httpCall = req.clone({
            headers: req.headers.set('Pragma', 'no-cache')
                                .set('Cache-Control', 'no-cache')
        });
        return next.handle(httpCall);
    }
}

This, just add this 2 headers to all the Http calls that I am doing in the application.

I know that for test if the interceptor adds the headers I have to do something like this:

it('should add Pragma header to request', () => {
        const req = new Http();
        expect(req.request.headers.has('Pragma')).toBeTruthy();
        expect(req.request.headers.get('Pragma')).toEqual('no-cache');
    });

it('should add Cache-Control header to request', () => {
        const req = new Http();
        expect(req.request.headers.has('Cache-Control')).toBeTruthy();
        expect(req.request.headers.get('Cache-Control')).toEqual('no-cache');

Any idea how can I coverage this code, I am new on karma testing :S

Aucun commentaire:

Enregistrer un commentaire