servicex-sh / httpx

httpx - CLI to test HTTP/gRPC/RSocket/Kafka... services by HTTP DSL
https://servicex.sh
Apache License 2.0
132 stars 9 forks source link

Cannot simple test the response with client test #3

Closed nhahv closed 2 years ago

nhahv commented 2 years ago
### hello ip
GET https://httpbin.org/ip

> {%
    client.test("Request executed successfully", function() {
        client.log(response.status);
        client.log(response.contentType);
        client.log(response.body);
    });
%}
httpx --version  
0.23.0
httpx
GET https://httpbin.org/ip

Status: 200 OK 
Date : Fri, 08 Apr 2022 05:09:13 GMT
Content-Type : application/json
Connection : keep-alive
Server : gunicorn/19.9.0
Access-Control-Allow-Origin : *
Access-Control-Allow-Credentials : true
content-length : 31

{
  "origin" : "00.177.64.37"
}
============Execute JS Test============
[stdin]:92
        let bodyText = decodeURIComponent(atob(base64Text))
                       ^

ReferenceError: atob is not defined
    at HttpResponse.setBase64Body ([stdin]:92:24)
    at [stdin]:140:10
    at Script.runInThisContext (vm.js:134:12)
    at Object.runInThisContext (vm.js:311:38)
    at internal/process/execution.js:77:19
    at [stdin]-wrapper:6:22
    at evalScript (internal/process/execution.js:76:60)
    at internal/main/eval_stdin.js:29:5
    at Socket.<anonymous> (internal/process/execution.js:205:5)
    at Socket.emit (events.js:387:35)

=========JavaScript Code===========
class ContentType {
    mimeType = ""
    charset = "utf-8";

    constructor(mimeType) {
        this.mimeType = mimeType;
    }
}

class Variables {
    store = new Map();

    set(varName, varValue) {
        this.store.set(varName, varValue);
    }

    get(varName) {
        return this.store.get(varName)
    }

    isEmpty() {
        return this.store.size === 0
    }

    clear(varName) {
        this.store.delete(varName)
    }

    clearAll() {
        this.store.clear()
    }
}

class ResponseHeaders {
    store = new Map();

    valueOf(headerName) {
        return this.store.get(headerName)
    }

    valuesOf(headerName) {
        let value = this.store.get(headerName);
        if (value == null) {
            return []
        } else if (value && !Array.isArray(value)) {
            return [value];
        } else {
            return value;
        }
    }

    add(name, value) {
        this.store.set(name, value);
    }
}

class HttpResponse {
    /**
     * response body: object if application/json
     *
     * @type {string | object | LineStreamResponse}
     */
    body;
    headers = new ResponseHeaders();
    status = 200;
    /**
     *  content type
     * @type {ContentType}
     */
    contentType;

    constructor(status, contentType) {
        this.status = status;
        if (typeof contentType === "string") {
            let parts = contentType.split(";");
            this.contentType = new ContentType(parts[0]);
            if (parts.length > 1) {
                this.contentType.charset = parts[1];
            }
        }
    }

    setHeaders(headers) {
        for (const [key, value] of Object.entries(variables)) {
            this.headers.add(key, value);
        }
    }

    setBase64Body(base64Text) {
        let bodyText = decodeURIComponent(atob(base64Text))
        if (this.contentType.mimeType.indexOf("json") >= 0) {
            this.body = JSON.parse(bodyText);
        } else {
            this.body = bodyText;
        }
    }

}

class HttpClient {
    global = new Variables();

    constructor(variables) {
        for (const [key, value] of Object.entries(variables)) {
            this.global.set(key, value)
        }
    }

    test(testName, func) {
        func();
    }

    log(message) {
        console.log(message);
    }

    assert(condition, message) {
        console.assert(condition, message);
    }

    exit() {
        process.exit(0)
    }
}

function encodeBody(plainText) {
    return btoa(encodeURIComponent(plainText));
}

const statusCode = 200;
const contentType = 'application/json';
const headers = {"content-length":"31","Server":"gunicorn/19.9.0","Access-Control-Allow-Origin":"*","Access-Control-Allow-Credentials":"true","Connection":"keep-alive","Date":"Fri, 08 Apr 2022 05:09:13 GMT","Content-Type":"application/json"};
const variables = {id: 111};
const client = new HttpClient(variables);
const response = new HttpResponse(statusCode, contentType);
response.setHeaders(headers);
const base64Body = 'JTdCJTBBJTIwJTIwJTIyb3JpZ2luJTIyJTNBJTIwJTIyMTQuMTc3LjY0LjM3JTIyJTBBJTdEJTBB';
response.setBase64Body(base64Body);

/*
client.test("Request executed successfully", function () {
    client.log(response.status)
    client.log(response.contentType)
    client.log(response.body)
});
*/

    client.test("Request executed successfully", function() {
        client.log(response.status);
        client.log(response.contentType);
        client.log(response.body);
    });
linux-china commented 2 years ago

what's your node.js version? node --version

linux-china commented 2 years ago

Please try new 0.24.0 version.

nhahv commented 2 years ago

Fixed in new version 0.24.0