Skip to content

fix(http): Reset headers, progress, and statusCode when using set() in HttpResource #62873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/common/http/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,14 @@ class HttpResourceImpl<T>
this.client = injector.get(HttpClient);
}

override set(value: T): void {
super.set(value);

this._headers.set(undefined);
this._progress.set(undefined);
this._statusCode.set(undefined);
}

// This is a type only override of the method
declare hasValue: () => this is HttpResourceRef<Exclude<T, undefined>>;
}
15 changes: 15 additions & 0 deletions packages/common/http/test/resource_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,19 @@ describe('httpResource', () => {
req = backend.expectOne('/data');
req.flush([]);
});

it('should reset past request data when using set()', async () => {
const backend = TestBed.inject(HttpTestingController);
const res = httpResource(() => '/data', {injector: TestBed.inject(Injector)});
TestBed.tick();
const req = backend.expectOne('/data');
req.flush([]);
await TestBed.inject(ApplicationRef).whenStable();

res.set([]);

expect(res.headers()).toBe(undefined);
expect(res.progress()).toBe(undefined);
expect(res.statusCode()).toBe(undefined);
});
});