8000 HttpTestingController can't mock HttpErrorResponse with successful status · Issue #26161 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content
HttpTestingController can't mock HttpErrorResponse with successful status #26161
Closed
@GuruRAM

Description

@GuruRAM

I'm submitting a...


[x] Regression (a behavior that used to work and stopped working in a new release)

Current behavior

I want to test HttpInterceptor when HttpErrorResponse has a successful (200) status. The situation became possible with the latest http client if response JSON is not valid source. But it's not possible to Mock HttpErrorResponse with a successful status source. TestRequest.error() is not allowing to mock such a behavior

NOTE:
I try to intercept requests with html content and perform a special action on them. With the previous implementation of http client such responses were handled as successful HttpResponses and should be handled in next callback, now they considered as HttpErrorResponses and should be handled in error handler. The current http client also ignores response Content-Type taking into account only request's Content-Type(Accept header): https://github.com/angular/angular/blob/317d40d879cc5eac1073a9182384423596fc098b/packages/common/http/src/xhr.ts#L182 I would rather analyze both response and request Content-Type but that can be tricky and not as straightforward as extending/fixing TestRequest.

Expected behavior

I should be able to mock HttpErrorResponse with successful status because it's possible to reproduce this case with the real http client

Minimal reproduction of the problem with instructions

describe('HttpTestingController test', () => {
    let httpMock: HttpTestingController;
    let http: HttpClient;
    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [HttpClientTestingModule]
        });
        httpMock = TestBed.get(HttpTestingController);
        http = TestBed.get(HttpClient);
    });

    it('should do request', () => {
        const uri = 'test\\api';
        http.get<{ data: string }>(uri).subscribe((e) => console.log(e));
        let response = httpMock.expectOne(uri);
        // shouldn't throw here:
        response.error(<any>{}, { status: 200, statusText: "OK", headers: new HttpHeaders({ 'Content-Type': 'text/html' }) });
    });
})

What is the motivation / use case for changing the behavior?

I want to mock HttpErrorResponse with successful status to unit test custom HttpInterceptor

Environment


Angular version: 6.1.1


Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4A relatively minor issue that is not relevant to core functionsarea: common/httpIssues related to HTTP and HTTP Clientfreq1: lowhelp wantedAn issue that is suitable for a community contributor (based on its complexity/scope).state: confirmedtype: bug/fix

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0