testcontainers-mockserver 7.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 29ff7e8bdedf1475e38853d86161078462ffa1feb4515ce00c0121ab3b7106c5
4
+ data.tar.gz: 7aa54f86ae05a6e42deaac805a2a16edc12ce33e122b5d120011b65c372a1462
5
+ SHA512:
6
+ metadata.gz: c87272ad66182de8ded033c7db1f65f9c6fd5e6a131c83314275806f0b947ecb09ca2e418cf98f4811fff066c23838977c9d0738f43170e7c106d03eb826e248
7
+ data.tar.gz: fa99c0dd424769f73908006c48d9638da1384def02bb01a070a46ece9b75278c503bbae43faba99ecc58b81789deea8870b481697566b58348c4f1e67290758b
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ # During in-repo development and CI the MockServer Ruby client is resolved from
8
+ # the sibling monorepo directory rather than RubyGems, so the testcontainers
9
+ # module always tests against the current client. Published gem consumers get
10
+ # the released "mockserver-client" gem via the gemspec runtime dependency.
11
+ gem "mockserver-client", path: "../../mockserver-client-ruby"
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # testcontainers-mockserver (Ruby)
2
+
3
+ A [Testcontainers](https://github.com/testcontainers/testcontainers-ruby) module for
4
+ [MockServer](https://www.mock-server.com) — the open-source HTTP(S) mock server and proxy.
5
+
6
+ It starts the official `mockserver/mockserver` Docker image, waits for MockServer to
7
+ become ready (`PUT /mockserver/status` → `200`), and exposes connection helpers plus a
8
+ ready-wired [`mockserver-client`](https://rubygems.org/gems/mockserver-client) instance.
9
+
10
+ ## Install
11
+
12
+ ```ruby
13
+ # Gemfile
14
+ gem "testcontainers-mockserver", group: :test
15
+ ```
16
+
17
+ ```sh
18
+ bundle install
19
+ ```
20
+
21
+ Requires Ruby >= 3.0 and a running Docker daemon.
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require "testcontainers/mockserver"
27
+
28
+ container = Testcontainers::MockServerContainer.new.start
29
+
30
+ # A ready-wired MockServer client pointed at the mapped host/port
31
+ client = container.client
32
+ client
33
+ .when(MockServer::HttpRequest.request(path: "/hello"))
34
+ .respond(MockServer::HttpResponse.response(status_code: 200, body: "world"))
35
+
36
+ # Point the system under test at the container endpoint
37
+ container.endpoint # => "http://localhost:49152"
38
+ container.secure_endpoint # => "https://localhost:49152" (HTTP/HTTPS share the port)
39
+
40
+ container.stop # also closes the cached client
41
+ ```
42
+
43
+ Block form auto-stops the container:
44
+
45
+ ```ruby
46
+ Testcontainers::MockServerContainer.new.use do |container|
47
+ client = container.client
48
+ # ...
49
+ end
50
+ ```
51
+
52
+ By default the image tag is derived from the MockServer client library version on the
53
+ load path (`mockserver/mockserver:mockserver-<version>`), so the container and client
54
+ always match. Falls back to `:latest` when the version cannot be resolved. Pass an
55
+ explicit image to pin it:
56
+
57
+ ```ruby
58
+ Testcontainers::MockServerContainer.new("mockserver/mockserver:mockserver-7.4.0")
59
+ ```
60
+
61
+ ## Configuration helpers
62
+
63
+ Each returns the container for chaining, and must be called before `start`.
64
+
65
+ | Helper | What it does |
66
+ |--------|--------------|
67
+ | `with_server_port(port)` | Change the port MockServer listens on inside the container. |
68
+ | `with_log_level(level)` | Set `MOCKSERVER_LOG_LEVEL` (e.g. `"DEBUG"`). |
69
+ | `with_property(key, value)` | Set any MockServer configuration property by its env-var name. |
70
+ | `with_initialization_json(path)` | Mount a JSON file and load its expectations at startup. |
71
+
72
+ ## Connection helpers
73
+
74
+ | Method | Returns |
75
+ |--------|---------|
76
+ | `client` | A cached `MockServer::Client` connected to the container. |
77
+ | `endpoint` | `http://host:port` |
78
+ | `secure_endpoint` | `https://host:port` (same unified port) |
79
+ | `server_port` | The mapped host port. |
80
+ | `host` | The host MockServer is reachable on. |
81
+
82
+ ## License
83
+
84
+ Apache-2.0
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Testcontainers
4
+ module Mockserver
5
+ # Version of the testcontainers-mockserver gem. Kept in lockstep with the
6
+ # MockServer release version by the release tooling.
7
+ VERSION = "7.4.0"
8
+ end
9
+ end
@@ -0,0 +1,195 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "uri"
5
+
6
+ require "testcontainers"
7
+ require "mockserver-client"
8
+
9
+ require_relative "mockserver/version"
10
+
11
+ module Testcontainers
12
+ # A Testcontainers module for MockServer.
13
+ #
14
+ # Starts the official +mockserver/mockserver+ Docker image, waits for the
15
+ # server to begin listening, and exposes connection helpers (host, mapped
16
+ # port, HTTP/HTTPS endpoints) plus a ready-wired {#client}.
17
+ #
18
+ # MockServer serves HTTP, HTTPS, SOCKS, and HTTP CONNECT on a single unified
19
+ # port (default +1080+).
20
+ #
21
+ # @example Basic usage
22
+ # container = Testcontainers::MockServerContainer.new.start
23
+ # client = container.client
24
+ # client.when(
25
+ # MockServer::HttpRequest.request(path: "/hello")
26
+ # ).respond(
27
+ # MockServer::HttpResponse.response(body: "world")
28
+ # )
29
+ # # point the system under test at container.endpoint
30
+ # container.stop
31
+ #
32
+ # @example Block form (auto-stop)
33
+ # Testcontainers::MockServerContainer.new.use do |container|
34
+ # client = container.client
35
+ # # ...
36
+ # end
37
+ class MockServerContainer < ::Testcontainers::DockerContainer
38
+ # Default MockServer port (HTTP, HTTPS, SOCKS, and HTTP CONNECT are all
39
+ # served on a single unified port).
40
+ MOCKSERVER_DEFAULT_PORT = 1080
41
+
42
+ # The Docker image name on Docker Hub.
43
+ IMAGE = "mockserver/mockserver"
44
+
45
+ # Version of this gem (kept in lockstep with the MockServer release).
46
+ VERSION = ::Testcontainers::Mockserver::VERSION
47
+
48
+ # @param image [String, nil] full Docker image reference; when +nil+ the tag
49
+ # is derived from the MockServer client library version (see {.default_image}).
50
+ # @param port [Integer] the port MockServer listens on inside the container
51
+ def initialize(image = nil, port: MOCKSERVER_DEFAULT_PORT, **kwargs)
52
+ super(image || self.class.default_image, **kwargs)
53
+ @port = port
54
+ @client = nil
55
+ add_exposed_port(port)
56
+ # Wait until MockServer answers PUT /mockserver/status with HTTP 200, the
57
+ # same readiness signal used by the sibling polyglot modules. A host-side
58
+ # TCP-port wait is unreliable here because the Docker port proxy accepts
59
+ # connections before MockServer has bound inside the container.
60
+ @wait_for = ->(container) { container.wait_until_ready }
61
+ with_env("SERVER_PORT", port.to_s)
62
+ end
63
+
64
+ # Returns the mapped host port for MockServer.
65
+ #
66
+ # @return [Integer] the host port mapped to the MockServer container port
67
+ def server_port
68
+ mapped_port(@port)
69
+ end
70
+
71
+ # Blocks until MockServer answers PUT /mockserver/status with HTTP 200.
72
+ # Used as the container's wait strategy.
73
+ #
74
+ # @param timeout [Integer] maximum seconds to wait
75
+ # @param interval [Float] seconds between polls
76
+ # @return [true] once MockServer is ready
77
+ # @raise [Testcontainers::TimeoutError] if MockServer is not ready in time
78
+ def wait_until_ready(timeout: 60, interval: 0.25)
79
+ uri = URI("http://#{host}:#{server_port}/mockserver/status")
80
+ deadline = Time.now + timeout
81
+ loop do
82
+ begin
83
+ response = Net::HTTP.start(uri.hostname, uri.port, open_timeout: 2, read_timeout: 5) do |http|
84
+ http.request(Net::HTTP::Put.new(uri))
85
+ end
86
+ return true if response.code == "200"
87
+ rescue StandardError
88
+ # MockServer is not accepting connections yet
89
+ end
90
+ raise ::Testcontainers::TimeoutError, "MockServer did not become ready within #{timeout}s" if Time.now > deadline
91
+
92
+ sleep interval
93
+ end
94
+ end
95
+
96
+ # @return [String] the HTTP endpoint in the form +http://host:port+
97
+ def endpoint
98
+ "http://#{host}:#{server_port}"
99
+ end
100
+
101
+ # MockServer serves HTTP and HTTPS on the same unified port.
102
+ #
103
+ # @return [String] the HTTPS endpoint in the form +https://host:port+
104
+ def secure_endpoint
105
+ "https://#{host}:#{server_port}"
106
+ end
107
+
108
+ # Returns a {MockServer::Client} connected to this container. The client is
109
+ # created lazily on first call and cached; it is closed automatically when
110
+ # the container is stopped via {#stop}.
111
+ #
112
+ # @return [MockServer::Client] a client connected to the running container
113
+ def client
114
+ @client ||= MockServer::Client.new(host, server_port)
115
+ end
116
+
117
+ # Stops the cached client (if any) and the container.
118
+ #
119
+ # @return [MockServerContainer] self
120
+ def stop(force: false)
121
+ if @client
122
+ begin
123
+ @client.close
124
+ rescue StandardError # rubocop:disable Lint/SuppressedException
125
+ # best-effort close; ignore errors while tearing down the client
126
+ ensure
127
+ @client = nil
128
+ end
129
+ end
130
+ super
131
+ end
132
+
133
+ # Overrides the port MockServer listens on inside the container. Replaces
134
+ # the exposed port so the TCP wait strategy targets the correct port.
135
+ #
136
+ # @param port [Integer]
137
+ # @return [MockServerContainer] self
138
+ def with_server_port(port)
139
+ @port = port
140
+ # Replace the exposed port (and its binding) rather than appending, so the
141
+ # TCP wait strategy never blocks on a port MockServer is not listening on.
142
+ @exposed_ports = nil
143
+ @port_bindings = nil
144
+ add_exposed_port(port)
145
+ @wait_for = ->(container) { container.wait_until_ready }
146
+ # Replace (not append) the SERVER_PORT env var — @env is an ordered list
147
+ # and get_env returns the first match, so a stale entry would shadow this.
148
+ @env&.reject! { |entry| entry.start_with?("SERVER_PORT=") }
149
+ with_env("SERVER_PORT", port.to_s)
150
+ end
151
+
152
+ # Sets the MockServer log level (e.g. "INFO", "DEBUG", "WARN", "ERROR", "TRACE").
153
+ #
154
+ # @param level [String]
155
+ # @return [MockServerContainer] self
156
+ def with_log_level(level)
157
+ with_env("MOCKSERVER_LOG_LEVEL", level)
158
+ end
159
+
160
+ # Sets a single MockServer property as an environment variable. The key must
161
+ # be in MockServer env-var form (e.g. "MOCKSERVER_MAX_EXPECTATIONS").
162
+ #
163
+ # @param key [String]
164
+ # @param value [String]
165
+ # @return [MockServerContainer] self
166
+ def with_property(key, value)
167
+ with_env(key, value.to_s)
168
+ end
169
+
170
+ # Mounts an initialization JSON file into the container and configures
171
+ # MockServer to load its expectations at startup.
172
+ #
173
+ # @param host_init_json_path [String] path on the host to the JSON file
174
+ # @return [MockServerContainer] self
175
+ def with_initialization_json(host_init_json_path)
176
+ container_path = "/config/initializerJson.json"
177
+ add_filesystem_bind(File.expand_path(host_init_json_path), container_path, "ro")
178
+ with_env("MOCKSERVER_INITIALIZATION_JSON_PATH", container_path)
179
+ end
180
+
181
+ # Resolves the default Docker image, deriving the tag from the MockServer
182
+ # client library version so the container image stays in lockstep with the
183
+ # client. Falls back to +:latest+ when the version cannot be resolved.
184
+ #
185
+ # @return [String] e.g. "mockserver/mockserver:mockserver-7.3.0"
186
+ def self.default_image
187
+ tag = if defined?(MockServer::VERSION) && !MockServer::VERSION.to_s.empty?
188
+ "mockserver-#{MockServer::VERSION}"
189
+ else
190
+ "latest"
191
+ end
192
+ "#{IMAGE}:#{tag}"
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "testcontainers/mockserver/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "testcontainers-mockserver"
9
+ spec.version = Testcontainers::Mockserver::VERSION
10
+ spec.authors = ["James Bloom"]
11
+ spec.email = ["jamesdbloom@gmail.com"]
12
+ spec.summary = "Testcontainers module for MockServer"
13
+ spec.description = "Testcontainers module for MockServer — starts a mockserver/mockserver " \
14
+ "Docker container, waits for readiness, and exposes connection helpers " \
15
+ "plus a ready-wired MockServer client."
16
+ spec.homepage = "https://www.mock-server.com"
17
+ spec.license = "Apache-2.0"
18
+
19
+ spec.required_ruby_version = ">= 3.0"
20
+
21
+ spec.metadata = {
22
+ "source_code_uri" => "https://github.com/mock-server/mockserver-monorepo",
23
+ "changelog_uri" => "https://www.mock-server.com/mock_server/changelog.html",
24
+ "bug_tracker_uri" => "https://github.com/mock-server/mockserver-monorepo/issues",
25
+ "documentation_uri" => "https://www.mock-server.com/mock_server/mockserver_testcontainers.html",
26
+ }
27
+
28
+ spec.files = Dir["lib/**/*.rb"] + %w[README.md Gemfile testcontainers-mockserver.gemspec]
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "mockserver-client", ">= 5.15"
32
+ spec.add_dependency "testcontainers-core", "~> 0.2"
33
+
34
+ spec.add_development_dependency "rspec", "~> 3.12"
35
+ spec.add_development_dependency "rspec_junit_formatter", "~> 0.6"
36
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: testcontainers-mockserver
3
+ version: !ruby/object:Gem::Version
4
+ version: 7.4.0
5
+ platform: ruby
6
+ authors:
7
+ - James Bloom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-07-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mockserver-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.15'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: testcontainers-core
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec_junit_formatter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.6'
69
+ description: Testcontainers module for MockServer — starts a mockserver/mockserver
70
+ Docker container, waits for readiness, and exposes connection helpers plus a ready-wired
71
+ MockServer client.
72
+ email:
73
+ - jamesdbloom@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - Gemfile
79
+ - README.md
80
+ - lib/testcontainers/mockserver.rb
81
+ - lib/testcontainers/mockserver/version.rb
82
+ - testcontainers-mockserver.gemspec
83
+ homepage: https://www.mock-server.com
84
+ licenses:
85
+ - Apache-2.0
86
+ metadata:
87
+ source_code_uri: https://github.com/mock-server/mockserver-monorepo
88
+ changelog_uri: https://www.mock-server.com/mock_server/changelog.html
89
+ bug_tracker_uri: https://github.com/mock-server/mockserver-monorepo/issues
90
+ documentation_uri: https://www.mock-server.com/mock_server/mockserver_testcontainers.html
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '3.0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubygems_version: 3.4.19
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Testcontainers module for MockServer
110
+ test_files: []