ServiceWorker: Consolidate version manipulation functions in SWProviderContext
[chromium-blink-merge.git] / sync / test / fake_server / fake_server_http_post_provider.cc
blobfbfe16f35aecae5827169669c3ee1031da2b5025
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/sequenced_task_runner.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "sync/test/fake_server/fake_server.h"
16 using syncer::HttpPostProviderInterface;
18 namespace fake_server {
20 FakeServerHttpPostProviderFactory::FakeServerHttpPostProviderFactory(
21 FakeServer* fake_server,
22 scoped_refptr<base::SequencedTaskRunner> task_runner)
23 : fake_server_(fake_server), task_runner_(task_runner) { }
25 FakeServerHttpPostProviderFactory::~FakeServerHttpPostProviderFactory() { }
27 void FakeServerHttpPostProviderFactory::Init(const std::string& user_agent) { }
29 HttpPostProviderInterface* FakeServerHttpPostProviderFactory::Create() {
30 FakeServerHttpPostProvider* http =
31 new FakeServerHttpPostProvider(fake_server_, task_runner_);
32 http->AddRef();
33 return http;
36 void FakeServerHttpPostProviderFactory::Destroy(
37 HttpPostProviderInterface* http) {
38 static_cast<FakeServerHttpPostProvider*>(http)->Release();
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) { }
48 FakeServerHttpPostProvider::~FakeServerHttpPostProvider() { }
50 void FakeServerHttpPostProvider::SetExtraRequestHeaders(const char* headers) {
51 // TODO(pvalenzuela): Add assertions on this value.
52 extra_request_headers_.assign(headers);
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;
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);
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();
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;
93 int FakeServerHttpPostProvider::GetResponseContentLength() const {
94 return response_.length();
97 const char* FakeServerHttpPostProvider::GetResponseContent() const {
98 return response_.c_str();
101 const std::string FakeServerHttpPostProvider::GetResponseHeaderValue(
102 const std::string& name) const {
103 return std::string();
106 void FakeServerHttpPostProvider::Abort() {
109 } // namespace fake_server