Source: Expression.js

  1. const _ = require("lodash");
  2. const Util = require("../util/Util");
  3. /**
  4. * Represents request expressions used to modify the request before doing the test
  5. */
  6. class Expression {
  7. /**
  8. *
  9. * @param {object} element - one of the elements generated during the yaml parsing
  10. * @return {boolean}
  11. */
  12. static isExpression(element) {
  13. const keys = Object.keys(element);
  14. return (keys.length > 0 &&
  15. (keys[0].startsWith("request") || keys[0].startsWith("set "))
  16. );
  17. }
  18. /**
  19. *
  20. * @param {object} element - one of the elements generated during the yaml parsing
  21. */
  22. constructor(element) {
  23. this._path = Object.keys(element)[0];
  24. this._value = element[this._path];
  25. }
  26. /**
  27. * the path of the request property we want to modify
  28. * @return {string}
  29. */
  30. get path() {
  31. return this._path;
  32. }
  33. /**
  34. * the value we want to set in the request property path
  35. * @return {string}
  36. */
  37. get value() {
  38. return Util.cleanValue(this._value);
  39. }
  40. /**
  41. * Replace the property indicated by the expression path with the value indicated
  42. * @param {object} json - request that we are going to modify
  43. */
  44. apply(json) {
  45. // Chop off the mandatory request at the front
  46. const paths = this.path.split(".").slice(1);
  47. const path = paths.join(".");
  48. if (path) {
  49. _.set(json, path, this.value);
  50. }
  51. }
  52. /**
  53. * Returns the interaction part of a yaml object
  54. * {input: string, expect: object[]}
  55. * @return {object}
  56. */
  57. toYamlObject() {
  58. return {
  59. path: this.path,
  60. value: this.value,
  61. };
  62. }
  63. }
  64. module.exports = Expression;