winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

MockWebServer.h (1869B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 
      4 #pragma once
      5 
      6 #include "Result.h"
      7 
      8 #include <memory>
      9 #include <queue>
     10 #include <string>
     11 #include <unordered_map>
     12 
     13 namespace SFS::details
     14 {
     15 enum class HttpHeader;
     16 }
     17 
     18 namespace SFS::test
     19 {
     20 namespace details
     21 {
     22 class MockWebServerImpl;
     23 }
     24 
     25 using HttpCode = int;
     26 using HeaderMap = std::unordered_map<std::string, std::string>;
     27 
     28 struct MockPrerequisite
     29 {
     30     std::string name;
     31     std::string version;
     32 };
     33 
     34 class MockWebServer
     35 {
     36   public:
     37     MockWebServer();
     38     ~MockWebServer();
     39 
     40     MockWebServer(const MockWebServer&) = delete;
     41     MockWebServer& operator=(const MockWebServer&) = delete;
     42 
     43     [[nodiscard]] Result Stop();
     44 
     45     std::string GetBaseUrl() const;
     46 
     47     /// @brief Registers a product with the server. Will fill the other data with gibberish for testing purposes
     48     void RegisterProduct(std::string name, std::string version);
     49 
     50     /// @brief Registers an app with the server. Will fill the other data with gibberish for testing purposes
     51     void RegisterAppProduct(std::string name, std::string version, std::vector<MockPrerequisite> prerequisites);
     52 
     53     /// @brief Registers the expectation of a given header to the present in the request
     54     void RegisterExpectedRequestHeader(SFS::details::HttpHeader header, std::string value);
     55 
     56     /**
     57      * @brief Registers a sequence of HTTP error codes that will be sent by the server in the order in which they are
     58      * passed.
     59      */
     60     void SetForcedHttpErrors(std::queue<HttpCode> forcedErrors);
     61 
     62     /// @brief Registers a set of headers that will be sent depending on the HTTP code
     63     void SetResponseHeaders(std::unordered_map<HttpCode, HeaderMap> headersByCode);
     64 
     65   private:
     66     std::unique_ptr<details::MockWebServerImpl> m_impl;
     67 };
     68 } // namespace SFS::test