Skip to content

Commit 19b1066

Browse files
committed
feat: allow setting of pact specification version to output on mock server creation
1 parent 87316f4 commit 19b1066

5 files changed

Lines changed: 51 additions & 26 deletions

File tree

pact_mock_server_cli/README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,17 @@ Creates a new mock server from a pact file
120120
Usage: pact_mock_server_cli create [OPTIONS] --file <file>
121121

122122
Options:
123-
-f, --file <file> the pact file to define the mock server
124-
--help Print help and exit
125-
-c, --cors-preflight Handle CORS pre-flight requests
126-
-v, --version Print version information and exit
127-
-p, --port <port> port the master mock server runs on (defaults to 8080)
128-
--tls Enable TLS with the mock server (will use a self-signed certificate)
129-
-h, --host <host> hostname the master mock server runs on (defaults to localhost)
130-
-l, --loglevel <loglevel> Log level for mock servers to write to the log file (defaults to info) [possible values: error, warn, info, debug, trace, none]
131-
--no-term-log Turns off using terminal ANSI escape codes
132-
--no-file-log Do not log to an output file
123+
-f, --file <file> the pact file to define the mock server
124+
--help Print help and exit
125+
-c, --cors-preflight Handle CORS pre-flight requests
126+
-v, --version Print version information and exit
127+
-p, --port <port> port the master mock server runs on (defaults to 8080)
128+
--specification <specification> The Pact specification version to use (defaults to V4)
129+
-h, --host <host> hostname the master mock server runs on (defaults to localhost)
130+
--tls Enable TLS with the mock server (will use a self-signed certificate)
131+
-l, --loglevel <loglevel> Log level for mock servers to write to the log file (defaults to info) [possible values: error, warn, info, debug, trace, none]
132+
--no-term-log Turns off using terminal ANSI escape codes
133+
--no-file-log Do not log to an output file
133134

134135

135136
```

pact_mock_server_cli/src/create_mock.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ pub async fn create_mock_server(host: &str, port: u16, matches: &ArgMatches, usa
1515

1616
match RequestResponsePact::read_pact(Path::new(file)) {
1717
Ok(ref pact) => {
18-
let mut args = vec![];
18+
let mut args = Vec::<String>::new();
1919
if matches.get_flag("cors") {
2020
info!("Setting mock server to handle CORS pre-flight requests");
21-
args.push("cors=true");
21+
args.push("cors=true".to_string());
22+
}
23+
if let Some(specification) = matches.get_one::<String>("specification") {
24+
info!("Setting mock server to use pact specification {}", specification);
25+
let spec_arg = format!("specification={}", specification);
26+
args.push(spec_arg);
2227
}
2328
if matches.get_flag("tls") {
2429
info!("Setting mock server to use TLS");
25-
args.push("tls=true");
30+
args.push("tls=true".to_string());
2631
}
2732
let url = if args.is_empty() {
2833
format!("http://{}:{}/", host, port)

pact_mock_server_cli/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,12 @@ pub fn setup_args() -> Command {
208208
.short('c')
209209
.long("cors-preflight")
210210
.action(ArgAction::SetTrue)
211-
.help("Handle CORS pre-flight requests"));
211+
.help("Handle CORS pre-flight requests"))
212+
.arg(Arg::new("specification")
213+
.long("specification")
214+
.action(ArgAction::Set)
215+
.num_args(1)
216+
.help("The Pact specification version to use (defaults to V4)"));
212217

213218
#[cfg(feature = "tls")]
214219
{

pact_mock_server_cli/src/server.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,23 @@ fn start_provider(context: &mut WebmachineContext) -> Result<bool, u16> {
9393
})?;
9494
debug!("Loaded pact = {:?}", pact);
9595
let mock_server_id = generate_hexadecimal(8);
96-
let config = MockServerConfig {
96+
let pact_specification = match context.request.query.get("specification") {
97+
Some(specs) => {
98+
match specs.first() {
99+
Some(spec) if !spec.is_empty() => Some(spec.clone()),
100+
_ => None,
101+
}
102+
},
103+
None => None
104+
};
105+
106+
let mut config = MockServerConfig {
97107
cors_preflight: query_param_set(context, "cors"),
98108
.. MockServerConfig::default()
99-
};
109+
};
110+
if let Some(spec) = pact_specification {
111+
config.pact_specification = spec.into();
112+
}
100113
debug!("Mock server config = {:?}", config);
101114

102115
#[allow(unused_assignments)]

pact_mock_server_cli/tests/cmd/create.stdout

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ Creates a new mock server from a pact file
33
Usage: pact_mock_server_cli create [OPTIONS] --file <file>
44

55
Options:
6-
-f, --file <file> the pact file to define the mock server
7-
--help Print help and exit
8-
-c, --cors-preflight Handle CORS pre-flight requests
9-
-v, --version Print version information and exit
10-
-p, --port <port> port the master mock server runs on (defaults to 8080)
11-
--tls Enable TLS with the mock server (will use a self-signed certificate)
12-
-h, --host <host> hostname the master mock server runs on (defaults to localhost)
13-
-l, --loglevel <loglevel> Log level for mock servers to write to the log file (defaults to info) [possible values: error, warn, info, debug, trace, none]
14-
--no-term-log Turns off using terminal ANSI escape codes
15-
--no-file-log Do not log to an output file
6+
-f, --file <file> the pact file to define the mock server
7+
--help Print help and exit
8+
-c, --cors-preflight Handle CORS pre-flight requests
9+
-v, --version Print version information and exit
10+
-p, --port <port> port the master mock server runs on (defaults to 8080)
11+
--specification <specification> The Pact specification version to use (defaults to V4)
12+
-h, --host <host> hostname the master mock server runs on (defaults to localhost)
13+
--tls Enable TLS with the mock server (will use a self-signed certificate)
14+
-l, --loglevel <loglevel> Log level for mock servers to write to the log file (defaults to info) [possible values: error, warn, info, debug, trace, none]
15+
--no-term-log Turns off using terminal ANSI escape codes
16+
--no-file-log Do not log to an output file
1617

0 commit comments

Comments
 (0)