Pact C++ Consumer DSL  0.0.7
DSL to support writing consumer tests in C++
consumer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <unordered_map>
5 #include <vector>
6 #include <pact.h>
7 #include <filesystem>
8 #include <optional>
9 #include <functional>
10 #include "matchers.h"
11 
12 namespace pact_consumer {
13 
17  void init();
18 
19  class Interaction;
20 
25  public:
26  MockServerHandle(PactHandle);
28 
32  bool started_ok() const;
33 
37  std::string get_url() const;
38 
42  int32_t get_port() const;
43 
44  private:
45  int32_t port;
46  };
47 
52  Mismatches = (1u << 0),
53  UserCodeFailed = (1u << 1),
54  PactFileError = (1u << 2),
55  MockServerFailed = (1u << 3)
56  };
57 
62  public:
64 
68  void add_state(TestResultState state);
69 
73  void add_state(TestResultState state, std::string message);
74 
78  void add_state(TestResultState state, std::string message, std::string ex);
79 
83  bool is_ok() const;
84 
88  void display_errors();
89 
90  private:
91  unsigned int status = 0;
92  std::vector<std::string> messages;
93  std::optional<std::string> ex;
94  };
95 
99  class Pact {
100  public:
104  Pact(const char* consumer, const char* provider);
105 
109  Interaction given(const char* provider_state) const;
110 
114  Interaction given(const char* provider_state, const std::unordered_map<std::string, std::string>& parameters) const;
115 
119  Interaction uponReceiving(const char* description) const;
120 
125  PactTestResult run_test(std::function<bool(const MockServerHandle*)> callback) const;
126 
127  PactHandle pact;
128 
132  std::string pact_directory;
133 
134  private:
135  std::string consumer;
136  std::string provider;
137  };
138 
142  class Interaction {
143  public:
144  Interaction(const Pact* parent, const char *description);
145 
149  Interaction given(const char* provider_state) const;
150 
154  Interaction given(const char* provider_state, const std::unordered_map<std::string, std::string>& parameters) const;
155 
159  Interaction uponReceiving(const char* description) const;
160 
164  Interaction withRequest(const char* method, const char* path) const;
165 
169  Interaction withQuery(const std::unordered_map<std::string, std::vector<std::string>>& query) const;
170 
174  Interaction withHeaders(const std::unordered_map<std::string, std::vector<std::string>>& headers) const;
175 
179  Interaction withBody(const std::string& body, const std::string& content_type) const;
180 
185 
190  Interaction withBinaryFile(const std::string& content_type, const std::filesystem::path& example_file) const;
191 
196  Interaction withMultipartFileUpload(const std::string& part_name, const std::string& content_type, const std::filesystem::path& example_file) const;
197 
201  Interaction willRespondWith(size_t status) const;
202 
206  Interaction withResponseHeaders(const std::unordered_map<std::string, std::vector<std::string>>& headers) const;
207 
211  Interaction withResponseBody(const std::string& body, const std::string& content_type) const;
212 
217 
222  Interaction withResponseBinaryFile(const std::string& content_type, const std::filesystem::path& example_file) const;
223 
228  Interaction withResponseMultipartFileUpload(const std::string& part_name, const std::string& content_type, const std::filesystem::path& example_file) const;
229 
230  InteractionHandle interaction;
231 
232  private:
233  std::string description;
234  const Pact* pact;
235  };
236 }
Definition: consumer.h:142
Interaction withResponseHeaders(const std::unordered_map< std::string, std::vector< std::string >> &headers) const
Definition: consumer.cpp:175
Interaction withResponseMultipartFileUpload(const std::string &part_name, const std::string &content_type, const std::filesystem::path &example_file) const
Definition: consumer.cpp:210
Interaction withQuery(const std::unordered_map< std::string, std::vector< std::string >> &query) const
Definition: consumer.cpp:116
InteractionHandle interaction
Definition: consumer.h:230
Interaction withResponseBody(const std::string &body, const std::string &content_type) const
Definition: consumer.cpp:184
Interaction uponReceiving(const char *description) const
Definition: consumer.cpp:94
Interaction withResponseBinaryFile(const std::string &content_type, const std::filesystem::path &example_file) const
Definition: consumer.cpp:196
Interaction withBinaryFile(const std::string &content_type, const std::filesystem::path &example_file) const
Definition: consumer.cpp:145
Interaction withHeaders(const std::unordered_map< std::string, std::vector< std::string >> &headers) const
Definition: consumer.cpp:125
Interaction withResponseJsonBody(pact_consumer::matchers::IMatcher::Ptr body) const
Definition: consumer.cpp:190
Interaction withMultipartFileUpload(const std::string &part_name, const std::string &content_type, const std::filesystem::path &example_file) const
Definition: consumer.cpp:159
Interaction given(const char *provider_state) const
Definition: consumer.cpp:99
Interaction withJsonBody(pact_consumer::matchers::IMatcher::Ptr body) const
Definition: consumer.cpp:139
Interaction withRequest(const char *method, const char *path) const
Definition: consumer.cpp:111
Interaction willRespondWith(size_t status) const
Definition: consumer.cpp:170
Interaction withBody(const std::string &body, const std::string &content_type) const
Definition: consumer.cpp:134
Interaction(const Pact *parent, const char *description)
Definition: consumer.cpp:85
Definition: consumer.h:24
MockServerHandle(PactHandle)
Definition: consumer.cpp:225
~MockServerHandle()
Definition: consumer.cpp:229
bool started_ok() const
Definition: consumer.cpp:233
std::string get_url() const
Definition: consumer.cpp:237
int32_t get_port() const
Definition: consumer.cpp:243
Definition: consumer.h:61
void add_state(TestResultState state)
Definition: consumer.cpp:255
void display_errors()
Definition: consumer.cpp:274
PactTestResult()
Definition: consumer.cpp:251
bool is_ok() const
Definition: consumer.cpp:270
Definition: consumer.h:99
Interaction uponReceiving(const char *description) const
Definition: consumer.cpp:26
std::string pact_directory
Definition: consumer.h:132
PactHandle pact
Definition: consumer.h:127
Interaction given(const char *provider_state) const
Definition: consumer.cpp:30
Pact(const char *consumer, const char *provider)
Definition: consumer.cpp:20
PactTestResult run_test(std::function< bool(const MockServerHandle *)> callback) const
Definition: consumer.cpp:38
std::shared_ptr< IMatcher > Ptr
Definition: matchers.h:15
Definition: consumer.cpp:11
TestResultState
Definition: consumer.h:51
@ PactFileError
Definition: consumer.h:54
@ Mismatches
Definition: consumer.h:52
@ MockServerFailed
Definition: consumer.h:55
@ UserCodeFailed
Definition: consumer.h:53
void init()
Definition: consumer.cpp:12