Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
Client.hpp
Go to the documentation of this file.
1#pragma once
5#include <orbit/http/json.hpp>
6#include <chrono>
7#include <map>
8#include <memory>
9#include <optional>
10#include <string>
11#include <string_view>
12#include <unordered_map>
13#include <utility>
14#include <vector>
15
16namespace orbit::server { class App; }
17namespace orbit::routing { class Router; }
18
19namespace orbit::testing {
20
24struct Response {
25 int status = 0;
27 std::map<std::string, std::string> headers;
29 std::string body;
31 std::vector<std::string> chunks;
32 std::vector<http::Cookie> cookies;
35 bool complete = false;
36
38 std::optional<std::string> header(std::string_view name) const;
41};
42
43class Client;
44
49public:
50 RequestBuilder& header(std::string name, std::string value);
52 RequestBuilder& body(std::string body, std::string content_type = "text/plain");
56 RequestBuilder& cookie(std::string name, std::string value);
58 RequestBuilder& client_ip(std::string ip);
59
66 Response send(std::chrono::milliseconds timeout = std::chrono::seconds(5));
67
68private:
69 friend class Client;
70 RequestBuilder(Client& client, http::HttpMethod method, std::string target);
71
74 std::string target_;
75 std::vector<std::pair<std::string, std::string>> headers_;
76 std::string body_;
77 std::unordered_map<std::string, std::string> cookies_;
78 std::string client_ip_ = "127.0.0.1";
79};
80
105class Client {
106public:
107 explicit Client(server::App& app, size_t worker_threads = 2);
108 explicit Client(const routing::Router& router, size_t worker_threads = 2);
110 Client(const Client&) = delete;
111 Client& operator=(const Client&) = delete;
112
113 RequestBuilder get(std::string target) { return {*this, http::HttpMethod::GET, std::move(target)}; }
114 RequestBuilder post(std::string target) { return {*this, http::HttpMethod::POST, std::move(target)}; }
115 RequestBuilder put(std::string target) { return {*this, http::HttpMethod::PUT, std::move(target)}; }
116 RequestBuilder patch(std::string target) { return {*this, http::HttpMethod::PATCH, std::move(target)}; }
117 RequestBuilder del(std::string target); // out of line: windows.h may define DELETE as a macro
118 RequestBuilder options(std::string target) { return {*this, http::HttpMethod::OPTIONS, std::move(target)}; }
119 RequestBuilder head(std::string target) { return {*this, http::HttpMethod::HEAD, std::move(target)}; }
120 RequestBuilder request(http::HttpMethod method, std::string target) { return {*this, method, std::move(target)}; }
121
123 std::map<std::string, std::string>& cookies() { return cookies_; }
124
125private:
126 friend class RequestBuilder;
127 struct Runtime; // thread pool and event loop for handlers
128
130 std::unique_ptr<Runtime> runtime_;
131 std::map<std::string, std::string> cookies_;
132};
133
134} // namespace orbit::testing
a class to store JSON values
Definition json.hpp:19401
Manages routing of HTTP requests to their appropriate handlers.
Definition Router.hpp:46
The main application class for the Orbit Framework.
Definition App.hpp:36
Calls an application's routes in-process, without listen() or sockets: for fast, port-free tests.
Definition Client.hpp:105
RequestBuilder options(std::string target)
Definition Client.hpp:118
Client(server::App &app, size_t worker_threads=2)
RequestBuilder put(std::string target)
Definition Client.hpp:115
Client(const routing::Router &router, size_t worker_threads=2)
RequestBuilder head(std::string target)
Definition Client.hpp:119
std::map< std::string, std::string > cookies_
Definition Client.hpp:131
Client & operator=(const Client &)=delete
RequestBuilder patch(std::string target)
Definition Client.hpp:116
RequestBuilder get(std::string target)
Definition Client.hpp:113
Client(const Client &)=delete
const routing::Router & router_
Definition Client.hpp:129
RequestBuilder request(http::HttpMethod method, std::string target)
Definition Client.hpp:120
RequestBuilder post(std::string target)
Definition Client.hpp:114
std::map< std::string, std::string > & cookies()
The cookie jar: name -> value, updated from every response.
Definition Client.hpp:123
RequestBuilder del(std::string target)
std::unique_ptr< Runtime > runtime_
Definition Client.hpp:130
One request, built fluently and run with send().
Definition Client.hpp:48
RequestBuilder & body(std::string body, std::string content_type="text/plain")
The body and, unless content_type is empty, its Content-Type.
std::string body_
Definition Client.hpp:76
RequestBuilder & client_ip(std::string ip)
The client address the application sees (default 127.0.0.1).
std::unordered_map< std::string, std::string > cookies_
Definition Client.hpp:77
Client & client_
Definition Client.hpp:72
RequestBuilder(Client &client, http::HttpMethod method, std::string target)
RequestBuilder & header(std::string name, std::string value)
RequestBuilder & json(const nlohmann::json &value)
A JSON body (Content-Type: application/json).
std::string target_
Definition Client.hpp:74
Response send(std::chrono::milliseconds timeout=std::chrono::seconds(5))
Runs the request through the application and waits until the response is complete,...
http::HttpMethod method_
Definition Client.hpp:73
std::string client_ip_
Definition Client.hpp:78
std::vector< std::pair< std::string, std::string > > headers_
Definition Client.hpp:75
RequestBuilder & cookie(std::string name, std::string value)
A cookie for this request only, on top of the client's cookie jar.
HttpMethod
Represents standard HTTP methods.
Definition HttpRequest.hpp:28
Definition legacy_namespaces.hpp:24
Definition Http2Session.hpp:18
Definition Client.hpp:19
server::App App
Definition orbit.hpp:91
What the application answered, as a test sees it.
Definition Client.hpp:24
std::optional< std::string > header(std::string_view name) const
A header, by case-insensitive name.
std::string body
The whole body: the response body, or every chunk of a streamed one.
Definition Client.hpp:29
bool complete
Definition Client.hpp:35
std::vector< std::string > chunks
A streamed response's chunks (write_chunk() and SSE events), in order.
Definition Client.hpp:31
int status
Definition Client.hpp:25
std::map< std::string, std::string > headers
Header names as the application set them; look them up with header().
Definition Client.hpp:27
std::vector< http::Cookie > cookies
Definition Client.hpp:32
nlohmann::json json() const
The body parsed as JSON (null if it is not JSON).