From ba11d966ff86b2f1dc2611e1be7f4ceb1ad3f310 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Julian=20B=C3=A4ume?= <julian.baeume@open-xchange.com>
Date: Mon, 4 Apr 2022 09:44:16 +0200
Subject: [PATCH] OXUIB-1528: Read-through cache not working

Root cause: cache keys handled differently when reading/writing
Solution: make sure keys are handled equally

Fixes OXUIB-1528
---
 spec/file_caching_test.js | 17 +++++++++++++++++
 src/files.js              |  2 +-
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/spec/file_caching_test.js b/spec/file_caching_test.js
index 71c7981..ccad2ea 100644
--- a/spec/file_caching_test.js
+++ b/spec/file_caching_test.js
@@ -12,6 +12,7 @@ describe('File caching service', function () {
   let app
 
   before(async function () {
+    let count = 0
     mockConfig({ urls: ['http://ui-server/'] })
     mockFetch({
       'http://ui-server': {
@@ -33,6 +34,13 @@ describe('File caching service', function () {
         '/index.html': () => new Response('<html><head></head><body>it\'s me</body></html>', { headers: { 'content-type': 'text/html' } }),
         '/main.css': () => new Response('.foo { color: #000; }', { headers: { 'content-type': 'text/css' } }),
         '/favicon.ico': 'not really a favicon, though',
+        '/test.svg': () => {
+          if (count > 0) {
+            return new Response(null, { status: 404 })
+          }
+          count++
+          return new Response('<svg></svg>', { headers: { 'content-type': 'image/svg' } })
+        },
         '/image.png': () => {
           return new Response(image, {
             headers: {
@@ -89,6 +97,15 @@ describe('File caching service', function () {
     expect(response.text).to.equal('not really a favicon, though')
   })
 
+  it('caches files not referenced in manifest.json fetched from upstream servers', async function () {
+    let response = await request(app).get('/test.svg')
+    expect(response.statusCode).to.equal(200)
+    expect(String(response.body)).to.equal('<svg></svg>')
+    response = await request(app).get('/test.svg')
+    expect(response.statusCode).to.equal(200)
+    expect(String(response.body)).to.equal('<svg></svg>')
+  })
+
   it('returns 404 if file can not be resolved', async function () {
     const response = await request(app).get('/unknown-file.txt')
     expect(response.statusCode).to.equal(404)
diff --git a/src/files.js b/src/files.js
index 81413a9..815f7d6 100644
--- a/src/files.js
+++ b/src/files.js
@@ -94,7 +94,7 @@ class FileCache {
     const [[key, value]] =
       (await Promise.allSettled(config.urls.map(baseUrl => fetchData(path, baseUrl))))
         .filter(r => r.status === 'fulfilled').map(r => r.value)
-    this._cache[key] = value
+    this._cache[key.slice(1)] = value
     return value
   }
 
-- 
GitLab