Skip to content

Commit

Permalink
Merge branch 'main' of github.com:agrostar/zzapi
Browse files Browse the repository at this point in the history
  • Loading branch information
vasan-agrostar committed Jan 7, 2024
2 parents 5cb5f3d + e3bf608 commit 2c47819
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 133 deletions.
54 changes: 26 additions & 28 deletions examples/tests-bundle.zzb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

common:
baseUrl: https://postman-echo.com
headers:
headers:
Content-type: application/json
tests:
status: 200
Expand All @@ -17,14 +17,13 @@ variables:
addressVar:
streetAddress: 7th street
city: Nara
postalCode: "560001" # string. This will make a difference when used in the body
postalCode: "560001" # string. This will make a difference when used in the body
getUrl: /get
fooVar: bar
agrostar: https://test-farmerapp.agrostar.in
pi: 3.14

requests:

simple-get: { method: GET, url: /get }

get-with-params:
Expand All @@ -33,7 +32,7 @@ requests:
params:
foo1: bar1
foo2: bar2
tests: # old way of specifying json tests
tests: # old way of specifying json tests
json:
$.args.foo1: bar1
$.args.foo2: bar2
Expand All @@ -44,7 +43,7 @@ requests:
params:
foo1: bar1
foo2:
tests: # new way of specifying json
tests: # new way of specifying json
$.url: "https://postman-echo.com/get?foo1=bar1&foo2"

get-with-params-as-array:
Expand All @@ -56,19 +55,19 @@ requests:
- { name: foo2, value: multi-2 }
tests:
$.args.foo1: bar1
$.args.foo2: { $eq: ["multi-1","multi-2"] }
$.args.foo2: { $eq: ["multi-1", "multi-2"] }

get-with-params-values-as-array:
method: GET
url: /get
params:
foo1: bar1
foo2:
- multi-1
- multi-2
- multi-1
- multi-2
tests:
$.args.foo1: bar1
$.args.foo2: { $eq: ["multi-1","multi-2"] }
$.args.foo2: { $eq: ["multi-1", "multi-2"] }

post-header-merge:
url: /post
Expand All @@ -84,7 +83,7 @@ requests:
post-header-override:
url: /post
method: POST
headers:
headers:
Content-Type: text/plain
body: { foo: bar }
tests:
Expand All @@ -94,14 +93,14 @@ requests:
status-404:
url: /notfound
method: GET
tests:
tests:
status: 404
$h.content-type: { $exists: false }

status-401:
url: /status/401
method: GET
tests: { status: 401, { $.status: 401 } }
tests: { status: 401, $.status: 401 }

encoding:
method: GET
Expand All @@ -118,7 +117,7 @@ requests:
params:
foo: 30%25+of+200
options:
raw: true
rawParams: true
tests:
$.url: https://postman-echo.com/get?foo=30%25+of+200
$.args.foo: 30% of 200
Expand All @@ -129,7 +128,7 @@ requests:
url: /cookies/set
tests:
status: 302
headers: { Location: /cookies, Content-Type: { $ne: application/json } }
headers: { Location: /cookies, Content-Type: { $ne: application/json } }

redirects:
options:
Expand All @@ -145,7 +144,7 @@ requests:
url: /encoding/utf8
tests:
body: { $regex: unicode demo, $options: i }
headers:
headers:
content-type: text/html; charset=utf-8

