How to use the postman-collection.Response function in postman-collection

To help you get started, we’ve selected a few postman-collection examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / postman-collection / postman-collection-tests.ts View on Github external
pmCollection.RequestBody.MODES.formdata; // $ExpectType string
pmCollection.RequestBody.MODES.urlencoded; // $ExpectType string
pmCollection.RequestBody.MODES.file; // $ExpectType string

// ResponseDefinition Tests
const respDef: pmCollection.ResponseDefinition = {code: 200, responseTime: 1};
respDef.code; // $ExpectType number
respDef.header; // $ExpectType HeaderDefinition[] | undefined
respDef.cookie; // $ExpectType CookieDefinition[] | undefined
respDef.body; // $ExpectType string | undefined
respDef.stream; // $ExpectType Buffer | Uint8Array | undefined
respDef.responseTime; // $ExpectType number
respDef.originalRequest; // $ExpectType RequestDefinition | undefined

// Response Tests
const response = new pmCollection.Response(respDef);
response.body; // $ExpectType string | undefined
response.code; // $ExpectType number
response.cookies; // $ExpectType CookieList
response.headers; // $ExpectType HeaderList
response.originalRequest; // $ExpectType Request | undefined
response.responseTime; // $ExpectType number
response.status; // $ExpectType string
response.stream; // $ExpectType Buffer | Uint8Array | undefined
response.responseSize; // $ExpectType number | undefined

response.update(respDef); // $ExpectType void

response.toJSON(); // $ExpectType any

response.reason(); // ExpectType string | undefined
github postmanlabs / postman-runtime / lib / requester / requester.js View on Github external
// emit the response.start event which eventually
                        // triggers responseStart callback
                        self.emit(responseStartEventName, null, sdkResponse, request, cookies, history);

                        // trigger completion of responseStart
                        onComplete(RESPONSE_START);
                    };

                // @todo get rid of jsonifyResponse
                responseJSON = core.jsonifyResponse(response, requestOptions);

                // transform response headers to SDK compatible HeaderList
                responseHeaders = _.transform(responseJSON.headers, transformMultiValueHeaders, []);

                // initialize SDK Response instance
                sdkResponse = new sdk.Response({
                    status: response && response.statusMessage,
                    code: responseJSON.statusCode,
                    header: responseHeaders
                });

                // add missing request headers so that they get bubbled up into the UI
                addMissingRequestHeaders(responseJSON.request && responseJSON.request.headers);

                // prepare history from request debug data
                history = getExecutionHistory(_.get(response, 'request._debug'));

                // Pull out cookies from the cookie jar, and make them chrome compatible.
                if (cookieJar && _.isFunction(cookieJar.getCookies)) {
                    cookieJar.getCookies(requestOptions.url, function (err, cookiesFromJar) {
                        if (err) {
                            return done();
github postmanlabs / postman-runtime / lib / requester / requester.js View on Github external
// Calculate the time taken for us to get the response.
            responseTime = Date.now() - startTime;

            if (res && res.timings) {
                // update response time to actual response end time
                // of the final request in the redirect chain.
                responseTime = Math.ceil(res.timings.end);
            }

            // prepare history from request debug data
            history = getExecutionHistory(debug);

            // Response in the SDK format
            // @todo reuse same response instance used for responseStart callback
            response = new sdk.Response({
                code: responseJSON.statusCode,
                status: res && res.statusMessage,
                header: responseHeaders,
                stream: resBody,
                responseTime: responseTime
            });

            onComplete(RESPONSE_END, response, history);
        });
    },
github postmanlabs / postman-runtime / lib / runner / extensions / event.command.js View on Github external
// Ensure that we have SDK instances, not serialized plain objects.
                    // @todo - should this be handled by the sandbox?
                    result && result._variables && (result._variables = new sdk.VariableScope(result._variables));
                    result && result.environment && (result.environment = new sdk.VariableScope(result.environment));
                    result && result.globals && (result.globals = new sdk.VariableScope(result.globals));
                    result && result.collectionVariables &&
                        (result.collectionVariables = new sdk.VariableScope(result.collectionVariables));
                    result && result.request && (result.request = new sdk.Request(result.request));

                    // @note Since postman-sandbox@3.5.2, response object is not included in the execution result.
                    // Refer: https://github.com/postmanlabs/postman-sandbox/pull/512
                    // Adding back here to avoid breaking change in `script` callback.
                    // @todo revisit script callback args in runtime v8.
                    payload.context && payload.context.response &&
                        (result.response = new sdk.Response(payload.context.response));

                    // persist the pm.variables for the next script
                    result && result._variables &&
                        (payload.context._variables = new sdk.VariableScope(result._variables));

                    // persist the pm.variables for the next request
                    result && result._variables && (this.state._variables = new sdk.VariableScope(result._variables));

                    // persist the mutated request in payload context,
                    // @note this will be used for the next prerequest script or
                    // upcoming commands(request, httprequest).
                    result && result.request && (payload.context.request = result.request);

                    // now that this script is done executing, we trigger the event and move to the next script
                    this.triggers.script(err || null, scriptCursor, result, script, event, item);
github postmanlabs / openapi-to-postman / lib / util.js View on Github external
}
    else if (response.content && Object.keys(response.content).length > 0) {
      responseHeaders.push({ key: 'Content-Type', value: Object.keys(response.content)[0] });
      if (this.getHeaderFamily(Object.keys(response.content)[0]) === HEADER_TYPE.JSON) {
        previewLanguage = PREVIEW_LANGUAGE.JSON;
      }
      else if (this.getHeaderFamily(Object.keys(response.content)[0]) === HEADER_TYPE.XML) {
        previewLanguage = PREVIEW_LANGUAGE.XML;
      }
    }
    else {
      responseHeaders.push({ key: 'Content-Type', value: TEXT_PLAIN });
    }
    code = code.replace(/X/g, '0');

    sdkResponse = new sdk.Response({
      name: response.description,
      code: code === 'default' ? 500 : Number(code),
      header: responseHeaders,
      body: responseBodyWrapper.responseBody,
      originalRequest: originalRequest
    });
    sdkResponse._postman_previewlanguage = previewLanguage;

    return sdkResponse;
  },
github postmanlabs / postman-sandbox / lib / sandbox / execution.js View on Github external
/**
         * @note:
         * this reference is passed on as `pm.request`, pm api adds helper functions like `to` to `pm.request`
         * sandbox overrides collection Request.prototype.toJSON to remove helpers before toJSON, see `purse.js`
         */
        this.request = sdk.Request.isRequest(context.request) ? context.request : new sdk.Request(context.request);
    }

    if (TARGETS_WITH_RESPONSE[this.target] || _.has(context, PROPERTY.RESPONSE)) {
        /**
         * @note:
         * this reference is passed on as `pm.response`, pm api adds helper functions like `to` to `pm.response`
         * sandbox overrides collection Response.prototype.toJSON to remove helpers before toJSON, see `purse.js`
         */
        this.response = sdk.Response.isResponse(context.response) ?
            context.response : new sdk.Response(context.response);
    }

    this.return = {};
};