diff --git a/controller/viewController.js b/controller/viewController.js index 94d1272..6ac103f 100644 --- a/controller/viewController.js +++ b/controller/viewController.js @@ -6,10 +6,12 @@ document.addEventListener("DOMContentLoaded", function () { if (curlText) { try { - stringHandler.convertCurl(curlText); + printResultToUser(stringHandler.convertCurl(curlText)); + } catch (error) { printResultToUser('Invalid CUrl. Verify and try again.') console.log(error) + } } diff --git a/service/stringHandlers.js b/service/stringHandlers.js index 61d61e1..5c83dd5 100644 --- a/service/stringHandlers.js +++ b/service/stringHandlers.js @@ -1,4 +1,3 @@ -import * as viewController from "../controller/viewController.js"; export function convertCurl(curlCommand) { @@ -6,7 +5,7 @@ export function convertCurl(curlCommand) { const cypressObjectModel = cypressObjectAdapter(curlObject) const cyRequestText = objectToCyRequest(cypressObjectModel) - viewController.printResultToUser(cyRequestText) + return cyRequestText } export function parseCurl(curlString) { diff --git a/service/stringHandlers.test.js b/service/stringHandlers.test.js new file mode 100644 index 0000000..1716617 --- /dev/null +++ b/service/stringHandlers.test.js @@ -0,0 +1,96 @@ +const stringHandlers = require('./stringHandlers'); + +describe('Extract method from cURL string', () => { + + const cases = [ + { input: "curl --location 'https://example.com/api/v1/id/endpoint' --header 'client: abc' --header 'token: xyz'", expected: 'GET' }, + { input: "curl --location --request POST 'https://example.com/api/v1/id/endpoint' --header 'client: abc' --header 'token: xyz'", expected: 'POST' }, + { input: "curl --location --request PUT 'https://example.com/api/v1/id/endpoint' --header 'client: abc' --header 'token: xyz'", expected: 'PUT' }, + ]; + + cases.forEach(testcase => { + + test('should extract method from cURL string', () => { + + const resultVerb = stringHandlers.getVerbAndUrl(testcase.input).method; + const expectedVerb = testcase.expected; + + expect(resultVerb).toBe(expectedVerb); + + }) + + }); +}); + +describe('Url handler', () => { + + test('should extract Url from cURL string', () => { + + const exampleCurl = "curl --location 'https://example.com/api/v1/id/endpoint?query=abcdef' --header 'client: abc' --header 'token: xyz'" + + const resultUrl = stringHandlers.getVerbAndUrl(exampleCurl).url; + const expectedUrl = 'https://example.com/api/v1/id/endpoint?query=abcdef'; + + expect(resultUrl).toBe(expectedUrl); + + }) + + test('should extract Query string from Url', () => { + + const exampleUrl = "https://example.com/api/v1/id/endpoint?find=abcdef&sort=asc" + + const resultQueryString = stringHandlers.getQueryString(exampleUrl); + const expectedQueryString = { "find": "abcdef", "sort": "asc" }; + + expect(resultQueryString).toStrictEqual(expectedQueryString); + + }) + + test('should not found Query string in Url', () => { + + const exampleUrl = "https://example.com/api/v1/id/endpoint" + + const resultQueryString = stringHandlers.getQueryString(exampleUrl); + const expectedQueryString = {}; + + expect(resultQueryString).toStrictEqual(expectedQueryString); + + }) + + test('should remove Query string from Url', () => { + + const exampleUrl = "https://example.com/api/v1/id/endpoint?find=abcdef&sort=asc" + + const resultUrl = stringHandlers.removeQueryString(exampleUrl); + const expectedUrl = 'https://example.com/api/v1/id/endpoint'; + + expect(resultUrl).toBe(expectedUrl); + + }) + + + +}); + +describe('Extract headers from cURL string', () => { + + const cases = [ + { input: "curl --location 'https://example.com/api/v1/id/endpoint' ", expected: [] }, + { input: "curl --location --request POST 'https://example.com/api/v1/id/endpoint' --header 'client: abc'", expected: [{ "key": "client", "value": "abc" }] }, + { input: "curl --location --request PUT 'https://example.com/api/v1/id/endpoint' --header 'client: abc' --header 'token: xyz' --header 'user: 150'", expected: [{ "key": "client", "value": "abc" }, { "key": "token", "value": "xyz" }, { "key": "user", "value": "150" }] }, + ]; + + cases.forEach(testcase => { + + test('should extract headers from cURL string', () => { + + const resultHeader = stringHandlers.getHeaders(testcase.input); + const expectedHeader = testcase.expected; + + expect(resultHeader).toStrictEqual(expectedHeader); + + }) + + }); +}); +