如何编写测试用例来删除角度中的cookie

问题描述 投票:0回答:1

我的拦截器看起来像这样:

import { Injectable } from '@angular/core';
import {
  HttpInterceptor,
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpErrorResponse
} from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { CookieService } from 'ngx-cookie-service';
@Injectable({
  providedIn: 'root'
})
export class HttpErrorService implements HttpInterceptor {
  constructor(private cookieService: CookieService) { }
  intercept(request: HttpRequest<any>,next: HttpHandler): Observable<HttpEvent<any>> {
    if(document.cookie.length > 4500){
      document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); });
    }
    return next.handle(request).pipe(
      catchError((error: HttpErrorResponse) => {
        return throwError(() => { return 'Something bad happened; please try again later.' })
      })
    );
  }
}

我只关注return.next之前的if block。我想编写删除 cookie 的测试用例。

 if(document.cookie.length > 4500){
      document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); });
    }

未找到合适的资源。我需要编写仅删除 cookie 的测试用例。

javascript angular unit-testing cookies jasmine
1个回答
0
投票

这是一种方法:

import { HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
import { CookieService } from 'ngx-cookie-service';
import { HttpErrorService } from './http-error.interceptor';

function deleteCookies() {
  document.cookie =
    'cookie1=existing-value; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
  document.cookie =
    'cookie2=existing-value; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
}

function executeInterceptor() {
  const requestDummy = {} as HttpRequest<unknown>;
  const nextDummy: any = {
    handle: () => {
      return new Observable((subscriber): void => {
        subscriber.complete();
      });
    },
  };
  const cookieServiceDummy = {} as CookieService;

  new HttpErrorService(cookieServiceDummy).intercept(requestDummy, nextDummy);
}

describe('HttpErrorService', () => {
  beforeEach(() => {
    deleteCookies();
  });

  it('deletes all cookies if total cookie length is > 4500', () => {
    document.cookie = 'cookie1=' + 'x'.repeat(2500);
    document.cookie = 'cookie2=' + 'y'.repeat(2000);

    executeInterceptor();

    expect(document.cookie).not.toContain('cookie1=');
    expect(document.cookie).not.toContain('cookie2=');
  });
});
import { Injectable } from '@angular/core';
import {
  HttpInterceptor,
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpErrorResponse,
} from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { CookieService } from 'ngx-cookie-service';

@Injectable({
  providedIn: 'root',
})
export class HttpErrorService implements HttpInterceptor {
  constructor(private cookieService: CookieService) {}
  intercept(
    request: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    if (document.cookie.length > 4500) {
      document.cookie.split(';').forEach(function (c) {
        document.cookie = c
          .replace(/^ +/, '')
          .replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/');
      });
    }
    return next.handle(request).pipe(
      catchError((error: HttpErrorResponse) => {
        return throwError(() => {
          return 'Something bad happened; please try again later.';
        });
      })
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.