http_connection_fake.h revision f5effe9e46e479e6fdc4f6928da545d48dcd3997
1c91307af2622f6625525f3c1f9c954376df950adChia-chi Yeh// Copyright 2014 The Chromium OS Authors. All rights reserved. 20a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang// Use of this source code is governed by a BSD-style license that can be 30a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang// found in the LICENSE file. 40a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang 50a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#ifndef LIBCHROMEOS_CHROMEOS_HTTP_HTTP_CONNECTION_FAKE_H_ 60a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#define LIBCHROMEOS_CHROMEOS_HTTP_HTTP_CONNECTION_FAKE_H_ 70a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang 80a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <map> 90a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <string> 100a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <vector> 110a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang 120a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <base/macros.h> 130a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <chromeos/http/http_connection.h> 140a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <chromeos/http/http_transport_fake.h> 150a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang 160a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangnamespace chromeos { 170a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangnamespace http { 180a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangnamespace fake { 190a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang 200a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang// This is a fake implementation of http::Connection for unit testing. 210a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangclass Connection : public http::Connection { 220a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang public: 230a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang Connection(const std::string& url, const std::string& method, 240a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang const std::shared_ptr<http::Transport>& transport); 250a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang virtual ~Connection(); 260a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang 270a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang // Overrides from http::Connection. 280a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang // See http_connection.h for description of these methods. 290a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang bool SendHeaders(const HeaderList& headers, 300a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang chromeos::ErrorPtr* error) override; 310a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang bool SetRequestData(std::unique_ptr<DataReaderInterface> data_reader, 320a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang chromeos::ErrorPtr* error) override; 330a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang bool FinishRequest(chromeos::ErrorPtr* error) override; 340a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang void FinishRequestAsync(const SuccessCallback& success_callback, 350a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang const ErrorCallback& error_callback) override; 360a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang 370a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang int GetResponseStatusCode() const override; 380a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang std::string GetResponseStatusText() const override; 390a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang std::string GetProtocolVersion() const override; 400a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang std::string GetResponseHeader(const std::string& header_name) const override; 410a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang uint64_t GetResponseDataSize() const override; 420a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang bool ReadResponseData(void* data, size_t buffer_size, 430a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang size_t* size_read, chromeos::ErrorPtr* error) override; 440a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang 450a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang private: 460a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang // A helper method for FinishRequestAsync() implementation. 470a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang void FinishRequestAsyncHelper(const SuccessCallback& success_callback, 480a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang const ErrorCallback& error_callback); 490a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang 500a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang // Request and response objects passed to the user-provided request handler 510a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang // callback. The request object contains all the request information. 520a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang // The response object is the server response that is created by 530a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang // the handler in response to the request. 540a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang ServerRequest request_; 550a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang ServerResponse response_; 56adfbc90a9f63d1e0c87b4b17689c07cd3c781a0eChia-chi Yeh 57adfbc90a9f63d1e0c87b4b17689c07cd3c781a0eChia-chi Yeh // Internal read data pointer needed for ReadResponseData() implementation. 58adfbc90a9f63d1e0c87b4b17689c07cd3c781a0eChia-chi Yeh size_t response_data_ptr_ = 0; 59adfbc90a9f63d1e0c87b4b17689c07cd3c781a0eChia-chi Yeh 60adfbc90a9f63d1e0c87b4b17689c07cd3c781a0eChia-chi Yeh DISALLOW_COPY_AND_ASSIGN(Connection); 61adfbc90a9f63d1e0c87b4b17689c07cd3c781a0eChia-chi Yeh}; 62adfbc90a9f63d1e0c87b4b17689c07cd3c781a0eChia-chi Yeh 63adfbc90a9f63d1e0c87b4b17689c07cd3c781a0eChia-chi Yeh} // namespace fake 64adfbc90a9f63d1e0c87b4b17689c07cd3c781a0eChia-chi Yeh} // namespace http 650a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang} // namespace chromeos 660a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang 670a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#endif // LIBCHROMEOS_CHROMEOS_HTTP_HTTP_CONNECTION_FAKE_H_ 680a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang