mirror of
https://github.com/foss42/apidash.git
synced 2025-06-17 11:54:51 +08:00
Merge pull request #302 from PCoder23/kotlin-okhttp-tests
added test cases for kotli okhttp3
This commit is contained in:
@ -7,7 +7,7 @@ import 'package:apidash/consts.dart';
|
|||||||
|
|
||||||
class KotlinOkHttpCodeGen {
|
class KotlinOkHttpCodeGen {
|
||||||
final String kTemplateStart = """import okhttp3.OkHttpClient
|
final String kTemplateStart = """import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request{{importForQuery}}{{importForBody}}{{importForFormData}}
|
import okhttp3.Request{{importForQuery}}{{importForBody}}{{importForFormData}}{{importForFile}}
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
val client = OkHttpClient()
|
val client = OkHttpClient()
|
||||||
@ -27,6 +27,12 @@ import okhttp3.MediaType.Companion.toMediaType""";
|
|||||||
|
|
||||||
import okhttp3.MultipartBody""";
|
import okhttp3.MultipartBody""";
|
||||||
|
|
||||||
|
final String kStringImportForFile = """
|
||||||
|
|
||||||
|
import java.io.File
|
||||||
|
import okhttp3.RequestBody.Companion.asRequestBody
|
||||||
|
import okhttp3.MediaType.Companion.toMediaType""";
|
||||||
|
|
||||||
final String kTemplateUrl = '''
|
final String kTemplateUrl = '''
|
||||||
|
|
||||||
val url = "{{url}}"
|
val url = "{{url}}"
|
||||||
@ -68,7 +74,7 @@ import okhttp3.MultipartBody""";
|
|||||||
// Converting list of form data objects to kolin multi part data
|
// Converting list of form data objects to kolin multi part data
|
||||||
String kFormDataBody = '''
|
String kFormDataBody = '''
|
||||||
val body = MultipartBody.Builder().setType(MultipartBody.FORM){% for item in formDataList %}{% if item.type == 'file' %}
|
val body = MultipartBody.Builder().setType(MultipartBody.FORM){% for item in formDataList %}{% if item.type == 'file' %}
|
||||||
.addFormDataPart("{{item.name}}",null,File("{{item.value}}").asRequestBody("application/octet-stream".toMediaType()))
|
.addFormDataPart("{{item.name}}",File("{{item.value}}").name,File("{{item.value}}").asRequestBody("application/octet-stream".toMediaType()))
|
||||||
{% else %}.addFormDataPart("{{item.name}}","{{item.value}}")
|
{% else %}.addFormDataPart("{{item.name}}","{{item.value}}")
|
||||||
{% endif %}{% endfor %}.build()
|
{% endif %}{% endfor %}.build()
|
||||||
''';
|
''';
|
||||||
@ -81,6 +87,7 @@ import okhttp3.MultipartBody""";
|
|||||||
bool hasQuery = false;
|
bool hasQuery = false;
|
||||||
bool hasBody = false;
|
bool hasBody = false;
|
||||||
bool hasFormData = false;
|
bool hasFormData = false;
|
||||||
|
bool hasFile = false;
|
||||||
|
|
||||||
var rec = getValidRequestUri(
|
var rec = getValidRequestUri(
|
||||||
requestModel.url,
|
requestModel.url,
|
||||||
@ -111,8 +118,34 @@ import okhttp3.MultipartBody""";
|
|||||||
hasFormData = true;
|
hasFormData = true;
|
||||||
var formDataTemplate = jj.Template(kFormDataBody);
|
var formDataTemplate = jj.Template(kFormDataBody);
|
||||||
|
|
||||||
|
List<Map<String,String>> modifiedFormDataList = [];
|
||||||
|
for (var item in requestModel.formDataList) {
|
||||||
|
if (item.type == FormDataType.file ) {
|
||||||
|
if (item.value[0] == "/") {
|
||||||
|
modifiedFormDataList.add({
|
||||||
|
"name": item.name,
|
||||||
|
"value": item.value.substring(1),
|
||||||
|
"type": "file"
|
||||||
|
});
|
||||||
|
}else{
|
||||||
|
modifiedFormDataList.add({
|
||||||
|
"name": item.name,
|
||||||
|
"value": item.value,
|
||||||
|
"type": "file"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
hasFile = true;
|
||||||
|
}else{
|
||||||
|
modifiedFormDataList.add({
|
||||||
|
"name": item.name,
|
||||||
|
"value": item.value,
|
||||||
|
"type": "text"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
result += formDataTemplate.render({
|
result += formDataTemplate.render({
|
||||||
"formDataList": requestModel.formDataMapList,
|
"formDataList": modifiedFormDataList,
|
||||||
});
|
});
|
||||||
} else if (kMethodsWithBody.contains(method) && requestBody != null) {
|
} else if (kMethodsWithBody.contains(method) && requestBody != null) {
|
||||||
var contentLength = utf8.encode(requestBody).length;
|
var contentLength = utf8.encode(requestBody).length;
|
||||||
@ -129,7 +162,8 @@ import okhttp3.MultipartBody""";
|
|||||||
var stringStart = templateStart.render({
|
var stringStart = templateStart.render({
|
||||||
"importForQuery": hasQuery ? kStringImportForQuery : "",
|
"importForQuery": hasQuery ? kStringImportForQuery : "",
|
||||||
"importForBody": hasBody ? kStringImportForBody : "",
|
"importForBody": hasBody ? kStringImportForBody : "",
|
||||||
"importForFormData": hasFormData ? kStringImportForFormData : ""
|
"importForFormData": hasFormData ? kStringImportForFormData : "",
|
||||||
|
"importForFile": hasFile ? kStringImportForFile : "",
|
||||||
});
|
});
|
||||||
|
|
||||||
result = stringStart + result;
|
result = stringStart + result;
|
||||||
|
@ -517,6 +517,35 @@ fun main() {
|
|||||||
CodegenLanguage.kotlinOkHttp, requestModelPost3, "https"),
|
CodegenLanguage.kotlinOkHttp, requestModelPost3, "https"),
|
||||||
expectedCode);
|
expectedCode);
|
||||||
});
|
});
|
||||||
|
test('POST 4', () {
|
||||||
|
const expectedCode = r'''import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.Request
|
||||||
|
import okhttp3.MultipartBody
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val client = OkHttpClient()
|
||||||
|
|
||||||
|
val url = "https://api.apidash.dev/io/form"
|
||||||
|
val body = MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("text","API")
|
||||||
|
.addFormDataPart("sep","|")
|
||||||
|
.addFormDataPart("times","3")
|
||||||
|
.build()
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.post(body)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
val response = client.newCall(request).execute()
|
||||||
|
|
||||||
|
println(response.code)
|
||||||
|
println(response.body?.string())
|
||||||
|
}
|
||||||
|
''';
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.kotlinOkHttp, requestModelPost4, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
test('POST 5', () {
|
test('POST 5', () {
|
||||||
const expectedCode = r'''import okhttp3.OkHttpClient
|
const expectedCode = r'''import okhttp3.OkHttpClient
|
||||||
@ -548,6 +577,141 @@ fun main() {
|
|||||||
CodegenLanguage.kotlinOkHttp, requestModelPost5, "https"),
|
CodegenLanguage.kotlinOkHttp, requestModelPost5, "https"),
|
||||||
expectedCode);
|
expectedCode);
|
||||||
});
|
});
|
||||||
|
test('POST 6', () {
|
||||||
|
const expectedCode = r'''import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.Request
|
||||||
|
import okhttp3.MultipartBody
|
||||||
|
import java.io.File
|
||||||
|
import okhttp3.RequestBody.Companion.asRequestBody
|
||||||
|
import okhttp3.MediaType.Companion.toMediaType
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val client = OkHttpClient()
|
||||||
|
|
||||||
|
val url = "https://api.apidash.dev/io/img"
|
||||||
|
val body = MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("token","xyz")
|
||||||
|
|
||||||
|
.addFormDataPart("imfile",File("Documents/up/1.png").name,File("Documents/up/1.png").asRequestBody("application/octet-stream".toMediaType()))
|
||||||
|
.build()
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.post(body)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
val response = client.newCall(request).execute()
|
||||||
|
|
||||||
|
println(response.code)
|
||||||
|
println(response.body?.string())
|
||||||
|
}
|
||||||
|
''';
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.kotlinOkHttp, requestModelPost6, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
test('POST 7', () {
|
||||||
|
const expectedCode = r'''import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.Request
|
||||||
|
import okhttp3.MultipartBody
|
||||||
|
import java.io.File
|
||||||
|
import okhttp3.RequestBody.Companion.asRequestBody
|
||||||
|
import okhttp3.MediaType.Companion.toMediaType
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val client = OkHttpClient()
|
||||||
|
|
||||||
|
val url = "https://api.apidash.dev/io/img"
|
||||||
|
val body = MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("token","xyz")
|
||||||
|
|
||||||
|
.addFormDataPart("imfile",File("Documents/up/1.png").name,File("Documents/up/1.png").asRequestBody("application/octet-stream".toMediaType()))
|
||||||
|
.build()
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.post(body)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
val response = client.newCall(request).execute()
|
||||||
|
|
||||||
|
println(response.code)
|
||||||
|
println(response.body?.string())
|
||||||
|
}
|
||||||
|
''';
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.kotlinOkHttp, requestModelPost7, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
test('POST 8', () {
|
||||||
|
const expectedCode = r'''import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.Request
|
||||||
|
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||||
|
import okhttp3.MultipartBody
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val client = OkHttpClient()
|
||||||
|
|
||||||
|
val url = "https://api.apidash.dev/io/form".toHttpUrl().newBuilder()
|
||||||
|
.addQueryParameter("size", "2")
|
||||||
|
.addQueryParameter("len", "3")
|
||||||
|
.build()
|
||||||
|
val body = MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("text","API")
|
||||||
|
.addFormDataPart("sep","|")
|
||||||
|
.addFormDataPart("times","3")
|
||||||
|
.build()
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.post(body)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
val response = client.newCall(request).execute()
|
||||||
|
|
||||||
|
println(response.code)
|
||||||
|
println(response.body?.string())
|
||||||
|
}
|
||||||
|
''';
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.kotlinOkHttp, requestModelPost8, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
test('POST 9', () {
|
||||||
|
const expectedCode = r'''import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.Request
|
||||||
|
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||||
|
import okhttp3.MultipartBody
|
||||||
|
import java.io.File
|
||||||
|
import okhttp3.RequestBody.Companion.asRequestBody
|
||||||
|
import okhttp3.MediaType.Companion.toMediaType
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val client = OkHttpClient()
|
||||||
|
|
||||||
|
val url = "https://api.apidash.dev/io/img".toHttpUrl().newBuilder()
|
||||||
|
.addQueryParameter("size", "2")
|
||||||
|
.addQueryParameter("len", "3")
|
||||||
|
.build()
|
||||||
|
val body = MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("token","xyz")
|
||||||
|
|
||||||
|
.addFormDataPart("imfile",File("Documents/up/1.png").name,File("Documents/up/1.png").asRequestBody("application/octet-stream".toMediaType()))
|
||||||
|
.build()
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.addHeader("User-Agent", "Test Agent")
|
||||||
|
.addHeader("Keep-Alive", "true")
|
||||||
|
.post(body)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
val response = client.newCall(request).execute()
|
||||||
|
|
||||||
|
println(response.code)
|
||||||
|
println(response.body?.string())
|
||||||
|
}
|
||||||
|
''';
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.kotlinOkHttp, requestModelPost9, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
group('PUT Request', () {
|
group('PUT Request', () {
|
||||||
|
Reference in New Issue
Block a user