xref: /aosp_15_r20/external/libbrillo/brillo/http/http_connection_fake.h (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright 2014 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li 
5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_HTTP_HTTP_CONNECTION_FAKE_H_
6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_HTTP_HTTP_CONNECTION_FAKE_H_
7*1a96fba6SXin Li 
8*1a96fba6SXin Li #include <map>
9*1a96fba6SXin Li #include <memory>
10*1a96fba6SXin Li #include <string>
11*1a96fba6SXin Li #include <utility>
12*1a96fba6SXin Li #include <vector>
13*1a96fba6SXin Li 
14*1a96fba6SXin Li #include <base/macros.h>
15*1a96fba6SXin Li #include <brillo/http/http_connection.h>
16*1a96fba6SXin Li #include <brillo/http/http_transport_fake.h>
17*1a96fba6SXin Li 
18*1a96fba6SXin Li namespace brillo {
19*1a96fba6SXin Li namespace http {
20*1a96fba6SXin Li namespace fake {
21*1a96fba6SXin Li 
22*1a96fba6SXin Li // This is a fake implementation of http::Connection for unit testing.
23*1a96fba6SXin Li class Connection : public http::Connection {
24*1a96fba6SXin Li  public:
25*1a96fba6SXin Li   Connection(const std::string& url,
26*1a96fba6SXin Li              const std::string& method,
27*1a96fba6SXin Li              const std::shared_ptr<http::Transport>& transport);
28*1a96fba6SXin Li   ~Connection() override;
29*1a96fba6SXin Li 
30*1a96fba6SXin Li   // Overrides from http::Connection.
31*1a96fba6SXin Li   // See http_connection.h for description of these methods.
32*1a96fba6SXin Li   bool SendHeaders(const HeaderList& headers, brillo::ErrorPtr* error) override;
33*1a96fba6SXin Li   bool SetRequestData(StreamPtr stream, brillo::ErrorPtr* error) override;
SetResponseData(StreamPtr)34*1a96fba6SXin Li   void SetResponseData(StreamPtr /* stream */) override {}
35*1a96fba6SXin Li   bool FinishRequest(brillo::ErrorPtr* error) override;
36*1a96fba6SXin Li   RequestID FinishRequestAsync(const SuccessCallback& success_callback,
37*1a96fba6SXin Li                                const ErrorCallback& error_callback) override;
38*1a96fba6SXin Li 
39*1a96fba6SXin Li   int GetResponseStatusCode() const override;
40*1a96fba6SXin Li   std::string GetResponseStatusText() const override;
41*1a96fba6SXin Li   std::string GetProtocolVersion() const override;
42*1a96fba6SXin Li   std::string GetResponseHeader(const std::string& header_name) const override;
43*1a96fba6SXin Li   StreamPtr ExtractDataStream(brillo::ErrorPtr* error) override;
44*1a96fba6SXin Li 
45*1a96fba6SXin Li  private:
46*1a96fba6SXin Li   // A helper method for FinishRequestAsync() implementation.
47*1a96fba6SXin Li   void FinishRequestAsyncHelper(const SuccessCallback& success_callback,
48*1a96fba6SXin Li                                 const ErrorCallback& error_callback);
49*1a96fba6SXin Li 
50*1a96fba6SXin Li   // Request and response objects passed to the user-provided request handler
51*1a96fba6SXin Li   // callback. The request object contains all the request information.
52*1a96fba6SXin Li   // The response object is the server response that is created by
53*1a96fba6SXin Li   // the handler in response to the request.
54*1a96fba6SXin Li   ServerRequest request_;
55*1a96fba6SXin Li   ServerResponse response_;
56*1a96fba6SXin Li 
57*1a96fba6SXin Li   DISALLOW_COPY_AND_ASSIGN(Connection);
58*1a96fba6SXin Li };
59*1a96fba6SXin Li 
60*1a96fba6SXin Li }  // namespace fake
61*1a96fba6SXin Li }  // namespace http
62*1a96fba6SXin Li }  // namespace brillo
63*1a96fba6SXin Li 
64*1a96fba6SXin Li #endif  // LIBBRILLO_BRILLO_HTTP_HTTP_CONNECTION_FAKE_H_
65