Android-cuttlefish cvd tool
fake_http_client.h
Go to the documentation of this file.
1//
2// Copyright (C) 2025 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#pragma once
17
18#include <functional>
19#include <mutex>
20#include <string>
21#include <unordered_map>
22#include <vector>
23
24#include "absl/strings/string_view.h"
25
28
29namespace cuttlefish {
30
31class FakeHttpClient : public HttpClient {
32 public:
33 using Handler = std::function<HttpResponse<std::string>(const HttpRequest&)>;
34
35 // The longest string that is a complete substring of `url` is used to match
36 // requests.
37 void SetResponse(std::string data, std::string url = "");
38 void SetResponse(HttpResponse<std::string> response, std::string url = "");
39 void SetResponse(Handler handler, std::string url = "");
40 bool RequestMade(absl::string_view url) const;
41 // Returns response's status code.
43 HttpRequest request, HttpClient::ResultDataCallback callback) override;
44
45 private:
46 const Handler* FindHandler(absl::string_view url) const;
47
48 mutable std::mutex mutex_;
49 std::unordered_map<std::string, Handler> responses_;
50 std::vector<std::string> requested_urls_;
51};
52
53bool HasAuthorization(const std::vector<std::string>& headers);
54
55} // namespace cuttlefish
Definition: fake_http_client.h:31
std::mutex mutex_
Definition: fake_http_client.h:48
Result< HttpResponse< void > > DownloadToCallback(HttpRequest request, HttpClient::ResultDataCallback callback) override
Definition: fake_http_client.cc:79
std::unordered_map< std::string, Handler > responses_
Definition: fake_http_client.h:49
std::function< HttpResponse< std::string >(const HttpRequest &)> Handler
Definition: fake_http_client.h:33
std::vector< std::string > requested_urls_
Definition: fake_http_client.h:50
const Handler * FindHandler(absl::string_view url) const
Definition: fake_http_client.cc:63
void SetResponse(std::string data, std::string url="")
Definition: fake_http_client.cc:30
bool RequestMade(absl::string_view url) const
Definition: fake_http_client.cc:47
Definition: http_client.h:100
std::function< Result< void >(char *, size_t)> ResultDataCallback
Definition: http_client.h:103
char data[Size]
Definition: incremental_server.cpp:1
Definition: alloc_driver.h:21
bool HasAuthorization(const std::vector< std::string > &headers)
Definition: fake_http_client.cc:97
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
Definition: http_client.h:93
Definition: http_client.h:42