This is an automated email from the ASF dual-hosted git repository.

glynnbird pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb-nano.git


The following commit(s) were added to refs/heads/main by this push:
     new 93a9eff  to avoid circular references (#275)
93a9eff is described below

commit 93a9effbf76e2affe30719f27a9842801640ce21
Author: Glynn Bird <[email protected]>
AuthorDate: Wed Sep 1 10:33:31 2021 +0100

    to avoid circular references (#275)
    
    * use selective attribute copy instead of clone to avoid circular 
references - fixes issue #274
    
    * add a couple of tests to prevent circular references silently regressing
    
    Co-authored-by: Glynn Bird <[email protected]>
---
 lib/nano.js                    |  7 ++++--
 test/attachment.insert.test.js | 17 +++++++++++++++
 test/nano.agent.test.js        | 48 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/lib/nano.js b/lib/nano.js
index f065022..a9a81c4 100644
--- a/lib/nano.js
+++ b/lib/nano.js
@@ -381,8 +381,11 @@ module.exports = exports = function dbScope (cfg) {
     }
 
     // scrub and log
-    delete req.agent // Agent contains circular refs that does not works well 
with JSON.stringify
-    const scrubbedReq = JSON.parse(JSON.stringify(req))
+    const scrubbedReq = {
+      method: req.method,
+      headers: JSON.parse(JSON.stringify(req.headers)),
+      url: req.url
+    }
     scrubRequest(scrubbedReq, true)
     log(scrubbedReq)
 
diff --git a/test/attachment.insert.test.js b/test/attachment.insert.test.js
index 9125e01..8619e2d 100644
--- a/test/attachment.insert.test.js
+++ b/test/attachment.insert.test.js
@@ -93,3 +93,20 @@ test('should be able to insert document attachment as stream 
- PUT /db/docname/a
   expect(reply).toStrictEqual(response)
   expect(scope.isDone()).toBe(true)
 })
+
+test('should be able to insert document attachment as stream with circular 
reference - PUT /db/docname/attachment - db.attachment.insert', async () => {
+  // mocks
+  const response = { ok: true, id: 'docname', rev: '2-456' }
+  const scope = nock(COUCH_URL, { reqheaders: { 'content-type': 'image/jpg' } 
})
+    .put('/db/docname/logo2.jpg?rev=1-150', image2)
+    .reply(200, response)
+
+  // test PUT /db/docname/attachment
+  const rs = fs.createReadStream('./test/logo.jpg')
+  // create artificial circular reference to make sure nano doesn't trip over 
on it
+  rs.test = rs
+  const db = nano.db.use('db')
+  const reply = await db.attachment.insert('docname', 'logo2.jpg', rs, 
'image/jpg', { rev: '1-150' })
+  expect(reply).toStrictEqual(response)
+  expect(scope.isDone()).toBe(true)
+})
diff --git a/test/nano.agent.test.js b/test/nano.agent.test.js
new file mode 100644
index 0000000..88cd1e2
--- /dev/null
+++ b/test/nano.agent.test.js
@@ -0,0 +1,48 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy 
of
+// the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations 
under
+// the License.
+
+const nock = require('nock')
+const Nano = require('..')
+const COUCH_URL = 'http://localhost:5984'
+const http = require('http')
+const httpAgent = new http.Agent()
+
+afterEach(() => {
+  nock.cleanAll()
+})
+
+test('should be able to log output with a user-supplied http agent', async () 
=> {
+  // setup Nano with custom logger
+  const logs = []
+  const nano = Nano({
+    url: COUCH_URL,
+    log: (data) => {
+      logs.push(data)
+    },
+    requestDefaults: {
+      agent: httpAgent
+    }
+  })
+
+  // mocks
+  const response = { _id: 'id', rev: '1-123', a: 1, b: 'two', c: true }
+  const scope = nock(COUCH_URL)
+    .get('/db/id')
+    .reply(200, response)
+
+  // test GET /db/id
+  const db = nano.db.use('db')
+  const p = await db.get('id')
+  expect(p).toStrictEqual(response)
+  expect(logs.length).toBe(2)
+  expect(scope.isDone()).toBe(true)
+})

Reply via email to