All files / src/core request.js

100% Statements 15/15
100% Branches 4/4
100% Functions 3/3
100% Lines 14/14

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        1x                                 8x           8x   8x                 3x                 2x                   2x 2x   2x 4x   4x   2x   2x       2x      
/**
 * @fileoverview Request object implementation.
 * @author Dmytro Antonenko <dmitry.antonenko@pubwebkit.com>
 */
const Uri = goog.require('goog.Uri');
 
/**
 * Application request.
 * @extends {Uri}
 */
export class Request extends Uri {
  /**
   * Creates a simple request object based on the provided URL.
   * @param {!Object} routeData Object that contains information about current
   * route.
   * @param {string} uri The uri we are constructing this object from.
   * @param {string} queryVals The values for query path.
   * @param {boolean=} opt_ignoreCase Whether or not we are performing a case
   * sensitive parse.
   */
  constructor(routeData, uri, queryVals, opt_ignoreCase) {
    super(uri, opt_ignoreCase);
 
    /**
     * @const {!Object}
     * @private
     */
    this.routeData_ = routeData;
 
    this.setQueryData(queryVals, false);
  }
 
  /**
   * Return route data by key or all values as object
   * @param {string=} opt_key
   * @return {!Object|string}
   */
  getRouteData(opt_key) {
    return opt_key != null ? this.routeData_[opt_key] : this.routeData_;
  }
 
  /**
   * Convert this object to a simple JSON object.
   * @return {!Object}
   * @override
   */
  toJSON() {
    const obj = {
      domain: this.getDomain(),
      path: this.getPath(),
      port: this.getPort(),
      query: this.getQuery(),
      scheme: this.getScheme(),
      userInfo: this.getUserInfo(),
      routeData: this.routeData_,
      queryData: {}
    };
    const queryData = this.getQueryData();
    const keys = queryData.getKeys();
 
    for (let i = 0; i < keys.length; i++) {
      let values = queryData.getValues(keys[i]);
 
      if (values.length > 1) {
        // If a key is used multiple times.
        obj.queryData[keys[i]] = values;
      } else {
        obj.queryData[keys[i]] = queryData.get(keys[i]);
      }
    }
 
    return obj;
  }
}