package testcontainers-mockserver

  1. Overview
  2. Docs

Source file mockserver_container.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(** MockServer container module for mocking HTTP services *)

open Lwt.Syntax
open Testcontainers

let default_image = "mockserver/mockserver:5.15.0"
let default_port = Port.tcp 1080

type t = {
  image : string;
  port : Port.exposed_port;
  log_level : string;
  max_expectations : int option;
}

let create () =
  {
    image = default_image;
    port = default_port;
    log_level = "INFO";
    max_expectations = None;
  }

let with_image image t = { t with image }
let with_log_level level t = { t with log_level = level }
let with_max_expectations n t = { t with max_expectations = Some n }

let to_request t =
  let envs =
    [ ("MOCKSERVER_LOG_LEVEL", t.log_level) ]
    @
    match t.max_expectations with
    | Some n -> [ ("MOCKSERVER_MAX_EXPECTATIONS", string_of_int n) ]
    | None -> []
  in
  Container_request.create t.image
  |> Container_request.with_exposed_port t.port
  |> Container_request.with_envs envs
  |> Container_request.with_wait_strategy
       (Wait_strategy.for_log ~timeout:60.0 "started on port")

let start t =
  let request = to_request t in
  Container.start request

let url t container =
  let* host = Container.host container in
  let* port = Container.mapped_port container t.port in
  Lwt.return (Printf.sprintf "http://%s:%d" host port)

let host container = Container.host container
let port t container = Container.mapped_port container t.port

let with_mockserver ?config f =
  let t = match config with Some cfg -> cfg (create ()) | None -> create () in
  let* container = start t in
  let* endpoint = url t container in
  Lwt.finalize
    (fun () -> f container endpoint)
    (fun () -> Container.terminate container)