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 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
}
}

View File

@ -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<Query[]> => {
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]

View File

@ -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", {})
})
})
})

View File

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

View File

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