Skip to content

Commit

Permalink
fix Blob is not defined In SSR mode (#18033)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Paweł Fraś <fras.pawel@yahoo.com>
  • Loading branch information
3 people authored Nov 9, 2023
1 parent 5604580 commit b0bbddc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/

import {
HttpClient,
HTTP_INTERCEPTORS,
HttpClient,
HttpErrorResponse,
} from '@angular/common/http';
import {
Expand All @@ -18,10 +18,20 @@ import { TestBed } from '@angular/core/testing';
import { FileReaderService } from '@spartacus/storefront';
import { take } from 'rxjs/operators';
import { BlobErrorInterceptor } from './blob-error.interceptor';
import { WindowRef } from '@spartacus/core';

const errors = JSON.stringify({
errors: [{ type: 'InvalidTokenError' }],
});

const error = new Blob([errors], {
type: 'application/json',
});

describe('BlobErrorInterceptor', () => {
let httpMock: HttpTestingController;
let http: HttpClient;
let windowRef: WindowRef;

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -38,32 +48,50 @@ describe('BlobErrorInterceptor', () => {

httpMock = TestBed.inject(HttpTestingController);
http = TestBed.inject(HttpClient);
windowRef = TestBed.inject(WindowRef);
});

it(`Should extract JSON from errors wrapped in blob`, (done: DoneFn) => {
it(`Should extract json from errors wrapped in blob`, (done) => {
spyOn(windowRef, 'isBrowser').and.returnValue(true);

http
.get('/occ', { responseType: 'blob' as 'json' })
.pipe(take(1))
.subscribe({
error: (err: HttpErrorResponse) => {
expect(err.status).toEqual(401);
expect(err.error.errors[0].type).toEqual('InvalidTokenError');
done();
},
});

const mockReq: TestRequest = httpMock.expectOne((req) => {
return req.method === 'GET' && req.url === '/occ';
});

const errors = { errors: [{ type: 'InvalidTokenError' }] };
const errorBlob = new Blob([JSON.stringify(errors)], {
type: 'application/json',
});

mockReq.flush(errorBlob, {
mockReq.flush(error, {
status: 401,
statusText: 'Unauthorized',
});

expect(windowRef.isBrowser).toHaveBeenCalled();
done();
});

it(`Should extract json from errors wrapped in blob`, (done) => {
spyOn(windowRef, 'isBrowser').and.returnValue(false);

http
.get('/occ', { responseType: 'blob' as 'json' })
.pipe(take(1))
.subscribe();

const mockReq: TestRequest = httpMock.expectOne((req) => {
return req.method === 'GET' && req.url === '/occ';
});

mockReq.flush(error);

expect(windowRef.isBrowser).not.toHaveBeenCalled();
done();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import {
HttpInterceptor,
HttpRequest,
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { FileReaderService } from '@spartacus/storefront';
import { WindowRef } from '@spartacus/core';
import { Observable, throwError } from 'rxjs';
import { catchError, switchMap } from 'rxjs/operators';

@Injectable({
providedIn: 'root',
})
export class BlobErrorInterceptor implements HttpInterceptor {
constructor(private fileReaderService: FileReaderService) {}
protected readonly fileReaderService = inject(FileReaderService);
protected readonly windowRef = inject(WindowRef);

intercept(
request: HttpRequest<any>,
Expand All @@ -29,6 +31,7 @@ export class BlobErrorInterceptor implements HttpInterceptor {
return next.handle(request).pipe(
catchError((errResponse: any) => {
if (
this.windowRef.isBrowser() &&
errResponse instanceof HttpErrorResponse &&
errResponse.error instanceof Blob &&
errResponse.error.type === 'application/json'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <spartacus-team@sap.com>
*
* SPDX-License-Identifier: Apache-2.0
*/

export * from './blob-error.interceptor';
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './config/index';
export * from './facade/index';
export * from './feature-name';
export * from './model/index';
export * from './http-interceptors';

0 comments on commit b0bbddc

Please sign in to comment.