mockserver-client 1.0.8.pre → 6.0.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 +5 -5
- data/Gemfile +2 -1
- data/README.md +79 -227
- data/lib/mockserver/client.rb +518 -0
- data/lib/mockserver/errors.rb +18 -0
- data/lib/mockserver/forward_chain_expectation.rb +117 -0
- data/lib/mockserver/models.rb +1507 -0
- data/lib/mockserver/version.rb +3 -3
- data/lib/mockserver/websocket_client.rb +353 -0
- data/lib/mockserver-client.rb +7 -16
- data/mockserver-client.gemspec +26 -27
- metadata +54 -206
- data/.gitignore +0 -21
- data/.rubocop.yml +0 -7
- data/Rakefile +0 -10
- data/bin/mockserver +0 -9
- data/lib/cli.rb +0 -146
- data/lib/mockserver/abstract_client.rb +0 -111
- data/lib/mockserver/mock_server_client.rb +0 -46
- data/lib/mockserver/model/array_of.rb +0 -85
- data/lib/mockserver/model/body.rb +0 -56
- data/lib/mockserver/model/cookie.rb +0 -36
- data/lib/mockserver/model/delay.rb +0 -34
- data/lib/mockserver/model/enum.rb +0 -47
- data/lib/mockserver/model/expectation.rb +0 -139
- data/lib/mockserver/model/forward.rb +0 -41
- data/lib/mockserver/model/header.rb +0 -43
- data/lib/mockserver/model/parameter.rb +0 -43
- data/lib/mockserver/model/request.rb +0 -81
- data/lib/mockserver/model/response.rb +0 -45
- data/lib/mockserver/model/times.rb +0 -61
- data/lib/mockserver/proxy_client.rb +0 -9
- data/lib/mockserver/utility_methods.rb +0 -59
- data/pom.xml +0 -118
- data/spec/fixtures/forward_mockserver.json +0 -7
- data/spec/fixtures/incorrect_login_response.json +0 -20
- data/spec/fixtures/post_login_request.json +0 -22
- data/spec/fixtures/register_expectation.json +0 -50
- data/spec/fixtures/retrieved_request.json +0 -22
- data/spec/fixtures/search_request.json +0 -6
- data/spec/fixtures/times_once.json +0 -6
- data/spec/integration/mock_client_integration_spec.rb +0 -82
- data/spec/mockserver/builder_spec.rb +0 -90
- data/spec/mockserver/mock_client_spec.rb +0 -80
- data/spec/mockserver/proxy_client_spec.rb +0 -38
- data/spec/spec_helper.rb +0 -61
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 52c92809897fcdfe317afdab740419e5bef114b46482df4a9756821916bd4a73
|
|
4
|
+
data.tar.gz: 8b8ee1a6a5c341edecbe70a90ccb1dda4c481162eca2f30e83ee121beaa656d5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 476994411a429882a93e0fe6852527b14ac9ce60ffc2ac328f0536eef2adee69f7c2ce8061bc8dfda7cc32869fe0e227ed3f5e979a3a1e1513c1ae3540be2f27
|
|
7
|
+
data.tar.gz: 94828363b6159101e0288c15b604e7e9acf19716f67efb5edf56f591a79946aaf987ed2c32f079f421d3ad74045d61fa0aac62b93820d162df3299b284108cd7
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,260 +1,112 @@
|
|
|
1
|
-
#
|
|
1
|
+
# MockServer Ruby Client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Hand-written Ruby client for [MockServer](https://www.mock-server.com) with full REST API, fluent builder DSL, and WebSocket callback support.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
|
-
Add
|
|
7
|
+
Add to your Gemfile:
|
|
8
8
|
|
|
9
9
|
```ruby
|
|
10
|
-
gem 'mockserver-client'
|
|
10
|
+
gem 'mockserver-client', '~> 5.16'
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
bundle
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
Or install it yourself as:
|
|
13
|
+
Or install directly:
|
|
20
14
|
|
|
21
15
|
```bash
|
|
22
16
|
gem install mockserver-client
|
|
23
17
|
```
|
|
24
18
|
|
|
25
|
-
##
|
|
26
|
-
|
|
27
|
-
The usage notes here compare the Java syntax with the Ruby DSL for the various actions the MockServer client/proxy client supports. The Ruby code here assumes you have included `MockServer` and `MockServer::Model::DSL` modules. So these lines should typically be at the top of your code:
|
|
19
|
+
## Quick Start
|
|
28
20
|
|
|
29
21
|
```ruby
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
)
|
|
53
|
-
.respond(
|
|
54
|
-
response()
|
|
55
|
-
.withStatusCode(401)
|
|
56
|
-
.withHeaders(
|
|
57
|
-
new Header("Content-Type", "application/json; charset=utf-8"),
|
|
58
|
-
new Header("Cache-Control", "public, max-age=86400")
|
|
59
|
-
)
|
|
60
|
-
.withBody("{ message: 'incorrect username and password combination' }")
|
|
61
|
-
.withDelay(new Delay(TimeUnit.SECONDS, 1))
|
|
62
|
-
);
|
|
22
|
+
require 'mockserver-client'
|
|
23
|
+
|
|
24
|
+
# Create a client
|
|
25
|
+
client = MockServer::Client.new('localhost', 1080)
|
|
26
|
+
|
|
27
|
+
# Set up an expectation using the fluent API
|
|
28
|
+
client.when(
|
|
29
|
+
MockServer::HttpRequest.request(path: '/hello')
|
|
30
|
+
.with_method('GET')
|
|
31
|
+
).respond(
|
|
32
|
+
MockServer::HttpResponse.response(body: 'world', status_code: 200)
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
# Verify a request was received
|
|
36
|
+
client.verify(
|
|
37
|
+
MockServer::HttpRequest.request(path: '/hello'),
|
|
38
|
+
times: MockServer::VerificationTimes.at_least(1)
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
# Clean up
|
|
42
|
+
client.reset
|
|
43
|
+
client.close
|
|
63
44
|
```
|
|
64
45
|
|
|
65
|
-
|
|
46
|
+
## Block Form
|
|
66
47
|
|
|
67
48
|
```ruby
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
request.cookies = [cookie('sessionId', '2By8LOhBmaW5nZXJwcmludCIlMDAzMW')]
|
|
75
|
-
request.body = exact("{username: 'foo', password: 'bar'}")
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
expectation.response do |response|
|
|
79
|
-
response.status_code = 401
|
|
80
|
-
response.headers << header('Content-Type', 'application/json; charset=utf-8')
|
|
81
|
-
response.headers << header('Cache-Control', 'public, max-age=86400')
|
|
82
|
-
response.body = body("{ message: 'incorrect username and password combination' }")
|
|
83
|
-
response.delay = delay_by(:SECONDS, 1)
|
|
84
|
-
end
|
|
49
|
+
MockServer::Client.new('localhost', 1080) do |client|
|
|
50
|
+
client.when(
|
|
51
|
+
MockServer::HttpRequest.request(path: '/api/test')
|
|
52
|
+
).respond(
|
|
53
|
+
MockServer::HttpResponse.response(body: '{"status":"ok"}', status_code: 200)
|
|
54
|
+
)
|
|
85
55
|
end
|
|
86
|
-
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
### Clear & Reset Server
|
|
90
|
-
|
|
91
|
-
##### Java
|
|
92
|
-
|
|
93
|
-
```java
|
|
94
|
-
mockServerClient.clear(
|
|
95
|
-
request()
|
|
96
|
-
.withMethod("POST")
|
|
97
|
-
.withPath("/login")
|
|
98
|
-
);
|
|
99
|
-
mockServerClient.reset();
|
|
56
|
+
# Client is automatically closed when the block exits
|
|
100
57
|
```
|
|
101
58
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
```ruby
|
|
105
|
-
client.clear(request('POST', '/login'))
|
|
106
|
-
client.reset
|
|
107
|
-
```
|
|
108
|
-
### Verifying Behavior
|
|
109
|
-
|
|
110
|
-
##### Java
|
|
111
|
-
|
|
112
|
-
```java
|
|
113
|
-
new ProxyClient("localhost", 9090).verify(
|
|
114
|
-
request()
|
|
115
|
-
.withMethod("POST")
|
|
116
|
-
.withPath("/login")
|
|
117
|
-
.withBody(exact("{username: 'foo', password: 'bar'}"))
|
|
118
|
-
.withCookies(
|
|
119
|
-
new Cookie("sessionId", ".*")
|
|
120
|
-
),
|
|
121
|
-
Times.exactly(1)
|
|
122
|
-
);
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
##### Ruby
|
|
59
|
+
## WebSocket Callbacks
|
|
126
60
|
|
|
127
61
|
```ruby
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
62
|
+
client = MockServer::Client.new('localhost', 1080)
|
|
63
|
+
|
|
64
|
+
# Response callback - dynamically generate responses
|
|
65
|
+
client.mock_with_callback(
|
|
66
|
+
MockServer::HttpRequest.request(path: '/dynamic'),
|
|
67
|
+
->(request) {
|
|
68
|
+
MockServer::HttpResponse.new(
|
|
69
|
+
status_code: 200,
|
|
70
|
+
body: "Echo: #{request.path}"
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
# Forward callback - modify requests before forwarding
|
|
76
|
+
client.mock_with_forward_callback(
|
|
77
|
+
MockServer::HttpRequest.request(path: '/proxy'),
|
|
78
|
+
->(request) {
|
|
79
|
+
request.with_header('X-Proxied', 'true')
|
|
80
|
+
}
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
client.close
|
|
133
84
|
```
|
|
134
85
|
|
|
86
|
+
## Models
|
|
135
87
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
##### Java
|
|
139
|
-
|
|
140
|
-
```java
|
|
141
|
-
new ProxyClient("localhost", 9090).dumpToLogAsJava(
|
|
142
|
-
request()
|
|
143
|
-
.withMethod("POST")
|
|
144
|
-
.withPath("/login")
|
|
145
|
-
);
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
##### Ruby
|
|
149
|
-
|
|
150
|
-
```ruby
|
|
151
|
-
# Second argument is true to set output to Java; false to set output to default JSON (default)
|
|
152
|
-
ProxyClient.new('localhost', 9090).dump_log(request(:POST, '/login'), true)
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
## Complete Ruby DSL
|
|
156
|
-
The DSL is provided via the `MockServer::Model::DSL` module. Include this module in your code to make the DSL available.
|
|
88
|
+
All 25 domain model classes are available under the `MockServer` module:
|
|
157
89
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
90
|
+
- `Delay`, `Times`, `TimeToLive`
|
|
91
|
+
- `KeyToMultiValue`, `Body`, `SocketAddress`
|
|
92
|
+
- `HttpRequest`, `HttpResponse`, `HttpForward`, `HttpTemplate`
|
|
93
|
+
- `HttpClassCallback`, `HttpObjectCallback`, `HttpError`
|
|
94
|
+
- `HttpOverrideForwardedRequest`, `HttpRequestAndHttpResponse`
|
|
95
|
+
- `ConnectionOptions`
|
|
96
|
+
- `Expectation`, `ExpectationId`
|
|
97
|
+
- `OpenAPIDefinition`, `OpenAPIExpectation`
|
|
98
|
+
- `Verification`, `VerificationSequence`, `VerificationTimes`
|
|
99
|
+
- `Ports`
|
|
100
|
+
- `RequestDefinition` (alias for `HttpRequest`)
|
|
161
101
|
|
|
162
|
-
|
|
163
|
-
* **body**: Create a string body object (use in a response). Example: `body("unaccepted")`.
|
|
164
|
-
* **exact**: Create a body of type `STRING`. Example: `exact('{"reason": "unauthorized"}')`.
|
|
165
|
-
* **regex**: Create a body of type `REGEX`. Example: `regex('username[a-z]{4}')`.
|
|
166
|
-
* **xpath**: Used to create a body of type `XPATH`. Example: `xpath("/element[key = 'some_key' and value = 'some_value']")`.
|
|
167
|
-
* **parameterized**; Create a body to type `PARAMETERS`. Example `parameterized(parameter('someValue', 1, 2), parameter('otherValue', 4, 5))`.
|
|
168
|
-
|
|
169
|
-
Parameters
|
|
170
|
-
* **parameter**: Create a generic parameter. Example: `parameter('key', 'value1' , 'value2')`.
|
|
171
|
-
* **cookie**: Create a cookie (same as `parameter` above but exists as syntactic sugar). Example: `cookie('sessionId', 'sessionid1ldj')`.
|
|
172
|
-
* **header**: Create a header (same as `parameter` above but exists as syntactic sugar). Example: `header('Content-Type', 'application/json')`.
|
|
173
|
-
|
|
174
|
-
Forward
|
|
175
|
-
* **forward**: Create a forwarding response. If a block is passed, will configure forward response first and then return the configured object. Example: `forward {|f| f.scheme = 'HTTPS' }`.
|
|
176
|
-
* **http_forward**: Alias for `forward`.
|
|
177
|
-
|
|
178
|
-
Response
|
|
179
|
-
* **response**: Create a response object. If a block is passed, will configure response first and then return the configured response. Example: `response {|r| r.status_code = 201 }`.
|
|
180
|
-
* **http_response**: Alias for `response`.
|
|
181
|
-
|
|
182
|
-
Delay
|
|
183
|
-
* **delay_by**. Create a delay object in a response. Example : `delay_by(:MICROSECONDS, 20)`.
|
|
184
|
-
|
|
185
|
-
Times (used in Expectation)
|
|
186
|
-
* **times**: Create an 'times' object. If a block is passed, will configure the object first before returning it. Example: `times {|t| t.unlimited = false }`.
|
|
187
|
-
* **unlimited**: Create an object with unlimited repeats. Example: `unlimited()`. (No parameters).
|
|
188
|
-
* **once**. Create an object that repeats only once. Example: `once()`. (No parameters).
|
|
189
|
-
* **exactly**. Create an object that repeats exactly the number of times specified. Example: `exactly(2)`.
|
|
190
|
-
* **at_least**: Create an object that repeats at least the given number of times. (Use in verify). Example: `at_least(2)`.
|
|
191
|
-
|
|
192
|
-
Expectation (use in register)
|
|
193
|
-
* **expectation**: Create an expectation object. If a block is passed, will configure the object first before returning it. Example: `expectation {|e| e.request {|r| r.path = "index.html} }`.
|
|
194
|
-
Getter methods for `request`, `response` and `forward` methods will optionally accept a block. If block is passed object is configured before it is returned. The attribute `times` has conventional getter and setter methods.
|
|
195
|
-
|
|
196
|
-
## CLI
|
|
197
|
-
|
|
198
|
-
This gem comes with a command line interface which allow you to run the Mock Server calls from the command line. When this gem is installed, you will have the `mockserver` executable on your PATH. Type `mockserver --help` and you will get this output:
|
|
199
|
-
|
|
200
|
-
```bash
|
|
201
|
-
mockserver --help
|
|
202
|
-
|
|
203
|
-
Commands:
|
|
204
|
-
mockserver clear # Clears all stored mock request/responses from server.
|
|
205
|
-
mockserver dump_log # Dumps the matching request to the mock server logs.
|
|
206
|
-
mockserver help [COMMAND] # Describe available commands or one specific command
|
|
207
|
-
mockserver register # Register an expectation with the mock server.
|
|
208
|
-
mockserver reset # Resets the server clearing all data.
|
|
209
|
-
mockserver retrieve # Retrieve the list of requests that have been made to the mock/proxy server.
|
|
210
|
-
mockserver verify # Verify that a request has been made the specified number of times to the server.
|
|
211
|
-
|
|
212
|
-
Options:
|
|
213
|
-
-h, --host=HOST # The host for the MockServer client.
|
|
214
|
-
# Default: localhost
|
|
215
|
-
-p, --port=N # The port for the MockServer client.
|
|
216
|
-
# Default: 8080
|
|
217
|
-
-d, [--data=DATA] # A JSON or YAML file containing the request payload.
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
To get help for an individual command, e.g. `dump_log`, you would do:
|
|
221
|
-
|
|
222
|
-
```bash
|
|
223
|
-
mockserver --help dump_log
|
|
224
|
-
|
|
225
|
-
Usage:
|
|
226
|
-
mockserver dump_log
|
|
227
|
-
|
|
228
|
-
Options:
|
|
229
|
-
-j, [--java], [--no-java] # A switch to turn Java format for logs on/off.
|
|
230
|
-
-h, --host=HOST # The host for the MockServer client.
|
|
231
|
-
# Default: localhost
|
|
232
|
-
-p, --port=N # The port for the MockServer client.
|
|
233
|
-
# Default: 8080
|
|
234
|
-
-d, [--data=DATA] # A JSON or YAML file containing the request payload.
|
|
235
|
-
|
|
236
|
-
Dumps the matching request to the mock server logs.
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
Here is an example on how you would run the command:
|
|
240
|
-
|
|
241
|
-
```bash
|
|
242
|
-
mockserver dump_log -j true
|
|
243
|
-
|
|
244
|
-
Running with parameters:
|
|
245
|
-
host: localhost
|
|
246
|
-
port: 8080
|
|
247
|
-
java: true
|
|
248
|
-
|
|
249
|
-
[2014-06-21 09:23:32] DEBUG [MockServerClient] Sending dump log request to mockserver
|
|
250
|
-
[2014-06-21 09:23:32] DEBUG [MockServerClient] URL: /dumpToLog?type=java. Payload: {}
|
|
251
|
-
[2014-06-21 09:23:32] DEBUG [MockServerClient] Got dump to log response: 202
|
|
252
|
-
```
|
|
102
|
+
## License
|
|
253
103
|
|
|
254
|
-
|
|
104
|
+
Apache-2.0
|
|
255
105
|
|
|
256
|
-
|
|
106
|
+
## AI Assistant Integration
|
|
257
107
|
|
|
258
|
-
|
|
108
|
+
MockServer includes a built-in [MCP](https://modelcontextprotocol.io) (Model Context Protocol) server that enables AI coding assistants to create expectations, verify requests, and debug HTTP traffic programmatically.
|
|
259
109
|
|
|
260
|
-
|
|
110
|
+
- **MCP Endpoint:** `http://localhost:1080/mockserver/mcp`
|
|
111
|
+
- **AI Documentation:** [llms.txt](https://www.mock-server.com/llms.txt)
|
|
112
|
+
- **Setup Guide:** [AI Integration](https://www.mock-server.com/mock_server/ai_mcp_setup.html)
|