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

Using query parameters in response data generation #151

Closed
sebastianbuechler opened this issue Jun 15, 2023 · 2 comments
Closed

Using query parameters in response data generation #151

sebastianbuechler opened this issue Jun 15, 2023 · 2 comments

Comments

@sebastianbuechler
Copy link
Collaborator

sebastianbuechler commented Jun 15, 2023

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?

@sebastianbuechler
Copy link
Collaborator Author

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.

@sebastianbuechler
Copy link
Collaborator Author

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;
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant