add codegen for nodejs native and instrument snapshot tests
This commit is contained in:
@@ -4,6 +4,7 @@ import { CurlCodegen } from "./generators/curl"
|
||||
import { JSAxiosCodegen } from "./generators/js-axios"
|
||||
import { GoNativeCodegen } from "./generators/go-native"
|
||||
import { NodeJsRequestCodegen } from "./generators/nodejs-request"
|
||||
import { NodeJsNativeCodegen } from "./generators/nodejs-native"
|
||||
import { JSjQueryCodegen } from "./generators/js-jQuery"
|
||||
import { PowerShellRestMethod } from "./generators/powershell"
|
||||
import { PhpCurlCodegen } from "./generators/php-curl"
|
||||
@@ -25,6 +26,7 @@ export const codegens = [
|
||||
JSjQueryCodegen,
|
||||
JSXHRCodegen,
|
||||
NodeJsRequestCodegen,
|
||||
NodeJsNativeCodegen,
|
||||
PhpCurlCodegen,
|
||||
PowerShellRestMethod,
|
||||
PythonRequestsCodegen,
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`generate request for NodeJs Native generate GET request 1`] = `
|
||||
"const http = require('http');
|
||||
|
||||
const url = 'https://httpbin.org/path/to?a=b';
|
||||
const options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
\\"Authorization\\": \\"Basic bW9ja1VzZXI6bW9ja1Bhc3N3b3Jk\\",
|
||||
\\"h1\\": \\"h1v\\",
|
||||
\\"h2\\": \\"h2v\\"
|
||||
}};
|
||||
|
||||
const request = http.request(url, options, (response) => {
|
||||
console.log(response);
|
||||
});
|
||||
|
||||
request.on('error', (e) => {
|
||||
console.error(e);
|
||||
});
|
||||
request.end();"
|
||||
`;
|
||||
|
||||
exports[`generate request for NodeJs Native generate POST request for JSON 1`] = `
|
||||
"const http = require('http');
|
||||
|
||||
const url = 'https://httpbin.org/path/to?a=b';
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
\\"Authorization\\": \\"Bearer abcdefghijklmn\\",
|
||||
\\"Content-Type\\": \\"application/json; charset=utf-8\\",
|
||||
\\"h1\\": \\"h1v\\",
|
||||
\\"h2\\": \\"h2v\\"
|
||||
}};
|
||||
|
||||
const request = http.request(url, options, (response) => {
|
||||
console.log(response);
|
||||
});
|
||||
|
||||
request.on('error', (e) => {
|
||||
console.error(e);
|
||||
});
|
||||
|
||||
request.write(JSON.stringify({\\"foo\\": \\"bar\\", \\"baz\\": \\"qux\\"}));
|
||||
request.end();"
|
||||
`;
|
||||
|
||||
exports[`generate request for NodeJs Native generate POST request for XML 1`] = `
|
||||
"const http = require('http');
|
||||
|
||||
const url = 'https://httpbin.org/path/to?a=b';
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
\\"Authorization\\": \\"Bearer abcdefghijklmn\\",
|
||||
\\"Content-Type\\": \\"application/xml; charset=utf-8\\",
|
||||
\\"h1\\": \\"h1v\\",
|
||||
\\"h2\\": \\"h2v\\"
|
||||
}};
|
||||
|
||||
const request = http.request(url, options, (response) => {
|
||||
console.log(response);
|
||||
});
|
||||
|
||||
request.on('error', (e) => {
|
||||
console.error(e);
|
||||
});
|
||||
|
||||
request.write(\`<?xml version='1.0' encoding='utf-8'?>
|
||||
<xml>
|
||||
<element foo=\\"bar\\"></element>
|
||||
</xml>\`);
|
||||
request.end();"
|
||||
`;
|
||||
|
||||
exports[`generate request for NodeJs Native generate PUT request for www-form-urlencoded 1`] = `
|
||||
"const http = require('http');
|
||||
|
||||
const url = 'https://httpbin.org/path/to?a=b';
|
||||
const options = {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
\\"Content-Type\\": \\"application/x-www-form-urlencoded; charset=utf-8\\"
|
||||
}};
|
||||
|
||||
const request = http.request(url, options, (response) => {
|
||||
console.log(response);
|
||||
});
|
||||
|
||||
request.on('error', (e) => {
|
||||
console.error(e);
|
||||
});
|
||||
|
||||
request.write(\`foo=bar&baz=qux\`);
|
||||
request.end();"
|
||||
`;
|
||||
16
helpers/codegen/generators/__tests__/fixtures.js
Normal file
16
helpers/codegen/generators/__tests__/fixtures.js
Normal file
@@ -0,0 +1,16 @@
|
||||
export const TEST_URL = "https://httpbin.org"
|
||||
export const TEST_PATH_NAME = "/path/to"
|
||||
export const TEST_QUERY_STRING = "?a=b"
|
||||
export const TEST_HTTP_USER = "mockUser"
|
||||
export const TEST_HTTP_PASSWORD = "mockPassword"
|
||||
export const TEST_BEARER_TOKEN = "abcdefghijklmn"
|
||||
export const TEST_RAW_REQUEST_BODY = "foo=bar&baz=qux"
|
||||
export const TEST_RAW_PARAMS_JSON = '{"foo": "bar", "baz": "qux"}'
|
||||
export const TEST_RAW_PARAMS_XML = `<?xml version=\'1.0\' encoding=\'utf-8\'?>
|
||||
<xml>
|
||||
<element foo="bar"></element>
|
||||
</xml>`
|
||||
export const TEST_HEADERS = [
|
||||
{ key: "h1", value: "h1v" },
|
||||
{ key: "h2", value: "h2v" },
|
||||
]
|
||||
74
helpers/codegen/generators/__tests__/nodejs-native.spec.js
Normal file
74
helpers/codegen/generators/__tests__/nodejs-native.spec.js
Normal file
@@ -0,0 +1,74 @@
|
||||
import { NodeJsNativeCodegen } from "../nodejs-native"
|
||||
import {
|
||||
TEST_BEARER_TOKEN,
|
||||
TEST_HEADERS,
|
||||
TEST_HTTP_PASSWORD,
|
||||
TEST_HTTP_USER,
|
||||
TEST_PATH_NAME,
|
||||
TEST_QUERY_STRING,
|
||||
TEST_RAW_PARAMS_JSON,
|
||||
TEST_RAW_PARAMS_XML,
|
||||
TEST_RAW_REQUEST_BODY,
|
||||
TEST_URL,
|
||||
} from "./fixtures"
|
||||
|
||||
describe("generate request for NodeJs Native", () => {
|
||||
test("generate GET request", () => {
|
||||
const result = NodeJsNativeCodegen.generator({
|
||||
url: TEST_URL,
|
||||
pathName: TEST_PATH_NAME,
|
||||
queryString: TEST_QUERY_STRING,
|
||||
auth: "Basic Auth",
|
||||
httpUser: TEST_HTTP_USER,
|
||||
httpPassword: TEST_HTTP_PASSWORD,
|
||||
method: "GET",
|
||||
headers: TEST_HEADERS,
|
||||
})
|
||||
expect(result).toMatchSnapshot()
|
||||
})
|
||||
|
||||
test("generate POST request for JSON", () => {
|
||||
const result = NodeJsNativeCodegen.generator({
|
||||
url: TEST_URL,
|
||||
pathName: TEST_PATH_NAME,
|
||||
queryString: TEST_QUERY_STRING,
|
||||
auth: "Bearer Token",
|
||||
bearerToken: TEST_BEARER_TOKEN,
|
||||
method: "POST",
|
||||
rawInput: true,
|
||||
rawParams: TEST_RAW_PARAMS_JSON,
|
||||
contentType: "application/json",
|
||||
headers: TEST_HEADERS,
|
||||
})
|
||||
expect(result).toMatchSnapshot()
|
||||
})
|
||||
|
||||
test("generate POST request for XML", () => {
|
||||
const result = NodeJsNativeCodegen.generator({
|
||||
url: TEST_URL,
|
||||
pathName: TEST_PATH_NAME,
|
||||
queryString: TEST_QUERY_STRING,
|
||||
auth: "OAuth 2.0",
|
||||
bearerToken: TEST_BEARER_TOKEN,
|
||||
method: "POST",
|
||||
rawInput: true,
|
||||
rawParams: TEST_RAW_PARAMS_XML,
|
||||
contentType: "application/xml",
|
||||
headers: TEST_HEADERS,
|
||||
})
|
||||
expect(result).toMatchSnapshot()
|
||||
})
|
||||
|
||||
test("generate PUT request for www-form-urlencoded", () => {
|
||||
const result = NodeJsNativeCodegen.generator({
|
||||
url: TEST_URL,
|
||||
pathName: TEST_PATH_NAME,
|
||||
queryString: TEST_QUERY_STRING,
|
||||
method: "PUT",
|
||||
rawInput: false,
|
||||
rawRequestBody: TEST_RAW_REQUEST_BODY,
|
||||
contentType: "application/x-www-form-urlencoded",
|
||||
})
|
||||
expect(result).toMatchSnapshot()
|
||||
})
|
||||
})
|
||||
81
helpers/codegen/generators/nodejs-native.js
Normal file
81
helpers/codegen/generators/nodejs-native.js
Normal file
@@ -0,0 +1,81 @@
|
||||
import { isJSONContentType } from "~/helpers/utils/contenttypes"
|
||||
|
||||
export const NodeJsNativeCodegen = {
|
||||
id: "nodejs-native",
|
||||
name: "NodeJs Native",
|
||||
generator: ({
|
||||
url,
|
||||
pathName,
|
||||
queryString,
|
||||
auth,
|
||||
httpUser,
|
||||
httpPassword,
|
||||
bearerToken,
|
||||
method,
|
||||
rawInput,
|
||||
rawParams,
|
||||
rawRequestBody,
|
||||
contentType,
|
||||
headers,
|
||||
}) => {
|
||||
const requestString = []
|
||||
const genHeaders = []
|
||||
|
||||
requestString.push(`const http = require('http');\n\n`)
|
||||
|
||||
requestString.push(`const url = '${url}${pathName}${queryString}';\n`)
|
||||
|
||||
requestString.push(`const options = {\n`)
|
||||
requestString.push(` method: '${method}',\n`)
|
||||
|
||||
if (auth === "Basic Auth") {
|
||||
const basic = `${httpUser}:${httpPassword}`
|
||||
genHeaders.push(
|
||||
` "Authorization": "Basic ${window.btoa(unescape(encodeURIComponent(basic)))}",\n`
|
||||
)
|
||||
} else if (auth === "Bearer Token" || auth === "OAuth 2.0") {
|
||||
genHeaders.push(` "Authorization": "Bearer ${bearerToken}",\n`)
|
||||
}
|
||||
|
||||
let requestBody
|
||||
if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
|
||||
console.log("===== rawInput = " + rawInput)
|
||||
console.log("===== rawParams = " + rawParams)
|
||||
console.log("===== rawRequestBody = " + rawRequestBody)
|
||||
requestBody = rawInput ? rawParams : rawRequestBody
|
||||
if (isJSONContentType(contentType)) {
|
||||
requestBody = `JSON.stringify(${requestBody})`
|
||||
} else {
|
||||
requestBody = `\`${requestBody}\``
|
||||
}
|
||||
if (contentType) {
|
||||
genHeaders.push(` "Content-Type": "${contentType}; charset=utf-8",\n`)
|
||||
}
|
||||
}
|
||||
|
||||
if (headers) {
|
||||
headers.forEach(({ key, value }) => {
|
||||
if (key) genHeaders.push(` "${key}": "${value}",\n`)
|
||||
})
|
||||
}
|
||||
if (genHeaders.length > 0 || headers.length > 0) {
|
||||
requestString.push(` headers: {\n${genHeaders.join("").slice(0, -2)}\n }`)
|
||||
}
|
||||
requestString.push(`};\n\n`)
|
||||
|
||||
requestString.push(`const request = http.request(url, options, (response) => {\n`)
|
||||
requestString.push(` console.log(response);\n`)
|
||||
requestString.push(`});\n\n`)
|
||||
|
||||
requestString.push(`request.on('error', (e) => {\n`)
|
||||
requestString.push(` console.error(e);\n`)
|
||||
requestString.push(`});\n`)
|
||||
|
||||
if (requestBody) {
|
||||
requestString.push(`\nrequest.write(${requestBody});\n`)
|
||||
}
|
||||
|
||||
requestString.push(`request.end();`)
|
||||
return requestString.join("")
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user