Introducing: How to Mock Outbound Http Calls for Easier Testing (2025 release wave 1)
HTTP client Handler for mocking outbound HTTP calls in AL tests (2025 release wave 1). Covers intercepting requests, simulating responses, telling requests apart, the fall-through mechanism, and the test HTTP request policy options. Known limits: no cookies or redirection status codes.
- Testing challenges with external services
- Introducing HTTP client Handler
- Setting up the Handler and simulating responses
- Distinguishing between different requests
- Handling secret URIs and limitations
- Fall-through mechanism for selective mocking
- Live debugging demonstration
- Test HTTP request policy and scenarios
Overview
The video explains why testing code that calls external services is hard: tests depend on the service being up, and running them against a live service can affect production. It introduces a new test Handler, the HTTP client Handler, which intercepts outbound HTTP requests during test execution so the test can return a mocked response instead.
The demo shows how to set up the Handler, attach it to a test with the Handler functions attribute, and use request properties to return different responses. It then covers the fall-through mechanism for mixing mocked and real calls, a live debugging session, and the test HTTP request policy that controls which outbound requests are allowed.
Key points
- Mark a procedure with the HTTP client Handler attribute and attach it to a test with the Handler functions attribute; all HTTP calls during that test are then routed to the Handler.
- The request object exposes request type, path, query parameters and a has-secret-URI property, which you can use to return different responses per request.
- When the request URI is set using secret text, path and query parameters are not available.
- Fall-through: the Handler returns a Boolean. Exit true issues the original request; exit false (the default) uses the mocked response, so an empty Handler still blocks the outbound call.
- Test HTTP request policy on a test codeunit has three options: allow all outbound requests (default), allow outbound only from the Handler, or block all outbound requests.
- Blocking outbound requests is suggested for automated CI/CD pipelines so tests never hit real endpoints.
- As of now, cookies and redirection status codes cannot be set in mocked responses.
Features
Every feature below: status not stated, demoed.
| Feature | At |
|---|---|
| HTTP client Handler | 1:25 |
| HTTP client Handler attribute | 2:26 |
| Handler functions attribute | 2:44 |
| Request object properties | 3:45 |
| Fall-through mechanism | 5:16 |
| Test HTTP request policy | 8:20 |
| Allow all outbound requests policy | 8:52 |
| Allow outbound from Handler policy | 9:32 |
| Block outbound requests policy | 10:08 |
AL objects mentioned
As heard in the captions. A name that matches one object page by exact type and name links to it; the others stay as named.
- other “document service” at 1:46
- codeunit “test code unit” at 2:06
- other “response object” at 2:26
- other “request object” at 3:45
Not found in BC28-30: codeunit “test code unit”.
Quotes
- 0:04 “if we connect to the actual service we need to consider how that service will be impacted by running tests will it impact production”
- 1:25 “to address many of these challenges we have introduced a new test Handler in this release the HTTP client Handler”
- 2:06 “however this setup requires that the external service is up and running instead we can define an HTTP client hander to intercept the request”
- 4:21 “it is important to mention that as of now it is not possible to set cookies or redirection status codes so far so good”
- 5:16 “we have implemented a fall through mechanism that allows the programmer to decide in the Handler whether they want to Mark the response for”
- 5:53 “keep in mind that the default return value is false therefore a completely empty hander procedure would still block the outbound request and return”
- 8:31 “you can add it to test code units to specify how outbound request should be treated during test executions by default all requests are”
Disclaimers in the video
- 4:21 other: as of now it is not possible to set cookies or redirection status codes
Presenters (as heard): Stefan, Balash.
