From 4cfa658fde09c01b4e8615e28e83b1e073401f39 Mon Sep 17 00:00:00 2001 From: Girish Ramakrishnan Date: Sat, 15 Feb 2025 15:54:39 +0100 Subject: [PATCH] add some superagent tests --- src/superagent.js | 3 +-- src/test/superagent-test.js | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 src/test/superagent-test.js diff --git a/src/superagent.js b/src/superagent.js index f9e65a1ba..1f66ea11a 100644 --- a/src/superagent.js +++ b/src/superagent.js @@ -31,7 +31,6 @@ class Request { this.okFunc = (response) => response.ok; this.timer = { timeout: 0, id: null, controller: null }; this.retryCount = 0; - this.fetchOptions.headers.set('Content-Type', 'application/json'); } async _makeRequest() { @@ -101,7 +100,7 @@ class Request { send(data) { const contentType = this.fetchOptions.headers.get('Content-Type'); - if (contentType === 'application/json') { + if (!contentType || contentType === 'application/json') { this.fetchOptions.headers.set('Content-Type', 'application/json'); this.fetchOptions.body = JSON.stringify(data); } else if (contentType === 'application/x-www-form-urlencoded') { diff --git a/src/test/superagent-test.js b/src/test/superagent-test.js new file mode 100644 index 000000000..ac79ce5be --- /dev/null +++ b/src/test/superagent-test.js @@ -0,0 +1,25 @@ +/* global it:false */ +/* global describe:false */ +/* global before:false */ + +'use strict'; + +const expect = require('expect.js'), + safe = require('safetydance'), + superagent = require('../superagent.js'); + +describe('Superagent', function () { + it('can get URL', async function () { + await superagent.get('https://www.cloudron.io').set('User-Agent', 'Mozilla').timeout(10*1000); + }); + + it('follows redirect', async function () { + const response = await superagent.get('https://cloudron.io').set('User-Agent', 'Mozilla').timeout(10*1000); + expect(response.url).to.be('https://www.cloudron.io/'); + }); + + it('can disable redirects', async function () { + const [error] = await safe(superagent.get('https://cloudron.io').set('User-Agent', 'Mozilla').timeout(10*1000).redirects(0)); + expect(error).to.be.ok(); + }); +});