The Daraja Framework unit tests (DUnit/FPCUnit based) are now more self-documenting. A new HTTPTestCase test base class provides methods CheckGETResponseEquals, CheckPOSTResponseEquals, CheckGETResponseContains, CheckGETResponse404, and others, which allow to write more concise unit test methods.
The changes are available in the master branch, a snapshot is available for download here.

Example #1: TestNoMatchingContextReturns404

This test creates a context and verifies that requests without a matching context return a 404 status code.

type
  TExamplePage = class(TdjWebComponent)
  public
    procedure OnGet(Request: TdjRequest; Response: TdjResponse);
      override;
  end;

{ TExamplePage }

procedure TExamplePage.OnGet(Request: TdjRequest; Response: TdjResponse);
begin
  Response.ContentText := 'example';
end;

procedure TAPIConfigTests.TestNoMatchingContextReturns404;
var
  Server: TdjServer;
  Context: TdjWebAppContext;
begin
  Server := TdjServer.Create;
  try
    Context := TdjWebAppContext.Create('example');
    Context.AddWebComponent(TExamplePage, '*.html');
    Server.Add(Context);
    Server.Start;

    CheckGETResponse404('/example2/a.html');
    CheckGETResponse404('/Example/b.html');
    CheckGETResponse200('/example/c.html');

  finally
    Server.Free;
  end;
end;

Example #2: TestIPv6ConnectionToLoopback

The test creates a IPv6 connector on the loopback device and verifies that the server handles a GET request, and returns the expected content as response body.

procedure TAPIConfigTests.TestIPv6ConnectionToLoopback;
var
  Server: TdjServer;
  Context: TdjWebAppContext;
begin
  Server := TdjServer.Create;
  try
    Server.AddConnector('::1');
    Context := TdjWebAppContext.Create('example');
    Context.Add(TExamplePage, '/index.html');
    Server.Add(Context);
    Server.Start;

    CheckGETResponseEquals('example',
        'http://[::1]/example/index.html');

  finally
    Server.Free;
  end;
end;

Example #3: TestPOSTMethodResponse

This test adds a web component which only serves POST requests and verifies that a POST request returns the expected content, and a GET request returns a 405 error.

type
  TPostComponent = class(TdjWebComponent)
  public
    procedure OnPost(Request: TdjRequest; Response: TdjResponse);
      override;
  end;

{ TPostComponent }

procedure TPostComponent.OnPost(Request: TdjRequest; Response: TdjResponse);
begin
  Response.ContentText := 'thank you for POSTing';
end;

procedure TAPIConfigTests.TestPOSTMethodResponse;
var
  Server: TdjServer;
  Context: TdjWebAppContext;
begin
  Server := TdjServer.Create;
  try
    Context := TdjWebAppContext.Create('example');
    Context.AddWebComponent(TPostComponent, '/index.html');
    Server.Add(Context);
    Server.Start;

    CheckPOSTResponseEquals('thank you for POSTing',
        '/example/index.html');
    CheckGETResponse405('/example/index.html');

  finally
    Server.Free;
  end;
end;

daraja_logo_landscape_2016_3

Related projects

  • daraja-restful: https://github.com/michaelJustin/daraja-restful
  • slf4p: https://github.com/michaelJustin/slf4p
  • Internet Direct (Indy): http://indyproject.org/Sockets/index.aspx

Discover more from Habarisoft Blog

Subscribe to get the latest posts sent to your email.

Leave a Reply

Your email address will not be published. Required fields are marked *