【问题标题】:How to add GET request parameters in AWS canaries如何在 AWS 金丝雀中添加 GET 请求参数
【发布时间】:2021-01-21 14:00:09
【问题描述】:

我是 Cloudwatch 合成的新手。我想用带参数的 Get 请求来测试我的 API。请建议添加参数的配置或提供在get请求中使用参数的蓝图脚本。

谢谢

【问题讨论】:

    标签: amazon-web-services amazon-cloudwatch


    【解决方案1】:

    试试这个...

    var synthetics = require('Synthetics');
        const log = require('SyntheticsLogger');
        
        const apiCanaryBlueprint = async function () {
            
            // Handle validation for positive scenario
            const validatePositiveCase = async function(res) {
                return new Promise((resolve, reject) => {
                    if (res.statusCode < 200 || res.statusCode > 299) {
                        throw res.statusCode + ' ' + res.statusMessage;
                    }
             
                    let responseBody = '';
                    res.on('data', (d) => {
                        responseBody += d;
                    });
             
                    res.on('end', () => {
                        // Add validation on 'responseBody' here if required. For ex, your status code is 200 but data might be empty
                        resolve();
                    });
                });
            };
            
            // Handle validation for negative scenario
            const validateNegativeCase = async function(res) {
                return new Promise((resolve, reject) => {
                    if (res.statusCode < 400) {
                        throw res.statusCode + ' ' + res.statusMessage;
                    }
                });
            };
            
            let requestOptionsStep1 = {
                'hostname': 'myproductsEndpoint.com',
                'method': 'GET',
                'path': '/test/product/validProductName',
                'port': 443,
                'protocol': 'https:'
            };
            
            let headers = {};
            headers['User-Agent'] = [synthetics.getCanaryUserAgentString(), headers['User-Agent']].join(' ');
            
            requestOptionsStep1['headers'] = headers;
        
            // By default headers, post data and response body are not included in the report for security reasons. 
            // Change the configuration at global level or add as step configuration for individual steps
            let stepConfig = {
                includeRequestHeaders: true, 
                includeResponseHeaders: true,
                restrictedHeaders: ['X-Amz-Security-Token', 'Authorization'], // Restricted header values do not appear in report generated.
                includeRequestBody: true,
                includeResponseBody: true
            };
               
        
            await synthetics.executeHttpStep('Verify GET products API with valid name', requestOptionsStep1, validatePositiveCase, stepConfig);
            
            let requestOptionsStep2 = {
                'hostname': ‘myproductsEndpoint.com',
                'method': 'GET',
                'path': '/test/canary/InvalidName(',
                'port': 443,
                'protocol': 'https:'
            };
            
            headers = {};
            headers['User-Agent'] = [synthetics.getCanaryUserAgentString(), headers['User-Agent']].join(' ');
            
            requestOptionsStep2['headers'] = headers;
        
            // By default headers, post data and response body are not included in the report for security reasons. 
            // Change the configuration at global level or add as step configuration for individual steps
            stepConfig = {
                includeRequestHeaders: true, 
                includeResponseHeaders: true,
                restrictedHeaders: ['X-Amz-Security-Token', 'Authorization'], // Restricted header values do not appear in report generated.
                includeRequestBody: true,
                includeResponseBody: true
            };
            
            await synthetics.executeHttpStep('Verify GET products API with invalid name', requestOptionsStep2, validateNegativeCase, stepConfig);
            
        };
        
        exports.handler = async () => {
            return await apiCanaryBlueprint();
        };
    

    【讨论】:

      猜你喜欢
      • 2022-07-28
      • 2018-02-01
      • 2016-02-12
      • 1970-01-01
      • 2022-10-12
      • 2022-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多