Comments (2)
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
andendDate
. I can mock it using this nice package, but the response is always the same. Is it possible to access thequeryParams
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 ofRequestMatcher
. 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 thequeryParameters
/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.
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)
- can i request to onDownload when add unit test ? HOT 2
- [v0.4.0] collection version conflict between the v0.4.0 and flutter_test HOT 9
- Lower the Dart SDK constraint to 2.15.0
- Let server return different responses HOT 7
- FullHttpRequestMatcher: not identical data returns success response HOT 3
- use tester.pump rather than Future.delayed to allow tests to run faster HOT 1
- Split up `MockServer` `reply` method into static and dynamic version HOT 11
- http_mock_adapter 0.5.0 conflicts with flutter_test collection version. HOT 8
- Async mocking not possible HOT 1
- Semantic release with github action HOT 2
- Add methods onDownload & onDownloadUri
- Library not compatible with flutter 3.16.0 - incorrect type definition HOT 3
- LDClient.get() was called before init()!
- Ability to mock whole API interface for local development without a need of a server. HOT 2
- Allow timeout simulations in replies HOT 2
- QUESTION: how to use dio.simpleGet HOT 2
- http_mock_adapter-0.4.4/lib/src/mixins/request_handling.dart:32:42: Error: A value of type 'FutureOr<List<int>>' can't be assigned to a variable of type 'List<int>'. HOT 1
- Latest registered callback is called when multiple HTTP methods have been mocked
- TypeError Occurs When Map Data and Headers are Set Together
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from http-mock-adapter.