From fcc940ec2e1cf338c2924db39cb9b32c35523c20 Mon Sep 17 00:00:00 2001 From: Jeet Dalal Date: Sun, 11 Aug 2024 13:40:47 +0530 Subject: [PATCH] resolved issue #178(switch from GET->POST if payload exists) --- .../request_pane/request_body.dart | 31 +++++++++++- .../request_pane/request_form_data.dart | 5 +- pubspec.yaml | 1 + test/providers/collection_providers_test.dart | 50 +++++++++++++++++++ test/providers/helpers.dart | 40 +++++++++++++++ test/providers/ui_providers_test.dart | 2 - 6 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 test/providers/collection_providers_test.dart create mode 100644 test/providers/helpers.dart diff --git a/lib/screens/home_page/editor_pane/details_card/request_pane/request_body.dart b/lib/screens/home_page/editor_pane/details_card/request_pane/request_body.dart index 39011d810..065feefe1 100644 --- a/lib/screens/home_page/editor_pane/details_card/request_pane/request_body.dart +++ b/lib/screens/home_page/editor_pane/details_card/request_pane/request_body.dart @@ -1,3 +1,6 @@ +import 'dart:developer'; + +import 'package:apidash/models/models.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:apidash/providers/providers.dart'; @@ -17,6 +20,25 @@ class EditRequestBody extends ConsumerWidget { final contentType = ref.watch(selectedRequestModelProvider .select((value) => value?.httpRequestModel?.bodyContentType)); + void changeToPostMethod() { + RequestModel? model = ref + .read(collectionStateNotifierProvider.notifier) + .getRequestModel(selectedId); + + if (model!.httpRequestModel!.method == HTTPVerb.get) { + ref + .read(collectionStateNotifierProvider.notifier) + .update(selectedId, method: HTTPVerb.post); + ScaffoldMessenger.of(context).showSnackBar(const SnackBar( + content: Text( + "Switched to POST method", + style: TextStyle(color: Colors.white), + ), + backgroundColor: Colors.black, + )); + } + } + return Column( children: [ const SizedBox( @@ -33,8 +55,11 @@ class EditRequestBody extends ConsumerWidget { ), Expanded( child: switch (contentType) { - ContentType.formdata => - const Padding(padding: kPh4, child: FormDataWidget()), + ContentType.formdata => Padding( + padding: kPh4, + child: FormDataWidget( + changeMethodToPost: changeToPostMethod, + )), // TODO: Fix JsonTextFieldEditor & plug it here ContentType.json => Padding( padding: kPt5o10, @@ -43,6 +68,7 @@ class EditRequestBody extends ConsumerWidget { fieldKey: "$selectedId-json-body-editor", initialValue: requestModel?.httpRequestModel?.body, onChanged: (String value) { + changeToPostMethod(); ref .read(collectionStateNotifierProvider.notifier) .update(selectedId, body: value); @@ -56,6 +82,7 @@ class EditRequestBody extends ConsumerWidget { fieldKey: "$selectedId-body-editor", initialValue: requestModel?.httpRequestModel?.body, onChanged: (String value) { + changeToPostMethod(); ref .read(collectionStateNotifierProvider.notifier) .update(selectedId, body: value); diff --git a/lib/screens/home_page/editor_pane/details_card/request_pane/request_form_data.dart b/lib/screens/home_page/editor_pane/details_card/request_pane/request_form_data.dart index 5228c6604..eff4c8215 100644 --- a/lib/screens/home_page/editor_pane/details_card/request_pane/request_form_data.dart +++ b/lib/screens/home_page/editor_pane/details_card/request_pane/request_form_data.dart @@ -9,7 +9,8 @@ import 'package:apidash/utils/utils.dart'; import 'package:apidash/consts.dart'; class FormDataWidget extends ConsumerStatefulWidget { - const FormDataWidget({super.key}); + final Function changeMethodToPost; + const FormDataWidget({required this.changeMethodToPost, super.key}); @override ConsumerState createState() => _FormDataBodyState(); } @@ -27,6 +28,7 @@ class _FormDataBodyState extends ConsumerState { } void _onFieldChange(String selectedId) { + widget.changeMethodToPost(); ref.read(collectionStateNotifierProvider.notifier).update( selectedId, formData: formRows.sublist(0, formRows.length - 1), @@ -41,6 +43,7 @@ class _FormDataBodyState extends ConsumerState { .select((value) => value?.httpRequestModel?.formData?.length)); var rF = ref.read(selectedRequestModelProvider)?.httpRequestModel?.formData; bool isFormDataEmpty = rF == null || rF.isEmpty; + formRows = isFormDataEmpty ? [ kFormDataEmptyModel, diff --git a/pubspec.yaml b/pubspec.yaml index 7d119b215..36b5640c4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -73,6 +73,7 @@ dependencies: git: url: https://github.com/foss42/curl_converter.git ref: 726e8cd04aeb326211af27f75920be5b21c90bb4 + dependency_overrides: web: ^0.5.0 diff --git a/test/providers/collection_providers_test.dart b/test/providers/collection_providers_test.dart new file mode 100644 index 000000000..7cabaae4b --- /dev/null +++ b/test/providers/collection_providers_test.dart @@ -0,0 +1,50 @@ +import 'package:apidash/consts.dart'; +import 'package:apidash/screens/home_page/editor_pane/details_card/request_pane/request_body.dart'; +import 'package:apidash/widgets/editor.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:apidash/providers/providers.dart'; +import 'helpers.dart'; + +void main() async { + setUp(() async => await testSetUp()); + + testWidgets( + 'Request method changes from GET to POST when body is added and Snackbar is shown', + (WidgetTester tester) async { + // Set up the test environment + final container = createContainer(); + final notifier = container.read(collectionStateNotifierProvider.notifier); + + // Ensure the initial request is a GET request with no body + final id = notifier.state!.entries.first.key; + expect( + notifier.getRequestModel(id)!.httpRequestModel!.method, HTTPVerb.get); + expect(notifier.getRequestModel(id)!.httpRequestModel!.body, isNull); + + // Build the EditRequestBody widget + await tester.pumpWidget( + ProviderScope( + // ignore: deprecated_member_use + parent: container, + child: const MaterialApp( + home: Scaffold( + body: EditRequestBody(), + ), + ), + ), + ); + + // Add a body to the request, which should trigger the method change + await tester.enterText(find.byType(TextFieldEditor), 'new body added'); + await tester.pump(); // Process the state change + + // Verify that the request method changed to POST + expect( + notifier.getRequestModel(id)!.httpRequestModel!.method, HTTPVerb.post); + + // Verify that the Snackbar is shown + expect(find.text('Switched to POST method'), findsOneWidget); + }); +} diff --git a/test/providers/helpers.dart b/test/providers/helpers.dart new file mode 100644 index 000000000..f0ab29151 --- /dev/null +++ b/test/providers/helpers.dart @@ -0,0 +1,40 @@ +import 'package:apidash/services/hive_services.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:test/test.dart'; + +/// A testing utility which creates a [ProviderContainer] and automatically +/// disposes it at the end of the test. +ProviderContainer createContainer({ + ProviderContainer? parent, + List overrides = const [], + List? observers, +}) { + // Create a ProviderContainer, and optionally allow specifying parameters. + final container = ProviderContainer( + parent: parent, + overrides: overrides, + observers: observers, + ); + + // When the test ends, dispose the container. + addTearDown(container.dispose); + + return container; +} + +Future testSetUp() async { + // override path_provider methodCall to point + // path to temporary location for all unit tests + TestWidgetsFlutterBinding.ensureInitialized(); + const MethodChannel channel = + MethodChannel('plugins.flutter.io/path_provider'); + + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger + .setMockMethodCallHandler(channel, (MethodCall methodCall) async { + return './test/unit-test-hive-storage/'; + }); + + await openBoxes(); +} diff --git a/test/providers/ui_providers_test.dart b/test/providers/ui_providers_test.dart index ba2b2b853..ea2b50124 100644 --- a/test/providers/ui_providers_test.dart +++ b/test/providers/ui_providers_test.dart @@ -1,6 +1,4 @@ import 'dart:io'; -import 'package:spot/spot.dart'; -import 'package:apidash/consts.dart'; import 'package:apidash/providers/providers.dart'; import 'package:apidash/screens/common_widgets/common_widgets.dart'; import 'package:apidash/screens/dashboard.dart';