Giter Club home page Giter Club logo

Comments (2)

sebastianbuechler avatar sebastianbuechler commented on June 25, 2024 1

Description

I have an endpoint that responds with a list of objects that exhibit a timestamp. Because there are potentially a lot of these objects in the list I use to filter them with the help of two query parameters startDate and endDate. I can mock it using this nice package, but the response is always the same. Is it possible to access the queryParams object in the callback for the request so that my data generation function can take the query parameters into account and filter the list before it returns?

So something like this (either through an additional parameter or the existing server object):

      dioAdapter.onGet(
        endpoint,
        (server, request) => server.reply(statusCode, generateMockData(request.queryParams)),
        queryParameters: queryParameters,
        headers: headers,
      );

When I print out the server parameter I can see that it's an instance of RequestMatcher. In the code I can see that this actually has the request object already in there:

/// Matches a [Request] to a [MockResponse].
class RequestMatcher extends RequestHandler {
  /// This is a request sent by the the client.
  final Request request;

  RequestMatcher(this.request);
}

Could we make the private request field (or add getters for the queryParameters/headers) public so that the generated mock can depend on them?

This can already be done easily with the proper use of the MockDataCallback:

 (RequestOptions request) {
      final queryParameters = request.queryParameters;
      final statusQueryParameter =
          queryParameters["status"] as String;
     return statusQueryParameter;
  );
}

from http-mock-adapter.

sebastianbuechler avatar sebastianbuechler commented on June 25, 2024

Also, it could be really helpful for non-GET requests to allow any input data for randomized tests.

This works:

 test(
      "test post mock",
      () async {
        final dio = Dio();
        final dioAdapter = DioAdapter(dio: dio);

        dio.httpClientAdapter = dioAdapter;

        const path = 'https://example.com/';

        dioAdapter.onPost(
          path,
          (request) => request.reply(
            200,
            {'message': 'Successfully mocked POST!'},
          ),
          data: {"test": true},
        );

        final getResponse = await dio.post(
          path,
          data: {"test": true},
        );
        expect({'message': 'Successfully mocked POST!'}, getResponse.data);
      },
    );

But this does not:

 test(
      "test post mock",
      () async {
        final dio = Dio();
        final dioAdapter = DioAdapter(dio: dio);

        dio.httpClientAdapter = dioAdapter;

        const path = 'https://example.com/';

        dioAdapter.onPost(
          path,
          (request) => request.reply(
            200,
            {'message': 'Successfully mocked POST!'},
          ),
        );

        final getResponse = await dio.post(
          path,
          data: {"test": true},
        );
        expect({'message': 'Successfully mocked POST!'}, getResponse.data);
      },
    );

Other mocking libraries I've seen allow for setting something like an any. So we could set for example: data: Payload.Any() which then forces the matcher to skip the payload.

from http-mock-adapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.