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 <MockServerEventsHistory.hpp>
47 : #include <MockServerEvent.hpp>
48 :
49 :
50 : namespace h2agent
51 : {
52 : namespace model
53 : {
54 :
55 :
56 : /**
57 : * This class stores the mock server 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 MockServerData : public MockData
64 : {
65 : std::shared_ptr<MockServerEvent> last_loaded_event_{};
66 :
67 : public:
68 310 : MockServerData() {};
69 310 : ~MockServerData() = default;
70 :
71 : /**
72 : * Loads event data
73 : *
74 : * @param dataKey Events key (method & uri).
75 : *
76 : * @param previousState Previous request state
77 : * @param state Request state
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 requestBodyDataPart Request body
84 : * @param responseBody Response body
85 : * @param serverSequence Server sequence (1..N)
86 : * @param responseDelayMs Response delay in milliseconds
87 : *
88 : * @param historyEnabled Events complete history storage
89 : * @param virtualOriginComingFromMethod Marks event as virtual one, adding a field with the origin method which caused it. Non-virtual by default (empty parameter).
90 : * @param virtualOriginComingFromUri Marks event as virtual one, adding a field with the origin uri which caused it. Non-virtual by default (empty parameter).
91 : */
92 : void loadEvent(const DataKey &dataKey, const std::string &previousState, const std::string &state, const std::chrono::microseconds &receptionTimestampUs, unsigned int responseStatusCode, const nghttp2::asio_http2::header_map &requestHeaders, const nghttp2::asio_http2::header_map &responseHeaders, DataPart &requestBodyDataPart, const std::string &responseBody, std::uint64_t serverSequence, unsigned int responseDelayMs, bool historyEnabled, const std::string &virtualOriginComingFromMethod = "", const std::string &virtualOriginComingFromUri = "");
93 :
94 : /**
95 : * Removes event matching a given receive sequence within a data key
96 : *
97 : * @param dataKey Events key (method & uri).
98 : * @param recvSeq Receive sequence to match
99 : *
100 : * @return Boolean about if something was deleted
101 : */
102 : bool removeEventByRecvSeq(const DataKey &dataKey, std::uint64_t recvSeq);
103 :
104 : /**
105 : * Gets event matching a given receive sequence within a data key
106 : *
107 : * @param dataKey Events key (method & uri).
108 : * @param recvSeq Receive sequence to match
109 : *
110 : * @return Mock event or nullptr if not found
111 : */
112 : std::shared_ptr<MockEvent> getEventByRecvSeq(const DataKey &dataKey, std::uint64_t recvSeq);
113 :
114 : /**
115 : * Gets the last loaded event (from the most recent loadEvent call)
116 : *
117 : * @return Last loaded server event
118 : */
119 0 : std::shared_ptr<MockServerEvent> getLastLoadedEvent() const {
120 0 : return last_loaded_event_;
121 : }
122 :
123 : /**
124 : * Gets an event addressed by a URI prefix (literal 'startsWith') instead of an exact URI key.
125 : *
126 : * Unlike the exact-key lookup (getEvent, which requires the full method+uri key), this method
127 : * selects ALL stored keys for the given method whose URI starts with the provided literal prefix,
128 : * merges their events into a single list ordered by reception timestamp, and returns the one at
129 : * the requested position. This is useful when part of the URI (always a trailing suffix) is unknown
130 : * in advance (e.g. a server-minted identifier appended to a known path prefix).
131 : *
132 : * The prefix match is a LITERAL string comparison (no regular expression): metacharacters such as
133 : * '|', '.', '{' or '[' carry no special meaning and are compared verbatim. The comparison is done
134 : * against the normalized stored URI (same normalization applied to exact-key addressing).
135 : *
136 : * @param requestMethod Exact HTTP method to filter keys (e.g. 'POST'). Mandatory.
137 : * @param uriPrefix Literal URI prefix to match (keys whose URI starts with it). Mandatory.
138 : * @param eventNumber Position within the merged, timestamp-ordered set of matching events (1..N);
139 : * '-1' selects the latest (most recent reception). Value '0' is not accepted (nullptr returned).
140 : *
141 : * @return Mock event or nullptr if no match is found or the position is out of range.
142 : */
143 : std::shared_ptr<MockEvent> getEventByUriStartsWith(const std::string &requestMethod, const std::string &uriPrefix, const std::string &eventNumber) const;
144 :
145 : /**
146 : * Gets chronologically ordered sequence of events (method + uri + timestamps)
147 : *
148 : * @param fromTimestampUs Minimum reception timestamp filter (0 = no filter)
149 : * @param toTimestampUs Maximum reception timestamp filter (0 = no filter)
150 : * @param requestMethod Filter by exact method (empty = no filter)
151 : * @param uriRegex Filter by URI regex (nullptr = no filter)
152 : *
153 : * @return JSON array sorted by receptionTimestampUs
154 : */
155 : std::string getSequence(std::uint64_t fromTimestampUs, std::uint64_t toTimestampUs, const std::string &requestMethod, const std::regex *uriRegex) const;
156 : };
157 :
158 : }
159 : }
160 :
|