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.
4
5 #include "sync/test/fake_server/fake_server_http_post_provider.h"
6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/sequenced_task_runner.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "sync/test/fake_server/fake_server.h"
15
16 using syncer::HttpPostProviderInterface;
17
18 namespace fake_server {
19
FakeServerHttpPostProviderFactory(FakeServer * fake_server,scoped_refptr<base::SequencedTaskRunner> task_runner)20 FakeServerHttpPostProviderFactory::FakeServerHttpPostProviderFactory(
21 FakeServer* fake_server,
22 scoped_refptr<base::SequencedTaskRunner> task_runner)
23 : fake_server_(fake_server), task_runner_(task_runner) { }
24
~FakeServerHttpPostProviderFactory()25 FakeServerHttpPostProviderFactory::~FakeServerHttpPostProviderFactory() { }
26
Init(const std::string & user_agent)27 void FakeServerHttpPostProviderFactory::Init(const std::string& user_agent) { }
28
Create()29 HttpPostProviderInterface* FakeServerHttpPostProviderFactory::Create() {
30 FakeServerHttpPostProvider* http =
31 new FakeServerHttpPostProvider(fake_server_, task_runner_);
32 http->AddRef();
33 return http;
34 }
35
Destroy(HttpPostProviderInterface * http)36 void FakeServerHttpPostProviderFactory::Destroy(
37 HttpPostProviderInterface* http) {
38 static_cast<FakeServerHttpPostProvider*>(http)->Release();
39 }
40
FakeServerHttpPostProvider(FakeServer * fake_server,scoped_refptr<base::SequencedTaskRunner> task_runner)41 FakeServerHttpPostProvider::FakeServerHttpPostProvider(
42 FakeServer* fake_server,
43 scoped_refptr<base::SequencedTaskRunner> task_runner)
44 : fake_server_(fake_server),
45 task_runner_(task_runner),
46 post_complete_(false, false) { }
47
~FakeServerHttpPostProvider()48 FakeServerHttpPostProvider::~FakeServerHttpPostProvider() { }
49
SetExtraRequestHeaders(const char * headers)50 void FakeServerHttpPostProvider::SetExtraRequestHeaders(const char* headers) {
51 // TODO(pvalenzuela): Add assertions on this value.
52 extra_request_headers_.assign(headers);
53 }
54
SetURL(const char * url,int port)55 void FakeServerHttpPostProvider::SetURL(const char* url, int port) {
56 // TODO(pvalenzuela): Add assertions on these values.
57 request_url_.assign(url);
58 request_port_ = port;
59 }
60
SetPostPayload(const char * content_type,int content_length,const char * content)61 void FakeServerHttpPostProvider::SetPostPayload(const char* content_type,
62 int content_length,
63 const char* content) {
64 request_content_type_.assign(content_type);
65 request_content_.assign(content, content_length);
66 }
67
OnPostComplete(int error_code,int response_code,const std::string & response)68 void FakeServerHttpPostProvider::OnPostComplete(int error_code,
69 int response_code,
70 const std::string& response) {
71 post_error_code_ = error_code;
72 post_response_code_ = response_code;
73 response_ = response;
74 post_complete_.Signal();
75 }
76
MakeSynchronousPost(int * error_code,int * response_code)77 bool FakeServerHttpPostProvider::MakeSynchronousPost(int* error_code,
78 int* response_code) {
79 // It is assumed that a POST is being made to /command.
80 FakeServer::HandleCommandCallback callback = base::Bind(
81 &FakeServerHttpPostProvider::OnPostComplete, base::Unretained(this));
82 task_runner_->PostNonNestableTask(FROM_HERE,
83 base::Bind(&FakeServer::HandleCommand,
84 base::Unretained(fake_server_),
85 base::ConstRef(request_content_),
86 base::ConstRef(callback)));
87 post_complete_.Wait();
88 *error_code = post_error_code_;
89 *response_code = post_response_code_;
90 return *error_code == 0;
91 }
92
GetResponseContentLength() const93 int FakeServerHttpPostProvider::GetResponseContentLength() const {
94 return response_.length();
95 }
96
GetResponseContent() const97 const char* FakeServerHttpPostProvider::GetResponseContent() const {
98 return response_.c_str();
99 }
100
GetResponseHeaderValue(const std::string & name) const101 const std::string FakeServerHttpPostProvider::GetResponseHeaderValue(
102 const std::string& name) const {
103 return std::string();
104 }
105
Abort()106 void FakeServerHttpPostProvider::Abort() {
107 }
108
109 } // namespace fake_server
110