mirror of
https://github.com/foss42/apidash.git
synced 2025-05-30 05:21:15 +08:00
test(fetch): update tests with respect to new changes
This commit is contained in:
@ -8,25 +8,22 @@ void main() {
|
||||
|
||||
group('GET Request', () {
|
||||
test('GET 1', () {
|
||||
const expectedCode = r"""let url = 'https://api.apidash.dev';
|
||||
const expectedCode = r"""const url = 'https://api.apidash.dev';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -36,25 +33,22 @@ fetch(url, options)
|
||||
|
||||
test('GET 2', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.apidash.dev/country/data?code=US';
|
||||
r"""const url = 'https://api.apidash.dev/country/data?code=US';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -64,25 +58,22 @@ fetch(url, options)
|
||||
|
||||
test('GET 3', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.apidash.dev/country/data?code=IND';
|
||||
r"""const url = 'https://api.apidash.dev/country/data?code=IND';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -92,25 +83,22 @@ fetch(url, options)
|
||||
|
||||
test('GET 4', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.apidash.dev/humanize/social?num=8700000&digits=3&system=SS&add_space=true&trailing_zeros=true';
|
||||
r"""const url = 'https://api.apidash.dev/humanize/social?num=8700000&digits=3&system=SS&add_space=true&trailing_zeros=true';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -120,28 +108,25 @@ fetch(url, options)
|
||||
|
||||
test('GET 5', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.github.com/repos/foss42/apidash';
|
||||
r"""const url = 'https://api.github.com/repos/foss42/apidash';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent"
|
||||
}
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -151,28 +136,25 @@ fetch(url, options)
|
||||
|
||||
test('GET 6', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.github.com/repos/foss42/apidash?raw=true';
|
||||
r"""const url = 'https://api.github.com/repos/foss42/apidash?raw=true';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent"
|
||||
}
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -181,25 +163,22 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('GET 7', () {
|
||||
const expectedCode = r"""let url = 'https://api.apidash.dev';
|
||||
const expectedCode = r"""const url = 'https://api.apidash.dev';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -209,28 +188,25 @@ fetch(url, options)
|
||||
|
||||
test('GET 8', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.github.com/repos/foss42/apidash?raw=true';
|
||||
r"""const url = 'https://api.github.com/repos/foss42/apidash?raw=true';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent"
|
||||
}
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -240,25 +216,22 @@ fetch(url, options)
|
||||
|
||||
test('GET 9', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.apidash.dev/humanize/social?num=8700000&add_space=true';
|
||||
r"""const url = 'https://api.apidash.dev/humanize/social?num=8700000&add_space=true';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -268,28 +241,25 @@ fetch(url, options)
|
||||
|
||||
test('GET 10', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.apidash.dev/humanize/social';
|
||||
r"""const url = 'https://api.apidash.dev/humanize/social';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent"
|
||||
}
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -303,28 +273,25 @@ fetch(url, options)
|
||||
|
||||
test('GET 11', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.apidash.dev/humanize/social?num=8700000&digits=3';
|
||||
r"""const url = 'https://api.apidash.dev/humanize/social?num=8700000&digits=3';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent"
|
||||
}
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -334,25 +301,22 @@ fetch(url, options)
|
||||
|
||||
test('GET 12', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.apidash.dev/humanize/social';
|
||||
r"""const url = 'https://api.apidash.dev/humanize/social';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -363,25 +327,22 @@ fetch(url, options)
|
||||
|
||||
group('HEAD Request', () {
|
||||
test('HEAD 1', () {
|
||||
const expectedCode = r"""let url = 'https://api.apidash.dev';
|
||||
const expectedCode = r"""const url = 'https://api.apidash.dev';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'HEAD'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -390,25 +351,22 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('HEAD 2', () {
|
||||
const expectedCode = r"""let url = 'http://api.apidash.dev';
|
||||
const expectedCode = r"""const url = 'http://api.apidash.dev';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'HEAD'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -419,30 +377,26 @@ fetch(url, options)
|
||||
|
||||
group('POST Request', () {
|
||||
test('POST 1', () {
|
||||
const expectedCode = r"""let url = 'https://api.apidash.dev/case/lower';
|
||||
const expectedCode = r"""const url = 'https://api.apidash.dev/case/lower';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Content-Type": "text/plain"
|
||||
},
|
||||
body:
|
||||
"{\n\"text\": \"I LOVE Flutter\"\n}"
|
||||
body: "{\n\"text\": \"I LOVE Flutter\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -451,30 +405,26 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('POST 2', () {
|
||||
const expectedCode = r"""let url = 'https://api.apidash.dev/case/lower';
|
||||
const expectedCode = r"""const url = 'https://api.apidash.dev/case/lower';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body:
|
||||
"{\n\"text\": \"I LOVE Flutter\",\n\"flag\": null,\n\"male\": true,\n\"female\": false,\n\"no\": 1.2,\n\"arr\": [\"null\", \"true\", \"false\", null]\n}"
|
||||
body: "{\n\"text\": \"I LOVE Flutter\",\n\"flag\": null,\n\"male\": true,\n\"female\": false,\n\"no\": 1.2,\n\"arr\": [\"null\", \"true\", \"false\", null]\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -483,65 +433,278 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('POST 3', () {
|
||||
const expectedCode = r"""let url = 'https://api.apidash.dev/case/lower';
|
||||
const expectedCode = r"""const url = 'https://api.apidash.dev/case/lower';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "Test Agent"
|
||||
},
|
||||
body:
|
||||
"{\n\"text\": \"I LOVE Flutter\"\n}"
|
||||
body: "{\n\"text\": \"I LOVE Flutter\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsFetch, requestModelPost3, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 4', () {
|
||||
const expectedCode =
|
||||
r"""// refer https://github.com/foss42/apidash/issues/293#issuecomment-1995208098 for details regarding integration
|
||||
|
||||
const payload = new FormData();
|
||||
payload.append("text", "API")
|
||||
payload.append("sep", "|")
|
||||
payload.append("times", "3")
|
||||
|
||||
const url = 'https://api.apidash.dev/io/form';
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
body: payload
|
||||
};
|
||||
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.jsFetch,
|
||||
requestModelPost4,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test('POST 5', () {
|
||||
const expectedCode =
|
||||
r"""// refer https://github.com/foss42/apidash/issues/293#issuecomment-1995208098 for details regarding integration
|
||||
|
||||
const payload = new FormData();
|
||||
payload.append("text", "API")
|
||||
payload.append("sep", "|")
|
||||
payload.append("times", "3")
|
||||
|
||||
const url = 'https://api.apidash.dev/io/form';
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent"
|
||||
},
|
||||
body: payload
|
||||
};
|
||||
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.jsFetch,
|
||||
requestModelPost5,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 6', () {
|
||||
const expectedCode =
|
||||
r"""// refer https://github.com/foss42/apidash/issues/293#issuecomment-1995208098 for details regarding integration
|
||||
|
||||
const payload = new FormData();
|
||||
payload.append("token", "xyz")
|
||||
payload.append("imfile", fileInput1.files[0])
|
||||
|
||||
const url = 'https://api.apidash.dev/io/img';
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
body: payload
|
||||
};
|
||||
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.jsFetch,
|
||||
requestModelPost6,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 7', () {
|
||||
const expectedCode =
|
||||
r"""// refer https://github.com/foss42/apidash/issues/293#issuecomment-1995208098 for details regarding integration
|
||||
|
||||
const payload = new FormData();
|
||||
payload.append("token", "xyz")
|
||||
payload.append("imfile", fileInput1.files[0])
|
||||
|
||||
const url = 'https://api.apidash.dev/io/img';
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
body: payload
|
||||
};
|
||||
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.jsFetch,
|
||||
requestModelPost7,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 8', () {
|
||||
const expectedCode =
|
||||
r"""// refer https://github.com/foss42/apidash/issues/293#issuecomment-1995208098 for details regarding integration
|
||||
|
||||
const payload = new FormData();
|
||||
payload.append("text", "API")
|
||||
payload.append("sep", "|")
|
||||
payload.append("times", "3")
|
||||
|
||||
const url = 'https://api.apidash.dev/io/form?size=2&len=3';
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
body: payload
|
||||
};
|
||||
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.jsFetch,
|
||||
requestModelPost8,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 9', () {
|
||||
const expectedCode =
|
||||
r"""// refer https://github.com/foss42/apidash/issues/293#issuecomment-1995208098 for details regarding integration
|
||||
|
||||
const payload = new FormData();
|
||||
payload.append("token", "xyz")
|
||||
payload.append("imfile", fileInput1.files[0])
|
||||
|
||||
const url = 'https://api.apidash.dev/io/img?size=2&len=3';
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent",
|
||||
"Keep-Alive": "true"
|
||||
},
|
||||
body: payload
|
||||
};
|
||||
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.jsFetch,
|
||||
requestModelPost9,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('PUT Request', () {
|
||||
test('PUT 1', () {
|
||||
const expectedCode = r"""let url = 'https://reqres.in/api/users/2';
|
||||
const expectedCode = r"""const url = 'https://reqres.in/api/users/2';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body:
|
||||
"{\n\"name\": \"morpheus\",\n\"job\": \"zion resident\"\n}"
|
||||
body: "{\n\"name\": \"morpheus\",\n\"job\": \"zion resident\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -552,30 +715,26 @@ fetch(url, options)
|
||||
|
||||
group('PATCH Request', () {
|
||||
test('PATCH 1', () {
|
||||
const expectedCode = r"""let url = 'https://reqres.in/api/users/2';
|
||||
const expectedCode = r"""const url = 'https://reqres.in/api/users/2';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body:
|
||||
"{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}"
|
||||
body: "{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -586,25 +745,22 @@ fetch(url, options)
|
||||
|
||||
group('DELETE Request', () {
|
||||
test('DELETE 1', () {
|
||||
const expectedCode = r"""let url = 'https://reqres.in/api/users/2';
|
||||
const expectedCode = r"""const url = 'https://reqres.in/api/users/2';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'DELETE'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -614,30 +770,26 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('DELETE 2', () {
|
||||
const expectedCode = r"""let url = 'https://reqres.in/api/users/2';
|
||||
const expectedCode = r"""const url = 'https://reqres.in/api/users/2';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body:
|
||||
"{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}"
|
||||
body: "{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
|
@ -8,27 +8,24 @@ void main() {
|
||||
|
||||
group('GET Request', () {
|
||||
test('GET 1', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.apidash.dev';
|
||||
const url = 'https://api.apidash.dev';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -38,27 +35,24 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('GET 2', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.apidash.dev/country/data?code=US';
|
||||
const url = 'https://api.apidash.dev/country/data?code=US';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -68,27 +62,24 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('GET 3', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.apidash.dev/country/data?code=IND';
|
||||
const url = 'https://api.apidash.dev/country/data?code=IND';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -98,27 +89,24 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('GET 4', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.apidash.dev/humanize/social?num=8700000&digits=3&system=SS&add_space=true&trailing_zeros=true';
|
||||
const url = 'https://api.apidash.dev/humanize/social?num=8700000&digits=3&system=SS&add_space=true&trailing_zeros=true';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -128,30 +116,27 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('GET 5', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.github.com/repos/foss42/apidash';
|
||||
const url = 'https://api.github.com/repos/foss42/apidash';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent"
|
||||
}
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -161,30 +146,27 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('GET 6', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.github.com/repos/foss42/apidash?raw=true';
|
||||
const url = 'https://api.github.com/repos/foss42/apidash?raw=true';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent"
|
||||
}
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -194,27 +176,24 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('GET 7', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.apidash.dev';
|
||||
const url = 'https://api.apidash.dev';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -224,30 +203,27 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('GET 8', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.github.com/repos/foss42/apidash?raw=true';
|
||||
const url = 'https://api.github.com/repos/foss42/apidash?raw=true';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent"
|
||||
}
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -257,27 +233,24 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('GET 9', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.apidash.dev/humanize/social?num=8700000&add_space=true';
|
||||
const url = 'https://api.apidash.dev/humanize/social?num=8700000&add_space=true';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -287,30 +260,27 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('GET 10', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.apidash.dev/humanize/social';
|
||||
const url = 'https://api.apidash.dev/humanize/social';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent"
|
||||
}
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -323,30 +293,27 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('GET 11', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.apidash.dev/humanize/social?num=8700000&digits=3';
|
||||
const url = 'https://api.apidash.dev/humanize/social?num=8700000&digits=3';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent"
|
||||
}
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -356,27 +323,24 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('GET 12', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.apidash.dev/humanize/social';
|
||||
const url = 'https://api.apidash.dev/humanize/social';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -388,27 +352,24 @@ fetch(url, options)
|
||||
|
||||
group('HEAD Request', () {
|
||||
test('HEAD 1', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.apidash.dev';
|
||||
const url = 'https://api.apidash.dev';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'HEAD'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -418,27 +379,24 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('HEAD 2', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'http://api.apidash.dev';
|
||||
const url = 'http://api.apidash.dev';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'HEAD'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -450,32 +408,28 @@ fetch(url, options)
|
||||
|
||||
group('POST Request', () {
|
||||
test('POST 1', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.apidash.dev/case/lower';
|
||||
const url = 'https://api.apidash.dev/case/lower';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Content-Type": "text/plain"
|
||||
},
|
||||
body:
|
||||
"{\n\"text\": \"I LOVE Flutter\"\n}"
|
||||
body: "{\n\"text\": \"I LOVE Flutter\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -485,32 +439,28 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('POST 2', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.apidash.dev/case/lower';
|
||||
const url = 'https://api.apidash.dev/case/lower';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body:
|
||||
"{\n\"text\": \"I LOVE Flutter\",\n\"flag\": null,\n\"male\": true,\n\"female\": false,\n\"no\": 1.2,\n\"arr\": [\"null\", \"true\", \"false\", null]\n}"
|
||||
body: "{\n\"text\": \"I LOVE Flutter\",\n\"flag\": null,\n\"male\": true,\n\"female\": false,\n\"no\": 1.2,\n\"arr\": [\"null\", \"true\", \"false\", null]\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -520,33 +470,29 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('POST 3', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://api.apidash.dev/case/lower';
|
||||
const url = 'https://api.apidash.dev/case/lower';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "Test Agent"
|
||||
},
|
||||
body:
|
||||
"{\n\"text\": \"I LOVE Flutter\"\n}"
|
||||
body: "{\n\"text\": \"I LOVE Flutter\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -554,36 +500,241 @@ fetch(url, options)
|
||||
CodegenLanguage.nodejsFetch, requestModelPost3, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 4', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
import { fileFromSync, FormData } from 'node-fetch'
|
||||
|
||||
const payload = new FormData();
|
||||
payload.append("text", "API")
|
||||
payload.append("sep", "|")
|
||||
payload.append("times", "3")
|
||||
|
||||
const url = 'https://api.apidash.dev/io/form';
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
body: payload
|
||||
};
|
||||
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.nodejsFetch, requestModelPost4, "https",
|
||||
boundary: "test"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test('POST 5', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
import { fileFromSync, FormData } from 'node-fetch'
|
||||
|
||||
const payload = new FormData();
|
||||
payload.append("text", "API")
|
||||
payload.append("sep", "|")
|
||||
payload.append("times", "3")
|
||||
|
||||
const url = 'https://api.apidash.dev/io/form';
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent"
|
||||
},
|
||||
body: payload
|
||||
};
|
||||
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.nodejsFetch, requestModelPost5, "https",
|
||||
boundary: "test"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 6', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
import { fileFromSync, FormData } from 'node-fetch'
|
||||
|
||||
const payload = new FormData();
|
||||
payload.append("token", "xyz")
|
||||
payload.append("imfile", fileFromSync("/Documents/up/1.png"))
|
||||
|
||||
const url = 'https://api.apidash.dev/io/img';
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
body: payload
|
||||
};
|
||||
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.nodejsFetch, requestModelPost6, "https",
|
||||
boundary: "test"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 7', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
import { fileFromSync, FormData } from 'node-fetch'
|
||||
|
||||
const payload = new FormData();
|
||||
payload.append("token", "xyz")
|
||||
payload.append("imfile", fileFromSync("/Documents/up/1.png"))
|
||||
|
||||
const url = 'https://api.apidash.dev/io/img';
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
body: payload
|
||||
};
|
||||
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.nodejsFetch, requestModelPost7, "https",
|
||||
boundary: "test"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 8', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
import { fileFromSync, FormData } from 'node-fetch'
|
||||
|
||||
const payload = new FormData();
|
||||
payload.append("text", "API")
|
||||
payload.append("sep", "|")
|
||||
payload.append("times", "3")
|
||||
|
||||
const url = 'https://api.apidash.dev/io/form?size=2&len=3';
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
body: payload
|
||||
};
|
||||
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.nodejsFetch, requestModelPost8, "https",
|
||||
boundary: "test"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 9', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
import { fileFromSync, FormData } from 'node-fetch'
|
||||
|
||||
const payload = new FormData();
|
||||
payload.append("token", "xyz")
|
||||
payload.append("imfile", fileFromSync("/Documents/up/1.png"))
|
||||
|
||||
const url = 'https://api.apidash.dev/io/img?size=2&len=3';
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent",
|
||||
"Keep-Alive": "true"
|
||||
},
|
||||
body: payload
|
||||
};
|
||||
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.nodejsFetch, requestModelPost9, "https",
|
||||
boundary: "test"),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('PUT Request', () {
|
||||
test('PUT 1', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://reqres.in/api/users/2';
|
||||
const url = 'https://reqres.in/api/users/2';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body:
|
||||
"{\n\"name\": \"morpheus\",\n\"job\": \"zion resident\"\n}"
|
||||
body: "{\n\"name\": \"morpheus\",\n\"job\": \"zion resident\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -595,32 +746,28 @@ fetch(url, options)
|
||||
|
||||
group('PATCH Request', () {
|
||||
test('PATCH 1', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://reqres.in/api/users/2';
|
||||
const url = 'https://reqres.in/api/users/2';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body:
|
||||
"{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}"
|
||||
body: "{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -632,27 +779,24 @@ fetch(url, options)
|
||||
|
||||
group('DELETE Request', () {
|
||||
test('DELETE 1', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://reqres.in/api/users/2';
|
||||
const url = 'https://reqres.in/api/users/2';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'DELETE'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
@ -662,32 +806,28 @@ fetch(url, options)
|
||||
});
|
||||
|
||||
test('DELETE 2', () {
|
||||
const expectedCode = r"""import fetch from 'node-fetch';
|
||||
const expectedCode = r"""import fetch from 'node-fetch'
|
||||
|
||||
let url = 'https://reqres.in/api/users/2';
|
||||
const url = 'https://reqres.in/api/users/2';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body:
|
||||
"{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}"
|
||||
body: "{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
console.error(`error:${err}`);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
|
Reference in New Issue
Block a user