From 72ad470c0fbcd1730ed0ebef5ce9f451155ddbb3 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Wed, 1 Dec 2021 11:58:53 +0000 Subject: [PATCH] Curl tests finished --- .../query/import/sources/base/index.ts | 9 ++ .../controllers/query/import/sources/curl.ts | 26 +++++- .../import/sources/tests/curl/curl.spec.js | 92 +++++++++++-------- .../sources/tests/curl/data/empty-body.txt | 2 + .../sources/tests/curl/data/headers.txt | 2 +- 5 files changed, 90 insertions(+), 41 deletions(-) create mode 100644 packages/server/src/api/controllers/query/import/sources/tests/curl/data/empty-body.txt diff --git a/packages/server/src/api/controllers/query/import/sources/base/index.ts b/packages/server/src/api/controllers/query/import/sources/base/index.ts index f6a6b2b57c..747b7b3d66 100644 --- a/packages/server/src/api/controllers/query/import/sources/base/index.ts +++ b/packages/server/src/api/controllers/query/import/sources/base/index.ts @@ -54,6 +54,7 @@ export abstract class ImportSource { const transformer = "return data" const schema = {} path = this.processPath(path) + queryString = this.processQuery(queryString) const query: Query = { datasourceId, @@ -89,4 +90,12 @@ export abstract class ImportSource { return path } + + processQuery = (queryString: string): string => { + if (queryString?.startsWith("?")) { + return queryString.substring(1) + } + + return queryString + } } diff --git a/packages/server/src/api/controllers/query/import/sources/curl.ts b/packages/server/src/api/controllers/query/import/sources/curl.ts index b5a3e624d9..6dbbe012af 100644 --- a/packages/server/src/api/controllers/query/import/sources/curl.ts +++ b/packages/server/src/api/controllers/query/import/sources/curl.ts @@ -7,6 +7,25 @@ const parseCurl = (data: string): any => { return JSON.parse(curlJson) } +/** + * The curl converter parses the request body into the key field of an object + * e.g. --d '{"key":"val"}' produces an object { "{"key":"val"}" : "" } + */ +const parseBody = (curl: any) => { + if (curl.data) { + const keys = Object.keys(curl.data) + if (keys.length) { + const key = keys[0] + try { + return JSON.parse(key) + } catch (e) { + // do nothing + } + } + } + return undefined +} + /** * Curl * https://curl.se/docs/manpage.html @@ -33,12 +52,13 @@ export class Curl extends ImportSource { } getQueries = async (datasourceId: string): Promise => { - const url = new URL(this.curl.url) + const url = new URL(this.curl.raw_url) const name = url.pathname const path = url.pathname const method = this.curl.method const queryString = url.search const headers = this.curl.headers + const requestBody = parseBody(this.curl) const query = this.constructQuery( datasourceId, @@ -46,7 +66,9 @@ export class Curl extends ImportSource { method, path, queryString, - headers + headers, + [], + requestBody ) return [query] diff --git a/packages/server/src/api/controllers/query/import/sources/tests/curl/curl.spec.js b/packages/server/src/api/controllers/query/import/sources/tests/curl/curl.spec.js index 6fc6008b0d..0ce8465277 100644 --- a/packages/server/src/api/controllers/query/import/sources/tests/curl/curl.spec.js +++ b/packages/server/src/api/controllers/query/import/sources/tests/curl/curl.spec.js @@ -1,26 +1,11 @@ -// const Airtable = require("airtable") -// const AirtableIntegration = require("../airtable") const { Curl } = require("../../curl") const fs = require("fs") const path = require('path') -jest.mock("airtable") - -// class TestConfiguration { -// constructor(config = {}) { -// this.integration = new AirtableIntegration.integration(config) -// this.client = { -// create: jest.fn(), -// select: jest.fn(), -// update: jest.fn(), -// destroy: jest.fn(), -// } -// this.integration.client = () => this.client -// } -// } const getData = (file) => { return fs.readFileSync(path.join(__dirname, `./data/${file}.txt`), "utf8") } + describe("Curl Import", () => { let curl @@ -43,46 +28,77 @@ describe("Curl Import", () => { expect(supported).toBe(false) }) + const init = async (file) => { + await curl.isSupported(getData(file)) + } + it("returns import info", async () => { - const data = getData() - await curl.isSupported(data) + await init("get") const info = await curl.getInfo() expect(info.url).toBe("http://example.com") expect(info.name).toBe("example.com") }) describe("Returns queries", () => { - describe("populates verb", () => { - const testVerb = async (file, verb) => { - const data = getData(file) - await curl.isSupported(data) - const queries = await curl.getQueries(data) - expect(queries.length).toBe(1) - expect(queries[0].verb).toBe(verb) - } - it("populates verb", async () => { - await testVerb("get", "read") - await testVerb("post", "create") - await testVerb("put", "update") - await testVerb("delete", "delete") - await testVerb("patch", "patch") - }) - }) + + const getQueries = async (file) => { + await init(file) + const queries = await curl.getQueries() + expect(queries.length).toBe(1) + return queries + } + + const testVerb = async (file, verb) => { + const queries = await getQueries(file) + expect(queries[0].queryVerb).toBe(verb) + } + + it("populates verb", async () => { + await testVerb("get", "read") + await testVerb("post", "create") + await testVerb("put", "update") + await testVerb("delete", "delete") + await testVerb("patch", "patch") + }) + + const testPath = async (file, urlPath) => { + const queries = await getQueries(file) + expect(queries[0].fields.path).toBe(urlPath) + } it("populates path", async () => { - + await testPath("get", "") + await testPath("path", "paths/abc") }) + const testHeaders = async (file, headers) => { + const queries = await getQueries(file) + expect(queries[0].fields.headers).toStrictEqual(headers) + } + it("populates headers", async () => { - + await testHeaders("get", {}) + await testHeaders("headers", { "x-bb-header-1" : "123", "x-bb-header-2" : "456"} ) }) + const testQuery = async (file, queryString) => { + const queries = await getQueries(file) + expect(queries[0].fields.queryString).toBe(queryString) + } it("populates query", async () => { - + await testQuery("get", "") + await testQuery("query", "q1=v1&q1=v2") }) + const testBody = async (file, queryString) => { + const queries = await getQueries(file) + expect(queries[0].fields.requestBody).toStrictEqual(queryString) + } + it("populates body", async () => { - + await testBody("get", undefined) + await testBody("post", { "key" : "val" }) + await testBody("empty-body", {}) }) }) }) \ No newline at end of file diff --git a/packages/server/src/api/controllers/query/import/sources/tests/curl/data/empty-body.txt b/packages/server/src/api/controllers/query/import/sources/tests/curl/data/empty-body.txt new file mode 100644 index 0000000000..3ae78cea6f --- /dev/null +++ b/packages/server/src/api/controllers/query/import/sources/tests/curl/data/empty-body.txt @@ -0,0 +1,2 @@ +curl -X POST 'http://example.com' \ + --data-raw '{}' \ No newline at end of file diff --git a/packages/server/src/api/controllers/query/import/sources/tests/curl/data/headers.txt b/packages/server/src/api/controllers/query/import/sources/tests/curl/data/headers.txt index aba6d05cbf..82ca16f496 100644 --- a/packages/server/src/api/controllers/query/import/sources/tests/curl/data/headers.txt +++ b/packages/server/src/api/controllers/query/import/sources/tests/curl/data/headers.txt @@ -1,3 +1,3 @@ curl 'http://example.com' \ -H 'x-bb-header-1: 123' \ - -H 'x-bb-header-2: 1456' \ No newline at end of file + -H 'x-bb-header-2: 456' \ No newline at end of file