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

JS tests do not run for every target #15

Closed apatrida closed 2 years ago

apatrida commented 2 years ago

Running httpx with -a with 4 targets, each having a JS test, it only indicates it is running for one or two of the middle 2.

test file:

### ERROR 401: Exchange for Device token with bad secret
POST {{baseUri}}/device/auth/test-ABC-123-XYZ-001
Accept: application/json
Content-Type: application/json

{
  "registrationSecret": "secret-is-wrong"
}

> {%
    client.test("Bad attempt at token exchange", function() {
        client.log("test 1 log");
        client.assert(response.status === 401, "Response status is not 401");
    });
%}

### ERROR 401: Exchange for Device token with bad device
POST {{baseUri}}/device/auth/no-such-device
Accept: application/json
Content-Type: application/json

{
  "registrationSecret": "secret-is-wrong"
}

> {%
    client.test("Bad attempt at token exchange", function() {
        client.log("test 2 log");
        client.assert(response.status === 401, "Response status is not 401");
    });
%}

### Exchange secret for Device registration token
POST {{baseUri}}/device/auth/test-ABC-123-XYZ-001
Accept: application/json
Content-Type: application/json

{
  "registrationSecret": "secret-ABC-123-XYZ-001"
}

> {%
    client.test("Successful token exchange", function() {
        client.log("test 3 log");
        client.assert(response.status === 200, "Response status is not 200");
        client.assert(response.body.apiToken.length > 0, "Should have token");
        client.global.set("_deviceRegistrationToken", response.body.apiToken);
    });
%}

### Error 403: Device check token should not succeed with a registration token, as it is the wrong type!
GET {{baseUri}}/device/registered/test-ABC-123-XYZ-001
Accept: application/json
Authorization: Bearer {{_deviceRegistrationToken}}

> {%
    client.test("Device should not validate a registration token token", function() {
        client.log("test 4 log");
        client.assert(response.status === 403, "Response status is not 403");
    });
%}

and output from httpx

❯ httpx -f src/test/http/devices-auth.http -a 
=============1==================
POST http://localhost:9090/device/auth/test-ABC-123-XYZ-001

Status: 401 Unauthorized 
Content-Type : application/json
www-authenticate : Bearer
content-length : 0

=============2==================
POST http://localhost:9090/device/auth/no-such-device

Status: 401 Unauthorized 
Content-Type : application/json
www-authenticate : Bearer
content-length : 0

=============3==================
POST http://localhost:9090/device/auth/test-ABC-123-XYZ-001

Status: 200 OK 
Content-Type : application/json;charset=UTF-8
content-length : 789

{
  "deviceId" : "test-ABC-123-XYZ-001",
  "apiToken" : "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJodHRwczovL2Rldi50YXRlYW0udGhlb3JlbW9uZS5jby9pc3N1ZXIiLCJ1cG4iOiJ0ZXN0LUFCQy0xMjMtWFlaLTAwMSIsInN1YiI6InRlc3QtQUJDLTEyMy1YWVotMDAxIiwiZXhwIjoxNjY1NDE5MTU2LCJncm91cHMiOlsiREVWSUNFX1JFRyIsIkFQSSJdLCJsZW1tYS50cyI6MTY2NTQxOTA5Njc0NywianRpIjoiYmZmOTc3YjctNzBjNi00ZTI2LWI1MzktNzM3YjViYTJkMGI3IiwiaWF0IjoxNjY1NDE5MDk2fQ.eBKI_mH7T7ULQ9Cgn6xuAck7HTI3eMd7SUC1xV08g8iznoOkosWSMNtX7qX4rFRFp3ixBhbwABrRgOLCSLJpDWHwLdwuL8vvdUcL4_mRAgNQiRV9fz2nh3f9Syzc877wJkawMllQFgOBWPp_hh4tTOB4kpTVeyCI2NXw6G7Gusq5lmhqr2Hao5OxPwF6BgIfecOZTpFz2iblu5UYcPrle1S_V6pEhqymmqAknD5C0GdKnWnlhwajF1LW3gWNjpoquuBkSdHwKoCbSOpzw6X8CYytoeZ21Li8D9R4cV4l5Zb9X6_zwn6RUhC-6vthiifEfYPw2IZBGUk7vCOGQl9dMw",
  "expiresAt" : 1665419156,
  "expiresIn" : 60
}
============Execute JS Test============
test 3 log

=============4==================
GET http://localhost:9090/device/registered/test-ABC-123-XYZ-001

Status: 403 Forbidden 
Content-Type : application/json
content-length : 0
apatrida commented 2 years ago

Is it only running tests for OK 200 status responses and not all responses? If so, this differs from IntelliJ IDEA which always runs the tests/assertions (i.e. you are testing for the error intentionally)

linux-china commented 2 years ago

@apatrida it's a bug. Now JS test only works when response body is not empty and content-type is json. I will fix this bug ASAP.

                    String contentType = responseHeaders.get("Content-Type");
                    return byteBufMono.asByteArray().doOnNext(content -> {
                        if (contentType != null && isPrintable(contentType)) {
                            if (contentType.contains("json")) {
                                final String result = new String(content, StandardCharsets.UTF_8);
                                final String body = prettyJsonFormatWithJsonPath(result, httpRequest.getHeader("X-JSON-Path"));
                                System.out.print(body);
                                final String javaScriptTestCode = httpRequest.getJavaScriptTestCode();
                                if (javaScriptTestCode != null && !javaScriptTestCode.isEmpty()) {
                                    System.out.println();
                                    System.out.println("============Execute JS Test============");
                                    final String jsTestOutput = Nodejs.executeHttpClientCode(javaScriptTestCode, httpStatus.code(), httpResponseHeaders, contentType, result);
                                    System.out.println(jsTestOutput);
                                }
                            } else {
                                System.out.print(new String(content));
                            }
                        }
                    });
apatrida commented 2 years ago

thanks

linux-china commented 2 years ago

Released with version 0.36.0 https://github.com/servicex-sh/httpx/releases/tag/v0.36.0