]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright The OpenTelemetry Authors |
2 | // SPDX-License-Identifier: Apache-2.0 | |
3 | ||
4 | #pragma once | |
5 | ||
6 | #ifdef ENABLE_TEST | |
7 | # include "opentelemetry/ext/http/client/http_client.h" | |
8 | # include "opentelemetry/ext/http/common/url_parser.h" | |
9 | # include "opentelemetry/version.h" | |
10 | ||
11 | # include <map> | |
12 | # include <string> | |
13 | # include <vector> | |
14 | ||
15 | # include <gtest/gtest.h> | |
16 | # include "gmock/gmock.h" | |
17 | ||
18 | using namespace testing; | |
19 | OPENTELEMETRY_BEGIN_NAMESPACE | |
20 | namespace ext | |
21 | { | |
22 | namespace http | |
23 | { | |
24 | namespace client | |
25 | { | |
26 | namespace nosend | |
27 | { | |
28 | ||
29 | const opentelemetry::ext::http::client::StatusCode Http_Ok = 200; | |
30 | ||
31 | class Request : public opentelemetry::ext::http::client::Request | |
32 | { | |
33 | public: | |
34 | Request() : method_(opentelemetry::ext::http::client::Method::Get), uri_("/") {} | |
35 | ||
36 | void SetMethod(opentelemetry::ext::http::client::Method method) noexcept override | |
37 | { | |
38 | method_ = method; | |
39 | } | |
40 | ||
41 | void SetBody(opentelemetry::ext::http::client::Body &body) noexcept override | |
42 | { | |
43 | body_ = std::move(body); | |
44 | } | |
45 | ||
46 | void AddHeader(nostd::string_view name, nostd::string_view value) noexcept override | |
47 | { | |
48 | headers_.insert(std::pair<std::string, std::string>(static_cast<std::string>(name), | |
49 | static_cast<std::string>(value))); | |
50 | } | |
51 | ||
52 | void ReplaceHeader(nostd::string_view name, nostd::string_view value) noexcept override; | |
53 | ||
54 | virtual void SetUri(nostd::string_view uri) noexcept override | |
55 | { | |
56 | uri_ = static_cast<std::string>(uri); | |
57 | } | |
58 | ||
59 | void SetTimeoutMs(std::chrono::milliseconds timeout_ms) noexcept override | |
60 | { | |
61 | timeout_ms_ = timeout_ms; | |
62 | } | |
63 | ||
64 | public: | |
65 | opentelemetry::ext::http::client::Method method_; | |
66 | opentelemetry::ext::http::client::Body body_; | |
67 | opentelemetry::ext::http::client::Headers headers_; | |
68 | std::string uri_; | |
69 | std::chrono::milliseconds timeout_ms_{5000}; // ms | |
70 | }; | |
71 | ||
72 | class Response : public opentelemetry::ext::http::client::Response | |
73 | { | |
74 | public: | |
75 | Response() : status_code_(Http_Ok) {} | |
76 | ||
77 | virtual const opentelemetry::ext::http::client::Body &GetBody() const noexcept override | |
78 | { | |
79 | return body_; | |
80 | } | |
81 | ||
82 | virtual bool ForEachHeader( | |
83 | nostd::function_ref<bool(nostd::string_view name, nostd::string_view value)> callable) | |
84 | const noexcept override; | |
85 | ||
86 | virtual bool ForEachHeader( | |
87 | const nostd::string_view &name, | |
88 | nostd::function_ref<bool(nostd::string_view name, nostd::string_view value)> callable) | |
89 | const noexcept override; | |
90 | ||
91 | virtual opentelemetry::ext::http::client::StatusCode GetStatusCode() const noexcept override | |
92 | { | |
93 | return status_code_; | |
94 | } | |
95 | ||
96 | public: | |
97 | Headers headers_; | |
98 | opentelemetry::ext::http::client::Body body_; | |
99 | opentelemetry::ext::http::client::StatusCode status_code_; | |
100 | }; | |
101 | ||
102 | class HttpClient; | |
103 | ||
104 | class Session : public opentelemetry::ext::http::client::Session | |
105 | { | |
106 | public: | |
107 | Session(HttpClient &http_client, | |
108 | std::string scheme = "http", | |
109 | const std::string &host = "", | |
110 | uint16_t port = 80) | |
111 | : http_client_(http_client), is_session_active_(false) | |
112 | { | |
113 | host_ = scheme + "://" + host + ":" + std::to_string(port) + "/"; | |
114 | } | |
115 | ||
116 | std::shared_ptr<opentelemetry::ext::http::client::Request> CreateRequest() noexcept override | |
117 | { | |
118 | http_request_.reset(new Request()); | |
119 | return http_request_; | |
120 | } | |
121 | ||
122 | MOCK_METHOD(void, | |
123 | SendRequest, | |
124 | (opentelemetry::ext::http::client::EventHandler &), | |
125 | (noexcept, override)); | |
126 | ||
127 | virtual bool CancelSession() noexcept override; | |
128 | ||
129 | virtual bool FinishSession() noexcept override; | |
130 | ||
131 | virtual bool IsSessionActive() noexcept override { return is_session_active_; } | |
132 | ||
133 | void SetId(uint64_t session_id) { session_id_ = session_id; } | |
134 | ||
135 | /** | |
136 | * Returns the base URI. | |
137 | * @return the base URI as a string consisting of scheme, host and port. | |
138 | */ | |
139 | const std::string &GetBaseUri() const { return host_; } | |
140 | ||
141 | std::shared_ptr<Request> GetRequest() { return http_request_; } | |
142 | ||
143 | private: | |
144 | std::shared_ptr<Request> http_request_; | |
145 | std::string host_; | |
146 | uint64_t session_id_; | |
147 | HttpClient &http_client_; | |
148 | bool is_session_active_; | |
149 | }; | |
150 | ||
151 | class HttpClient : public opentelemetry::ext::http::client::HttpClient | |
152 | { | |
153 | public: | |
154 | HttpClient() { session_ = std::shared_ptr<Session>{new Session(*this)}; } | |
155 | ||
156 | std::shared_ptr<opentelemetry::ext::http::client::Session> CreateSession( | |
157 | nostd::string_view) noexcept override | |
158 | { | |
159 | return session_; | |
160 | } | |
161 | ||
162 | bool CancelAllSessions() noexcept override | |
163 | { | |
164 | session_->CancelSession(); | |
165 | return true; | |
166 | } | |
167 | ||
168 | bool FinishAllSessions() noexcept override | |
169 | { | |
170 | session_->FinishSession(); | |
171 | return true; | |
172 | } | |
173 | ||
174 | void CleanupSession(uint64_t session_id) {} | |
175 | ||
176 | std::shared_ptr<Session> session_; | |
177 | }; | |
178 | ||
179 | } // namespace nosend | |
180 | } // namespace client | |
181 | } // namespace http | |
182 | } // namespace ext | |
183 | OPENTELEMETRY_END_NAMESPACE | |
184 | #endif |