Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FullHttpRequestMatcher: not identical data returns success response #149

Closed
katjai1 opened this issue Jun 8, 2023 · 3 comments
Closed
Labels
bug Something isn't working

Comments

@katjai1
Copy link

katjai1 commented Jun 8, 2023

Not identical data returns success response, probably need to add strict data check to the FullHttpRequestMatcher

    test('Not identical data and FullHttpRequestMatcher', () async {
      var dio = Dio();
      var dioAdapter = DioAdapter(dio: dio);
      var path = '/abc';
      var data = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5};
      var dataExpected = {'a': 1, 'b': 2};
      dioAdapter.onPost(
        path,
        (server) => server.reply(
          200,
          'OK',
        ),
        data: dataExpected,
      );

      expect(
        () async => await dio.post(path, data: data),
        throwsA(isA<DioException>()),
      );
    });

HTTP mock test test Not identical data and FullHttpRequestMatcher [E]                                                                                                                                                                                        
  Expected: throws <Instance of 'DioException'>
    Actual: <Closure: () => Future<Response<dynamic>>>
     Which: returned a Future that emitted Response<dynamic>:<OK>
@katjai1 katjai1 added the bug Something isn't working label Jun 8, 2023
@sebastianbuechler
Copy link
Collaborator

It seems that currently the request body check is not strict as this test on the matches_request_test.dart will fail:

        test('actual is a subset of expected', () {
          const actual = {
            'a': 'a',
            'b': 'b',
            'c': 'c',
          };

          const expected = {
            'a': 'a',
            'b': 'b',
          };

          expect(options.matches(actual, expected), false);
        });

@LukaGiorgadze Is this behavior intended?

We could reason that as long as the required subset is present in the body the condition is fulfilled, as usually that's how most web servers will also evaluate if the request body is good or bad.

Maybe there is demand for a new request matcher that only allows strict body checks?

@LukaGiorgadze
Copy link
Member

@sebastianbuechler Yeh, that makes sense.
Adding a new checker that checks nested data like deep equal is worth it.

Thanks @katjai1

@sebastianbuechler
Copy link
Collaborator

This is solved in #158 and will be available in the next release.

Usage:

      dio = Dio(BaseOptions(contentType: Headers.jsonContentType));
      dioAdapter = DioAdapter(
        dio: dio,
        matcher: const FullHttpRequestMatcher(needsExactBody: true),
      );

needsExactBody is defaulting to false, so no change is needed for existing code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants