winget-cli

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

TestRestRequestHandler.cpp (3167B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "TestCommon.h"
      5 #include "TestRestRequestHandler.h"
      6 #include <Rest/Schema/1_0/Interface.h>
      7 #include <Rest/Schema/IRestClient.h>
      8 
      9 std::shared_ptr<TestRestRequestHandler> GetTestRestRequestHandler(
     10     const web::http::status_code statusCode, const utility::string_t& sampleResponseString, const utility::string_t& mimeType)
     11 {
     12     return std::make_shared<TestRestRequestHandler>([statusCode, sampleResponseString, mimeType](web::http::http_request) ->
     13         pplx::task<web::http::http_response>
     14         {
     15             web::http::http_response response;
     16             if (sampleResponseString.empty())
     17             {
     18                 response.set_body(utf16string{});
     19             }
     20             else
     21             {
     22                 response.set_body(web::json::value::parse(sampleResponseString));
     23             }
     24 
     25             response.headers().set_content_type(mimeType);
     26             response.set_status_code(statusCode);
     27             return pplx::task_from_result(response);
     28         });
     29 }
     30 
     31 std::shared_ptr<TestRestRequestHandler> GetTestRestRequestHandler(
     32     std::function<web::http::status_code(const web::http::http_request& request)> handler)
     33 {
     34     return std::make_shared<TestRestRequestHandler>([handler = std::move(handler)](web::http::http_request request) ->
     35         pplx::task<web::http::http_response>
     36         {
     37             web::http::http_response response;
     38             response.set_body(utf16string{});
     39 
     40             response.headers().set_content_type(web::http::details::mime_types::application_json);
     41             response.set_status_code(handler(request));
     42             return pplx::task_from_result(response);
     43         });
     44 }
     45 
     46 std::shared_ptr<TestRestRequestHandler> GetHeaderVerificationHandler(
     47     const web::http::status_code statusCode, const utility::string_t& sampleResponseString, const std::pair<utility::string_t, utility::string_t>& header, web::http::status_code statusCodeOnFailure)
     48 {
     49     return std::make_shared<TestRestRequestHandler>([statusCode, sampleResponseString, header, statusCodeOnFailure](web::http::http_request request) ->
     50         pplx::task<web::http::http_response>
     51         {
     52             web::http::http_response response;
     53             auto& headers = request.headers();
     54             if (!headers.has(header.first) ||
     55                 (utility::conversions::to_utf8string(header.second).compare(utility::conversions::to_utf8string(headers[header.first]))) != 0)
     56             {
     57                 response.set_body(utf16string{ L"Expected header not found" });
     58                 response.set_status_code(statusCodeOnFailure);
     59                 return pplx::task_from_result(response);
     60             }
     61 
     62             if (!sampleResponseString.empty())
     63             {
     64                 response.set_body(web::json::value::parse(sampleResponseString));
     65             }
     66 
     67             response.headers().set_content_type(web::http::details::mime_types::application_json);
     68             response.set_status_code(statusCode);
     69             return pplx::task_from_result(response);
     70         });
     71 }