deceiver 0.5.0
deceiver: ^0.5.0 copied to clipboard
Configure and provide fake HTTP responses for Dart's package:http Client. Intercept requests with path templates, toggle mocks at runtime, and passthrough unmatched calls.
0.5.0 #
- Dart DevTools extension: a "deceiver" tab to turn mocks on and off,
switch strict mode and scenarios, and see the handled requests live,
including the response of the mock. Works without setup for every
Deceiverin an app that runs in debug or profile mode. Deceiver(name: ...)names an instance in DevTools.- Tools and AI agents can control Deceiver in a running app through the
same
ext.deceiver.*VM service extensions as DevTools, for example with the Dart MCP server.ext.deceiver.helplists them,instancemay be left out with one Deceiver, andsetMockEnabledalso takes adescription(andscenario). Errors say what to pass. Documented in the README; there is also an agent skill inskills/deceiver. deceiver.setAllMocksEnabled()turns every mock on or off, base and scenario mocks alike, for example to then turn on only the one you need. The DevTools extension has a "Disable all" / "Enable all" button.DeceiverData.responseInfotells what a mock responds: the status code, headers and body ofjsonResponse(),errorResponse()andemptyResponse(), the variants ofsequentialResponse()andrandomResponse(), orcustomfor a builder of your own. The DevTools extension shows it for every mock.- The example also covers path parameters, strict mode, base URLs and
multiple APIs, recorded requests,
reset(), listeners andresponseInfo. deceiver.addListener()andremoveListener(): get notified whenenabled,options, the mocks, the scenarios, the active scenario or a mock'sisEnabledchange.DeceiverRecordedRequesthas theresponseanddurationof the mock, thetimestamp, and theerrorof strict mode or of a mock that threw. Requests are recorded when the mock has answered, sodeceiver.requestsis in the order the requests were answered. Requests that are still being answered duringclearRequests()orreset()are not recorded.
0.4.0 #
request.pathParametersgives response builders the values of the{name}placeholders in the path, percent-decoded.Deceiver(baseUrl: ...)only handles requests to that scheme, host, port and path. Mock paths are relative to the base URL. Other requests always go to the real client, even in strict mode. AddedDeceiver.appliesTo().deceiver.requestsrecords the handled requests together with the mock that answered them. Keeps the last 100 by default; change this withDeceiverOptions.maxRecordedRequests. AddedclearRequests().on.at(url)registers mocks for another API, also inside scenarios. A full URL as path (on.get('https://maps.example.com/geocode')) registers a mock for an API with a fixed URL. Requests to these URLs count for strict mode and are recorded. AddedDeceiverData.baseUrl.deceiver.reset()removes all mocks, scenarios and recorded requests.reset(baseUrl: ...)also changes the base URL, for example when the app switches environments.- A path that looks like a URL but isn't valid (
http:/x.com) throws anArgumentErrorinstead of never matching.
0.3.0 #
Breaking #
- Requires Dart 3.13 or later (Flutter 3.47 or later).
matchPath()(and soon.get(),on.post(), etc.) uses simple path templates instead of RFC 6570 URI templates. A{name}placeholder matches exactly one path segment. Templates with a query, a fragment or a placeholder that is not a whole segment ({+path},{?q},user-{id}) now throw anArgumentErrorwhen registering.- Strict mode (
onlyAllowMocks) throws aDeceiverExceptioninstead of aStateError.DeceiverExceptionextendsClientException, so code that handles network errors handles it too. The message leaves out the query string, because it may contain secrets.
Fixes #
- Path placeholders now match values with encoded or reserved characters,
such as
/users/john@doe.com,/slots/2026-11-18T10:00and/search/new%20york. These used to fall through to the real client. - With
enabled = false, requests go to the real client even whenonlyAllowMocksistrue. They used to throw. - Matchers and response builders can read the body of
MultipartRequestandStreamedRequest. It used to be empty. DeceiverBuilderandDeceiverClientare exported frompackage:deceiver/deceiver.dart.sequentialResponse()andrandomResponse()copy the list of builders, so later changes to that list don't affect them.
Other changes #
- Added
DeceiverOptions.copyWith(). - Removed the dependency on
package:uri.
0.2.0 #
sequentialResponse()helper -- cycle through a list of response builders on each successive call, looping back to the start after the last one.randomResponse()helper -- pick a random response builder from a list on each call. Accepts an optionalRandomfor deterministic testing.- Named scenarios via
deceiver.scenario(name, configure)-- define preset mock configurations and switch between them at runtime. - Scenario mocks layer on top of base mocks, overriding matching endpoints while unmatched endpoints fall through to base mocks.
activateScenario(),deactivateScenario(),activeScenario, andscenariosgetter for scenario management.scenarioRegistrations()for inspecting a scenario's mock list.toPrettyPrintedString()now includes scenario information.DeceiverBuilder.forList()constructor for building mocks into arbitrary lists (used internally by scenarios).
0.1.0 #
- Initial release.
Deceiverclass for managing mock HTTP response registrations.DeceiverClientextendingBaseClientfrompackage:httpwith request interception and optional passthrough to a real HTTP client.- Fluent registration API via
deceiver.on.get(),.post(),.put(),.delete(),.patch(),.head(), and.match()for custom matchers. - URI template path matching (RFC 6570) with reverse-match validation.
- Response helpers:
jsonResponse(),errorResponse(),emptyResponse(). FutureOr<Response>support for async response builders.- Global
enabledtoggle to disable all mocks at runtime. - Per-mock
isEnabledtoggle to disable individual registrations. onlyAllowMocksstrict mode that throwsStateErrorwith descriptive error messages for unmatched requests.toPrettyPrintedString()for debug output of all registered mocks.