Skip to content

Commit b49389c

Browse files
committed
refactor(http): Make sur to pass context & transferCache from httpResource to the underlying request.
Prior to this change, both were accepted as argument but never passed to the client.
1 parent c492db4 commit b49389c

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

packages/common/http/src/resource.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ function normalizeRequest(
279279
reportProgress: unwrappedRequest.reportProgress,
280280
withCredentials: unwrappedRequest.withCredentials,
281281
responseType,
282+
context: unwrappedRequest.context,
283+
transferCache: unwrappedRequest.transferCache,
282284
},
283285
);
284286
}

packages/common/http/test/resource_spec.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88

99
import {ApplicationRef, Injector, signal} from '@angular/core';
1010
import {TestBed} from '@angular/core/testing';
11-
import {HttpEventType, provideHttpClient, httpResource} from '@angular/common/http';
11+
import {
12+
HttpEventType,
13+
provideHttpClient,
14+
httpResource,
15+
HttpContext,
16+
HttpContextToken,
17+
} from '@angular/common/http';
1218
import {HttpTestingController, provideHttpClientTesting} from '@angular/common/http/testing';
1319

1420
describe('httpResource', () => {
@@ -145,6 +151,40 @@ describe('httpResource', () => {
145151
expect(res.value()).toEqual([]);
146152
});
147153

154+
it('should pass all request parameters', () => {
155+
const backend = TestBed.inject(HttpTestingController);
156+
157+
const CTX_TOKEN = new HttpContextToken(() => 'value');
158+
const res = httpResource(
159+
() => ({
160+
url: '/data',
161+
params: {
162+
'fast': 'yes',
163+
},
164+
responseType: 'text', // This one is not overwritten (and no excess property check from ts)
165+
headers: {
166+
'X-Tag': 'alpha,beta',
167+
},
168+
reportProgress: true,
169+
context: new HttpContext().set(CTX_TOKEN, 'bar'),
170+
withCredentials: true,
171+
transferCache: {includeHeaders: ['Y-Tag']},
172+
}),
173+
{
174+
injector: TestBed.inject(Injector),
175+
},
176+
);
177+
TestBed.flushEffects();
178+
179+
const req = TestBed.inject(HttpTestingController).expectOne('/data?fast=yes');
180+
expect(req.request.headers.get('X-Tag')).toEqual('alpha,beta');
181+
expect(req.request.responseType).toEqual('json');
182+
expect(req.request.withCredentials).toEqual(true);
183+
expect(req.request.context.get(CTX_TOKEN)).toEqual('bar');
184+
expect(req.request.reportProgress).toEqual(true);
185+
expect(req.request.transferCache).toEqual({includeHeaders: ['Y-Tag']});
186+
});
187+
148188
it('should allow mapping data to an arbitrary type', async () => {
149189
const backend = TestBed.inject(HttpTestingController);
150190
const res = httpResource(

0 commit comments

Comments
 (0)