Skip to content

Commit

Permalink
Expect cache logging context in httpGet requests
Browse files Browse the repository at this point in the history
  • Loading branch information
hudson-newey committed Nov 19, 2024
1 parent 5d3447f commit 91a0141
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 38 deletions.
33 changes: 17 additions & 16 deletions src/app/services/baw-api/baw-api.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
disableCache,
enableCache,
} from "@services/cache/ngHttpCachingConfig";
import { withCacheLogging } from "@services/cache/cache-logging.service";
import {
BawSessionService,
guestAuthToken,
Expand Down Expand Up @@ -534,10 +535,10 @@ describe("BawApiService", () => {
.subscribe();

const context = catchFunctionCall().request.context;
const expectedContext = withNgHttpCachingContext({
...defaultCachingConfig,
...cacheOptions,
});
const expectedContext = withNgHttpCachingContext(
{ ...defaultCachingConfig, ...cacheOptions },
withCacheLogging()
);
expectedContext.set(CREDENTIALS_CONTEXT, true);

expect(context).toEqual(expectedContext);
Expand All @@ -554,10 +555,10 @@ describe("BawApiService", () => {

const context = catchFunctionCall().request.context;

const expectedContext = withNgHttpCachingContext({
...defaultCachingConfig,
...cacheOptions,
});
const expectedContext = withNgHttpCachingContext(
{ ...defaultCachingConfig, ...cacheOptions },
withCacheLogging()
);
expectedContext.set(CREDENTIALS_CONTEXT, true);

expect(context).toEqual(expectedContext);
Expand All @@ -573,10 +574,10 @@ describe("BawApiService", () => {
.subscribe();

const context = catchFunctionCall().request.context;
const expectedContext = withNgHttpCachingContext({
...defaultCachingConfig,
...cacheOptions,
});
const expectedContext = withNgHttpCachingContext(
{ ...defaultCachingConfig, ...cacheOptions },
withCacheLogging()
);
expectedContext.set(CREDENTIALS_CONTEXT, true);

expect(context).toEqual(expectedContext);
Expand All @@ -601,10 +602,10 @@ describe("BawApiService", () => {
service.httpGet(testedApiPath, defaultApiHeaders, options).subscribe();

const context = catchFunctionCall().request.context;
const expectedContext = withNgHttpCachingContext({
...defaultCachingConfig,
...cacheOptions,
});
const expectedContext = withNgHttpCachingContext(
{ ...defaultCachingConfig, ...cacheOptions },
withCacheLogging()
);
expectedContext.set(CREDENTIALS_CONTEXT, false);

expect(context).toEqual(expectedContext);
Expand Down
3 changes: 2 additions & 1 deletion src/app/services/baw-api/baw-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
withNgHttpCachingContext,
} from "ng-http-caching";
import { defaultCachingConfig } from "@services/cache/ngHttpCachingConfig";
import { withCacheLogging } from "@services/cache/cache-logging.service";
import { BawSessionService } from "./baw-session.service";
import { CREDENTIALS_CONTEXT } from "./api.interceptor.service";
import { BAW_SERVICE_OPTIONS } from "./api-common";
Expand Down Expand Up @@ -457,7 +458,7 @@ export class BawApiService<
const fullOptions = this.buildServiceOptions(options);

const cachingOptions = this.buildCachingOptions(options);
const cacheContext = withNgHttpCachingContext(cachingOptions);
const cacheContext = withNgHttpCachingContext(cachingOptions, withCacheLogging());

const context = this.withCredentialsHttpContext(fullOptions, cacheContext);

Expand Down
11 changes: 1 addition & 10 deletions src/app/services/cache/cache-logging.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@ import { CacheSettings, CACHE_SETTINGS } from "./cache-settings";

const CACHE_LOGGING = new HttpContextToken<boolean>(() => false);

/**
* Log that a request was cached. This should be used in conjunction with the
* `withCache()` context
*
* ```ts
* return this.http.get("http://api/1", {
* context: withCache({context: withCacheLogging()}),
* });
* ```
*/
/** Log a request that was cached */
export const withCacheLogging = (context = new HttpContext()) =>
context.set(CACHE_LOGGING, true);

Expand Down
12 changes: 1 addition & 11 deletions src/app/services/cache/ngHttpCachingConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { HttpRequest } from "@angular/common/http";
import { NgHttpCachingConfig } from "ng-http-caching";
import { environment } from "src/environments/environment";
import { secondsToMilliseconds } from "@helpers/unitConverters/unitConverters";
import { withCacheLogging } from "./cache-logging.service";

type IsCacheablePredicate = NgHttpCachingConfig["isCacheable"];

Expand Down Expand Up @@ -74,16 +73,7 @@ export const defaultCachingConfig = {
function isCacheableDefault(req: HttpRequest<any>): boolean {
const shouldCacheMethod = req.method === "GET" || req.method === "HEAD";
// const isFilterRequest = req.method === "POST" && req.url.endsWith("/filter");
const isCacheable = shouldCacheMethod;

// we support debug logging of requests that will be cached through the sites
// admin panel
// this is useful for debugging caching issues
if (isCacheable) {
withCacheLogging(req.context);
}

return isCacheable;
return shouldCacheMethod;
}

// TODO: when we add support for caching filter requests we will need to use a
Expand Down

0 comments on commit 91a0141

Please sign in to comment.