From 5819a0aab503a3421a2bb22d25c72472fd901785 Mon Sep 17 00:00:00 2001 From: Sonia Zorba Date: Thu, 29 Feb 2024 16:17:15 +0100 Subject: [PATCH] Propagated HTTP errors in HTTPStore containsItem function --- src/storage/httpStore.ts | 3 +++ test/globalSetup.js | 3 +++ test/storage/httpStore.test.ts | 8 +++++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/storage/httpStore.ts b/src/storage/httpStore.ts index 629eb78..fece641 100644 --- a/src/storage/httpStore.ts +++ b/src/storage/httpStore.ts @@ -76,6 +76,9 @@ export class HTTPStore implements AsyncStor // Just check headers if HEAD method supported const method = this.supportedMethods.has(HTTPMethod.HEAD) ? HTTPMethod.HEAD : HTTPMethod.GET; const value = await fetch(url, { ...this.fetchOptions, method }); + if (value.status !== 200 && value.status !== 404) { + throw new HTTPError(String(value.status)); + } return value.status === 200; } } diff --git a/test/globalSetup.js b/test/globalSetup.js index 6d9dafb..a776e5a 100644 --- a/test/globalSetup.js +++ b/test/globalSetup.js @@ -3,6 +3,9 @@ const express = require("express"); const app = express(); app.use(express.static("fixtures", { dotfiles: 'allow' })); +app.use('/forbidden', function (_req, res) { + return res.status(403).end(); +}); const server = app.listen(3000); module.exports = async () => { diff --git a/test/storage/httpStore.test.ts b/test/storage/httpStore.test.ts index 6cf4ce4..130865e 100644 --- a/test/storage/httpStore.test.ts +++ b/test/storage/httpStore.test.ts @@ -1,9 +1,10 @@ (global as any).fetch = require('node-fetch'); +import { HTTPError } from "../../src/errors"; import { HTTPStore } from "../../src/storage/httpStore"; import { openArray } from "../../src/creation"; -describe("Test MemoryStore", () => { +describe("Test HTTPStore", () => { const simpleFixtureStoreLE = new HTTPStore("http://localhost:3000/simple_LE.zarr"); it("Can open simple fixture", async () => { @@ -60,4 +61,9 @@ describe("Test MemoryStore", () => { const z = await openArray({ store: baseUrlStore, path: "simple_LE.zarr" }); expect(z.shape).toEqual([8, 8]); }); + + const forbiddenStore = new HTTPStore("http://localhost:3000/forbidden"); + it("Propagates HTTP server error", async () => { + await expect(openArray({ store: forbiddenStore })).rejects.toThrowError(HTTPError); + }); });