add codegen for nodejs native and instrument snapshot tests
This commit is contained in:
@@ -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()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user