mirror of
https://github.com/foss42/apidash.git
synced 2025-06-05 01:46:21 +08:00
Migrate request models to a single file to test across codegens
This commit is contained in:
@ -1,18 +1,12 @@
|
||||
import 'package:apidash/codegen/kotlin/pkg_okhttp.dart';
|
||||
import 'package:apidash/models/models.dart' show KVRow, RequestModel;
|
||||
import 'package:test/test.dart';
|
||||
import 'package:apidash/consts.dart';
|
||||
import 'request_models.dart';
|
||||
|
||||
void main() {
|
||||
group('KotlinOkHttpCodeGen', () {
|
||||
final kotlinOkHttpCodeGen = KotlinOkHttpCodeGen();
|
||||
|
||||
test('getCode returns valid code for GET request', () {
|
||||
const requestModel = RequestModel(
|
||||
url: 'https://api.foss42.com',
|
||||
method: HTTPVerb.get,
|
||||
id: '',
|
||||
);
|
||||
const expectedCode = r"""import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.OkHttpClient
|
||||
@ -30,17 +24,10 @@ val response = client.newCall(request).execute()
|
||||
|
||||
println(response.body!!.string())
|
||||
""";
|
||||
expect(kotlinOkHttpCodeGen.getCode(requestModel), expectedCode);
|
||||
expect(kotlinOkHttpCodeGen.getCode(requestModelGet1), expectedCode);
|
||||
});
|
||||
|
||||
test('getCode returns valid code for POST request', () {
|
||||
const requestModel = RequestModel(
|
||||
url: 'https://api.foss42.com/case/lower',
|
||||
method: HTTPVerb.post,
|
||||
requestBody: '{"text": "IS Upper"}',
|
||||
requestBodyContentType: ContentType.json,
|
||||
id: '1',
|
||||
);
|
||||
const expectedCode = r"""import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.OkHttpClient
|
||||
@ -61,17 +48,10 @@ val response = client.newCall(request).execute()
|
||||
|
||||
println(response.body!!.string())
|
||||
""";
|
||||
expect(kotlinOkHttpCodeGen.getCode(requestModel), expectedCode);
|
||||
expect(kotlinOkHttpCodeGen.getCode(requestModelPost1), expectedCode);
|
||||
});
|
||||
|
||||
test('getCode returns valid code for DELETE request', () {
|
||||
const requestModel = RequestModel(
|
||||
url: 'https://jsonplaceholder.typicode.com/posts/1',
|
||||
method: HTTPVerb.delete,
|
||||
requestBody: '{"title": "foo","body": "bar","userId": 1}',
|
||||
requestBodyContentType: ContentType.json,
|
||||
id: '1',
|
||||
);
|
||||
const expectedCode = r"""import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.OkHttpClient
|
||||
@ -92,15 +72,10 @@ val response = client.newCall(request).execute()
|
||||
|
||||
println(response.body!!.string())
|
||||
""";
|
||||
expect(kotlinOkHttpCodeGen.getCode(requestModel), expectedCode);
|
||||
expect(kotlinOkHttpCodeGen.getCode(requestModelDelete1), expectedCode);
|
||||
});
|
||||
|
||||
test('getCode returns valid code for HEAD request', () {
|
||||
const requestModel = RequestModel(
|
||||
url: 'https://jsonplaceholder.typicode.com/posts/1',
|
||||
method: HTTPVerb.head,
|
||||
id: '1',
|
||||
);
|
||||
const expectedCode = """import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.OkHttpClient
|
||||
@ -119,24 +94,12 @@ val response = client.newCall(request).execute()
|
||||
|
||||
println(response.body!!.string())
|
||||
""";
|
||||
expect(kotlinOkHttpCodeGen.getCode(requestModel), expectedCode);
|
||||
expect(kotlinOkHttpCodeGen.getCode(requestModelHead1), expectedCode);
|
||||
});
|
||||
|
||||
test(
|
||||
'getCode returns valid code for requests with headers and query parameters',
|
||||
() {
|
||||
const requestModel = RequestModel(
|
||||
url: 'https://jsonplaceholder.typicode.com/posts',
|
||||
method: HTTPVerb.get,
|
||||
requestParams: [
|
||||
KVRow('userId', 1),
|
||||
],
|
||||
requestHeaders: [
|
||||
KVRow('Custom-Header-1', 'Value-1'),
|
||||
KVRow('Custom-Header-2', 'Value-2')
|
||||
],
|
||||
id: '1',
|
||||
);
|
||||
const expectedCode = """import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.OkHttpClient
|
||||
@ -157,7 +120,7 @@ val response = client.newCall(request).execute()
|
||||
|
||||
println(response.body!!.string())
|
||||
""";
|
||||
expect(kotlinOkHttpCodeGen.getCode(requestModel), expectedCode);
|
||||
expect(kotlinOkHttpCodeGen.getCode(requestModelGet2), expectedCode);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user