Curl tests finished

This commit is contained in:
Rory Powell 2021-12-01 11:58:53 +00:00
parent 6f254d3a3b
commit 72ad470c0f
5 changed files with 90 additions and 41 deletions

View File

@ -54,6 +54,7 @@ export abstract class ImportSource {
const transformer = "return data" const transformer = "return data"
const schema = {} const schema = {}
path = this.processPath(path) path = this.processPath(path)
queryString = this.processQuery(queryString)
const query: Query = { const query: Query = {
datasourceId, datasourceId,
@ -89,4 +90,12 @@ export abstract class ImportSource {
return path return path
} }
processQuery = (queryString: string): string => {
if (queryString?.startsWith("?")) {
return queryString.substring(1)
}
return queryString
}
} }

View File

@ -7,6 +7,25 @@ const parseCurl = (data: string): any => {
return JSON.parse(curlJson) 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 * Curl
* https://curl.se/docs/manpage.html * https://curl.se/docs/manpage.html
@ -33,12 +52,13 @@ export class Curl extends ImportSource {
} }
getQueries = async (datasourceId: string): Promise<Query[]> => { getQueries = async (datasourceId: string): Promise<Query[]> => {
const url = new URL(this.curl.url) const url = new URL(this.curl.raw_url)
const name = url.pathname const name = url.pathname
const path = url.pathname const path = url.pathname
const method = this.curl.method const method = this.curl.method
const queryString = url.search const queryString = url.search
const headers = this.curl.headers const headers = this.curl.headers
const requestBody = parseBody(this.curl)
const query = this.constructQuery( const query = this.constructQuery(
datasourceId, datasourceId,
@ -46,7 +66,9 @@ export class Curl extends ImportSource {
method, method,
path, path,
queryString, queryString,
headers headers,
[],
requestBody
) )
return [query] return [query]

View File

@ -1,26 +1,11 @@
// const Airtable = require("airtable")
// const AirtableIntegration = require("../airtable")
const { Curl } = require("../../curl") const { Curl } = require("../../curl")
const fs = require("fs") const fs = require("fs")
const path = require('path') 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) => { const getData = (file) => {
return fs.readFileSync(path.join(__dirname, `./data/${file}.txt`), "utf8") return fs.readFileSync(path.join(__dirname, `./data/${file}.txt`), "utf8")
} }
describe("Curl Import", () => { describe("Curl Import", () => {
let curl let curl
@ -43,23 +28,31 @@ describe("Curl Import", () => {
expect(supported).toBe(false) expect(supported).toBe(false)
}) })
const init = async (file) => {
await curl.isSupported(getData(file))
}
it("returns import info", async () => { it("returns import info", async () => {
const data = getData() await init("get")
await curl.isSupported(data)
const info = await curl.getInfo() const info = await curl.getInfo()
expect(info.url).toBe("http://example.com") expect(info.url).toBe("http://example.com")
expect(info.name).toBe("example.com") expect(info.name).toBe("example.com")
}) })
describe("Returns queries", () => { describe("Returns queries", () => {
describe("populates verb", () => {
const testVerb = async (file, verb) => { const getQueries = async (file) => {
const data = getData(file) await init(file)
await curl.isSupported(data) const queries = await curl.getQueries()
const queries = await curl.getQueries(data)
expect(queries.length).toBe(1) expect(queries.length).toBe(1)
expect(queries[0].verb).toBe(verb) return queries
} }
const testVerb = async (file, verb) => {
const queries = await getQueries(file)
expect(queries[0].queryVerb).toBe(verb)
}
it("populates verb", async () => { it("populates verb", async () => {
await testVerb("get", "read") await testVerb("get", "read")
await testVerb("post", "create") await testVerb("post", "create")
@ -67,22 +60,45 @@ describe("Curl Import", () => {
await testVerb("delete", "delete") await testVerb("delete", "delete")
await testVerb("patch", "patch") 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 () => { 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 () => { 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 () => { 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 () => { it("populates body", async () => {
await testBody("get", undefined)
await testBody("post", { "key" : "val" })
await testBody("empty-body", {})
}) })
}) })
}) })

View File

@ -0,0 +1,2 @@
curl -X POST 'http://example.com' \
--data-raw '{}'

View File

@ -1,3 +1,3 @@
curl 'http://example.com' \ curl 'http://example.com' \
-H 'x-bb-header-1: 123' \ -H 'x-bb-header-1: 123' \
-H 'x-bb-header-2: 1456' -H 'x-bb-header-2: 456'