title: How do I unit test my request, given that the return result is always encrypted?
body: <p>I have this helper class that helps to generate request header and return it as a <code>request.SuperAgentRequest</code> using <code>superagent-bluebird-promise</code>.</p>
  
  <p>My question here is</p>
  
  <p>How do I unit test my request? Given that the return result is always encrypted.</p>
  
  <p><strong>How to test <code>security.decryptJWE</code>?</strong></p>
  
  <p>What I have done in my unit testing so far is to only check the number of times the function is being called. </p>
  
  <p>Unit test class:</p>
  
  <pre><code>let sendRequestSpy = sinon.spy(helper, "sendRequest");
  
  main.updateInfo();
  
  sinon.assert.callCount(sendRequestSpy, 10);
  </code></pre>
  
  <p>helper class:</p>
  
  <pre><code>const restClient = require('superagent-bluebird-promise');
  
  function sendRequest(id) {
      ...
  
      _.set(headers, "Authorization", authHeaders);
  
      let request = restClient.get(url);
  
      // Set headers
      if (!_.isUndefined(headers) &amp;&amp; !_.isEmpty(headers)) {
          request.set(headers);
      }
  
      return request;
  }
  </code></pre>
  
  <p>main class:</p>
  
  <pre><code>function updateInfo {
  
      ...
          for (var i = 0; i &lt; list.length; i++) {
  
              let request = helper.sendRequest(id);
              request.buffer(true).end(function (err, res) {
              ...
                  let data = {
                      body: res.body,
                      text: res.text
                  };
  
                  let apiData = data.text;
  
                  let privateKey = "./resource/priv_key.pem";
  
                  var jweParts = apiData.split(".");
                  security.decryptJWE(jweParts[0], jweParts[1], jweParts[2], jweParts[3], jweParts[4], privateKey)
                  .then(apiData =&gt; {
                      ...
                      //process here
                  }
              }
      }
  }
  </code></pre>
  
  <p>security class:</p>
  
  <pre><code>...
  security.decryptJWE = function decryptJWE(header, encryptedKey, iv, cipherText, tag, privateKey) {
    console.log("Decrypting JWE".green + " (Format: " + "header".red + "." + "encryptedKey".cyan + "." + "iv".green + "." + "cipherText".magenta + "." + "tag".yellow + ")");
    console.log(header.red + "." + encryptedKey.cyan + "." + iv.green + "." + cipherText.magenta + "." + tag.yellow);
    return new Promise((resolve, reject) =&gt; {
  
      var keystore = jose.JWK.createKeyStore();
  
      console.log((new Buffer(header, 'base64')).toString('ascii'));
  
      var data = {
        "type": "compact",
        "ciphertext": cipherText,
        "protected": header,
        "encrypted_key": encryptedKey,
        "tag": tag,
        "iv": iv,
        "header": JSON.parse(jose.util.base64url.decode(header).toString())
      };
      keystore.add(fs.readFileSync(privateKey, 'utf8'), "pem")
        .then(function (jweKey) {
          // {result} is a jose.JWK.Key
          jose.JWE.createDecrypt(jweKey)
            .decrypt(data)
            .then(function (result) {
              resolve(JSON.parse(result.payload.toString()));
            })
            .catch(function (error) {
              reject(error);
            });
        });
  
    })
      .catch(error =&gt; {
        console.error("Error with decrypting JWE: %s".red, error);
        throw "Error with decrypting JWE";
      })
  }
  </code></pre>