response-headers:
Expand Down Expand Up @@ -185,7 +184,7 @@ requests:
variables-in-body:
method: POST
url: /post
body: | # Alternate way of supplying the JSON string or any raw content
body: | # Alternate way of supplying the JSON string or any raw content
{
"foo1": "$(fooVar)",
"foo2": $pi
Expand All @@ -208,7 +207,7 @@ requests:
header-case:
method: GET
url: $agrostar/userservice/ping/
tests:
tests:
headers:
content-type: application/json

Expand All @@ -227,7 +226,7 @@ requests:
phoneNumbers:
- type: mobile
number: 0123-4567-8888
available: [ 7, 22 ]
available: [7, 22]
- type: home
number: 0123-4567-8910
available: [18, 22]
Expand All @@ -246,8 +245,8 @@ requests:
$.data.middleName: null
$.data.otherName: { $exists: false, $type: undefined }
$.data.phoneNumbers[*].type: mobile
$.data.phoneNumbers[*].available: { $eq: [7,22] }
$.data.phoneNumbers.0.available: { $eq: [ 7, 22 ], $type: array }
$.data.phoneNumbers[*].available: { $eq: [7, 22] }
$.data.phoneNumbers.0.available: { $eq: [7, 22], $type: array }
$.data.phoneNumbers.1.available: { $eq: "[18,22]", $type: array }
# stress: ensure corner cases don't crash
$.data.otherName.value: { $exists: false } # don't recurse down undefined
Expand All @@ -261,7 +260,7 @@ requests:
age: 26
name: John
address: 1, example street
numbers: [ 444, 222 ]
numbers: [444, 222]
object: { foo: bar }
tests:
status: { $ne: 200 }
Expand All @@ -273,10 +272,10 @@ requests:
$.data.numbers.0: "444" # 444 is not same as "444". We use === for $eq and !== for $neq

# stress: ensure corner cases don't crash
$.data.age: { $size: 2 } # .length not supported for type: number
$.data.age: { $size: 2 } # .length not supported for type: number
$.data.numbers[?(.@)]: 4 # invalid path
$.data.age.something: 55 # jsonpath should take care of this.
$.data.numbers[5]: 0 # jsonpath should take care of this
$.data.age.something: 55 # jsonpath should take care of this.
$.data.numbers[5]: 0 # jsonpath should take care of this

# This request should tests should all fail due to bad tests schema
# Ensure we don't crash on these.
Expand All @@ -285,12 +284,12 @@ requests:
url: /post
body:
address: 1, example street
numbers: [ 444, 222 ]
numbers: [444, 222]
object: { foo: bar }
tests:
status: { $ne: 200 }
headers:
content-type: {$exists: false}
content-type: { $exists: false }
# Uncomment the following to run tests. Schema validation makes these invalid.
# $.data.operator: { badop: any } # invalid operator badop. If you want to match an entire object/array, use it as the value of the $eq operator.
# $.data.numbers: [444, 222]
Expand Down Expand Up @@ -344,7 +343,7 @@ requests:
tests:
$.data.name: Tom
$.data.address.city: Bangalore
$.data.address.pincode: 560001 # this time it is NOT a string.
$.data.address.pincode: 560001 # this time it is NOT a string.

capture-checks-object-option-2:
method: POST
Expand All @@ -355,4 +354,3 @@ requests:
tests:
$.data.name: Tom
$.data.address: { $eq: { city: Bangalore, pincode: 560001 } }

8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/checkTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ const VALID_OPTIONS: { [type: string]: boolean } = {
verifySSL: true,
keepRawJSON: true,
showHeaders: true,
raw: true,
rawParams: true,
};
function checkOptions(obj: any): string | undefined {
let ret = checkObjIsDict(obj, "options");
Expand Down
9 changes: 6 additions & 3 deletions src/constructCurl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { getBody, getParamsForUrl, getURL } from "./executeRequest";
import { getStringValueIfDefined } from "./utils/typeUtils";

import { getParamsForUrl, getURL } from "./executeRequest";
import { RequestSpec } from "./models";

export function getCurlRequest(request: RequestSpec): string {
Expand All @@ -12,7 +14,8 @@ export function getCurlRequest(request: RequestSpec): string {
}

let bodyFlag = "";
if (request.httpRequest.body !== undefined) bodyFlag += ` -d '${getBody(request.httpRequest.body)}'`;
if (request.httpRequest.body !== undefined)
bodyFlag += ` -d '${getStringValueIfDefined(request.httpRequest.body)}'`;

let followRedirectFlag = "";
if (request.options.follow) followRedirectFlag = " -L";
Expand All @@ -23,7 +26,7 @@ export function getCurlRequest(request: RequestSpec): string {
const url = ` '${getURL(
request.httpRequest.baseUrl,
request.httpRequest.url,
getParamsForUrl(request.httpRequest.params, request.options.raw)
getParamsForUrl(request.httpRequest.params, request.options.rawParams)
)}'`;

const finalCurl =
Expand Down
35 changes: 11 additions & 24 deletions src/executeRequest.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
import got, { Method } from "got";
import got, { Method, OptionsOfTextResponseBody } from "got";

import { getStringValueIfDefined } from "./utils/typeUtils";

import { GotRequest, Param, RequestSpec } from "./models";

export function constructGotRequest(allData: RequestSpec): GotRequest {
const completeUrl = getURL(
const completeUrl: string = getURL(
allData.httpRequest.baseUrl,
allData.httpRequest.url,
getParamsForUrl(allData.httpRequest.params, allData.options.raw)
getParamsForUrl(allData.httpRequest.params, allData.options.rawParams)
);

const options = {
const options: OptionsOfTextResponseBody = {
method: allData.httpRequest.method.toLowerCase() as Method,
body: getBody(allData.httpRequest.body),
body: getStringValueIfDefined(allData.httpRequest.body),
headers: allData.httpRequest.headers,
followRedirect: allData.options?.follow,
followRedirect: allData.options.follow,
https: { rejectUnauthorized: allData.options.verifySSL },
retry: { limit: 0 },

https: {
rejectUnauthorized: allData.options?.verifySSL,
},
};

return got(completeUrl, options);
}

export function getBody(body: any): string | undefined {
return getStringValueIfDefined(body);
}

export async function executeGotRequest(httpRequest: GotRequest): Promise<{
response: { [key: string]: any };
executionTime: number;
Expand All @@ -48,11 +41,7 @@ export async function executeGotRequest(httpRequest: GotRequest): Promise<{
const res = e.response;
if (res) {
responseObject = res;
if (res.body) {
size = Buffer.byteLength(res.body);
} else {
size = 0;
}
size = res.body ? Buffer.byteLength(res.body) : 0;
} else {
responseObject = {};
if (e.code === "ERR_INVALID_URL") {
Expand All @@ -68,18 +57,16 @@ export async function executeGotRequest(httpRequest: GotRequest): Promise<{
return { response: responseObject, executionTime: executionTime, byteLength: size, error: error };
}

export function getParamsForUrl(paramsArray: Param[] | undefined, raw: boolean): string {
if (!paramsArray) return "";
export function getParamsForUrl(params: Param[] | undefined, rawParams: boolean): string {
if (!params) return "";

let params: Param[] = paramsArray;
let paramArray: string[] = [];

params.forEach((param) => {
const key = param.name;
let value = param.value;
if (value == undefined) {
paramArray.push(key);
} else if (raw === true) {
} else if (rawParams) {
paramArray.push(`${key}=${getStringValueIfDefined(value)}`);
} else {
paramArray.push(`${key}=${encodeURIComponent(getStringValueIfDefined(value))}`);
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export { RequestPosition, ResponseData, RequestSpec, GotRequest, TestResult } from "./models";
export {
RequestPosition,
ResponseData,
RequestSpec,
GotRequest,
TestResult
} from "./models";

export { getRequestPositions, getAllRequestSpecs, getRequestSpec } from "./parseBundle";

Expand Down
4 changes: 2 additions & 2 deletions src/mergeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ function getMergedOptions(cOptions: RawOptions = {}, rOptions: RawOptions = {}):
const verifySSL = options.verifySSL == true;
const keepRawJSON = options.keepRawJSON == true;
const showHeaders = options.showHeaders == true;
const raw = options.raw == true;
const rawParams = options.rawParams == true;

return { follow, verifySSL, keepRawJSON, showHeaders, raw };
return { follow, verifySSL, keepRawJSON, showHeaders, rawParams };
}

function getMergedSetVars(
Expand Down
Loading

0 comments on commit 2c47819

Please sign in to comment.