123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820 |
- // @ts-check
- // ^^^ Enables Type Checking by the TypeScript compiler
- import { makeRESTRequest, rawKeyValueEntriesToString } from "@hoppscotch/data"
- import { parseCurlToHoppRESTReq } from ".."
- const samples = [
- {
- command: `
- curl --request GET \
- --url https://echo.hoppscotch.io/ \
- --header 'content-type: application/x-www-form-urlencoded' \
- --data a=b \
- --data c=d
- `,
- response: makeRESTRequest({
- method: "GET",
- name: "Untitled request",
- endpoint: "https://echo.hoppscotch.io/",
- auth: { authType: "none", authActive: true },
- body: {
- contentType: "application/x-www-form-urlencoded",
- body: rawKeyValueEntriesToString([
- {
- active: true,
- key: "a",
- value: "b",
- },
- {
- active: true,
- key: "c",
- value: "d",
- },
- ]),
- },
- headers: [],
- params: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: `
- curl 'http://avs:def@127.0.0.1:8000/api/admin/crm/brand/4'
- -X PUT
- -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'
- -H 'Accept: application/json, text/plain, */*'
- -H 'Accept-Language: en'
- --compressed
- -H 'Content-Type: application/hal+json;charset=utf-8'
- -H 'Origin: http://localhost:3012'
- -H 'Connection: keep-alive'
- -H 'Referer: http://localhost:3012/crm/company/4'
- --data-raw '{"id":4,"crm_company_id":4,"industry_primary_id":2,"industry_head_id":2,"industry_body_id":2,"code":"01","barcode":"222010101","summary":"Healt-Seasoning-Basic-Hori-Kello","name":"Kellolaa","sub_code":"01","sub_name":"Hori","created_at":"2020-06-08 08:50:02","updated_at":"2020-06-08 08:50:02","company":4,"primary":{"id":2,"code":"2","name":"Healt","created_at":"2020-05-19 07:05:02","updated_at":"2020-05-19 07:09:28"},"head":{"id":2,"code":"2","name":"Seasoning","created_at":"2020-04-14 19:34:33","updated_at":"2020-04-14 19:34:33"},"body":{"id":2,"code":"2","name":"Basic","created_at":"2020-04-14 19:33:54","updated_at":"2020-04-14 19:33:54"},"contacts":[]}'
- `,
- response: makeRESTRequest({
- method: "PUT",
- name: "Untitled request",
- endpoint: "http://127.0.0.1:8000/api/admin/crm/brand/4",
- auth: {
- authType: "basic",
- authActive: true,
- username: "avs",
- password: "def",
- },
- body: {
- contentType: "application/hal+json",
- body: `{
- "id": 4,
- "crm_company_id": 4,
- "industry_primary_id": 2,
- "industry_head_id": 2,
- "industry_body_id": 2,
- "code": "01",
- "barcode": "222010101",
- "summary": "Healt-Seasoning-Basic-Hori-Kello",
- "name": "Kellolaa",
- "sub_code": "01",
- "sub_name": "Hori",
- "created_at": "2020-06-08 08:50:02",
- "updated_at": "2020-06-08 08:50:02",
- "company": 4,
- "primary": {
- "id": 2,
- "code": "2",
- "name": "Healt",
- "created_at": "2020-05-19 07:05:02",
- "updated_at": "2020-05-19 07:09:28"
- },
- "head": {
- "id": 2,
- "code": "2",
- "name": "Seasoning",
- "created_at": "2020-04-14 19:34:33",
- "updated_at": "2020-04-14 19:34:33"
- },
- "body": {
- "id": 2,
- "code": "2",
- "name": "Basic",
- "created_at": "2020-04-14 19:33:54",
- "updated_at": "2020-04-14 19:33:54"
- },
- "contacts": []
- }`,
- },
- headers: [
- {
- active: true,
- key: "User-Agent",
- value:
- "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0",
- },
- {
- active: true,
- key: "Accept",
- value: "application/json, text/plain, */*",
- },
- {
- active: true,
- key: "Accept-Language",
- value: "en",
- },
- {
- active: true,
- key: "Origin",
- value: "http://localhost:3012",
- },
- {
- active: true,
- key: "Connection",
- value: "keep-alive",
- },
- {
- active: true,
- key: "Referer",
- value: "http://localhost:3012/crm/company/4",
- },
- ],
- params: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: `curl google.com`,
- response: makeRESTRequest({
- method: "GET",
- name: "Untitled request",
- endpoint: "https://google.com/",
- auth: { authType: "none", authActive: true },
- body: {
- contentType: null,
- body: null,
- },
- headers: [],
- params: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: `curl -X POST -d '{"foo":"bar"}' http://localhost:1111/hello/world/?bar=baz&buzz`,
- response: makeRESTRequest({
- method: "POST",
- name: "Untitled request",
- endpoint: "http://localhost:1111/hello/world/?buzz",
- auth: { authType: "none", authActive: true },
- body: {
- contentType: "application/json",
- body: `{\n "foo": "bar"\n}`,
- },
- headers: [],
- params: [
- {
- active: true,
- key: "bar",
- value: "baz",
- },
- ],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: `curl --get -d "tool=curl" -d "age=old" https://example.com`,
- response: makeRESTRequest({
- method: "GET",
- name: "Untitled request",
- endpoint: "https://example.com/",
- auth: { authType: "none", authActive: true },
- body: {
- contentType: null,
- body: null,
- },
- headers: [],
- params: [
- {
- active: true,
- key: "tool",
- value: "curl",
- },
- {
- active: true,
- key: "age",
- value: "old",
- },
- ],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: `curl -F hello=hello2 -F hello3=@hello4.txt bing.com`,
- response: makeRESTRequest({
- method: "POST",
- name: "Untitled request",
- endpoint: "https://bing.com/",
- auth: { authType: "none", authActive: true },
- body: {
- contentType: "multipart/form-data",
- body: [
- {
- active: true,
- isFile: false,
- key: "hello",
- value: "hello2",
- },
- {
- active: true,
- isFile: false,
- key: "hello3",
- value: "",
- },
- ],
- },
- headers: [],
- params: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command:
- "curl -X GET localhost -H 'Accept: application/json' --user root:toor",
- response: makeRESTRequest({
- method: "GET",
- name: "Untitled request",
- endpoint: "http://localhost/",
- auth: {
- authType: "basic",
- authActive: true,
- username: "root",
- password: "toor",
- },
- body: {
- contentType: null,
- body: null,
- },
- params: [],
- headers: [
- {
- active: true,
- key: "Accept",
- value: "application/json",
- },
- ],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command:
- "curl -X GET localhost --header 'Authorization: Basic dXNlcjpwYXNz'",
- response: makeRESTRequest({
- method: "GET",
- name: "Untitled request",
- endpoint: "http://localhost/",
- auth: {
- authType: "basic",
- authActive: true,
- username: "user",
- password: "pass",
- },
- body: {
- contentType: null,
- body: null,
- },
- params: [],
- headers: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command:
- "curl -X GET localhost:9900 --header 'Authorization: Basic 77898dXNlcjpwYXNz'",
- response: makeRESTRequest({
- method: "GET",
- name: "Untitled request",
- endpoint: "http://localhost:9900/",
- auth: {
- authType: "none",
- authActive: true,
- },
- body: {
- contentType: null,
- body: null,
- },
- params: [],
- headers: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command:
- "curl -X GET localhost --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'",
- response: makeRESTRequest({
- method: "GET",
- name: "Untitled request",
- endpoint: "http://localhost/",
- auth: {
- authType: "bearer",
- authActive: true,
- token:
- "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
- },
- body: {
- contentType: null,
- body: null,
- },
- params: [],
- headers: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: `curl --get -I -d "tool=curl" -d "platform=hoppscotch" -d"io" https://hoppscotch.io`,
- response: makeRESTRequest({
- method: "HEAD",
- name: "Untitled request",
- endpoint: "https://hoppscotch.io/?io",
- auth: {
- authActive: true,
- authType: "none",
- },
- body: {
- contentType: null,
- body: null,
- },
- params: [
- {
- active: true,
- key: "tool",
- value: "curl",
- },
- {
- active: true,
- key: "platform",
- value: "hoppscotch",
- },
- ],
- headers: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: `curl 'https://someshadywebsite.com/questionable/path/?and=params&so&stay=tuned&' \
- -H 'user-agent: Mozilla/5.0' \
- -H 'accept: text/html' \
- -H $'cookie: cookie-cookie' \
- --data $'------WebKitFormBoundaryj3oufpIISPa2DP7c\\r\\nContent-Disposition: form-data; name="EmailAddress"\\r\\n\\r\\ntest@test.com\\r\\n------WebKitFormBoundaryj3oufpIISPa2DP7c\\r\\nContent-Disposition: form-data; name="Entity"\\r\\n\\r\\n1\\r\\n------WebKitFormBoundaryj3oufpIISPa2DP7c--\\r\\n'`,
- response: makeRESTRequest({
- method: "POST",
- name: "Untitled request",
- endpoint: "https://someshadywebsite.com/questionable/path/?so",
- auth: {
- authActive: true,
- authType: "none",
- },
- body: {
- contentType: "multipart/form-data",
- body: [
- {
- active: true,
- isFile: false,
- key: "EmailAddress",
- value: "test@test.com",
- },
- {
- active: true,
- isFile: false,
- key: "Entity",
- value: "1",
- },
- ],
- },
- params: [
- {
- active: true,
- key: "and",
- value: "params",
- },
- {
- active: true,
- key: "stay",
- value: "tuned",
- },
- ],
- headers: [
- {
- active: true,
- key: "user-agent",
- value: "Mozilla/5.0",
- },
- {
- active: true,
- key: "accept",
- value: "text/html",
- },
- {
- active: true,
- key: "cookie",
- value: "cookie-cookie",
- },
- ],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command:
- "curl localhost -H 'content-type: multipart/form-data; boundary=------------------------d74496d66958873e' --data '-----------------------------d74496d66958873e\\r\\nContent-Disposition: form-data; name=\"file\"; filename=\"test.txt\"\\r\\nContent-Type: text/plain\\r\\n\\r\\nHello World\\r\\n\\r\\n-----------------------------d74496d66958873e--\\r\\n'",
- response: makeRESTRequest({
- method: "POST",
- name: "Untitled request",
- endpoint: "http://localhost/",
- auth: {
- authActive: true,
- authType: "none",
- },
- body: {
- contentType: "multipart/form-data",
- body: [
- {
- active: true,
- isFile: false,
- key: "file",
- value: "",
- },
- ],
- },
- params: [],
- headers: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: `curl 'https://hoppscotch.io/' \
- -H 'authority: hoppscotch.io' \
- -H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"' \
- -H 'accept: */*' \
- -H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36' \
- -H 'sec-ch-ua-platform: "Windows"' \
- -H 'accept-language: en-US,en;q=0.9,ml;q=0.8' \
- --compressed`,
- response: makeRESTRequest({
- method: "GET",
- name: "Untitled request",
- endpoint: "https://hoppscotch.io/",
- auth: { authType: "none", authActive: true },
- body: {
- contentType: null,
- body: null,
- },
- params: [],
- headers: [
- {
- active: true,
- key: "authority",
- value: "hoppscotch.io",
- },
- {
- active: true,
- key: "sec-ch-ua",
- value:
- '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
- },
- {
- active: true,
- key: "accept",
- value: "*/*",
- },
- {
- active: true,
- key: "user-agent",
- value:
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36",
- },
- {
- active: true,
- key: "sec-ch-ua-platform",
- value: '"Windows"',
- },
- {
- active: true,
- key: "accept-language",
- value: "en-US,en;q=0.9,ml;q=0.8",
- },
- ],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: `curl --request GET \
- --url 'https://echo.hoppscotch.io/?hello=there' \
- --header 'content-type: application/x-www-form-urlencoded' \
- --header 'something: other-thing' \
- --data a=b \
- --data c=d`,
- response: makeRESTRequest({
- method: "GET",
- name: "Untitled request",
- endpoint: "https://echo.hoppscotch.io/",
- auth: { authType: "none", authActive: true },
- body: {
- contentType: "application/x-www-form-urlencoded",
- body: rawKeyValueEntriesToString([
- {
- key: "a",
- value: "b",
- active: true,
- },
- {
- key: "c",
- value: "d",
- active: true,
- },
- ]),
- },
- params: [
- {
- active: true,
- key: "hello",
- value: "there",
- },
- ],
- headers: [
- {
- active: true,
- key: "something",
- value: "other-thing",
- },
- ],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: `curl --request POST \
- --url 'https://echo.hoppscotch.io/?hello=there' \
- --header 'content-type: multipart/form-data' \
- --header 'something: other-thing' \
- --form a=b \
- --form c=d`,
- response: makeRESTRequest({
- name: "Untitled request",
- endpoint: "https://echo.hoppscotch.io/",
- method: "POST",
- auth: { authType: "none", authActive: true },
- headers: [
- {
- active: true,
- key: "something",
- value: "other-thing",
- },
- ],
- body: {
- contentType: "multipart/form-data",
- body: [
- {
- active: true,
- isFile: false,
- key: "a",
- value: "b",
- },
- {
- active: true,
- isFile: false,
- key: "c",
- value: "d",
- },
- ],
- },
- params: [
- {
- active: true,
- key: "hello",
- value: "there",
- },
- ],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: "curl 'muxueqz.top/skybook.html'",
- response: makeRESTRequest({
- name: "Untitled request",
- endpoint: "https://muxueqz.top/skybook.html",
- method: "GET",
- auth: { authType: "none", authActive: true },
- headers: [],
- body: { contentType: null, body: null },
- params: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: "curl -F abcd=efghi",
- response: makeRESTRequest({
- name: "Untitled request",
- endpoint: "https://echo.hoppscotch.io/",
- method: "POST",
- auth: { authType: "none", authActive: true },
- headers: [],
- body: {
- contentType: "multipart/form-data",
- body: [
- {
- active: true,
- isFile: false,
- key: "abcd",
- value: "efghi",
- },
- ],
- },
- params: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: "curl 127.0.0.1 -X custommethod",
- response: makeRESTRequest({
- name: "Untitled request",
- endpoint: "http://127.0.0.1/",
- method: "CUSTOMMETHOD",
- auth: { authType: "none", authActive: true },
- headers: [],
- body: {
- contentType: null,
- body: null,
- },
- params: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: "curl echo.hoppscotch.io -A pinephone",
- response: makeRESTRequest({
- name: "Untitled request",
- endpoint: "https://echo.hoppscotch.io/",
- method: "GET",
- auth: { authType: "none", authActive: true },
- headers: [
- {
- active: true,
- key: "User-Agent",
- value: "pinephone",
- },
- ],
- body: {
- contentType: null,
- body: null,
- },
- params: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: "curl echo.hoppscotch.io -G",
- response: makeRESTRequest({
- name: "Untitled request",
- endpoint: "https://echo.hoppscotch.io/",
- method: "GET",
- auth: { authType: "none", authActive: true },
- headers: [],
- body: {
- contentType: null,
- body: null,
- },
- params: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: `curl --get -I -d "tool=hopp" https://example.org`,
- response: makeRESTRequest({
- name: "Untitled request",
- endpoint: "https://example.org/",
- method: "HEAD",
- auth: { authType: "none", authActive: true },
- headers: [],
- body: {
- contentType: null,
- body: null,
- },
- params: [
- {
- active: true,
- key: "tool",
- value: "hopp",
- },
- ],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: `curl google.com -u userx`,
- response: makeRESTRequest({
- method: "GET",
- name: "Untitled request",
- endpoint: "https://google.com/",
- auth: {
- authType: "basic",
- authActive: true,
- username: "userx",
- password: "",
- },
- body: {
- contentType: null,
- body: null,
- },
- params: [],
- headers: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: `curl google.com -H "Authorization"`,
- response: makeRESTRequest({
- method: "GET",
- name: "Untitled request",
- endpoint: "https://google.com/",
- auth: {
- authType: "none",
- authActive: true,
- },
- body: {
- contentType: null,
- body: null,
- },
- params: [],
- headers: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: `curl \`
- google.com -H "content-type: application/json"`,
- response: makeRESTRequest({
- method: "GET",
- name: "Untitled request",
- endpoint: "https://google.com/",
- auth: {
- authType: "none",
- authActive: true,
- },
- body: {
- contentType: null,
- body: null,
- },
- params: [],
- headers: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- {
- command: `curl 192.168.0.24:8080/ping`,
- response: makeRESTRequest({
- method: "GET",
- name: "Untitled request",
- endpoint: "http://192.168.0.24:8080/ping",
- auth: {
- authType: "none",
- authActive: true,
- },
- body: {
- contentType: null,
- body: null,
- },
- params: [],
- headers: [],
- preRequestScript: "",
- testScript: "",
- }),
- },
- ]
- describe("Parse curl command to Hopp REST Request", () => {
- for (const [i, { command, response }] of samples.entries()) {
- test(`for sample #${i + 1}:\n\n${command}`, () => {
- expect(parseCurlToHoppRESTReq(command)).toEqual(response)
- })
- }
- })
|