• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_TEST_EMBEDDED_TEST_SERVER_REQUEST_HANDLER_UTIL_H_
6 #define NET_TEST_EMBEDDED_TEST_SERVER_REQUEST_HANDLER_UTIL_H_
7 
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <vector>
12 
13 #include "base/files/file_path.h"
14 #include "base/strings/string_split.h"
15 #include "net/test/embedded_test_server/embedded_test_server.h"
16 #include "net/test/embedded_test_server/http_response.h"
17 
18 namespace url {
19 class GURL;
20 }
21 
22 namespace net::test_server {
23 struct HttpRequest;
24 
25 // The extension that is used to find a file containing mock headers to use
26 // for a test file. For example, a test data directory can have:
27 //   foo.html
28 //   foo.html.mock-http-headers
29 // When the test server serves foo.html, if it finds foo.html.mock-http-headers
30 // it will use the contents of that file for the headers.
31 extern const base::FilePath::CharType kMockHttpHeadersExtension[];
32 
33 // Returns the Content-Type header value for a path based on its extension.
34 std::string GetContentType(const base::FilePath& path);
35 
36 // This file is only meant for compatibility with testserver.py. No
37 // additional handlers should be added here that don't affect multiple
38 // distinct tests.
39 
40 using RequestQuery = std::map<std::string, std::vector<std::string>>;
41 
42 // Return whether |request| starts with a URL path of |url|.
43 bool ShouldHandle(const HttpRequest& request, const std::string& prefix_path);
44 
45 // Calls |handler| if the |request| URL starts with |prefix|.
46 std::unique_ptr<HttpResponse> HandlePrefixedRequest(
47     const std::string& prefix,
48     const EmbeddedTestServer::HandleRequestCallback& handler,
49     const HttpRequest& request);
50 
51 // Parses |url| to get the query and places it into a map.
52 RequestQuery ParseQuery(const GURL& url);
53 
54 // Returns a path that serves the contents of the file at |original_path|
55 // with all the text matching the elements of |text_to_replace| replaced
56 // with the corresponding values. The path is returned in |replacement_path|.
57 // The result path is only usable by HandleFileRequest which will perform the
58 // actual replacements of the file contents.
59 std::string GetFilePathWithReplacements(
60     const std::string& original_path,
61     const base::StringPairs& text_to_replace);
62 
63 // Handles |request| by serving a file from under |server_root|.
64 std::unique_ptr<HttpResponse> HandleFileRequest(
65     const base::FilePath& server_root,
66     const HttpRequest& request);
67 
68 }  // namespace net::test_server
69 
70 #endif  // NET_TEST_EMBEDDED_TEST_SERVER_REQUEST_HANDLER_UTIL_H_
71