SupervisedUser SafeSites: Switch to the new SafeSearch API
[chromium-blink-merge.git] / sync / test / fake_server / fake_server_http_post_provider.cc
blob75c5d46efc3ebaf5de666d406a7ce8fe69728c2e
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sync/test/fake_server/fake_server_http_post_provider.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "sync/test/fake_server/fake_server.h"
17 using syncer::HttpPostProviderInterface;
19 namespace fake_server {
21 FakeServerHttpPostProviderFactory::FakeServerHttpPostProviderFactory(
22 const base::WeakPtr<FakeServer>& fake_server,
23 scoped_refptr<base::SequencedTaskRunner> fake_server_task_runner)
24 : fake_server_(fake_server),
25 fake_server_task_runner_(fake_server_task_runner) { }
27 FakeServerHttpPostProviderFactory::~FakeServerHttpPostProviderFactory() { }
29 void FakeServerHttpPostProviderFactory::Init(const std::string& user_agent) { }
31 HttpPostProviderInterface* FakeServerHttpPostProviderFactory::Create() {
32 FakeServerHttpPostProvider* http =
33 new FakeServerHttpPostProvider(fake_server_, fake_server_task_runner_);
34 http->AddRef();
35 return http;
38 void FakeServerHttpPostProviderFactory::Destroy(
39 HttpPostProviderInterface* http) {
40 static_cast<FakeServerHttpPostProvider*>(http)->Release();
43 FakeServerHttpPostProvider::FakeServerHttpPostProvider(
44 const base::WeakPtr<FakeServer>& fake_server,
45 scoped_refptr<base::SequencedTaskRunner> fake_server_task_runner)
46 : fake_server_(fake_server),
47 fake_server_task_runner_(fake_server_task_runner) { }
49 FakeServerHttpPostProvider::~FakeServerHttpPostProvider() { }
51 void FakeServerHttpPostProvider::SetExtraRequestHeaders(const char* headers) {
52 // TODO(pvalenzuela): Add assertions on this value.
53 extra_request_headers_.assign(headers);
56 void FakeServerHttpPostProvider::SetURL(const char* url, int port) {
57 // TODO(pvalenzuela): Add assertions on these values.
58 request_url_.assign(url);
59 request_port_ = port;
62 void FakeServerHttpPostProvider::SetPostPayload(const char* content_type,
63 int content_length,
64 const char* content) {
65 request_content_type_.assign(content_type);
66 request_content_.assign(content, content_length);
69 bool FakeServerHttpPostProvider::MakeSynchronousPost(int* error_code,
70 int* response_code) {
71 // It is assumed that a POST is being made to /command.
72 int post_error_code = -1;
73 int post_response_code = -1;
74 std::string post_response;
76 base::WaitableEvent post_complete(false, false);
77 base::Closure signal_closure = base::Bind(&base::WaitableEvent::Signal,
78 base::Unretained(&post_complete));
80 bool result = fake_server_task_runner_->PostTask(
81 FROM_HERE,
82 base::Bind(&FakeServer::HandleCommand,
83 fake_server_,
84 base::ConstRef(request_content_),
85 base::ConstRef(signal_closure),
86 &post_error_code,
87 &post_response_code,
88 &post_response));
90 if (!result)
91 return false;
93 post_complete.Wait();
94 post_error_code_ = post_error_code;
95 post_response_code_ = post_response_code;
96 response_ = post_response;
98 *error_code = post_error_code_;
99 *response_code = post_response_code_;
100 return *error_code == 0;
103 int FakeServerHttpPostProvider::GetResponseContentLength() const {
104 return response_.length();
107 const char* FakeServerHttpPostProvider::GetResponseContent() const {
108 return response_.c_str();
111 const std::string FakeServerHttpPostProvider::GetResponseHeaderValue(
112 const std::string& name) const {
113 return std::string();
116 void FakeServerHttpPostProvider::Abort() {
119 } // namespace fake_server