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"
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(
30 const std::string
& user_agent
,
31 const syncer::BindToTrackerCallback
& bind_to_tracker_callback
) {}
33 HttpPostProviderInterface
* FakeServerHttpPostProviderFactory::Create() {
34 FakeServerHttpPostProvider
* http
=
35 new FakeServerHttpPostProvider(fake_server_
, fake_server_task_runner_
);
40 void FakeServerHttpPostProviderFactory::Destroy(
41 HttpPostProviderInterface
* http
) {
42 static_cast<FakeServerHttpPostProvider
*>(http
)->Release();
45 FakeServerHttpPostProvider::FakeServerHttpPostProvider(
46 const base::WeakPtr
<FakeServer
>& fake_server
,
47 scoped_refptr
<base::SequencedTaskRunner
> fake_server_task_runner
)
48 : fake_server_(fake_server
),
49 fake_server_task_runner_(fake_server_task_runner
) { }
51 FakeServerHttpPostProvider::~FakeServerHttpPostProvider() { }
53 void FakeServerHttpPostProvider::SetExtraRequestHeaders(const char* headers
) {
54 // TODO(pvalenzuela): Add assertions on this value.
55 extra_request_headers_
.assign(headers
);
58 void FakeServerHttpPostProvider::SetURL(const char* url
, int port
) {
59 // TODO(pvalenzuela): Add assertions on these values.
60 request_url_
.assign(url
);
64 void FakeServerHttpPostProvider::SetPostPayload(const char* content_type
,
66 const char* content
) {
67 request_content_type_
.assign(content_type
);
68 request_content_
.assign(content
, content_length
);
71 bool FakeServerHttpPostProvider::MakeSynchronousPost(int* error_code
,
73 // It is assumed that a POST is being made to /command.
74 int post_error_code
= -1;
75 int post_response_code
= -1;
76 std::string post_response
;
78 base::WaitableEvent
post_complete(false, false);
79 base::Closure signal_closure
= base::Bind(&base::WaitableEvent::Signal
,
80 base::Unretained(&post_complete
));
82 bool result
= fake_server_task_runner_
->PostTask(
84 base::Bind(&FakeServer::HandleCommand
,
86 base::ConstRef(request_content_
),
87 base::ConstRef(signal_closure
),
96 post_error_code_
= post_error_code
;
97 post_response_code_
= post_response_code
;
98 response_
= post_response
;
100 *error_code
= post_error_code_
;
101 *response_code
= post_response_code_
;
102 return *error_code
== 0;
105 int FakeServerHttpPostProvider::GetResponseContentLength() const {
106 return response_
.length();
109 const char* FakeServerHttpPostProvider::GetResponseContent() const {
110 return response_
.c_str();
113 const std::string
FakeServerHttpPostProvider::GetResponseHeaderValue(
114 const std::string
& name
) const {
115 return std::string();
118 void FakeServerHttpPostProvider::Abort() {
121 } // namespace fake_server