Skip to content

Commit 0905d66

Browse files
polish HTTP 304 docs/test/handling (#4129)
After #4120 was merged, here's a bit of polish on top. - docs: Optimize JSDoc and comments. - test: The existing 304 test is moved to the dedicated status code block and extended with an `errorSpy` to also verify that no `error` event is emitted. - refactor: The success branch is moved before the error branch in `fetch()`. This is more intuitive. The bottom line is that this PR improves maintainability. For users, this PR doesn't change anything.
1 parent 01ae1b4 commit 0905d66

2 files changed

Lines changed: 35 additions & 32 deletions

File tree

js/http_fetcher.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const ERROR_TYPE_TO_TRANSLATION = {
3232
* - Authentication support (Basic, Bearer)
3333
* - Self-signed certificate support
3434
* @augments EventEmitter
35-
* @fires HTTPFetcher#response - When fetch succeeds with ok response
35+
* @fires HTTPFetcher#response - When fetch succeeds (including 304 Not Modified)
3636
* @fires HTTPFetcher#error - When fetch fails or returns non-ok response
3737
* @example
3838
* const fetcher = new HTTPFetcher(url, { reloadInterval: 60000 });
@@ -295,21 +295,21 @@ class HTTPFetcher extends EventEmitter {
295295

296296
const isSuccessfulResponse = response.ok || response.status === 304;
297297

298-
if (!isSuccessfulResponse) {
299-
const { delay, errorInfo } = this.#getDelayForResponse(response);
300-
nextDelay = delay;
301-
this.emit("error", errorInfo);
302-
} else {
298+
if (isSuccessfulResponse) {
303299
// Reset error counts on success
304300
this.serverErrorCount = 0;
305301
this.networkErrorCount = 0;
306302

307303
/**
308-
* Response event - fired when fetch succeeds
304+
* Response event - fired when fetch succeeds (including 304)
309305
* @event HTTPFetcher#response
310306
* @type {Response}
311307
*/
312308
this.emit("response", response);
309+
} else {
310+
const { delay, errorInfo } = this.#getDelayForResponse(response);
311+
nextDelay = delay;
312+
this.emit("error", errorInfo);
313313
}
314314
} catch (error) {
315315
const isTimeout = error.name === "AbortError";

tests/unit/functions/http_fetcher_spec.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,6 @@ describe("HTTPFetcher", () => {
5151
expect(text).toBe(responseData);
5252
});
5353

54-
it("should treat 304 responses as successful and reset error counters", async () => {
55-
server.use(
56-
http.get(TEST_URL, () => {
57-
return new HttpResponse(null, { status: 304 });
58-
})
59-
);
60-
61-
fetcher = new HTTPFetcher(TEST_URL, { reloadInterval: 60000 });
62-
fetcher.serverErrorCount = 2;
63-
fetcher.networkErrorCount = 3;
64-
65-
const responsePromise = new Promise((resolve) => {
66-
fetcher.on("response", (response) => {
67-
resolve(response);
68-
});
69-
});
70-
71-
fetcher.startPeriodicFetch();
72-
const response = await responsePromise;
73-
74-
expect(response.status).toBe(304);
75-
expect(fetcher.serverErrorCount).toBe(0);
76-
expect(fetcher.networkErrorCount).toBe(0);
77-
});
78-
7954
it("should emit error event on network failure", async () => {
8055
server.use(
8156
http.get(TEST_URL, () => {
@@ -126,6 +101,34 @@ describe("HTTPFetcher", () => {
126101
});
127102

128103
describe("HTTPFetcher - HTTP status code handling", () => {
104+
describe("304 Not Modified", () => {
105+
it("should emit response event for 304 and not emit error", async () => {
106+
server.use(
107+
http.get(TEST_URL, () => {
108+
return new HttpResponse(null, { status: 304 });
109+
})
110+
);
111+
112+
fetcher = new HTTPFetcher(TEST_URL, { reloadInterval: 60000 });
113+
fetcher.serverErrorCount = 2;
114+
fetcher.networkErrorCount = 3;
115+
116+
const responsePromise = new Promise((resolve) => {
117+
fetcher.on("response", resolve);
118+
});
119+
const errorSpy = vi.fn();
120+
fetcher.on("error", errorSpy);
121+
122+
fetcher.startPeriodicFetch();
123+
const response = await responsePromise;
124+
125+
expect(response.status).toBe(304);
126+
expect(errorSpy).not.toHaveBeenCalled();
127+
expect(fetcher.serverErrorCount).toBe(0);
128+
expect(fetcher.networkErrorCount).toBe(0);
129+
});
130+
});
131+
129132
describe("401/403 errors (Auth failures)", () => {
130133
it("should emit error with AUTH_FAILURE for 401", async () => {
131134
server.use(

0 commit comments

Comments
 (0)