ipfs-chromium
Loading...
Searching...
No Matches
mock_api.h
1#ifndef IPFS_MOCK_API_H_
2#define IPFS_MOCK_API_H_
3
4#include <gtest/gtest.h>
5
6#include <ipfs_client/client.h>
7#include <ipfs_client/json_cbor_adapter.h>
8
9#include "mock_http_provider.h"
10#include "mock_gw_cfg.h"
11#include "mock_sig_vtor.h"
12
13namespace ipfs::gw {
14class GatewayRequest;
15};
16
17namespace i = ipfs;
18namespace ig = i::gw;
19namespace ic = i::ctx;
20namespace ii = i::ipld;
21using RT = ig::GatewayRequestType;
22
23using namespace std::literals;
24
25namespace {
26struct MockDnsTxt : public i::ctx::DnsTxtLookup {
27 struct DnsInvocation {
28 std::string host;
29 std::vector<std::string> txt_records;
30 };
31
32 std::vector<DnsInvocation> mutable expected_dns;
33 void SendDnsTextRequest(std::string host,
34 DnsTextResultsCallback rcb,
35 DnsTextCompleteCallback ccb) {
36 EXPECT_GE(expected_dns.size(), 1U);
37 auto& e = expected_dns.at(0);
38 EXPECT_EQ(e.host, host);
39 rcb(e.txt_records);
40 expected_dns.erase(expected_dns.begin());
41 ccb();
42 }
43};
44struct MockApi final : public i::Client {
45 MockDnsTxt* dns_;
46 MockGwCfg* gw_;
47 MockHttpProvider* h_;
48 MockApi() {
49 auto dns = std::make_unique<MockDnsTxt>();
50 dns_ = dns.get();
51 with(std::move(dns));
52 auto g = std::make_unique<MockGwCfg>();
53 gw_ = g.get();
54 with(std::move(g));
55 auto h = std::make_unique<MockHttpProvider>();
56 h_ = h.get();
57 with(std::move(h));
58 with(i::crypto::SigningKeyType::RSA, std::make_unique<MockSigVtor>());
59 with(i::crypto::SigningKeyType::Ed25519, std::make_unique<MockSigVtor>());
60 }
61 ~MockApi() noexcept override {}
62 std::vector<i::HttpRequestDescription> mutable http_requests_sent;
63 std::vector<ic::HttpApi::OnComplete> mutable cbs;
64 std::string MimeType(std::string extension,
65 std::string_view content,
66 std::string const& url) const {
67 return "";
68 }
69 std::string UnescapeUrlComponent(std::string_view url_comp) const {
70 return "";
71 }
72 bool VerifyKeySignature(SigningKeyType,
73 ByteView signature,
74 ByteView data,
75 ByteView key_bytes) const {
76 return true;
77 }
78
79 void Discover(std::function<void(std::vector<std::string>)> cb) {}
80#if HAS_JSON_CBOR_ADAPTER
81 std::unique_ptr<ipfs::DagCborValue> ParseCbor(ByteView bv) const {
82 return std::make_unique<ipfs::JsonCborAdapter>(nlohmann::json::from_cbor(
83 bv, false, true, nlohmann::detail::cbor_tag_handler_t::store));
84 }
85 std::unique_ptr<ipfs::DagJsonValue> ParseJson(std::string_view jstr) const {
86 return std::make_unique<ipfs::JsonCborAdapter>(nlohmann::json::parse(jstr));
87 }
88#else
89 std::unique_ptr<ipfs::DagCborValue> ParseCbor(ByteView bv) const {
90 return {};
91 }
92 std::unique_ptr<ipfs::DagJsonValue> ParseJson(std::string_view) const {
93 return {};
94 }
95#endif
96};
97} // namespace
98
99#endif // IPFS_MOCK_API_H_
Interface that provides functionality from whatever environment you're using this library in.
Definition client.h:42
Just an observing (non-owning) pointer.
Definition raw_ptr.h:31