codegen.sample 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { codegens } from "../codegen"
  2. const TEST_URL = "https://httpbin.org"
  3. const TEST_PATH_NAME = "/path/to"
  4. const TEST_QUERY_STRING = "?a=b"
  5. const TEST_HTTP_USER = "mockUser"
  6. const TEST_HTTP_PASSWORD = "mockPassword"
  7. const TEST_BEARER_TOKEN = "abcdefghijklmn"
  8. const TEST_RAW_REQUEST_BODY = "foo=bar&baz=qux"
  9. const TEST_RAW_PARAMS_JSON = '{"foo": "bar", "baz": "qux"}'
  10. const TEST_RAW_PARAMS_XML = `<?xml version='1.0' encoding='utf-8'?>
  11. <xml>
  12. <element foo="bar"></element>
  13. </xml>`
  14. const TEST_HEADERS = [
  15. { key: "h1", value: "h1v" },
  16. { key: "h2", value: "h2v" },
  17. ]
  18. codegens.forEach((codegen) => {
  19. describe(`generate request for ${codegen.name}`, () => {
  20. const testCases = [
  21. [
  22. "generate GET request",
  23. {
  24. url: TEST_URL,
  25. pathName: TEST_PATH_NAME,
  26. queryString: TEST_QUERY_STRING,
  27. auth: "Basic Auth",
  28. httpUser: TEST_HTTP_USER,
  29. httpPassword: TEST_HTTP_PASSWORD,
  30. method: "GET",
  31. rawInput: false,
  32. rawParams: "",
  33. rawRequestBody: "",
  34. headers: TEST_HEADERS,
  35. },
  36. ],
  37. [
  38. "generate POST request for JSON",
  39. {
  40. url: TEST_URL,
  41. pathName: TEST_PATH_NAME,
  42. queryString: TEST_QUERY_STRING,
  43. auth: "Bearer Token",
  44. bearerToken: TEST_BEARER_TOKEN,
  45. method: "POST",
  46. rawInput: true,
  47. rawParams: TEST_RAW_PARAMS_JSON,
  48. rawRequestBody: "",
  49. contentType: "application/json",
  50. headers: TEST_HEADERS,
  51. },
  52. ],
  53. [
  54. "generate POST request for XML",
  55. {
  56. url: TEST_URL,
  57. pathName: TEST_PATH_NAME,
  58. queryString: TEST_QUERY_STRING,
  59. auth: "OAuth 2.0",
  60. bearerToken: TEST_BEARER_TOKEN,
  61. method: "POST",
  62. rawInput: true,
  63. rawParams: TEST_RAW_PARAMS_XML,
  64. rawRequestBody: "",
  65. contentType: "application/xml",
  66. headers: TEST_HEADERS,
  67. },
  68. ],
  69. [
  70. "generate PUT request for www-form-urlencoded",
  71. {
  72. url: TEST_URL,
  73. pathName: TEST_PATH_NAME,
  74. queryString: TEST_QUERY_STRING,
  75. method: "PUT",
  76. rawInput: false,
  77. rawRequestBody: TEST_RAW_REQUEST_BODY,
  78. contentType: "application/x-www-form-urlencoded",
  79. headers: [],
  80. },
  81. ],
  82. ]
  83. test.each(testCases)("%s", (_, context) => {
  84. const result = codegen.generator(context)
  85. expect(result).toMatchSnapshot()
  86. })
  87. })
  88. })