test_server_helpers.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2021 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. #ifndef COMPONENTS_POLICY_TEST_SUPPORT_TEST_SERVER_HELPERS_H_
  5. #define COMPONENTS_POLICY_TEST_SUPPORT_TEST_SERVER_HELPERS_H_
  6. #include <memory>
  7. #include <string>
  8. #include "net/http/http_status_code.h"
  9. #include "net/test/embedded_test_server/http_response.h"
  10. #include "url/gurl.h"
  11. namespace net {
  12. namespace test_server {
  13. class HttpResponse;
  14. struct HttpRequest;
  15. } // namespace test_server
  16. } // namespace net
  17. namespace policy {
  18. // HTTP Response that supports custom HTTP status codes.
  19. class CustomHttpResponse : public net::test_server::BasicHttpResponse {
  20. public:
  21. void SendResponse(
  22. base::WeakPtr<net::test_server::HttpResponseDelegate> delegate) override;
  23. };
  24. // Returns the value associated with `key` in `url`'s query or empty string if
  25. // `key` is not present.
  26. std::string KeyValueFromUrl(GURL url, const std::string& key);
  27. // Check server-side requirements, as defined in
  28. // device_management_backend.proto.
  29. bool MeetsServerSideRequirements(GURL url);
  30. // Returns true if a token is specified in the request URL with prefix
  31. // `token_header_prefix`, in which case the token is copied to `out`.
  32. bool GetTokenFromAuthorization(const net::test_server::HttpRequest& request,
  33. const std::string& token_header_prefix,
  34. std::string* out);
  35. // Returns true if an enrollment token is specified in the request URL, in which
  36. // case the enrollment token is copied to `out`.
  37. bool GetEnrollmentTokenFromRequest(const net::test_server::HttpRequest& request,
  38. std::string* out);
  39. // Returns true if a device token is specified in the request URL, in which case
  40. // the device token is copied to `out`.
  41. bool GetDeviceTokenFromRequest(const net::test_server::HttpRequest& request,
  42. std::string* out);
  43. // Returns true if an auth toke is specified in the request URL with the
  44. // oauth_token parameter or if it is set as GoogleLogin token from the
  45. // Authorization header. The token is copied to `out` if available.
  46. bool GetGoogleLoginFromRequest(const net::test_server::HttpRequest& request,
  47. std::string* out);
  48. // Returns a text/plain HttpResponse with a given `code` and `content`.
  49. std::unique_ptr<net::test_server::HttpResponse> CreateHttpResponse(
  50. net::HttpStatusCode code,
  51. const std::string& content);
  52. } // namespace policy
  53. #endif // COMPONENTS_POLICY_TEST_SUPPORT_TEST_SERVER_HELPERS_H_