-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
lib.rs
115 lines (103 loc) Β· 4.59 KB
/
lib.rs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
tonic::include_proto!("io.pact.plugin");
#[cfg(test)]
mod tests {
use std::path::Path;
use std::collections::HashSet;
use expectest::prelude::*;
use maplit::hashset;
use pact_consumer::prelude::*;
use prost::Message;
use serde_json::json;
use pact_plugin_driver::utils::proto_value_to_string;
use super::*;
#[test_log::test(tokio::test(flavor = "multi_thread", worker_threads = 2))]
async fn test_proto_client() {
let mut pact_builder = PactBuilderAsync::new_v4("protobuf-consumer-rust", "protobuf-provider");
let proto_service = pact_builder
.using_plugin("protobuf", None).await
.message_interaction("init plugin message", |mut i| async move {
let proto_file = Path::new("../../../proto/plugin.proto")
.canonicalize().unwrap().to_string_lossy().to_string();
i.contents_from(json!({
"pact:proto": proto_file,
"pact:message-type": "InitPluginRequest",
"pact:content-type": "application/protobuf",
"implementation": "notEmpty('pact-jvm-driver')",
"version": "matching(semver, '0.0.0')"
})).await;
i
})
.await;
for message in proto_service.messages() {
let bytes = message.contents.contents.value().unwrap();
let request = InitPluginRequest::decode(bytes).unwrap();
expect!(request.implementation).to(be_equal_to("pact-jvm-driver"));
expect!(request.version).to(be_equal_to("0.0.0"));
}
}
#[test_log::test(tokio::test(flavor = "multi_thread", worker_threads = 2))]
async fn proto_with_message_fields() {
let mut pact_builder = PactBuilderAsync::new_v4("protobuf-consumer-rust", "protobuf-provider");
let proto_service = pact_builder
.using_plugin("protobuf", None).await
.message_interaction("Configure Interaction Response", |mut i| async move {
let proto_file = Path::new("../../../proto/plugin.proto")
.canonicalize().unwrap().to_string_lossy().to_string();
i.contents_from(json!({
"pact:proto": proto_file,
"pact:message-type": "InteractionResponse",
"pact:content-type": "application/protobuf",
"contents": {
"contentType": "notEmpty('application/json')",
"content": "matching(contentType, 'application/json', '{}')",
"contentTypeHint": "matching(equalTo, 'TEXT')"
},
"rules": {
"pact:match": "eachKey(matching(regex, '\\$(\\.\\w+)+', '$.test.one')), eachValue(matching(type, null))",
"$.test.one": {
"rule": {
"pact:match": "eachValue(matching($'items'))",
"items": {
"type": "notEmpty('regex')"
}
}
}
},
"generators": {
"$.test.one": {
"type": "notEmpty('DateTime')",
"values": {
"format": "matching(equalTo, 'YYYY-MM-DD')"
}
},
"$.test.two": {
"type": "notEmpty('DateTime')",
"values": {
"format": "matching(equalTo, 'YYYY-MM-DD')"
}
}
}
})).await;
i
})
.await;
for message in proto_service.messages() {
let response = InteractionResponse::decode(message.contents.contents.value().unwrap()).unwrap();
let contents = response.contents.unwrap();
let content = contents.clone().content.unwrap();
expect!(&contents.content_type).to(be_equal_to("application/json"));
expect!(content).to(be_equal_to("{}".bytes().collect::<Vec<u8>>()));
expect!(contents.content_type_hint()).to(be_equal_to(body::ContentTypeHint::Text));
expect!(response.generators.len()).to(be_equal_to(2));
expect!(response.generators.keys().map(|k| k.as_str()).collect::<HashSet<&str>>()).to( be_equal_to(hashset!["$.test.one", "$.test.two"]));
expect!(response.generators.get("$.test.one").map(|v| v.r#type.as_str())).to( be_some().value("DateTime"));
expect!(response.generators.get("$.test.one").map(|v| v.values.as_ref().map(|vals| vals.fields.get("format").map(|f| proto_value_to_string(f))))
.flatten().flatten().flatten()).to( be_some().value("YYYY-MM-DD"));
expect!(response.rules.len()).to(be_equal_to(1));
expect!(response.rules.keys().collect::<Vec<&String>>()).to(be_equal_to(vec!["$.test.one"]));
let matching_rules = response.rules.get("$.test.one").unwrap();
expect!(matching_rules.rule.len()).to(be_equal_to(1));
expect!(matching_rules.rule.get(0).map(|rule| rule.r#type.as_str())).to(be_some().value("regex"));
}
}
}