KiCad PCB EDA Suite
Loading...
Searching...
No Matches
http_lib_fake_server.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 * Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <ranges>
23
24
25void HTTP_LIB_FAKE_SERVER::SetResponse( const std::string& aUrl, RESPONSE aResponse )
26{
27 std::lock_guard lock( m_mutex );
28 m_responses[aUrl] = std::move( aResponse );
29}
30
31
32int HTTP_LIB_FAKE_SERVER::RequestCount( const std::string& aUrl ) const
33{
34 std::lock_guard lock( m_mutex );
35 return static_cast<int>( std::ranges::count( m_requests, aUrl ) );
36}
37
38
40{
41 return [this]( const std::string& aUrl, int& aStatusCode, std::string& aBody, std::string& )
42 {
43 std::lock_guard lock( m_mutex );
44 m_requests.push_back( aUrl );
45
46 if( auto it = m_responses.find( aUrl ); it == m_responses.end() )
47 {
48 aStatusCode = 404;
49 aBody.clear();
50 }
51 else
52 {
53 aStatusCode = it->second.statusCode;
54 aBody = it->second.body;
55 }
56
57 return true;
58 };
59}
60
61
62void HTTP_LIB_FAKE_SERVER::InstallStandardLibrary( const std::string& aRootUrl )
63{
64 SetResponse( aRootUrl, { 200, R"({"categories": "categories.json", "parts": "parts"})" } );
65
66 SetResponse( aRootUrl + "categories.json", { 200, R"([
67 { "id": "1", "name": "Resistors", "description": "Resistors" },
68 { "id": "2", "name": "Capacitors", "description": "Capacitors" }
69])" } );
70
71 SetResponse( aRootUrl + "parts/category/1.json", { 200, R"([
72 { "id": "res-10k", "name": "R_10K", "description": "10 kOhms resistor" },
73 { "id": "res-100k", "name": "R_100K", "description": "100 kOhms resistor" }
74])" } );
75
76 SetResponse( aRootUrl + "parts/category/2.json", { 200, R"([
77 { "id": "cap-100n", "name": "C_100n", "description": "100 nF capacitor" }
78])" } );
79
80 SetResponse( aRootUrl + "parts/res-10k.json", { 200, R"({
81 "id": "res-10k",
82 "name": "R_10K",
83 "exclude_from_bom": "False",
84 "exclude_from_board": "False",
85 "exclude_from_sim": "False",
86 "description": "10 kOhms resistor",
87 "keywords": "resistor",
88 "footprint_filters": ["Resistor_SMD:R_0603_1608Metric"],
89 "fields": {
90 "footprint": { "value": "Resistor_SMD:R_0603_1608Metric", "visible": "False" },
91 "value": { "value": "10k" },
92 "reference": { "value": "R" }
93 }
94})" } );
95
96 SetResponse( aRootUrl + "parts/res-100k.json", { 200, R"({
97 "id": "res-100k",
98 "name": "R_100K",
99 "exclude_from_bom": "False",
100 "exclude_from_board": "False",
101 "exclude_from_sim": "False",
102 "description": "100 kOhms resistor",
103 "fields": {
104 "footprint": { "value": "Resistor_SMD:R_0603_1608Metric", "visible": "False" },
105 "value": { "value": "100k" },
106 "reference": { "value": "R" }
107 }
108})" } );
109
110 SetResponse( aRootUrl + "parts/cap-100n.json", { 200, R"({
111 "id": "cap-100n",
112 "name": "C_100n",
113 "description": "100 nF capacitor",
114 "fields": {
115 "footprint": { "value": "Capacitor_SMD:C_0603_1608Metric", "visible": "False" },
116 "value": { "value": "100n" },
117 "reference": { "value": "C" }
118 }
119})" } );
120}
std::function< bool(const std::string &aUrl, int &aStatusCode, std::string &aBody, std::string &aError)> RETRIEVER
Allows replacing CURL fetches with a mock for testing.
std::vector< std::string > m_requests
int RequestCount(const std::string &aUrl) const
Number of times aUrl has been requested.
void InstallStandardLibrary(const std::string &aRootUrl)
void SetResponse(const std::string &aUrl, RESPONSE aResponse)
Replace the response served for an exact URL.
std::map< std::string, RESPONSE > m_responses
HTTP_LIB_CONNECTION::RETRIEVER MakeRetriever()