With version 15.0, Microsoft has introduced a new capability in AL Language a new handler type:HttpClientHandler. This new handler type allows developers to mock HTTP responses in their test methods, enabling more controlled and reliable testing of modules that interact with external services. This feature is currently limited to OnPrem instances only.


Why Mock HTTP Requests?

When developing extensions, integrating with external services via HTTP requests is common. However, testing these integrations can be challenging due to network dependencies, unpredictable responses, or API downtime. By mocking HTTP responses, developers can ensure consistency and focus on the logic of their applications, rather than external factors.


Introducing HttpClientHandler

The HttpClientHandler intercepts all HTTP requests issued during a test and directs them to a handler method, where a mocked response can be defined. This ensures controlled testing, without relying on actual external calls.


Handler Method Signature

The handler method receives:

  • A TestHttpRequestMessage, which contains details about the HTTP request.
  • A TestHttpResponseMessage, which is modified to return a mocked response.

The handler method should return a Boolean:

  • true allows the request to fall through and execute against the actual endpoint.
  • false ensures the request uses the mocked response instead.

Configuring Outbound HTTP Request Behavior

To provide developers with more control, Microsoft has introduced the TestHttpRequestPolicy property in test codeunits. This property defines how outbound HTTP requests should be handled during test execution.

Available Policies

  • BlockOutboundRequests: Any HTTP request that is not caught by an HTTP handler raises an exception.
  • AllowOutboundFromHandler: All HTTP requests must be caught by an HTTP handler. However, the handler can decide to let the request reach its actual external endpoint.
  • AllowAllOutboundRequests: All outbound HTTP requests are allowed without restrictions.

Example Implementation

Below is an example implementation demonstrating how to mock an unauthorized response for HTTP requests to http://example.com/.

Codeunit with an HTTP Request

codeunit 10 MyCodeunit
{
    procedure MethodWithHttpRequest()
    var
        Client: HttpClient;
        Response: HttpResponseMessage;
    begin
        Client.Get('http://example.com/', Response);
    end;
}

Test Codeunit with Mocked HTTP Response

codeunit 11 MyCodeunitTests
{
    Subtype = Test;
    TestHttpRequestPolicy = AllowOutboundFromHandler;

    [Test]
    [HandlerFunctions('HttpClientHandler')]
    procedure TestUnauthorizedResponseHandled()
    var
        MyCodeunit: Codeunit "MyCodeunit";
    begin
        MyCodeunit.MethodWithHttpRequest();
    end;

    [HttpClientHandler]
    procedure HttpClientHandler(request: TestHttpRequestMessage; var response: TestHttpResponseMessage): Boolean
    begin
        // Mock a '401 Unauthorized' response for the 'GET http://example.com/' request
        if (request.RequestType = HttpRequestType::Get) and (request.Path = 'http://example.com/') then begin
            response.HttpStatusCode := 401;
            response.ReasonPhrase := 'Unauthorized';
            exit(false); // Use the mocked response
        end;

        // Fall through and issue the original request in case of other requests
        exit(true);
    end;
}

Final Thoughts

With HttpClientHandler and TestHttpRequestPolicy, Microsoft has enhanced AL Language , giving developers full control over outbound HTTP requests in AL tests. This ensures external dependencies do not interfere with unit tests, allowing for consistent and predictable results.

Ready to incorporate it into your OnPrem instance? Try it out and see how it improves your testing workflow! Until it is available on SaaS.

Categorized in: