Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging in response handling scripts #2

Open
stanislav-bios-baranov opened this issue Jul 7, 2018 · 3 comments
Open

Logging in response handling scripts #2

stanislav-bios-baranov opened this issue Jul 7, 2018 · 3 comments

Comments

@stanislav-bios-baranov
Copy link

Hello. According to documentation (https://www.jetbrains.com/help/idea/http-client-in-product-code-editor.html#using-response-handler-scripts), response handler scripting API is limited to 2 objects:

  • client
  • response

From IntelliJ IDEA internal help:

/**
 * The file provides stubs for JavaScript objects accessible from HTTP Client response handler scripts.
 * It doesn't perform any real operation and should be used for documentation purpose.
 */

/**
 * The object holds HTTP Client session meta data, e.g. list of global variables.
 *
 * @type {HttpClient}
 */
var client = new HttpClient();

/**
 * The object holds information about HTTP Response.
 *
 * @type {HttpResponse}
 */
var response = new HttpResponse();

/**
 * HTTP Client session meta data, e.g. list of global variables.
 *
 * HTTP Client session is started on IDE start and ends on IDE close,
 * values are not preserved after IDE restart.
 */
function HttpClient() {
  /**
   * Global variables defined in response handler scripts,
   * can be used as variables in HTTP Requests,
   *
   * Example:
   * ### Authorization request, receives token as an attribute of json body
   * GET https://example.com/auth
   *
   * > {% client.global.set("auth_token", response.body.token) %}
   *
   * ### Request executed with received auth_token
   * GET http://example.com/get
   * Authorization: Bearer {{auth_token}}
   *
   * @type {Variables}
   */
  this.global = new Variables();

  /**
   * Creates test with name 'testName' and body 'func'.
   * All tests will be executed right after response handler script.
   * @param testName {string}
   * @param func {function}
   */
  this.test = function (testName, func) {
  };

  /**
   * Checks that condition is true and throw an exception otherwise.
   * @param condition {boolean}
   * @param message {string} optional parameter, if specified it will be used as an exception message.
   */
  this.assert = function (condition, message) {
  };
}

/**
 * Variables storage, can be used to define, undefine or retrieve variables.
 */
function Variables() {
  /**
   * Saves variable with name 'varName' and sets its value to 'varValue'.
   * @param varName {string}
   * @param varValue {string}
   */
  this.set = function (varName, varValue) {
  };

  /**
   * Returns value of variable 'varName'.
   * @param varName {string}
   * @returns {string}
   */
  this.get = function (varName) {
    return varValue
  };

  /**
   * Checks no variables are defined.
   * @returns {boolean}
   */
  this.isEmpty = function () {
    return true
  };

  /**
   * Removes variable 'varName'.
   * @param varName {string}
   */
  this.clear = function (varName) {
  };

  /**
   * Removes all variables.
   */
  this.clearAll = function () {
  };
}

/**
 * HTTP Response data object, contains information about response content, headers, status, etc.
 */
function HttpResponse() {
  /**
   * Response content, it is a string or JSON object if response content-type is json.
   * @type {string|object}
   */
  this.body = " ";

  /**
   * Response headers storage.
   * @type {ResponseHeaders}
   */
  this.headers = new ResponseHeaders();

  /**
   * Response status, e.g. 200, 404, etc.
   * @type {int}
   */
  this.status = 200;

  /**
   * Value of 'Content-Type' response header.
   * @type {ContentType}
   */
  this.contentType = new ContentType
}

/**
 * Headers storage, can be use to retrieve data about header value.
 */
function ResponseHeaders() {
  /**
   * Retrieves value of 'headerName' response header or null otherwise.
   * @param headerName {string}
   * @returns {string|null}
   */
  this.valueOf = function (headerName) {
    return headerValue
  };
}

/**
 * Content type data object, contains information from 'Content-Type' response header.
 */
function ContentType() {
  /**
   * MIME type of the response,
   * e.g. 'text/plain', 'text/xml', 'application/json'.
   * @type {string}
   */
  this.mimeType = "application/json";

  /**
   * String representation of the response charset,
   * e.g. utf-8.
   * @type {string}
   */
  this.charset = "utf-8";
}

Would be nice to have ability similar to console.log to have ability to post consumer produced messages into Response Handler output tab:

Response Handler output

For instance by altering client object as:

...
function HttpClient() {

  ...

  /**
   * Logs text message to ouput.
   * @param message {string} message to write to output.
   */
  this.log = function (message) {
  };
}
@SvetlanaZem
Copy link
Collaborator

It's possible to print to the console with print function, e.g. {% print("Hello") %}

Thanks for the suggestion, we'll reflect it in the js stub file next to client and response objects.

@stanislav-bios-baranov
Copy link
Author

@SvetlanaZem Thank you!

@SvetlanaZem
Copy link
Collaborator

Now it's also possible to log values as {% client.log("Hello") %} because it's easier to discover.

@SvetlanaZem SvetlanaZem self-assigned this Aug 26, 2019
@SvetlanaZem SvetlanaZem removed their assignment Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants