mirror of
https://github.com/foss42/apidash.git
synced 2025-06-01 06:42:07 +08:00
Add HttpRequestModel
This commit is contained in:
68
lib/models/http_request_model.dart
Normal file
68
lib/models/http_request_model.dart
Normal file
@ -0,0 +1,68 @@
|
||||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import '../utils/utils.dart'
|
||||
show rowsToFormDataMapList, rowsToMap, getEnabledRows;
|
||||
import '../consts.dart';
|
||||
import 'models.dart';
|
||||
|
||||
part 'http_request_model.freezed.dart';
|
||||
|
||||
part 'http_request_model.g.dart';
|
||||
|
||||
@freezed
|
||||
class HttpRequestModel with _$HttpRequestModel {
|
||||
const HttpRequestModel._();
|
||||
|
||||
@JsonSerializable(explicitToJson: true)
|
||||
const factory HttpRequestModel({
|
||||
@Default(HTTPVerb.get) HTTPVerb method,
|
||||
@Default("") String url,
|
||||
List<NameValueModel>? headers,
|
||||
List<NameValueModel>? params,
|
||||
List<bool>? isHeaderEnabledList,
|
||||
List<bool>? isParamEnabledList,
|
||||
@Default(ContentType.json) ContentType bodyContentType,
|
||||
String? body,
|
||||
List<FormDataModel>? formData,
|
||||
}) = _HttpRequestModel;
|
||||
|
||||
factory HttpRequestModel.fromJson(Map<String, Object?> json) =>
|
||||
_$HttpRequestModelFromJson(json);
|
||||
|
||||
Map<String, String> get headersMap => rowsToMap(headers) ?? {};
|
||||
Map<String, String> get paramsMap => rowsToMap(params) ?? {};
|
||||
List<NameValueModel>? get enabledHeaders =>
|
||||
getEnabledRows(headers, isHeaderEnabledList);
|
||||
List<NameValueModel>? get enabledParams =>
|
||||
getEnabledRows(params, isParamEnabledList);
|
||||
|
||||
Map<String, String> get enabledHeadersMap => rowsToMap(enabledHeaders) ?? {};
|
||||
Map<String, String> get enabledParamsMap => rowsToMap(enabledParams) ?? {};
|
||||
|
||||
bool get hasContentTypeHeader => enabledHeadersMap.keys
|
||||
.any((k) => k.toLowerCase() == HttpHeaders.contentTypeHeader);
|
||||
bool get hasFormDataContentType => bodyContentType == ContentType.formdata;
|
||||
bool get hasJsonContentType => bodyContentType == ContentType.json;
|
||||
bool get hasTextContentType => bodyContentType == ContentType.text;
|
||||
int get contentLength => utf8.encode(body ?? "").length;
|
||||
bool get hasBody => hasJsonData || hasTextData || hasFormData;
|
||||
bool get hasJsonData =>
|
||||
kMethodsWithBody.contains(method) &&
|
||||
hasJsonContentType &&
|
||||
contentLength > 0;
|
||||
bool get hasTextData =>
|
||||
kMethodsWithBody.contains(method) &&
|
||||
hasTextContentType &&
|
||||
contentLength > 0;
|
||||
bool get hasFormData =>
|
||||
kMethodsWithBody.contains(method) &&
|
||||
hasFormDataContentType &&
|
||||
formDataMapList.isNotEmpty;
|
||||
List<FormDataModel> get formDataList => formData ?? <FormDataModel>[];
|
||||
List<Map<String, String>> get formDataMapList =>
|
||||
rowsToFormDataMapList(formDataList) ?? [];
|
||||
bool get hasFileInFormData => formDataList
|
||||
.map((e) => e.type == FormDataType.file)
|
||||
.any((element) => element);
|
||||
}
|
381
lib/models/http_request_model.freezed.dart
Normal file
381
lib/models/http_request_model.freezed.dart
Normal file
@ -0,0 +1,381 @@
|
||||
// coverage:ignore-file
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
|
||||
|
||||
part of 'http_request_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// FreezedGenerator
|
||||
// **************************************************************************
|
||||
|
||||
T _$identity<T>(T value) => value;
|
||||
|
||||
final _privateConstructorUsedError = UnsupportedError(
|
||||
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');
|
||||
|
||||
HttpRequestModel _$HttpRequestModelFromJson(Map<String, dynamic> json) {
|
||||
return _HttpRequestModel.fromJson(json);
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
mixin _$HttpRequestModel {
|
||||
HTTPVerb get method => throw _privateConstructorUsedError;
|
||||
String get url => throw _privateConstructorUsedError;
|
||||
List<NameValueModel>? get headers => throw _privateConstructorUsedError;
|
||||
List<NameValueModel>? get params => throw _privateConstructorUsedError;
|
||||
List<bool>? get isHeaderEnabledList => throw _privateConstructorUsedError;
|
||||
List<bool>? get isParamEnabledList => throw _privateConstructorUsedError;
|
||||
ContentType get bodyContentType => throw _privateConstructorUsedError;
|
||||
String? get body => throw _privateConstructorUsedError;
|
||||
List<FormDataModel>? get formData => throw _privateConstructorUsedError;
|
||||
|
||||
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
|
||||
@JsonKey(ignore: true)
|
||||
$HttpRequestModelCopyWith<HttpRequestModel> get copyWith =>
|
||||
throw _privateConstructorUsedError;
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract class $HttpRequestModelCopyWith<$Res> {
|
||||
factory $HttpRequestModelCopyWith(
|
||||
HttpRequestModel value, $Res Function(HttpRequestModel) then) =
|
||||
_$HttpRequestModelCopyWithImpl<$Res, HttpRequestModel>;
|
||||
@useResult
|
||||
$Res call(
|
||||
{HTTPVerb method,
|
||||
String url,
|
||||
List<NameValueModel>? headers,
|
||||
List<NameValueModel>? params,
|
||||
List<bool>? isHeaderEnabledList,
|
||||
List<bool>? isParamEnabledList,
|
||||
ContentType bodyContentType,
|
||||
String? body,
|
||||
List<FormDataModel>? formData});
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
class _$HttpRequestModelCopyWithImpl<$Res, $Val extends HttpRequestModel>
|
||||
implements $HttpRequestModelCopyWith<$Res> {
|
||||
_$HttpRequestModelCopyWithImpl(this._value, this._then);
|
||||
|
||||
// ignore: unused_field
|
||||
final $Val _value;
|
||||
// ignore: unused_field
|
||||
final $Res Function($Val) _then;
|
||||
|
||||
@pragma('vm:prefer-inline')
|
||||
@override
|
||||
$Res call({
|
||||
Object? method = null,
|
||||
Object? url = null,
|
||||
Object? headers = freezed,
|
||||
Object? params = freezed,
|
||||
Object? isHeaderEnabledList = freezed,
|
||||
Object? isParamEnabledList = freezed,
|
||||
Object? bodyContentType = null,
|
||||
Object? body = freezed,
|
||||
Object? formData = freezed,
|
||||
}) {
|
||||
return _then(_value.copyWith(
|
||||
method: null == method
|
||||
? _value.method
|
||||
: method // ignore: cast_nullable_to_non_nullable
|
||||
as HTTPVerb,
|
||||
url: null == url
|
||||
? _value.url
|
||||
: url // ignore: cast_nullable_to_non_nullable
|
||||
as String,
|
||||
headers: freezed == headers
|
||||
? _value.headers
|
||||
: headers // ignore: cast_nullable_to_non_nullable
|
||||
as List<NameValueModel>?,
|
||||
params: freezed == params
|
||||
? _value.params
|
||||
: params // ignore: cast_nullable_to_non_nullable
|
||||
as List<NameValueModel>?,
|
||||
isHeaderEnabledList: freezed == isHeaderEnabledList
|
||||
? _value.isHeaderEnabledList
|
||||
: isHeaderEnabledList // ignore: cast_nullable_to_non_nullable
|
||||
as List<bool>?,
|
||||
isParamEnabledList: freezed == isParamEnabledList
|
||||
? _value.isParamEnabledList
|
||||
: isParamEnabledList // ignore: cast_nullable_to_non_nullable
|
||||
as List<bool>?,
|
||||
bodyContentType: null == bodyContentType
|
||||
? _value.bodyContentType
|
||||
: bodyContentType // ignore: cast_nullable_to_non_nullable
|
||||
as ContentType,
|
||||
body: freezed == body
|
||||
? _value.body
|
||||
: body // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
formData: freezed == formData
|
||||
? _value.formData
|
||||
: formData // ignore: cast_nullable_to_non_nullable
|
||||
as List<FormDataModel>?,
|
||||
) as $Val);
|
||||
}
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract class _$$HttpRequestModelImplCopyWith<$Res>
|
||||
implements $HttpRequestModelCopyWith<$Res> {
|
||||
factory _$$HttpRequestModelImplCopyWith(_$HttpRequestModelImpl value,
|
||||
$Res Function(_$HttpRequestModelImpl) then) =
|
||||
__$$HttpRequestModelImplCopyWithImpl<$Res>;
|
||||
@override
|
||||
@useResult
|
||||
$Res call(
|
||||
{HTTPVerb method,
|
||||
String url,
|
||||
List<NameValueModel>? headers,
|
||||
List<NameValueModel>? params,
|
||||
List<bool>? isHeaderEnabledList,
|
||||
List<bool>? isParamEnabledList,
|
||||
ContentType bodyContentType,
|
||||
String? body,
|
||||
List<FormDataModel>? formData});
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
class __$$HttpRequestModelImplCopyWithImpl<$Res>
|
||||
extends _$HttpRequestModelCopyWithImpl<$Res, _$HttpRequestModelImpl>
|
||||
implements _$$HttpRequestModelImplCopyWith<$Res> {
|
||||
__$$HttpRequestModelImplCopyWithImpl(_$HttpRequestModelImpl _value,
|
||||
$Res Function(_$HttpRequestModelImpl) _then)
|
||||
: super(_value, _then);
|
||||
|
||||
@pragma('vm:prefer-inline')
|
||||
@override
|
||||
$Res call({
|
||||
Object? method = null,
|
||||
Object? url = null,
|
||||
Object? headers = freezed,
|
||||
Object? params = freezed,
|
||||
Object? isHeaderEnabledList = freezed,
|
||||
Object? isParamEnabledList = freezed,
|
||||
Object? bodyContentType = null,
|
||||
Object? body = freezed,
|
||||
Object? formData = freezed,
|
||||
}) {
|
||||
return _then(_$HttpRequestModelImpl(
|
||||
method: null == method
|
||||
? _value.method
|
||||
: method // ignore: cast_nullable_to_non_nullable
|
||||
as HTTPVerb,
|
||||
url: null == url
|
||||
? _value.url
|
||||
: url // ignore: cast_nullable_to_non_nullable
|
||||
as String,
|
||||
headers: freezed == headers
|
||||
? _value._headers
|
||||
: headers // ignore: cast_nullable_to_non_nullable
|
||||
as List<NameValueModel>?,
|
||||
params: freezed == params
|
||||
? _value._params
|
||||
: params // ignore: cast_nullable_to_non_nullable
|
||||
as List<NameValueModel>?,
|
||||
isHeaderEnabledList: freezed == isHeaderEnabledList
|
||||
? _value._isHeaderEnabledList
|
||||
: isHeaderEnabledList // ignore: cast_nullable_to_non_nullable
|
||||
as List<bool>?,
|
||||
isParamEnabledList: freezed == isParamEnabledList
|
||||
? _value._isParamEnabledList
|
||||
: isParamEnabledList // ignore: cast_nullable_to_non_nullable
|
||||
as List<bool>?,
|
||||
bodyContentType: null == bodyContentType
|
||||
? _value.bodyContentType
|
||||
: bodyContentType // ignore: cast_nullable_to_non_nullable
|
||||
as ContentType,
|
||||
body: freezed == body
|
||||
? _value.body
|
||||
: body // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
formData: freezed == formData
|
||||
? _value._formData
|
||||
: formData // ignore: cast_nullable_to_non_nullable
|
||||
as List<FormDataModel>?,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
|
||||
@JsonSerializable(explicitToJson: true)
|
||||
class _$HttpRequestModelImpl extends _HttpRequestModel {
|
||||
const _$HttpRequestModelImpl(
|
||||
{this.method = HTTPVerb.get,
|
||||
this.url = "",
|
||||
final List<NameValueModel>? headers,
|
||||
final List<NameValueModel>? params,
|
||||
final List<bool>? isHeaderEnabledList,
|
||||
final List<bool>? isParamEnabledList,
|
||||
this.bodyContentType = ContentType.json,
|
||||
this.body,
|
||||
final List<FormDataModel>? formData})
|
||||
: _headers = headers,
|
||||
_params = params,
|
||||
_isHeaderEnabledList = isHeaderEnabledList,
|
||||
_isParamEnabledList = isParamEnabledList,
|
||||
_formData = formData,
|
||||
super._();
|
||||
|
||||
factory _$HttpRequestModelImpl.fromJson(Map<String, dynamic> json) =>
|
||||
_$$HttpRequestModelImplFromJson(json);
|
||||
|
||||
@override
|
||||
@JsonKey()
|
||||
final HTTPVerb method;
|
||||
@override
|
||||
@JsonKey()
|
||||
final String url;
|
||||
final List<NameValueModel>? _headers;
|
||||
@override
|
||||
List<NameValueModel>? get headers {
|
||||
final value = _headers;
|
||||
if (value == null) return null;
|
||||
if (_headers is EqualUnmodifiableListView) return _headers;
|
||||
// ignore: implicit_dynamic_type
|
||||
return EqualUnmodifiableListView(value);
|
||||
}
|
||||
|
||||
final List<NameValueModel>? _params;
|
||||
@override
|
||||
List<NameValueModel>? get params {
|
||||
final value = _params;
|
||||
if (value == null) return null;
|
||||
if (_params is EqualUnmodifiableListView) return _params;
|
||||
// ignore: implicit_dynamic_type
|
||||
return EqualUnmodifiableListView(value);
|
||||
}
|
||||
|
||||
final List<bool>? _isHeaderEnabledList;
|
||||
@override
|
||||
List<bool>? get isHeaderEnabledList {
|
||||
final value = _isHeaderEnabledList;
|
||||
if (value == null) return null;
|
||||
if (_isHeaderEnabledList is EqualUnmodifiableListView)
|
||||
return _isHeaderEnabledList;
|
||||
// ignore: implicit_dynamic_type
|
||||
return EqualUnmodifiableListView(value);
|
||||
}
|
||||
|
||||
final List<bool>? _isParamEnabledList;
|
||||
@override
|
||||
List<bool>? get isParamEnabledList {
|
||||
final value = _isParamEnabledList;
|
||||
if (value == null) return null;
|
||||
if (_isParamEnabledList is EqualUnmodifiableListView)
|
||||
return _isParamEnabledList;
|
||||
// ignore: implicit_dynamic_type
|
||||
return EqualUnmodifiableListView(value);
|
||||
}
|
||||
|
||||
@override
|
||||
@JsonKey()
|
||||
final ContentType bodyContentType;
|
||||
@override
|
||||
final String? body;
|
||||
final List<FormDataModel>? _formData;
|
||||
@override
|
||||
List<FormDataModel>? get formData {
|
||||
final value = _formData;
|
||||
if (value == null) return null;
|
||||
if (_formData is EqualUnmodifiableListView) return _formData;
|
||||
// ignore: implicit_dynamic_type
|
||||
return EqualUnmodifiableListView(value);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'HttpRequestModel(method: $method, url: $url, headers: $headers, params: $params, isHeaderEnabledList: $isHeaderEnabledList, isParamEnabledList: $isParamEnabledList, bodyContentType: $bodyContentType, body: $body, formData: $formData)';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return identical(this, other) ||
|
||||
(other.runtimeType == runtimeType &&
|
||||
other is _$HttpRequestModelImpl &&
|
||||
(identical(other.method, method) || other.method == method) &&
|
||||
(identical(other.url, url) || other.url == url) &&
|
||||
const DeepCollectionEquality().equals(other._headers, _headers) &&
|
||||
const DeepCollectionEquality().equals(other._params, _params) &&
|
||||
const DeepCollectionEquality()
|
||||
.equals(other._isHeaderEnabledList, _isHeaderEnabledList) &&
|
||||
const DeepCollectionEquality()
|
||||
.equals(other._isParamEnabledList, _isParamEnabledList) &&
|
||||
(identical(other.bodyContentType, bodyContentType) ||
|
||||
other.bodyContentType == bodyContentType) &&
|
||||
(identical(other.body, body) || other.body == body) &&
|
||||
const DeepCollectionEquality().equals(other._formData, _formData));
|
||||
}
|
||||
|
||||
@JsonKey(ignore: true)
|
||||
@override
|
||||
int get hashCode => Object.hash(
|
||||
runtimeType,
|
||||
method,
|
||||
url,
|
||||
const DeepCollectionEquality().hash(_headers),
|
||||
const DeepCollectionEquality().hash(_params),
|
||||
const DeepCollectionEquality().hash(_isHeaderEnabledList),
|
||||
const DeepCollectionEquality().hash(_isParamEnabledList),
|
||||
bodyContentType,
|
||||
body,
|
||||
const DeepCollectionEquality().hash(_formData));
|
||||
|
||||
@JsonKey(ignore: true)
|
||||
@override
|
||||
@pragma('vm:prefer-inline')
|
||||
_$$HttpRequestModelImplCopyWith<_$HttpRequestModelImpl> get copyWith =>
|
||||
__$$HttpRequestModelImplCopyWithImpl<_$HttpRequestModelImpl>(
|
||||
this, _$identity);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return _$$HttpRequestModelImplToJson(
|
||||
this,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class _HttpRequestModel extends HttpRequestModel {
|
||||
const factory _HttpRequestModel(
|
||||
{final HTTPVerb method,
|
||||
final String url,
|
||||
final List<NameValueModel>? headers,
|
||||
final List<NameValueModel>? params,
|
||||
final List<bool>? isHeaderEnabledList,
|
||||
final List<bool>? isParamEnabledList,
|
||||
final ContentType bodyContentType,
|
||||
final String? body,
|
||||
final List<FormDataModel>? formData}) = _$HttpRequestModelImpl;
|
||||
const _HttpRequestModel._() : super._();
|
||||
|
||||
factory _HttpRequestModel.fromJson(Map<String, dynamic> json) =
|
||||
_$HttpRequestModelImpl.fromJson;
|
||||
|
||||
@override
|
||||
HTTPVerb get method;
|
||||
@override
|
||||
String get url;
|
||||
@override
|
||||
List<NameValueModel>? get headers;
|
||||
@override
|
||||
List<NameValueModel>? get params;
|
||||
@override
|
||||
List<bool>? get isHeaderEnabledList;
|
||||
@override
|
||||
List<bool>? get isParamEnabledList;
|
||||
@override
|
||||
ContentType get bodyContentType;
|
||||
@override
|
||||
String? get body;
|
||||
@override
|
||||
List<FormDataModel>? get formData;
|
||||
@override
|
||||
@JsonKey(ignore: true)
|
||||
_$$HttpRequestModelImplCopyWith<_$HttpRequestModelImpl> get copyWith =>
|
||||
throw _privateConstructorUsedError;
|
||||
}
|
63
lib/models/http_request_model.g.dart
Normal file
63
lib/models/http_request_model.g.dart
Normal file
@ -0,0 +1,63 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'http_request_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
_$HttpRequestModelImpl _$$HttpRequestModelImplFromJson(
|
||||
Map<String, dynamic> json) =>
|
||||
_$HttpRequestModelImpl(
|
||||
method: $enumDecodeNullable(_$HTTPVerbEnumMap, json['method']) ??
|
||||
HTTPVerb.get,
|
||||
url: json['url'] as String? ?? "",
|
||||
headers: (json['headers'] as List<dynamic>?)
|
||||
?.map((e) => NameValueModel.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
params: (json['params'] as List<dynamic>?)
|
||||
?.map((e) => NameValueModel.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
isHeaderEnabledList: (json['isHeaderEnabledList'] as List<dynamic>?)
|
||||
?.map((e) => e as bool)
|
||||
.toList(),
|
||||
isParamEnabledList: (json['isParamEnabledList'] as List<dynamic>?)
|
||||
?.map((e) => e as bool)
|
||||
.toList(),
|
||||
bodyContentType:
|
||||
$enumDecodeNullable(_$ContentTypeEnumMap, json['bodyContentType']) ??
|
||||
ContentType.json,
|
||||
body: json['body'] as String?,
|
||||
formData: (json['formData'] as List<dynamic>?)
|
||||
?.map((e) => FormDataModel.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$$HttpRequestModelImplToJson(
|
||||
_$HttpRequestModelImpl instance) =>
|
||||
<String, dynamic>{
|
||||
'method': _$HTTPVerbEnumMap[instance.method]!,
|
||||
'url': instance.url,
|
||||
'headers': instance.headers?.map((e) => e.toJson()).toList(),
|
||||
'params': instance.params?.map((e) => e.toJson()).toList(),
|
||||
'isHeaderEnabledList': instance.isHeaderEnabledList,
|
||||
'isParamEnabledList': instance.isParamEnabledList,
|
||||
'bodyContentType': _$ContentTypeEnumMap[instance.bodyContentType]!,
|
||||
'body': instance.body,
|
||||
'formData': instance.formData?.map((e) => e.toJson()).toList(),
|
||||
};
|
||||
|
||||
const _$HTTPVerbEnumMap = {
|
||||
HTTPVerb.get: 'get',
|
||||
HTTPVerb.head: 'head',
|
||||
HTTPVerb.post: 'post',
|
||||
HTTPVerb.put: 'put',
|
||||
HTTPVerb.patch: 'patch',
|
||||
HTTPVerb.delete: 'delete',
|
||||
};
|
||||
|
||||
const _$ContentTypeEnumMap = {
|
||||
ContentType.json: 'json',
|
||||
ContentType.text: 'text',
|
||||
ContentType.formdata: 'formdata',
|
||||
};
|
@ -1,5 +1,6 @@
|
||||
export 'form_data_model.dart';
|
||||
export 'http_request_model.dart';
|
||||
export 'name_value_model.dart';
|
||||
export 'request_model.dart';
|
||||
export 'response_model.dart';
|
||||
export 'settings_model.dart';
|
||||
export 'form_data_model.dart';
|
||||
|
99
test/models/http_request_model_test.dart
Normal file
99
test/models/http_request_model_test.dart
Normal file
@ -0,0 +1,99 @@
|
||||
import 'package:test/test.dart';
|
||||
import 'package:apidash/models/models.dart';
|
||||
import 'package:apidash/consts.dart';
|
||||
import 'http_request_models.dart';
|
||||
|
||||
void main() {
|
||||
Map<String, dynamic> requestModelAsJson = {
|
||||
"method": 'post',
|
||||
"url": 'https://api.apidash.dev/case/lower',
|
||||
"headers": [
|
||||
{'name': 'User-Agent', 'value': 'Test Agent'},
|
||||
{'name': 'Content-Type', 'value': 'application/json; charset=utf-8'}
|
||||
],
|
||||
'params': [
|
||||
{'name': 'size', 'value': '2'},
|
||||
{'name': 'len', 'value': '3'}
|
||||
],
|
||||
'isHeaderEnabledList': [false, true],
|
||||
'isParamEnabledList': null,
|
||||
"bodyContentType": 'json',
|
||||
"body": '''{
|
||||
"text": "I LOVE Flutter"
|
||||
}''',
|
||||
'formData': [
|
||||
{'name': 'token', 'value': 'xyz', 'type': 'text'},
|
||||
{'name': 'imfile', 'value': '/Documents/up/1.png', 'type': 'file'}
|
||||
],
|
||||
};
|
||||
|
||||
test('Testing copyWith', () {
|
||||
var httpRequestModel = requestModelPost10;
|
||||
final httpRequestModelcopyWith =
|
||||
httpRequestModel.copyWith(url: 'https://apidash.dev');
|
||||
expect(httpRequestModelcopyWith.url, 'https://apidash.dev');
|
||||
// original model unchanged
|
||||
expect(httpRequestModel.url, 'https://api.apidash.dev/case/lower');
|
||||
});
|
||||
|
||||
test('Testing toJson', () {
|
||||
var httpRequestModel = requestModelPost10;
|
||||
expect(httpRequestModel.toJson(), requestModelAsJson);
|
||||
});
|
||||
|
||||
test('Testing fromJson', () {
|
||||
var httpRequestModel = requestModelPost10;
|
||||
final modelFromJson = HttpRequestModel.fromJson(requestModelAsJson);
|
||||
expect(modelFromJson, httpRequestModel);
|
||||
expect(modelFromJson.params, const [
|
||||
NameValueModel(name: 'size', value: '2'),
|
||||
NameValueModel(name: 'len', value: '3'),
|
||||
]);
|
||||
});
|
||||
|
||||
test('Testing getters', () {
|
||||
var httpRequestModel = requestModelPost10;
|
||||
expect(httpRequestModel.headersMap, {
|
||||
'User-Agent': 'Test Agent',
|
||||
'Content-Type': 'application/json; charset=utf-8'
|
||||
});
|
||||
expect(httpRequestModel.paramsMap, {'size': '2', 'len': '3'});
|
||||
expect(httpRequestModel.enabledHeaders, const [
|
||||
NameValueModel(
|
||||
name: 'Content-Type', value: 'application/json; charset=utf-8')
|
||||
]);
|
||||
expect(httpRequestModel.enabledParams, const [
|
||||
NameValueModel(name: 'size', value: '2'),
|
||||
NameValueModel(name: 'len', value: '3'),
|
||||
]);
|
||||
expect(httpRequestModel.enabledHeadersMap,
|
||||
{'Content-Type': 'application/json; charset=utf-8'});
|
||||
expect(httpRequestModel.enabledParamsMap, {'size': '2', 'len': '3'});
|
||||
expect(httpRequestModel.hasContentTypeHeader, true);
|
||||
|
||||
expect(httpRequestModel.hasFormDataContentType, false);
|
||||
expect(httpRequestModel.hasJsonContentType, true);
|
||||
expect(httpRequestModel.hasTextContentType, false);
|
||||
expect(httpRequestModel.hasFormData, false);
|
||||
|
||||
expect(httpRequestModel.hasJsonData, true);
|
||||
expect(httpRequestModel.hasTextData, false);
|
||||
expect(httpRequestModel.hasFormData, false);
|
||||
|
||||
httpRequestModel =
|
||||
httpRequestModel.copyWith(bodyContentType: ContentType.formdata);
|
||||
expect(httpRequestModel.hasFormData, true);
|
||||
expect(httpRequestModel.formDataList, const [
|
||||
FormDataModel(name: "token", value: "xyz", type: FormDataType.text),
|
||||
FormDataModel(
|
||||
name: "imfile",
|
||||
value: "/Documents/up/1.png",
|
||||
type: FormDataType.file),
|
||||
]);
|
||||
expect(httpRequestModel.formDataMapList, [
|
||||
{'name': 'token', 'value': 'xyz', 'type': 'text'},
|
||||
{'name': 'imfile', 'value': '/Documents/up/1.png', 'type': 'file'}
|
||||
]);
|
||||
expect(httpRequestModel.hasFileInFormData, true);
|
||||
});
|
||||
}
|
374
test/models/http_request_models.dart
Normal file
374
test/models/http_request_models.dart
Normal file
@ -0,0 +1,374 @@
|
||||
import 'package:apidash/models/models.dart'
|
||||
show FormDataModel, NameValueModel, HttpRequestModel;
|
||||
import 'package:apidash/consts.dart';
|
||||
|
||||
/// Basic GET request model
|
||||
const requestModelGet1 = HttpRequestModel(
|
||||
method: HTTPVerb.get,
|
||||
url: 'https://api.apidash.dev',
|
||||
);
|
||||
|
||||
/// GET request model with query params
|
||||
const requestModelGet2 = HttpRequestModel(
|
||||
method: HTTPVerb.get,
|
||||
url: 'https://api.apidash.dev/country/data',
|
||||
params: [
|
||||
NameValueModel(name: 'code', value: 'US'),
|
||||
],
|
||||
);
|
||||
|
||||
/// GET request model with override query params
|
||||
const requestModelGet3 = HttpRequestModel(
|
||||
url: 'https://api.apidash.dev/country/data?code=US',
|
||||
method: HTTPVerb.get,
|
||||
params: [
|
||||
NameValueModel(name: 'code', value: 'IND'),
|
||||
],
|
||||
);
|
||||
|
||||
/// GET request model with different types of query params
|
||||
const requestModelGet4 = HttpRequestModel(
|
||||
method: HTTPVerb.get,
|
||||
url: 'https://api.apidash.dev/humanize/social',
|
||||
params: [
|
||||
NameValueModel(name: 'num', value: '8700000'),
|
||||
NameValueModel(name: 'digits', value: '3'),
|
||||
NameValueModel(name: 'system', value: 'SS'),
|
||||
NameValueModel(name: 'add_space', value: 'true'),
|
||||
NameValueModel(name: 'trailing_zeros', value: 'true'),
|
||||
],
|
||||
);
|
||||
|
||||
/// GET request model with headers
|
||||
const requestModelGet5 = HttpRequestModel(
|
||||
method: HTTPVerb.get,
|
||||
url: 'https://api.github.com/repos/foss42/apidash',
|
||||
headers: [
|
||||
NameValueModel(name: 'User-Agent', value: 'Test Agent'),
|
||||
],
|
||||
);
|
||||
|
||||
/// GET request model with headers & query params
|
||||
const requestModelGet6 = HttpRequestModel(
|
||||
method: HTTPVerb.get,
|
||||
url: 'https://api.github.com/repos/foss42/apidash',
|
||||
headers: [
|
||||
NameValueModel(name: 'User-Agent', value: 'Test Agent'),
|
||||
],
|
||||
params: [
|
||||
NameValueModel(name: 'raw', value: 'true'),
|
||||
],
|
||||
);
|
||||
|
||||
/// GET request model with body
|
||||
const requestModelGet7 = HttpRequestModel(
|
||||
method: HTTPVerb.get,
|
||||
url: 'https://api.apidash.dev',
|
||||
bodyContentType: ContentType.text,
|
||||
body: 'This is a random text which should not be attached with a GET request',
|
||||
);
|
||||
|
||||
/// GET request model with empty header & query param name
|
||||
const requestModelGet8 = HttpRequestModel(
|
||||
method: HTTPVerb.get,
|
||||
url: 'https://api.github.com/repos/foss42/apidash',
|
||||
headers: [
|
||||
NameValueModel(name: 'User-Agent', value: 'Test Agent'),
|
||||
NameValueModel(name: '', value: 'Bearer XYZ'),
|
||||
],
|
||||
params: [
|
||||
NameValueModel(name: 'raw', value: 'true'),
|
||||
NameValueModel(name: '', value: 'true'),
|
||||
],
|
||||
);
|
||||
|
||||
/// GET request model with some params enabled
|
||||
const requestModelGet9 = HttpRequestModel(
|
||||
method: HTTPVerb.get,
|
||||
url: 'https://api.apidash.dev/humanize/social',
|
||||
params: [
|
||||
NameValueModel(name: 'num', value: '8700000'),
|
||||
NameValueModel(name: 'digits', value: '3'),
|
||||
NameValueModel(name: 'system', value: 'SS'),
|
||||
NameValueModel(name: 'add_space', value: 'true'),
|
||||
],
|
||||
isParamEnabledList: [
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
],
|
||||
);
|
||||
|
||||
/// GET Request model with some headers enabled
|
||||
const requestModelGet10 = HttpRequestModel(
|
||||
method: HTTPVerb.get,
|
||||
url: 'https://api.apidash.dev/humanize/social',
|
||||
headers: [
|
||||
NameValueModel(name: 'User-Agent', value: 'Test Agent'),
|
||||
NameValueModel(name: 'Content-Type', value: 'application/json'),
|
||||
],
|
||||
isHeaderEnabledList: [
|
||||
true,
|
||||
false,
|
||||
],
|
||||
);
|
||||
|
||||
/// GET Request model with some headers & URL parameters enabled
|
||||
const requestModelGet11 = HttpRequestModel(
|
||||
method: HTTPVerb.get,
|
||||
url: 'https://api.apidash.dev/humanize/social',
|
||||
params: [
|
||||
NameValueModel(name: 'num', value: '8700000'),
|
||||
NameValueModel(name: 'digits', value: '3'),
|
||||
NameValueModel(name: 'system', value: 'SS'),
|
||||
NameValueModel(name: 'add_space', value: 'true'),
|
||||
],
|
||||
headers: [
|
||||
NameValueModel(name: 'User-Agent', value: 'Test Agent'),
|
||||
NameValueModel(name: 'Content-Type', value: 'application/json'),
|
||||
],
|
||||
isParamEnabledList: [
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
],
|
||||
isHeaderEnabledList: [
|
||||
true,
|
||||
false,
|
||||
],
|
||||
);
|
||||
|
||||
/// Request model with all headers & URL parameters disabled
|
||||
const requestModelGet12 = HttpRequestModel(
|
||||
method: HTTPVerb.get,
|
||||
url: 'https://api.apidash.dev/humanize/social',
|
||||
params: [
|
||||
NameValueModel(name: 'num', value: '8700000'),
|
||||
NameValueModel(name: 'digits', value: '3'),
|
||||
NameValueModel(name: 'system', value: 'SS'),
|
||||
NameValueModel(name: 'add_space', value: 'true'),
|
||||
],
|
||||
headers: [
|
||||
NameValueModel(name: 'User-Agent', value: 'Test Agent'),
|
||||
NameValueModel(name: 'Content-Type', value: 'application/json'),
|
||||
],
|
||||
isParamEnabledList: [
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
],
|
||||
isHeaderEnabledList: [
|
||||
false,
|
||||
false,
|
||||
],
|
||||
);
|
||||
|
||||
/// Basic HEAD request model
|
||||
const requestModelHead1 = HttpRequestModel(
|
||||
method: HTTPVerb.head,
|
||||
url: 'https://api.apidash.dev',
|
||||
);
|
||||
|
||||
/// Without URI Scheme (pass default as http)
|
||||
const requestModelHead2 = HttpRequestModel(
|
||||
method: HTTPVerb.head,
|
||||
url: 'api.apidash.dev',
|
||||
);
|
||||
|
||||
/// Basic POST request model (txt body)
|
||||
const requestModelPost1 = HttpRequestModel(
|
||||
method: HTTPVerb.post,
|
||||
url: 'https://api.apidash.dev/case/lower',
|
||||
body: r"""{
|
||||
"text": "I LOVE Flutter"
|
||||
}""",
|
||||
bodyContentType: ContentType.text,
|
||||
);
|
||||
|
||||
/// POST request model with JSON body
|
||||
const requestModelPost2 = HttpRequestModel(
|
||||
method: HTTPVerb.post,
|
||||
url: 'https://api.apidash.dev/case/lower',
|
||||
body: r"""{
|
||||
"text": "I LOVE Flutter",
|
||||
"flag": null,
|
||||
"male": true,
|
||||
"female": false,
|
||||
"no": 1.2,
|
||||
"arr": ["null", "true", "false", null]
|
||||
}""",
|
||||
);
|
||||
|
||||
/// POST request model with headers
|
||||
const requestModelPost3 = HttpRequestModel(
|
||||
method: HTTPVerb.post,
|
||||
url: 'https://api.apidash.dev/case/lower',
|
||||
body: r"""{
|
||||
"text": "I LOVE Flutter"
|
||||
}""",
|
||||
bodyContentType: ContentType.json,
|
||||
headers: [
|
||||
NameValueModel(name: 'User-Agent', value: 'Test Agent'),
|
||||
],
|
||||
);
|
||||
|
||||
/// POST request model with multipart body(text)
|
||||
const requestModelPost4 = HttpRequestModel(
|
||||
method: HTTPVerb.post,
|
||||
url: 'https://api.apidash.dev/io/form',
|
||||
bodyContentType: ContentType.formdata,
|
||||
formData: [
|
||||
FormDataModel(name: "text", value: "API", type: FormDataType.text),
|
||||
FormDataModel(name: "sep", value: "|", type: FormDataType.text),
|
||||
FormDataModel(name: "times", value: "3", type: FormDataType.text),
|
||||
],
|
||||
);
|
||||
|
||||
/// POST request model with multipart body and headers
|
||||
const requestModelPost5 = HttpRequestModel(
|
||||
method: HTTPVerb.post,
|
||||
url: 'https://api.apidash.dev/io/form',
|
||||
headers: [
|
||||
NameValueModel(name: 'User-Agent', value: 'Test Agent'),
|
||||
],
|
||||
bodyContentType: ContentType.formdata,
|
||||
formData: [
|
||||
FormDataModel(name: "text", value: "API", type: FormDataType.text),
|
||||
FormDataModel(name: "sep", value: "|", type: FormDataType.text),
|
||||
FormDataModel(name: "times", value: "3", type: FormDataType.text),
|
||||
],
|
||||
);
|
||||
|
||||
/// POST request model with multipart body(text, file)
|
||||
const requestModelPost6 = HttpRequestModel(
|
||||
method: HTTPVerb.post,
|
||||
url: 'https://api.apidash.dev/io/img',
|
||||
bodyContentType: ContentType.formdata,
|
||||
formData: [
|
||||
FormDataModel(name: "token", value: "xyz", type: FormDataType.text),
|
||||
FormDataModel(
|
||||
name: "imfile", value: "/Documents/up/1.png", type: FormDataType.file),
|
||||
],
|
||||
);
|
||||
|
||||
/// POST request model with multipart body and requestBody (the requestBody shouldn't be in codegen)
|
||||
const requestModelPost7 = HttpRequestModel(
|
||||
method: HTTPVerb.post,
|
||||
url: 'https://api.apidash.dev/io/img',
|
||||
bodyContentType: ContentType.formdata,
|
||||
body: r"""{
|
||||
"text": "I LOVE Flutter"
|
||||
}""",
|
||||
formData: [
|
||||
FormDataModel(name: "token", value: "xyz", type: FormDataType.text),
|
||||
FormDataModel(
|
||||
name: "imfile", value: "/Documents/up/1.png", type: FormDataType.file),
|
||||
],
|
||||
);
|
||||
|
||||
/// POST request model with multipart body and requestParams
|
||||
const requestModelPost8 = HttpRequestModel(
|
||||
method: HTTPVerb.post,
|
||||
url: 'https://api.apidash.dev/io/form',
|
||||
params: [
|
||||
NameValueModel(name: 'size', value: '2'),
|
||||
NameValueModel(name: 'len', value: '3'),
|
||||
],
|
||||
bodyContentType: ContentType.formdata,
|
||||
formData: [
|
||||
FormDataModel(name: "text", value: "API", type: FormDataType.text),
|
||||
FormDataModel(name: "sep", value: "|", type: FormDataType.text),
|
||||
FormDataModel(name: "times", value: "3", type: FormDataType.text),
|
||||
],
|
||||
);
|
||||
|
||||
/// POST request model with multipart body(file and text), requestParams, requestHeaders and requestBody
|
||||
const requestModelPost9 = HttpRequestModel(
|
||||
method: HTTPVerb.post,
|
||||
url: 'https://api.apidash.dev/io/img',
|
||||
headers: [
|
||||
NameValueModel(name: 'User-Agent', value: 'Test Agent'),
|
||||
NameValueModel(name: 'Keep-Alive', value: 'true'),
|
||||
],
|
||||
params: [
|
||||
NameValueModel(name: 'size', value: '2'),
|
||||
NameValueModel(name: 'len', value: '3'),
|
||||
],
|
||||
bodyContentType: ContentType.formdata,
|
||||
body: r"""{
|
||||
"text": "I LOVE Flutter"
|
||||
}""",
|
||||
formData: [
|
||||
FormDataModel(name: "token", value: "xyz", type: FormDataType.text),
|
||||
FormDataModel(
|
||||
name: "imfile", value: "/Documents/up/1.png", type: FormDataType.file),
|
||||
],
|
||||
);
|
||||
|
||||
/// POST request model with content type override and all other params
|
||||
const requestModelPost10 = HttpRequestModel(
|
||||
method: HTTPVerb.post,
|
||||
url: 'https://api.apidash.dev/case/lower',
|
||||
headers: [
|
||||
NameValueModel(name: 'User-Agent', value: 'Test Agent'),
|
||||
NameValueModel(
|
||||
name: 'Content-Type', value: 'application/json; charset=utf-8'),
|
||||
],
|
||||
params: [
|
||||
NameValueModel(name: 'size', value: '2'),
|
||||
NameValueModel(name: 'len', value: '3'),
|
||||
],
|
||||
isHeaderEnabledList: [false, true],
|
||||
isParamEnabledList: null,
|
||||
bodyContentType: ContentType.json,
|
||||
body: r"""{
|
||||
"text": "I LOVE Flutter"
|
||||
}""",
|
||||
formData: [
|
||||
FormDataModel(name: "token", value: "xyz", type: FormDataType.text),
|
||||
FormDataModel(
|
||||
name: "imfile", value: "/Documents/up/1.png", type: FormDataType.file),
|
||||
],
|
||||
);
|
||||
|
||||
/// PUT request model
|
||||
const requestModelPut1 = HttpRequestModel(
|
||||
method: HTTPVerb.put,
|
||||
url: 'https://reqres.in/api/users/2',
|
||||
bodyContentType: ContentType.json,
|
||||
body: r"""{
|
||||
"name": "morpheus",
|
||||
"job": "zion resident"
|
||||
}""",
|
||||
);
|
||||
|
||||
/// PATCH request model
|
||||
const requestModelPatch1 = HttpRequestModel(
|
||||
method: HTTPVerb.patch,
|
||||
url: 'https://reqres.in/api/users/2',
|
||||
bodyContentType: ContentType.json,
|
||||
body: r"""{
|
||||
"name": "marfeus",
|
||||
"job": "accountant"
|
||||
}""",
|
||||
);
|
||||
|
||||
/// Basic DELETE request model
|
||||
const requestModelDelete1 = HttpRequestModel(
|
||||
method: HTTPVerb.delete,
|
||||
url: 'https://reqres.in/api/users/2',
|
||||
);
|
||||
|
||||
/// Basic DELETE with body
|
||||
const requestModelDelete2 = HttpRequestModel(
|
||||
method: HTTPVerb.delete,
|
||||
url: 'https://reqres.in/api/users/2',
|
||||
bodyContentType: ContentType.json,
|
||||
body: r"""{
|
||||
"name": "marfeus",
|
||||
"job": "accountant"
|
||||
}""",
|
||||
);
|
Reference in New Issue
Block a user