Line data Source code
1 : /*
2 : ___________________________________________
3 : | _ ___ _ |
4 : | | | |__ \ | | |
5 : | | |__ ) |__ _ __ _ ___ _ __ | |_ |
6 : | | '_ \ / // _` |/ _` |/ _ \ '_ \| __| | HTTP/2 AGENT FOR MOCK TESTING
7 : | | | | |/ /| (_| | (_| | __/ | | | |_ | Version 0.0.z
8 : | |_| |_|____\__,_|\__, |\___|_| |_|\__| | https://github.com/testillano/h2agent
9 : | __/ | |
10 : | |___/ |
11 : |___________________________________________|
12 :
13 : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
14 : SPDX-License-Identifier: MIT
15 : Copyright (c) 2021 Eduardo Ramos
16 :
17 : Permission is hereby granted, free of charge, to any person obtaining a copy
18 : of this software and associated documentation files (the "Software"), to deal
19 : in the Software without restriction, including without limitation the rights
20 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 : copies of the Software, and to permit persons to whom the Software is
22 : furnished to do so, subject to the following conditions:
23 :
24 : The above copyright notice and this permission notice shall be included in all
25 : copies or substantial portions of the Software.
26 :
27 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 : SOFTWARE.
34 : */
35 :
36 : #pragma once
37 :
38 : #include <vector>
39 : #include <cstdint>
40 : #include <regex>
41 :
42 : #include <nlohmann/json.hpp>
43 :
44 : #include <Map.hpp>
45 : #include <MockData.hpp>
46 : #include <MockClientEventsHistory.hpp>
47 : #include <MockClientEvent.hpp>
48 :
49 :
50 : namespace h2agent
51 : {
52 : namespace model
53 : {
54 :
55 :
56 : /**
57 : * This class stores the mock client events.
58 : *
59 : * This is useful to post-verify the internal data (content and schema validation) on testing system.
60 : * Also, the last request for an specific key, is used to know the state which is used to get the
61 : * corresponding provision information.
62 : */
63 : class MockClientData : public MockData
64 : {
65 : public:
66 334 : MockClientData() {};
67 333 : ~MockClientData() = default;
68 :
69 : /**
70 : * Loads event data
71 : *
72 : * @param dataKey Events key (client endpoint id, method & uri).
73 : *
74 : * @param clientProvisionId Client provision id responsible for the event
75 : * @param previousState Previous request state
76 : * @param state Request state
77 : * @param sendingTimestampUs Microseconds sending timestamp
78 : * @param receptionTimestampUs Microseconds reception timestamp
79 : * @param responseStatusCode Response status code
80 : * @param requestHeaders Request headers
81 : * @param responseHeaders Response headers
82 : *
83 : * @param requestBody Request body
84 : * @param responseBodyDataPart Response body
85 : * @param sendSeq Send sequence (1..N)
86 : * @param sequence test sequence (1..N)
87 : * @param requestDelayMs Request delay in milliseconds
88 : * @param timeoutMs Timeout in milliseconds
89 : *
90 : * @param historyEnabled Events complete history storage
91 : */
92 : void loadEvent(const DataKey &dataKey, const std::string &clientProvisionId, const std::string &previousState, const std::string &state, const std::chrono::microseconds &sendingTimestampUs, const std::chrono::microseconds &receptionTimestampUs, int responseStatusCode, const nghttp2::asio_http2::header_map &requestHeaders, const nghttp2::asio_http2::header_map &responseHeaders, const std::string &requestBody, DataPart &responseBodyDataPart, std::uint64_t sendSeq, std::int64_t sequence, unsigned int requestDelayMs, unsigned int timeoutMs, bool historyEnabled);
93 :
94 : /**
95 : * Removes a specific event identified by send sequence
96 : *
97 : * @param dataKey Events key (client endpoint id, method & uri).
98 : * @param sendSeq Send sequence identifying the event.
99 : *
100 : * @return Boolean about if something was deleted
101 : */
102 : bool removeEventBySendSeq(const DataKey &dataKey, std::uint64_t sendSeq);
103 :
104 : /**
105 : * Gets event matching a given send sequence within a data key
106 : *
107 : * @param dataKey Events key (client endpoint id, method & uri).
108 : * @param sendSeq Send sequence to match
109 : *
110 : * @return Mock event or nullptr if not found
111 : */
112 : std::shared_ptr<MockEvent> getEventBySendSeq(const DataKey &dataKey, std::uint64_t sendSeq);
113 :
114 : /**
115 : * Gets a client event addressed by a URI prefix (literal 'startsWith') instead of an exact URI key.
116 : *
117 : * Unlike the exact-key lookup (getEvent, which requires the full clientEndpointId+method+uri key),
118 : * this method selects ALL stored keys for the given clientEndpointId and method whose URI starts with
119 : * the provided literal prefix, merges their events into a single list ordered by sending timestamp,
120 : * and returns the one at the requested position. This is useful when part of the URI (always a trailing
121 : * suffix) is unknown in advance.
122 : *
123 : * The prefix match is a LITERAL string comparison (no regular expression): metacharacters such as
124 : * '|', '.', '{' or '[' carry no special meaning and are compared verbatim. The comparison is done
125 : * against the normalized stored URI (same normalization applied to exact-key addressing).
126 : *
127 : * @param clientEndpointId Exact client endpoint identifier to filter keys. Mandatory.
128 : * @param requestMethod Exact HTTP method to filter keys (e.g. 'GET'). Mandatory.
129 : * @param uriPrefix Literal URI prefix to match (keys whose URI starts with it). Mandatory.
130 : * @param eventNumber Position within the merged, timestamp-ordered set of matching events (1..N);
131 : * '-1' selects the latest (most recent sending). Value '0' is not accepted (nullptr returned).
132 : *
133 : * @return Mock event or nullptr if no match is found or the position is out of range.
134 : */
135 : std::shared_ptr<MockEvent> getEventByUriStartsWith(const std::string &clientEndpointId, const std::string &requestMethod, const std::string &uriPrefix, const std::string &eventNumber) const;
136 :
137 : /**
138 : * Gets chronologically ordered sequence of events (direction + method + uri + timestamps)
139 : *
140 : * @param fromTimestampUs Minimum timestamp filter (0 = no filter)
141 : * @param toTimestampUs Maximum timestamp filter (0 = no filter)
142 : * @param requestMethod Filter by exact method (empty = no filter)
143 : * @param uriRegex Filter by URI regex (nullptr = no filter)
144 : *
145 : * @return JSON array sorted by timestamp, interleaving send/recv
146 : */
147 : std::string getSequence(std::uint64_t fromTimestampUs, std::uint64_t toTimestampUs, const std::string &requestMethod, const std::regex *uriRegex) const;
148 : };
149 :
150 : }
151 : }
152 :
|