All files / lib/agent/batch Request.js

100% Statements 27/27
92.85% Branches 13/14
100% Functions 6/6
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147    8x 8x                                               11x   11x 11x         11x         11x         11x         11x         11x         11x         11x                           4x 4x 4x                                   10x                                 2x 2x 1x 1x 1x       1x                           1x 1x 1x 1x       8x  
"use strict";
 
const _ = require("lodash");
const Response = require("./Response");
 
/**
 * Request class implements OData particular request processing
 *
 * @public
 * @class Request
 */
class Request {
  /**
   * Initialize instance of the batch Request class
   *
   * @param {string} httpMethod is string (GET/POST/MERGE/PUT/DELETE) which identifies HTTP method
   * @param {string} inputUrl is relative path to the service endpoint
   * @param {Object} headers is headers (Accept header has to be defined and it has to be application/json
   * @param {string} payload body of the request
   *
   * @public
   * @memberof Request
   */
  constructor(httpMethod, inputUrl, headers, payload) {
    let resolveRequest;
    let rejectRequest;
 
    Object.defineProperty(this, "promise", {
      value: new Promise(function (resolve, reject) {
        resolveRequest = resolve;
        rejectRequest = reject;
      }),
      writable: false,
    });
 
    Object.defineProperty(this, "httpMethod", {
      value: httpMethod,
      writable: false,
    });
 
    Object.defineProperty(this, "inputUrl", {
      value: inputUrl,
      writable: false,
    });
 
    Object.defineProperty(this, "headers", {
      value: headers,
      writable: false,
    });
 
    Object.defineProperty(this, "content", {
      value: payload,
      writable: false,
    });
 
    Object.defineProperty(this, "resolve", {
      value: resolveRequest,
      writable: false,
    });
 
    Object.defineProperty(this, "reject", {
      value: rejectRequest,
      writable: false,
    });
 
    this.responseType = null;
  }
 
  /**
   * Generate HTTP request which is part of thh multipart/mixed content for the OData batch
   *
   * @param {string} csrfToken passed to request headers
   *
   * @returns {string} request converted to the string
   *
   * @private
   * @memberof Request
   */
  payload(csrfToken) {
    let inputUrl = this.inputUrl.replace(/^\//, "");
    let body = this.body();
    return _.concat(
      ["Content-Type: application/http"],
      body.length > 0 ? [] : [`x-csrf-token: ${csrfToken}`],
      [
        "Content-Transfer-Encoding: binary\n",
        `${this.httpMethod} ${inputUrl} HTTP/1.1`,
        _.map(
          _.assign(
            body.length > 0
              ? _.assign(
                  {
                    "Content-Length": _.get(body, 0).length,
                  },
                  csrfToken ? { "x-csrf-token": `${csrfToken}` } : {}
                )
              : {},
            this.headers
          ),
          (value, key) => `${key}: ${value}`
        ).join("\n"),
      ],
      body.length > 0 ? "" : "\n",
      body
    ).join("\n");
  }
 
  /**
   * Create JSON string which contains body of the request
   *
   * @returns {string} JSON content
   *
   * @private
   * @memberof Request
   */
  body() {
    let content = [];
    if (this.content && _.get(this, "headers.Accept") === "application/json") {
      content = [JSON.stringify(this.content)];
    } else Eif (this.content) {
      throw new Error(
        `Stringifying for ${_.get(this, "headers.Accept")} not supported`
      );
    }
    return content;
  }
 
  /**
   * Parse response part of the OData batch response for the particular specified
   *
   * @param {string} rawResponse - part of the batch response for the specified request
   *
   * @returns {Promise} promise which is resolved by the particular response is parsed
   *
   * @private
   * @memberof Batch
   */
  process(rawResponse) {
    let response = new Response(rawResponse);
    response.request = this;
    response.promise.then(this.resolve).catch(this.reject);
    return this.promise;
  }
}
 
module.exports = Request;