diff options
Diffstat (limited to 'net')
-rw-r--r-- | net/test/test_server.cc | 42 | ||||
-rw-r--r-- | net/test/test_server.h | 13 | ||||
-rwxr-xr-x | net/tools/testserver/testserver.py | 26 |
3 files changed, 65 insertions, 16 deletions
diff --git a/net/test/test_server.cc b/net/test/test_server.cc index 8e92699..5d197b9 100644 --- a/net/test/test_server.cc +++ b/net/test/test_server.cc @@ -14,6 +14,7 @@ #include "net/base/x509_certificate.h" #endif +#include "base/base64.h" #include "base/command_line.h" #include "base/debug/leak_annotations.h" #include "base/file_util.h" @@ -252,13 +253,13 @@ bool TestServer::GetAddressList(AddressList* address_list) const { return true; } -GURL TestServer::GetURL(const std::string& path) { +GURL TestServer::GetURL(const std::string& path) const { return GURL(GetScheme() + "://" + host_port_pair_.ToString() + "/" + path); } GURL TestServer::GetURLWithUser(const std::string& path, - const std::string& user) { + const std::string& user) const { return GURL(GetScheme() + "://" + user + "@" + host_port_pair_.ToString() + "/" + path); @@ -266,12 +267,47 @@ GURL TestServer::GetURLWithUser(const std::string& path, GURL TestServer::GetURLWithUserAndPassword(const std::string& path, const std::string& user, - const std::string& password) { + const std::string& password) const { return GURL(GetScheme() + "://" + user + ":" + password + "@" + host_port_pair_.ToString() + "/" + path); } +// static +bool TestServer::GetFilePathWithReplacements( + const std::string& original_file_path, + const std::vector<StringPair>& text_to_replace, + std::string* replacement_path) { + std::string new_file_path = original_file_path; + bool first_query_parameter = true; + const std::vector<StringPair>::const_iterator end = text_to_replace.end(); + for (std::vector<StringPair>::const_iterator it = text_to_replace.begin(); + it != end; + ++it) { + const std::string& old_text = it->first; + const std::string& new_text = it->second; + std::string base64_old; + std::string base64_new; + if (!base::Base64Encode(old_text, &base64_old)) + return false; + if (!base::Base64Encode(new_text, &base64_new)) + return false; + if (first_query_parameter) { + new_file_path += "?"; + first_query_parameter = false; + } else { + new_file_path += "&"; + } + new_file_path += "replace_text="; + new_file_path += base64_old; + new_file_path += ":"; + new_file_path += base64_new; + } + + *replacement_path = new_file_path; + return true; +} + bool TestServer::SetPythonPath() { FilePath third_party_dir; if (!PathService::Get(base::DIR_SOURCE_ROOT, &third_party_dir)) { diff --git a/net/test/test_server.h b/net/test/test_server.h index 8affc12..b92c91d 100644 --- a/net/test/test_server.h +++ b/net/test/test_server.h @@ -7,6 +7,7 @@ #pragma once #include <string> +#include <utility> #include <vector> #include "build/build_config.h" @@ -117,14 +118,20 @@ class TestServer { std::string GetScheme() const; bool GetAddressList(AddressList* address_list) const WARN_UNUSED_RESULT; - GURL GetURL(const std::string& path); + GURL GetURL(const std::string& path) const; GURL GetURLWithUser(const std::string& path, - const std::string& user); + const std::string& user) const; GURL GetURLWithUserAndPassword(const std::string& path, const std::string& user, - const std::string& password); + const std::string& password) const; + + typedef std::pair<std::string, std::string> StringPair; + static bool GetFilePathWithReplacements( + const std::string& original_path, + const std::vector<StringPair>& text_to_replace, + std::string* replacement_path); private: void Init(const FilePath& document_root); diff --git a/net/tools/testserver/testserver.py b/net/tools/testserver/testserver.py index 53444e3..ccb589b 100755 --- a/net/tools/testserver/testserver.py +++ b/net/tools/testserver/testserver.py @@ -577,20 +577,26 @@ class TestPageHandler(BaseHTTPServer.BaseHTTPRequestHandler): def _ReplaceFileData(self, data, query_parameters): """Replaces matching substrings in a file. - If the 'replace_orig' and 'replace_new' URL query parameters are present, - a new string is returned with all occasions of the 'replace_orig' value - replaced by the 'replace_new' value. + If the 'replace_text' URL query parameter is present, it is expected to be + of the form old_text:new_text, which indicates that any old_text strings in + the file are replaced with new_text. Multiple 'replace_text' parameters may + be specified. If the parameters are not present, |data| is returned. """ query_dict = cgi.parse_qs(query_parameters) - orig_values = query_dict.get('replace_orig', []) - new_values = query_dict.get('replace_new', []) - if not orig_values or not new_values: - return data - orig_value = orig_values[0] - new_value = new_values[0] - return data.replace(orig_value, new_value) + replace_text_values = query_dict.get('replace_text', []) + for replace_text_value in replace_text_values: + replace_text_args = replace_text_value.split(':') + if len(replace_text_args) != 2: + raise ValueError( + 'replace_text must be of form old_text:new_text. Actual value: %s' % + replace_text_value) + old_text_b64, new_text_b64 = replace_text_args + old_text = base64.urlsafe_b64decode(old_text_b64) + new_text = base64.urlsafe_b64decode(new_text_b64) + data = data.replace(old_text, new_text) + return data def FileHandler(self): """This handler sends the contents of the requested file. Wow, it's like |