mirror of
https://github.com/foss42/apidash.git
synced 2025-06-27 11:02:33 +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/codegen/kotlin/pkg_okhttp.dart';
|
||||||
import 'package:apidash/models/models.dart' show KVRow, RequestModel;
|
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
import 'package:apidash/consts.dart';
|
import 'request_models.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group('KotlinOkHttpCodeGen', () {
|
group('KotlinOkHttpCodeGen', () {
|
||||||
final kotlinOkHttpCodeGen = KotlinOkHttpCodeGen();
|
final kotlinOkHttpCodeGen = KotlinOkHttpCodeGen();
|
||||||
|
|
||||||
test('getCode returns valid code for GET request', () {
|
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
|
const expectedCode = r"""import okhttp3.MediaType.Companion.toMediaType
|
||||||
import okhttp3.MultipartBody
|
import okhttp3.MultipartBody
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
@ -30,17 +24,10 @@ val response = client.newCall(request).execute()
|
|||||||
|
|
||||||
println(response.body!!.string())
|
println(response.body!!.string())
|
||||||
""";
|
""";
|
||||||
expect(kotlinOkHttpCodeGen.getCode(requestModel), expectedCode);
|
expect(kotlinOkHttpCodeGen.getCode(requestModelGet1), expectedCode);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('getCode returns valid code for POST request', () {
|
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
|
const expectedCode = r"""import okhttp3.MediaType.Companion.toMediaType
|
||||||
import okhttp3.MultipartBody
|
import okhttp3.MultipartBody
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
@ -61,17 +48,10 @@ val response = client.newCall(request).execute()
|
|||||||
|
|
||||||
println(response.body!!.string())
|
println(response.body!!.string())
|
||||||
""";
|
""";
|
||||||
expect(kotlinOkHttpCodeGen.getCode(requestModel), expectedCode);
|
expect(kotlinOkHttpCodeGen.getCode(requestModelPost1), expectedCode);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('getCode returns valid code for DELETE request', () {
|
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
|
const expectedCode = r"""import okhttp3.MediaType.Companion.toMediaType
|
||||||
import okhttp3.MultipartBody
|
import okhttp3.MultipartBody
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
@ -92,15 +72,10 @@ val response = client.newCall(request).execute()
|
|||||||
|
|
||||||
println(response.body!!.string())
|
println(response.body!!.string())
|
||||||
""";
|
""";
|
||||||
expect(kotlinOkHttpCodeGen.getCode(requestModel), expectedCode);
|
expect(kotlinOkHttpCodeGen.getCode(requestModelDelete1), expectedCode);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('getCode returns valid code for HEAD request', () {
|
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
|
const expectedCode = """import okhttp3.MediaType.Companion.toMediaType
|
||||||
import okhttp3.MultipartBody
|
import okhttp3.MultipartBody
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
@ -119,24 +94,12 @@ val response = client.newCall(request).execute()
|
|||||||
|
|
||||||
println(response.body!!.string())
|
println(response.body!!.string())
|
||||||
""";
|
""";
|
||||||
expect(kotlinOkHttpCodeGen.getCode(requestModel), expectedCode);
|
expect(kotlinOkHttpCodeGen.getCode(requestModelHead1), expectedCode);
|
||||||
});
|
});
|
||||||
|
|
||||||
test(
|
test(
|
||||||
'getCode returns valid code for requests with headers and query parameters',
|
'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
|
const expectedCode = """import okhttp3.MediaType.Companion.toMediaType
|
||||||
import okhttp3.MultipartBody
|
import okhttp3.MultipartBody
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
@ -157,7 +120,7 @@ val response = client.newCall(request).execute()
|
|||||||
|
|
||||||
println(response.body!!.string())
|
println(response.body!!.string())
|
||||||
""";
|
""";
|
||||||
expect(kotlinOkHttpCodeGen.getCode(requestModel), expectedCode);
|
expect(kotlinOkHttpCodeGen.getCode(requestModelGet2), expectedCode);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
48
test/codegen/request_models.dart
Normal file
48
test/codegen/request_models.dart
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
import 'package:apidash/models/models.dart' show KVRow, RequestModel;
|
||||||
|
import 'package:apidash/consts.dart';
|
||||||
|
|
||||||
|
/// Basic GET request model
|
||||||
|
const requestModelGet1 = RequestModel(
|
||||||
|
url: 'https://api.foss42.com',
|
||||||
|
method: HTTPVerb.get,
|
||||||
|
id: '',
|
||||||
|
);
|
||||||
|
|
||||||
|
/// GET request model with headers and query params
|
||||||
|
const requestModelGet2 = 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',
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Basic HEAD request model
|
||||||
|
const requestModelHead1 = RequestModel(
|
||||||
|
url: 'https://jsonplaceholder.typicode.com/posts/1',
|
||||||
|
method: HTTPVerb.head,
|
||||||
|
id: '1',
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Basic POST request model
|
||||||
|
const requestModelPost1 = RequestModel(
|
||||||
|
url: 'https://api.foss42.com/case/lower',
|
||||||
|
method: HTTPVerb.post,
|
||||||
|
requestBody: '{"text": "IS Upper"}',
|
||||||
|
requestBodyContentType: ContentType.json,
|
||||||
|
id: '1',
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Basic DELETE request model
|
||||||
|
const requestModelDelete1 = RequestModel(
|
||||||
|
url: 'https://jsonplaceholder.typicode.com/posts/1',
|
||||||
|
method: HTTPVerb.delete,
|
||||||
|
requestBody: '{"title": "foo","body": "bar","userId": 1}',
|
||||||
|
requestBodyContentType: ContentType.json,
|
||||||
|
id: '1',
|
||||||
|
);
|
Reference in New Issue
Block a user