mirror of
https://github.com/foss42/apidash.git
synced 2025-05-29 04:50:36 +08:00
Merge branch 'main' into feat_ruby_net_http
This commit is contained in:
982
test/codegen/csharp_rest_sharp_codgen_test.dart
Normal file
982
test/codegen/csharp_rest_sharp_codgen_test.dart
Normal file
@ -0,0 +1,982 @@
|
||||
import 'package:apidash/consts.dart';
|
||||
import 'package:apidash/screens/home_page/editor_pane/details_card/code_pane.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import '../request_models.dart';
|
||||
|
||||
void main() {
|
||||
group("Get Request Test", () {
|
||||
test("Get 1", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("", Method.Get);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelGet1, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test("Get 2", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/country/data", Method.Get);
|
||||
|
||||
request.AddQueryParameter("code", "US");
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelGet2, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test("Get 3", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/country/data", Method.Get);
|
||||
|
||||
request.AddQueryParameter("code", "IND");
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelGet3, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test("Get 4", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/humanize/social", Method.Get);
|
||||
|
||||
request.AddQueryParameter("num", "8700000");
|
||||
request.AddQueryParameter("digits", "3");
|
||||
request.AddQueryParameter("system", "SS");
|
||||
request.AddQueryParameter("add_space", "true");
|
||||
request.AddQueryParameter("trailing_zeros", "true");
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelGet4, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test("Get 5", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.github.com";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/repos/foss42/apidash", Method.Get);
|
||||
|
||||
request.AddHeader("User-Agent", "Test Agent");
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelGet5, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test("Get 6", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.github.com";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/repos/foss42/apidash", Method.Get);
|
||||
|
||||
request.AddQueryParameter("raw", "true");
|
||||
|
||||
request.AddHeader("User-Agent", "Test Agent");
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelGet6, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test("Get 7", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("", Method.Get);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelGet7, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test("Get 8", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.github.com";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/repos/foss42/apidash", Method.Get);
|
||||
|
||||
request.AddQueryParameter("raw", "true");
|
||||
|
||||
request.AddHeader("User-Agent", "Test Agent");
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelGet8, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test("Get 9", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/humanize/social", Method.Get);
|
||||
|
||||
request.AddQueryParameter("num", "8700000");
|
||||
request.AddQueryParameter("add_space", "true");
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelGet9, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test("Get 10", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/humanize/social", Method.Get);
|
||||
|
||||
request.AddHeader("User-Agent", "Test Agent");
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelGet10, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test("Get 11", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/humanize/social", Method.Get);
|
||||
|
||||
request.AddQueryParameter("num", "8700000");
|
||||
request.AddQueryParameter("digits", "3");
|
||||
|
||||
request.AddHeader("User-Agent", "Test Agent");
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelGet11, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test("Get 12", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/humanize/social", Method.Get);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelGet12, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group("Head Request Test", () {
|
||||
test("Head 1", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("", Method.Head);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelHead1, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test("Head 2", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "http://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("", Method.Head);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelHead2, "http"),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group("Post Request Test", () {
|
||||
test("Post 1", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/case/lower", Method.Post);
|
||||
|
||||
request.AddHeader("Content-Type", "text/plain");
|
||||
|
||||
var textBody = "{\n\"text\": \"I LOVE Flutter\"\n}";
|
||||
request.AddStringBody(textBody, ContentType.Plain);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelPost1, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test("Post 2", () {
|
||||
const expectedCode = """
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/case/lower", Method.Post);
|
||||
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
var jsonBody = new {
|
||||
text = "I LOVE Flutter",
|
||||
flag = "null",
|
||||
male = "true",
|
||||
female = "false",
|
||||
no = "1.2",
|
||||
arr = "[null, true, false, null]"
|
||||
};
|
||||
request.AddJsonBody(jsonBody);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelPost2, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test("Post 3", () {
|
||||
const expectedCode = """
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/case/lower", Method.Post);
|
||||
|
||||
request.AddHeader("User-Agent", "Test Agent");
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
var jsonBody = new {
|
||||
text = "I LOVE Flutter"
|
||||
};
|
||||
request.AddJsonBody(jsonBody);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelPost3, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test("Post 4", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/io/form", Method.Post);
|
||||
|
||||
request.AlwaysMultipartFormData = true;
|
||||
request.AddParameter("text", "API", ParameterType.GetOrPost);
|
||||
request.AddParameter("sep", "|", ParameterType.GetOrPost);
|
||||
request.AddParameter("times", "3", ParameterType.GetOrPost);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelPost4, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test("Post 5", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/io/form", Method.Post);
|
||||
|
||||
request.AddHeader("User-Agent", "Test Agent");
|
||||
|
||||
request.AlwaysMultipartFormData = true;
|
||||
request.AddParameter("text", "API", ParameterType.GetOrPost);
|
||||
request.AddParameter("sep", "|", ParameterType.GetOrPost);
|
||||
request.AddParameter("times", "3", ParameterType.GetOrPost);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelPost5, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test("Post 6", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/io/img", Method.Post);
|
||||
|
||||
request.AlwaysMultipartFormData = true;
|
||||
var options = new FileParameterOptions
|
||||
{
|
||||
DisableFilenameEncoding = true
|
||||
};
|
||||
request.AddParameter("token", "xyz", ParameterType.GetOrPost);
|
||||
request.AddFile("imfile", "/Documents/up/1.png", options: options);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelPost6, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test("Post 7", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/io/img", Method.Post);
|
||||
|
||||
request.AlwaysMultipartFormData = true;
|
||||
var options = new FileParameterOptions
|
||||
{
|
||||
DisableFilenameEncoding = true
|
||||
};
|
||||
request.AddParameter("token", "xyz", ParameterType.GetOrPost);
|
||||
request.AddFile("imfile", "/Documents/up/1.png", options: options);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelPost7, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test("Post 8", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/io/form", Method.Post);
|
||||
|
||||
request.AddQueryParameter("size", "2");
|
||||
request.AddQueryParameter("len", "3");
|
||||
|
||||
request.AlwaysMultipartFormData = true;
|
||||
request.AddParameter("text", "API", ParameterType.GetOrPost);
|
||||
request.AddParameter("sep", "|", ParameterType.GetOrPost);
|
||||
request.AddParameter("times", "3", ParameterType.GetOrPost);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelPost8, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test("Post 9", () {
|
||||
const expectedCode = r"""
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://api.apidash.dev";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/io/img", Method.Post);
|
||||
|
||||
request.AddQueryParameter("size", "2");
|
||||
request.AddQueryParameter("len", "3");
|
||||
|
||||
request.AddHeader("User-Agent", "Test Agent");
|
||||
request.AddHeader("Keep-Alive", "true");
|
||||
|
||||
request.AlwaysMultipartFormData = true;
|
||||
var options = new FileParameterOptions
|
||||
{
|
||||
DisableFilenameEncoding = true
|
||||
};
|
||||
request.AddParameter("token", "xyz", ParameterType.GetOrPost);
|
||||
request.AddFile("imfile", "/Documents/up/1.png", options: options);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelPost9, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group("Put Request Test", () {
|
||||
test("Put 1", () {
|
||||
const expectedCode = """
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://reqres.in";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/api/users/2", Method.Put);
|
||||
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
var jsonBody = new {
|
||||
name = "morpheus",
|
||||
job = "zion resident"
|
||||
};
|
||||
request.AddJsonBody(jsonBody);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelPut1, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group("Patch Request Test", () {
|
||||
test("Patch 1", () {
|
||||
const expectedCode = """
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://reqres.in";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/api/users/2", Method.Patch);
|
||||
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
var jsonBody = new {
|
||||
name = "marfeus",
|
||||
job = "accountant"
|
||||
};
|
||||
request.AddJsonBody(jsonBody);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelPatch1, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group("Delete Request Test", () {
|
||||
test("Delete 1", () {
|
||||
const expectedCode = """
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://reqres.in";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/api/users/2", Method.Delete);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelDelete1, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test("Delete 2", () {
|
||||
const expectedCode = """
|
||||
using System;
|
||||
using RestSharp;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(){
|
||||
try{
|
||||
const String _baseUrl = "https://reqres.in";
|
||||
var client = new RestClient(_baseUrl);
|
||||
|
||||
var request = new RestRequest("/api/users/2", Method.Delete);
|
||||
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
var jsonBody = new {
|
||||
name = "marfeus",
|
||||
job = "accountant"
|
||||
};
|
||||
request.AddJsonBody(jsonBody);
|
||||
|
||||
var response = await client.ExecuteAsync(request);
|
||||
Console.WriteLine("Status Code: " + (int)response.StatusCode);
|
||||
Console.WriteLine("Response Content: " + response.Content);
|
||||
}
|
||||
catch(Exception ex){
|
||||
Console.WriteLine("Error: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
expect(
|
||||
codegen.getCode(
|
||||
CodegenLanguage.cSharpRestSharp, requestModelDelete2, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
}
|
1109
test/codegen/java_httpclient_codegen_test.dart
Normal file
1109
test/codegen/java_httpclient_codegen_test.dart
Normal file
File diff suppressed because it is too large
Load Diff
@ -8,76 +8,72 @@ void main() {
|
||||
|
||||
group('GET Request', () {
|
||||
test('GET 1', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev"
|
||||
|
||||
response = HTTP.request("GET", url, status_exception=false)
|
||||
|
||||
response = HTTP.get(url)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.juliaHttp, requestModelGet1, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('GET 2', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/country/data"
|
||||
|
||||
|
||||
params = Dict(
|
||||
"code"=> "US"
|
||||
)
|
||||
"code" => "US",
|
||||
)
|
||||
|
||||
response = HTTP.get(url, query=params)
|
||||
response = HTTP.request("GET", url, query=params, status_exception=false)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.juliaHttp, requestModelGet2, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('GET 3', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/country/data"
|
||||
|
||||
|
||||
params = Dict(
|
||||
"code"=> "IND"
|
||||
)
|
||||
"code" => "IND",
|
||||
)
|
||||
|
||||
response = HTTP.get(url, query=params)
|
||||
response = HTTP.request("GET", url, query=params, status_exception=false)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.juliaHttp, requestModelGet3, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('GET 4', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/humanize/social"
|
||||
|
||||
|
||||
params = Dict(
|
||||
"num"=> "8700000",
|
||||
"digits"=> "3",
|
||||
"system"=> "SS",
|
||||
"add_space"=> "true",
|
||||
"trailing_zeros"=> "true"
|
||||
)
|
||||
"num" => "8700000",
|
||||
"digits" => "3",
|
||||
"system" => "SS",
|
||||
"add_space" => "true",
|
||||
"trailing_zeros" => "true",
|
||||
)
|
||||
|
||||
response = HTTP.get(url, query=params)
|
||||
response = HTTP.request("GET", url, query=params, status_exception=false)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.juliaHttp, requestModelGet4, "https"),
|
||||
@ -85,19 +81,18 @@ println("Response Body:", String(response.body))
|
||||
});
|
||||
|
||||
test('GET 5', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.github.com/repos/foss42/apidash"
|
||||
|
||||
|
||||
headers = Dict(
|
||||
"User-Agent"=> "Test Agent"
|
||||
)
|
||||
"User-Agent" => "Test Agent",
|
||||
)
|
||||
|
||||
response = HTTP.get(url, headers=headers)
|
||||
response = HTTP.request("GET", url, headers=headers, status_exception=false)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.juliaHttp, requestModelGet5, "https"),
|
||||
@ -105,23 +100,22 @@ println("Response Body:", String(response.body))
|
||||
});
|
||||
|
||||
test('GET 6', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.github.com/repos/foss42/apidash"
|
||||
|
||||
|
||||
params = Dict(
|
||||
"raw"=> "true"
|
||||
)
|
||||
"raw" => "true",
|
||||
)
|
||||
|
||||
headers = Dict(
|
||||
"User-Agent"=> "Test Agent"
|
||||
)
|
||||
"User-Agent" => "Test Agent",
|
||||
)
|
||||
|
||||
response = HTTP.get(url, query=params, headers=headers)
|
||||
response = HTTP.request("GET", url, headers=headers, query=params, status_exception=false)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.juliaHttp, requestModelGet6, "https"),
|
||||
@ -129,15 +123,14 @@ println("Response Body:", String(response.body))
|
||||
});
|
||||
|
||||
test('GET 7', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev"
|
||||
|
||||
response = HTTP.request("GET", url, status_exception=false)
|
||||
|
||||
response = HTTP.get(url)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.juliaHttp, requestModelGet7, "https"),
|
||||
@ -145,23 +138,22 @@ println("Response Body:", String(response.body))
|
||||
});
|
||||
|
||||
test('GET 8', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.github.com/repos/foss42/apidash"
|
||||
|
||||
|
||||
params = Dict(
|
||||
"raw"=> "true"
|
||||
)
|
||||
"raw" => "true",
|
||||
)
|
||||
|
||||
headers = Dict(
|
||||
"User-Agent"=> "Test Agent"
|
||||
)
|
||||
"User-Agent" => "Test Agent",
|
||||
)
|
||||
|
||||
response = HTTP.get(url, query=params, headers=headers)
|
||||
response = HTTP.request("GET", url, headers=headers, query=params, status_exception=false)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.juliaHttp, requestModelGet8, "https"),
|
||||
@ -169,20 +161,19 @@ println("Response Body:", String(response.body))
|
||||
});
|
||||
|
||||
test('GET 9', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/humanize/social"
|
||||
|
||||
|
||||
params = Dict(
|
||||
"num"=> "8700000",
|
||||
"add_space"=> "true"
|
||||
)
|
||||
"num" => "8700000",
|
||||
"add_space" => "true",
|
||||
)
|
||||
|
||||
response = HTTP.get(url, query=params)
|
||||
response = HTTP.request("GET", url, query=params, status_exception=false)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.juliaHttp, requestModelGet9, "https"),
|
||||
@ -190,19 +181,18 @@ println("Response Body:", String(response.body))
|
||||
});
|
||||
|
||||
test('GET 10', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/humanize/social"
|
||||
|
||||
|
||||
headers = Dict(
|
||||
"User-Agent"=> "Test Agent"
|
||||
)
|
||||
"User-Agent" => "Test Agent",
|
||||
)
|
||||
|
||||
response = HTTP.get(url, headers=headers)
|
||||
response = HTTP.request("GET", url, headers=headers, status_exception=false)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -211,24 +201,23 @@ println("Response Body:", String(response.body))
|
||||
});
|
||||
|
||||
test('GET 11', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/humanize/social"
|
||||
|
||||
|
||||
params = Dict(
|
||||
"num"=> "8700000",
|
||||
"digits"=> "3"
|
||||
)
|
||||
"num" => "8700000",
|
||||
"digits" => "3",
|
||||
)
|
||||
|
||||
headers = Dict(
|
||||
"User-Agent"=> "Test Agent"
|
||||
)
|
||||
"User-Agent" => "Test Agent",
|
||||
)
|
||||
|
||||
response = HTTP.get(url, query=params, headers=headers)
|
||||
response = HTTP.request("GET", url, headers=headers, query=params, status_exception=false)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -237,15 +226,14 @@ println("Response Body:", String(response.body))
|
||||
});
|
||||
|
||||
test('GET 12', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/humanize/social"
|
||||
|
||||
response = HTTP.request("GET", url, status_exception=false)
|
||||
|
||||
response = HTTP.get(url)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -256,15 +244,14 @@ println("Response Body:", String(response.body))
|
||||
|
||||
group('HEAD Request', () {
|
||||
test('HEAD 1', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev"
|
||||
|
||||
response = HTTP.request("HEAD", url, status_exception=false)
|
||||
|
||||
response = HTTP.head(url)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -273,43 +260,40 @@ println("Response Body:", String(response.body))
|
||||
});
|
||||
|
||||
test('HEAD 2', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev"
|
||||
url = "http://api.apidash.dev"
|
||||
|
||||
response = HTTP.request("HEAD", url, status_exception=false)
|
||||
|
||||
response = HTTP.head(url)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.juliaHttp, requestModelHead2, "https"),
|
||||
codeGen.getCode(CodegenLanguage.juliaHttp, requestModelHead2, "http"),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('POST Request', () {
|
||||
test('POST 1', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r'''using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/case/lower"
|
||||
|
||||
|
||||
payload = Dict(
|
||||
"text"=> "I LOVE Flutter"
|
||||
)
|
||||
payload = """{
|
||||
"text": "I LOVE Flutter"
|
||||
}"""
|
||||
|
||||
headers = Dict(
|
||||
"content-type"=> "text/plain"
|
||||
)
|
||||
"content-type" => "text/plain",
|
||||
)
|
||||
|
||||
response = HTTP.post(url, payload=payload, headers=headers)
|
||||
response = HTTP.request("POST", url, headers=headers, body=payload, status_exception=false)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
""";
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
''';
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.juliaHttp, requestModelPost1, "https"),
|
||||
@ -317,72 +301,234 @@ println("Response Body:", String(response.body))
|
||||
});
|
||||
|
||||
test('POST 2', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r'''using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/case/lower"
|
||||
|
||||
payload = """{
|
||||
"text": "I LOVE Flutter",
|
||||
"flag": null,
|
||||
"male": true,
|
||||
"female": false,
|
||||
"no": 1.2,
|
||||
"arr": ["null", "true", "false", null]
|
||||
}"""
|
||||
|
||||
payload = Dict(
|
||||
"text"=> "I LOVE Flutter",
|
||||
"flag"=> null,
|
||||
"male"=> true,
|
||||
"female"=> false,
|
||||
"no"=> 1.2,
|
||||
"arr"=> ["null", "true", "false", null]
|
||||
headers = Dict(
|
||||
"content-type" => "application/json",
|
||||
)
|
||||
|
||||
response = HTTP.post(url, JSON.json(payload))
|
||||
response = HTTP.request("POST", url, headers=headers, body=payload, status_exception=false)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
""";
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
''';
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.juliaHttp, requestModelPost2, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 3', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r'''using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/case/lower"
|
||||
|
||||
|
||||
payload = Dict(
|
||||
"text"=> "I LOVE Flutter"
|
||||
)
|
||||
payload = """{
|
||||
"text": "I LOVE Flutter"
|
||||
}"""
|
||||
|
||||
headers = Dict(
|
||||
"User-Agent"=> "Test Agent"
|
||||
)
|
||||
"User-Agent" => "Test Agent",
|
||||
"content-type" => "application/json",
|
||||
)
|
||||
|
||||
response = HTTP.post(url, JSON.json(payload), headers=headers)
|
||||
response = HTTP.request("POST", url, headers=headers, body=payload, status_exception=false)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
""";
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
''';
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.juliaHttp, requestModelPost3, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test('POST 4', () {
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/io/form"
|
||||
|
||||
data = Dict(
|
||||
"text" => "API",
|
||||
"sep" => "|",
|
||||
"times" => "3",
|
||||
)
|
||||
|
||||
payload = HTTP.Form(data)
|
||||
|
||||
response = HTTP.request("POST", url, body=payload, status_exception=false)
|
||||
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.juliaHttp, requestModelPost4, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test('POST 5', () {
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/io/form"
|
||||
|
||||
data = Dict(
|
||||
"text" => "API",
|
||||
"sep" => "|",
|
||||
"times" => "3",
|
||||
)
|
||||
|
||||
payload = HTTP.Form(data)
|
||||
|
||||
headers = Dict(
|
||||
"User-Agent" => "Test Agent",
|
||||
)
|
||||
|
||||
response = HTTP.request("POST", url, headers=headers, body=payload, status_exception=false)
|
||||
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.juliaHttp, requestModelPost5, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 6', () {
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/io/img"
|
||||
|
||||
data = Dict(
|
||||
"token" => "xyz",
|
||||
"imfile" => open("/Documents/up/1.png"),
|
||||
)
|
||||
|
||||
payload = HTTP.Form(data)
|
||||
|
||||
response = HTTP.request("POST", url, body=payload, status_exception=false)
|
||||
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.juliaHttp, requestModelPost6, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 7', () {
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/io/img"
|
||||
|
||||
data = Dict(
|
||||
"token" => "xyz",
|
||||
"imfile" => open("/Documents/up/1.png"),
|
||||
)
|
||||
|
||||
payload = HTTP.Form(data)
|
||||
|
||||
response = HTTP.request("POST", url, body=payload, status_exception=false)
|
||||
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.juliaHttp, requestModelPost7, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 8', () {
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/io/form"
|
||||
|
||||
params = Dict(
|
||||
"size" => "2",
|
||||
"len" => "3",
|
||||
)
|
||||
|
||||
data = Dict(
|
||||
"text" => "API",
|
||||
"sep" => "|",
|
||||
"times" => "3",
|
||||
)
|
||||
|
||||
payload = HTTP.Form(data)
|
||||
|
||||
response = HTTP.request("POST", url, body=payload, query=params, status_exception=false)
|
||||
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.juliaHttp, requestModelPost8, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 9', () {
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://api.apidash.dev/io/img"
|
||||
|
||||
params = Dict(
|
||||
"size" => "2",
|
||||
"len" => "3",
|
||||
)
|
||||
|
||||
data = Dict(
|
||||
"token" => "xyz",
|
||||
"imfile" => open("/Documents/up/1.png"),
|
||||
)
|
||||
|
||||
payload = HTTP.Form(data)
|
||||
|
||||
headers = Dict(
|
||||
"User-Agent" => "Test Agent",
|
||||
"Keep-Alive" => "true",
|
||||
)
|
||||
|
||||
response = HTTP.request("POST", url, headers=headers, body=payload, query=params, status_exception=false)
|
||||
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.juliaHttp, requestModelPost9, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
group('PUT Request', () {
|
||||
test('PUT 1', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r'''using HTTP
|
||||
|
||||
url = "https://reqres.in/api/users/2"
|
||||
|
||||
payload = """{
|
||||
"name": "morpheus",
|
||||
"job": "zion resident"
|
||||
}"""
|
||||
|
||||
payload = Dict(
|
||||
"name"=> "morpheus",
|
||||
"job"=> "zion resident"
|
||||
headers = Dict(
|
||||
"content-type" => "application/json",
|
||||
)
|
||||
|
||||
response = HTTP.put(url, JSON.json(payload))
|
||||
response = HTTP.request("PUT", url, headers=headers, body=payload, status_exception=false)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
""";
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
''';
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.juliaHttp, requestModelPut1, "https"),
|
||||
expectedCode);
|
||||
@ -390,21 +536,24 @@ println("Response Body:", String(response.body))
|
||||
});
|
||||
group('PATCH Request', () {
|
||||
test('PATCH 1', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r'''using HTTP
|
||||
|
||||
url = "https://reqres.in/api/users/2"
|
||||
|
||||
payload = """{
|
||||
"name": "marfeus",
|
||||
"job": "accountant"
|
||||
}"""
|
||||
|
||||
payload = Dict(
|
||||
"name"=> "marfeus",
|
||||
"job"=> "accountant"
|
||||
headers = Dict(
|
||||
"content-type" => "application/json",
|
||||
)
|
||||
|
||||
response = HTTP.patch(url, JSON.json(payload))
|
||||
response = HTTP.request("PATCH", url, headers=headers, body=payload, status_exception=false)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
""";
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
''';
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.juliaHttp, requestModelPatch1, "https"),
|
||||
@ -413,15 +562,14 @@ println("Response Body:", String(response.body))
|
||||
});
|
||||
group('DELETE Request', () {
|
||||
test('DELETE 1', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r"""using HTTP
|
||||
|
||||
url = "https://reqres.in/api/users/2"
|
||||
|
||||
response = HTTP.request("DELETE", url, status_exception=false)
|
||||
|
||||
response = HTTP.delete(url)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -429,21 +577,24 @@ println("Response Body:", String(response.body))
|
||||
expectedCode);
|
||||
});
|
||||
test('DELETE 2', () {
|
||||
const expectedCode = r"""using HTTP,JSON
|
||||
const expectedCode = r'''using HTTP
|
||||
|
||||
url = "https://reqres.in/api/users/2"
|
||||
|
||||
payload = """{
|
||||
"name": "marfeus",
|
||||
"job": "accountant"
|
||||
}"""
|
||||
|
||||
payload = Dict(
|
||||
"name"=> "marfeus",
|
||||
"job"=> "accountant"
|
||||
headers = Dict(
|
||||
"content-type" => "application/json",
|
||||
)
|
||||
|
||||
response = HTTP.delete(url, JSON.json(payload))
|
||||
response = HTTP.request("DELETE", url, headers=headers, body=payload, status_exception=false)
|
||||
|
||||
println("Status Code:", response.status)
|
||||
println("Response Body:", String(response.body))
|
||||
""";
|
||||
println("Status Code: $(response.status) $(HTTP.StatusCodes.statustext(response.status))")
|
||||
println("Response Body: \n$(String(response.body))")
|
||||
''';
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.juliaHttp, requestModelDelete2, "https"),
|
||||
|
718
test/codegen/ruby_faraday_codegen_test.dart
Normal file
718
test/codegen/ruby_faraday_codegen_test.dart
Normal file
@ -0,0 +1,718 @@
|
||||
import 'package:apidash/codegen/codegen.dart';
|
||||
import 'package:apidash/consts.dart';
|
||||
import 'package:test/test.dart';
|
||||
import '../request_models.dart';
|
||||
|
||||
void main() {
|
||||
final codeGen = Codegen();
|
||||
|
||||
group('GET Request', () {
|
||||
test('GET 1', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev")
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.get(REQUEST_URL) do |req|
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet1, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 2', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/country/data")
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.get(REQUEST_URL) do |req|
|
||||
req.params = {
|
||||
"code" => "US",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet2, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 3', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/country/data")
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.get(REQUEST_URL) do |req|
|
||||
req.params = {
|
||||
"code" => "IND",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet3, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 4', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/humanize/social")
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.get(REQUEST_URL) do |req|
|
||||
req.params = {
|
||||
"num" => "8700000",
|
||||
"digits" => "3",
|
||||
"system" => "SS",
|
||||
"add_space" => "true",
|
||||
"trailing_zeros" => "true",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet4, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 5', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.github.com/repos/foss42/apidash")
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.get(REQUEST_URL) do |req|
|
||||
req.headers = {
|
||||
"User-Agent" => "Test Agent",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet5, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 6', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.github.com/repos/foss42/apidash")
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.get(REQUEST_URL) do |req|
|
||||
req.headers = {
|
||||
"User-Agent" => "Test Agent",
|
||||
}
|
||||
req.params = {
|
||||
"raw" => "true",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet6, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 7', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev")
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.get(REQUEST_URL) do |req|
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet7, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 8', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.github.com/repos/foss42/apidash")
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.get(REQUEST_URL) do |req|
|
||||
req.headers = {
|
||||
"User-Agent" => "Test Agent",
|
||||
}
|
||||
req.params = {
|
||||
"raw" => "true",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet8, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 9', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/humanize/social")
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.get(REQUEST_URL) do |req|
|
||||
req.params = {
|
||||
"num" => "8700000",
|
||||
"add_space" => "true",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet9, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 10', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/humanize/social")
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.get(REQUEST_URL) do |req|
|
||||
req.headers = {
|
||||
"User-Agent" => "Test Agent",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.rubyFaraday,
|
||||
requestModelGet10,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test('GET 11', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/humanize/social")
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.get(REQUEST_URL) do |req|
|
||||
req.headers = {
|
||||
"User-Agent" => "Test Agent",
|
||||
}
|
||||
req.params = {
|
||||
"num" => "8700000",
|
||||
"digits" => "3",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet11, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 12', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/humanize/social")
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.get(REQUEST_URL) do |req|
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet12, "https"), expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('HEAD Request', () {
|
||||
test('HEAD 1', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev")
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.head(REQUEST_URL) do |req|
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelHead1, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('HEAD 2', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("http://api.apidash.dev")
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.head(REQUEST_URL) do |req|
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelHead2, "http"), expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('POST Request', () {
|
||||
test('POST 1', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/case/lower")
|
||||
|
||||
PAYLOAD = <<HEREDOC
|
||||
{
|
||||
"text": "I LOVE Flutter"
|
||||
}
|
||||
HEREDOC
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.post(REQUEST_URL, PAYLOAD) do |req|
|
||||
req.headers = {
|
||||
"Content-Type" => "text/plain",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost1, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test('POST 2', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/case/lower")
|
||||
|
||||
PAYLOAD = <<HEREDOC
|
||||
{
|
||||
"text": "I LOVE Flutter",
|
||||
"flag": null,
|
||||
"male": true,
|
||||
"female": false,
|
||||
"no": 1.2,
|
||||
"arr": ["null", "true", "false", null]
|
||||
}
|
||||
HEREDOC
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.post(REQUEST_URL, PAYLOAD) do |req|
|
||||
req.headers = {
|
||||
"Content-Type" => "application/json",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost2, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test('POST 3', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/case/lower")
|
||||
|
||||
PAYLOAD = <<HEREDOC
|
||||
{
|
||||
"text": "I LOVE Flutter"
|
||||
}
|
||||
HEREDOC
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.post(REQUEST_URL, PAYLOAD) do |req|
|
||||
req.headers = {
|
||||
"User-Agent" => "Test Agent",
|
||||
"Content-Type" => "application/json",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost3, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test('POST 4', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/io/form")
|
||||
|
||||
PAYLOAD = URI.encode_www_form({
|
||||
"text" => "API",
|
||||
"sep" => "|",
|
||||
"times" => "3",
|
||||
})
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.post(REQUEST_URL, PAYLOAD) do |req|
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.rubyFaraday,
|
||||
requestModelPost4,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 5', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/io/form")
|
||||
|
||||
PAYLOAD = URI.encode_www_form({
|
||||
"text" => "API",
|
||||
"sep" => "|",
|
||||
"times" => "3",
|
||||
})
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.post(REQUEST_URL, PAYLOAD) do |req|
|
||||
req.headers = {
|
||||
"User-Agent" => "Test Agent",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost5, "https"), expectedCode);
|
||||
});
|
||||
test('POST 6', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
require 'faraday/multipart'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/io/img")
|
||||
|
||||
PAYLOAD = {
|
||||
"token" => Faraday::Multipart::ParamPart.new("xyz", "text/plain"),
|
||||
"imfile" => Faraday::Multipart::FilePart.new("/Documents/up/1.png", "application/octet-stream"),
|
||||
}
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
faraday.request :multipart
|
||||
end
|
||||
|
||||
response = conn.post(REQUEST_URL, PAYLOAD) do |req|
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost6, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 7', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
require 'faraday/multipart'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/io/img")
|
||||
|
||||
PAYLOAD = {
|
||||
"token" => Faraday::Multipart::ParamPart.new("xyz", "text/plain"),
|
||||
"imfile" => Faraday::Multipart::FilePart.new("/Documents/up/1.png", "application/octet-stream"),
|
||||
}
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
faraday.request :multipart
|
||||
end
|
||||
|
||||
response = conn.post(REQUEST_URL, PAYLOAD) do |req|
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost7, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 8', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/io/form")
|
||||
|
||||
PAYLOAD = URI.encode_www_form({
|
||||
"text" => "API",
|
||||
"sep" => "|",
|
||||
"times" => "3",
|
||||
})
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.post(REQUEST_URL, PAYLOAD) do |req|
|
||||
req.params = {
|
||||
"size" => "2",
|
||||
"len" => "3",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost8, "https"), expectedCode);
|
||||
});
|
||||
test('POST 9', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
require 'faraday/multipart'
|
||||
|
||||
REQUEST_URL = URI("https://api.apidash.dev/io/img")
|
||||
|
||||
PAYLOAD = {
|
||||
"token" => Faraday::Multipart::ParamPart.new("xyz", "text/plain"),
|
||||
"imfile" => Faraday::Multipart::FilePart.new("/Documents/up/1.png", "application/octet-stream"),
|
||||
}
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
faraday.request :multipart
|
||||
end
|
||||
|
||||
response = conn.post(REQUEST_URL, PAYLOAD) do |req|
|
||||
req.headers = {
|
||||
"User-Agent" => "Test Agent",
|
||||
"Keep-Alive" => "true",
|
||||
}
|
||||
req.params = {
|
||||
"size" => "2",
|
||||
"len" => "3",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost9, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('PUT Request', () {
|
||||
test('PUT 1', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://reqres.in/api/users/2")
|
||||
|
||||
PAYLOAD = <<HEREDOC
|
||||
{
|
||||
"name": "morpheus",
|
||||
"job": "zion resident"
|
||||
}
|
||||
HEREDOC
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.put(REQUEST_URL, PAYLOAD) do |req|
|
||||
req.headers = {
|
||||
"Content-Type" => "application/json",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPut1, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('PATCH Request', () {
|
||||
test('PATCH 1', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://reqres.in/api/users/2")
|
||||
|
||||
PAYLOAD = <<HEREDOC
|
||||
{
|
||||
"name": "marfeus",
|
||||
"job": "accountant"
|
||||
}
|
||||
HEREDOC
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.patch(REQUEST_URL, PAYLOAD) do |req|
|
||||
req.headers = {
|
||||
"Content-Type" => "application/json",
|
||||
}
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPatch1, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('DELETE Request', () {
|
||||
test('DELETE 1', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://reqres.in/api/users/2")
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.delete(REQUEST_URL) do |req|
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelDelete1, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('DELETE 2', () {
|
||||
const expectedCode = r"""require 'uri'
|
||||
require 'faraday'
|
||||
|
||||
REQUEST_URL = URI("https://reqres.in/api/users/2")
|
||||
|
||||
PAYLOAD = <<HEREDOC
|
||||
{
|
||||
"name": "marfeus",
|
||||
"job": "accountant"
|
||||
}
|
||||
HEREDOC
|
||||
|
||||
conn = Faraday.new do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.delete(REQUEST_URL) do |req|
|
||||
req.headers = {
|
||||
"Content-Type" => "application/json",
|
||||
}
|
||||
req.body = PAYLOAD
|
||||
end
|
||||
|
||||
puts "Status Code: #{response.status}"
|
||||
puts "Response Body: #{response.body}"
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelDelete2, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user