Skip to content

Commit dfad331

Browse files
committed
Add http request mocking support
1 parent 083a54e commit dfad331

File tree

3 files changed

+437
-0
lines changed

3 files changed

+437
-0
lines changed

features/http-mocking.feature

Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
Feature: HTTP request mocking
2+
3+
Scenario: Mock HTTP request in WP-CLI
4+
Given an empty directory
5+
And an HTTP request to https://api.github.com/repos/wp-cli/wp-cli/releases?per_page=100 with this response:
6+
"""
7+
HTTP/1.1 200
8+
Content-Type: application/json
9+
10+
[
11+
{
12+
"url": "https://api.github.com/repos/wp-cli/wp-cli/releases/169243978",
13+
"assets_url": "https://api.github.com/repos/wp-cli/wp-cli/releases/169243978/assets",
14+
"upload_url": "https://uploads.github.com/repos/wp-cli/wp-cli/releases/169243978/assets{?name,label}",
15+
"html_url": "https://github.com/wp-cli/wp-cli/releases/tag/v999.9.9",
16+
"id": 169243978,
17+
"author": {
18+
"login": "schlessera",
19+
"id": 83631,
20+
"node_id": "MDQ6VXNlcjgzNjMx",
21+
"avatar_url": "https://avatars.githubusercontent.com/u/83631?v=4",
22+
"gravatar_id": "",
23+
"url": "https://api.github.com/users/schlessera",
24+
"html_url": "https://github.com/schlessera",
25+
"followers_url": "https://api.github.com/users/schlessera/followers",
26+
"following_url": "https://api.github.com/users/schlessera/following{/other_user}",
27+
"gists_url": "https://api.github.com/users/schlessera/gists{/gist_id}",
28+
"starred_url": "https://api.github.com/users/schlessera/starred{/owner}{/repo}",
29+
"subscriptions_url": "https://api.github.com/users/schlessera/subscriptions",
30+
"organizations_url": "https://api.github.com/users/schlessera/orgs",
31+
"repos_url": "https://api.github.com/users/schlessera/repos",
32+
"events_url": "https://api.github.com/users/schlessera/events{/privacy}",
33+
"received_events_url": "https://api.github.com/users/schlessera/received_events",
34+
"type": "User",
35+
"user_view_type": "public",
36+
"site_admin": false
37+
},
38+
"node_id": "RE_kwDOACQFs84KFnVK",
39+
"tag_name": "v999.9.9",
40+
"target_commitish": "main",
41+
"name": "Version 999.9.9",
42+
"draft": false,
43+
"prerelease": false,
44+
"created_at": "2024-08-08T03:04:55Z",
45+
"published_at": "2024-08-08T03:51:13Z",
46+
"assets": [
47+
{
48+
"url": "https://api.github.com/repos/wp-cli/wp-cli/releases/assets/184590231",
49+
"id": 184590231,
50+
"node_id": "RA_kwDOACQFs84LAJ-X",
51+
"name": "wp-cli-999.9.9.phar",
52+
"label": null,
53+
"uploader": {
54+
"login": "schlessera",
55+
"id": 83631,
56+
"node_id": "MDQ6VXNlcjgzNjMx",
57+
"avatar_url": "https://avatars.githubusercontent.com/u/83631?v=4",
58+
"gravatar_id": "",
59+
"url": "https://api.github.com/users/schlessera",
60+
"html_url": "https://github.com/schlessera",
61+
"followers_url": "https://api.github.com/users/schlessera/followers",
62+
"following_url": "https://api.github.com/users/schlessera/following{/other_user}",
63+
"gists_url": "https://api.github.com/users/schlessera/gists{/gist_id}",
64+
"starred_url": "https://api.github.com/users/schlessera/starred{/owner}{/repo}",
65+
"subscriptions_url": "https://api.github.com/users/schlessera/subscriptions",
66+
"organizations_url": "https://api.github.com/users/schlessera/orgs",
67+
"repos_url": "https://api.github.com/users/schlessera/repos",
68+
"events_url": "https://api.github.com/users/schlessera/events{/privacy}",
69+
"received_events_url": "https://api.github.com/users/schlessera/received_events",
70+
"type": "User",
71+
"user_view_type": "public",
72+
"site_admin": false
73+
},
74+
"content_type": "application/octet-stream",
75+
"state": "uploaded",
76+
"size": 7048108,
77+
"download_count": 722639,
78+
"created_at": "2024-08-08T03:51:05Z",
79+
"updated_at": "2024-08-08T03:51:08Z",
80+
"browser_download_url": "https://github.com/wp-cli/wp-cli/releases/download/v999.9.9/wp-cli-999.9.9.phar"
81+
},
82+
{
83+
"url": "https://api.github.com/repos/wp-cli/wp-cli/releases/assets/184590236",
84+
"id": 184590236,
85+
"node_id": "RA_kwDOACQFs84LAJ-c",
86+
"name": "wp-cli-999.9.9.phar.asc",
87+
"label": null,
88+
"uploader": {
89+
"login": "schlessera",
90+
"id": 83631,
91+
"node_id": "MDQ6VXNlcjgzNjMx",
92+
"avatar_url": "https://avatars.githubusercontent.com/u/83631?v=4",
93+
"gravatar_id": "",
94+
"url": "https://api.github.com/users/schlessera",
95+
"html_url": "https://github.com/schlessera",
96+
"followers_url": "https://api.github.com/users/schlessera/followers",
97+
"following_url": "https://api.github.com/users/schlessera/following{/other_user}",
98+
"gists_url": "https://api.github.com/users/schlessera/gists{/gist_id}",
99+
"starred_url": "https://api.github.com/users/schlessera/starred{/owner}{/repo}",
100+
"subscriptions_url": "https://api.github.com/users/schlessera/subscriptions",
101+
"organizations_url": "https://api.github.com/users/schlessera/orgs",
102+
"repos_url": "https://api.github.com/users/schlessera/repos",
103+
"events_url": "https://api.github.com/users/schlessera/events{/privacy}",
104+
"received_events_url": "https://api.github.com/users/schlessera/received_events",
105+
"type": "User",
106+
"user_view_type": "public",
107+
"site_admin": false
108+
},
109+
"content_type": "application/octet-stream",
110+
"state": "uploaded",
111+
"size": 516,
112+
"download_count": 6279,
113+
"created_at": "2024-08-08T03:51:08Z",
114+
"updated_at": "2024-08-08T03:51:08Z",
115+
"browser_download_url": "https://github.com/wp-cli/wp-cli/releases/download/v999.9.9/wp-cli-999.9.9.phar.asc"
116+
},
117+
{
118+
"url": "https://api.github.com/repos/wp-cli/wp-cli/releases/assets/184590237",
119+
"id": 184590237,
120+
"node_id": "RA_kwDOACQFs84LAJ-d",
121+
"name": "wp-cli-999.9.9.phar.gpg",
122+
"label": null,
123+
"uploader": {
124+
"login": "schlessera",
125+
"id": 83631,
126+
"node_id": "MDQ6VXNlcjgzNjMx",
127+
"avatar_url": "https://avatars.githubusercontent.com/u/83631?v=4",
128+
"gravatar_id": "",
129+
"url": "https://api.github.com/users/schlessera",
130+
"html_url": "https://github.com/schlessera",
131+
"followers_url": "https://api.github.com/users/schlessera/followers",
132+
"following_url": "https://api.github.com/users/schlessera/following{/other_user}",
133+
"gists_url": "https://api.github.com/users/schlessera/gists{/gist_id}",
134+
"starred_url": "https://api.github.com/users/schlessera/starred{/owner}{/repo}",
135+
"subscriptions_url": "https://api.github.com/users/schlessera/subscriptions",
136+
"organizations_url": "https://api.github.com/users/schlessera/orgs",
137+
"repos_url": "https://api.github.com/users/schlessera/repos",
138+
"events_url": "https://api.github.com/users/schlessera/events{/privacy}",
139+
"received_events_url": "https://api.github.com/users/schlessera/received_events",
140+
"type": "User",
141+
"user_view_type": "public",
142+
"site_admin": false
143+
},
144+
"content_type": "application/octet-stream",
145+
"state": "uploaded",
146+
"size": 1654621,
147+
"download_count": 2089,
148+
"created_at": "2024-08-08T03:51:08Z",
149+
"updated_at": "2024-08-08T03:51:09Z",
150+
"browser_download_url": "https://github.com/wp-cli/wp-cli/releases/download/v999.9.9/wp-cli-999.9.9.phar.gpg"
151+
},
152+
{
153+
"url": "https://api.github.com/repos/wp-cli/wp-cli/releases/assets/184590241",
154+
"id": 184590241,
155+
"node_id": "RA_kwDOACQFs84LAJ-h",
156+
"name": "wp-cli-999.9.9.phar.md5",
157+
"label": null,
158+
"uploader": {
159+
"login": "schlessera",
160+
"id": 83631,
161+
"node_id": "MDQ6VXNlcjgzNjMx",
162+
"avatar_url": "https://avatars.githubusercontent.com/u/83631?v=4",
163+
"gravatar_id": "",
164+
"url": "https://api.github.com/users/schlessera",
165+
"html_url": "https://github.com/schlessera",
166+
"followers_url": "https://api.github.com/users/schlessera/followers",
167+
"following_url": "https://api.github.com/users/schlessera/following{/other_user}",
168+
"gists_url": "https://api.github.com/users/schlessera/gists{/gist_id}",
169+
"starred_url": "https://api.github.com/users/schlessera/starred{/owner}{/repo}",
170+
"subscriptions_url": "https://api.github.com/users/schlessera/subscriptions",
171+
"organizations_url": "https://api.github.com/users/schlessera/orgs",
172+
"repos_url": "https://api.github.com/users/schlessera/repos",
173+
"events_url": "https://api.github.com/users/schlessera/events{/privacy}",
174+
"received_events_url": "https://api.github.com/users/schlessera/received_events",
175+
"type": "User",
176+
"user_view_type": "public",
177+
"site_admin": false
178+
},
179+
"content_type": "application/octet-stream",
180+
"state": "uploaded",
181+
"size": 33,
182+
"download_count": 381247,
183+
"created_at": "2024-08-08T03:51:09Z",
184+
"updated_at": "2024-08-08T03:51:09Z",
185+
"browser_download_url": "https://github.com/wp-cli/wp-cli/releases/download/v999.9.9/wp-cli-999.9.9.phar.md5"
186+
},
187+
{
188+
"url": "https://api.github.com/repos/wp-cli/wp-cli/releases/assets/184590242",
189+
"id": 184590242,
190+
"node_id": "RA_kwDOACQFs84LAJ-i",
191+
"name": "wp-cli-999.9.9.phar.sha256",
192+
"label": null,
193+
"uploader": {
194+
"login": "schlessera",
195+
"id": 83631,
196+
"node_id": "MDQ6VXNlcjgzNjMx",
197+
"avatar_url": "https://avatars.githubusercontent.com/u/83631?v=4",
198+
"gravatar_id": "",
199+
"url": "https://api.github.com/users/schlessera",
200+
"html_url": "https://github.com/schlessera",
201+
"followers_url": "https://api.github.com/users/schlessera/followers",
202+
"following_url": "https://api.github.com/users/schlessera/following{/other_user}",
203+
"gists_url": "https://api.github.com/users/schlessera/gists{/gist_id}",
204+
"starred_url": "https://api.github.com/users/schlessera/starred{/owner}{/repo}",
205+
"subscriptions_url": "https://api.github.com/users/schlessera/subscriptions",
206+
"organizations_url": "https://api.github.com/users/schlessera/orgs",
207+
"repos_url": "https://api.github.com/users/schlessera/repos",
208+
"events_url": "https://api.github.com/users/schlessera/events{/privacy}",
209+
"received_events_url": "https://api.github.com/users/schlessera/received_events",
210+
"type": "User",
211+
"user_view_type": "public",
212+
"site_admin": false
213+
},
214+
"content_type": "application/octet-stream",
215+
"state": "uploaded",
216+
"size": 65,
217+
"download_count": 1104,
218+
"created_at": "2024-08-08T03:51:09Z",
219+
"updated_at": "2024-08-08T03:51:10Z",
220+
"browser_download_url": "https://github.com/wp-cli/wp-cli/releases/download/v999.9.9/wp-cli-999.9.9.phar.sha256"
221+
},
222+
{
223+
"url": "https://api.github.com/repos/wp-cli/wp-cli/releases/assets/184590243",
224+
"id": 184590243,
225+
"node_id": "RA_kwDOACQFs84LAJ-j",
226+
"name": "wp-cli-999.9.9.phar.sha512",
227+
"label": null,
228+
"uploader": {
229+
"login": "schlessera",
230+
"id": 83631,
231+
"node_id": "MDQ6VXNlcjgzNjMx",
232+
"avatar_url": "https://avatars.githubusercontent.com/u/83631?v=4",
233+
"gravatar_id": "",
234+
"url": "https://api.github.com/users/schlessera",
235+
"html_url": "https://github.com/schlessera",
236+
"followers_url": "https://api.github.com/users/schlessera/followers",
237+
"following_url": "https://api.github.com/users/schlessera/following{/other_user}",
238+
"gists_url": "https://api.github.com/users/schlessera/gists{/gist_id}",
239+
"starred_url": "https://api.github.com/users/schlessera/starred{/owner}{/repo}",
240+
"subscriptions_url": "https://api.github.com/users/schlessera/subscriptions",
241+
"organizations_url": "https://api.github.com/users/schlessera/orgs",
242+
"repos_url": "https://api.github.com/users/schlessera/repos",
243+
"events_url": "https://api.github.com/users/schlessera/events{/privacy}",
244+
"received_events_url": "https://api.github.com/users/schlessera/received_events",
245+
"type": "User",
246+
"user_view_type": "public",
247+
"site_admin": false
248+
},
249+
"content_type": "application/octet-stream",
250+
"state": "uploaded",
251+
"size": 129,
252+
"download_count": 6587,
253+
"created_at": "2024-08-08T03:51:10Z",
254+
"updated_at": "2024-08-08T03:51:10Z",
255+
"browser_download_url": "https://github.com/wp-cli/wp-cli/releases/download/v999.9.9/wp-cli-999.9.9.phar.sha512"
256+
}
257+
],
258+
"tarball_url": "https://api.github.com/repos/wp-cli/wp-cli/tarball/v999.9.9",
259+
"zipball_url": "https://api.github.com/repos/wp-cli/wp-cli/zipball/v999.9.9",
260+
"body": "- Allow manually dispatching tests workflow [[#5965](https://github.com/wp-cli/wp-cli/pull/5965)]\r\n- Add fish shell completion [[#5954](https://github.com/wp-cli/wp-cli/pull/5954)]\r\n- Add defaults and accepted values for runcommand() options in doc [[#5953](https://github.com/wp-cli/wp-cli/pull/5953)]\r\n- Address warnings with filenames ending in fullstop on Windows [[#5951](https://github.com/wp-cli/wp-cli/pull/5951)]\r\n- Fix unit tests [[#5950](https://github.com/wp-cli/wp-cli/pull/5950)]\r\n- Update copyright year in license [[#5942](https://github.com/wp-cli/wp-cli/pull/5942)]\r\n- Fix breaking multi-line CSV values on reading [[#5939](https://github.com/wp-cli/wp-cli/pull/5939)]\r\n- Fix broken Gutenberg test [[#5938](https://github.com/wp-cli/wp-cli/pull/5938)]\r\n- Update docker runner to resolve docker path using `/usr/bin/env` [[#5936](https://github.com/wp-cli/wp-cli/pull/5936)]\r\n- Fix `inherit` path in nested directory [[#5930](https://github.com/wp-cli/wp-cli/pull/5930)]\r\n- Minor docblock improvements [[#5929](https://github.com/wp-cli/wp-cli/pull/5929)]\r\n- Add Signup fetcher [[#5926](https://github.com/wp-cli/wp-cli/pull/5926)]\r\n- Ensure the alias has the leading `@` symbol when added [[#5924](https://github.com/wp-cli/wp-cli/pull/5924)]\r\n- Include any non default hook information in CompositeCommand [[#5921](https://github.com/wp-cli/wp-cli/pull/5921)]\r\n- Correct completion case when ends in = [[#5913](https://github.com/wp-cli/wp-cli/pull/5913)]\r\n- Docs: Fixes for inline comments [[#5912](https://github.com/wp-cli/wp-cli/pull/5912)]\r\n- Update Inline comments [[#5910](https://github.com/wp-cli/wp-cli/pull/5910)]\r\n- Add a real-world example for `wp cli has-command` [[#5908](https://github.com/wp-cli/wp-cli/pull/5908)]\r\n- Fix typos [[#5901](https://github.com/wp-cli/wp-cli/pull/5901)]\r\n- Avoid PHP deprecation notices in PHP 8.1.x [[#5899](https://github.com/wp-cli/wp-cli/pull/5899)]",
261+
"reactions": {
262+
"url": "https://api.github.com/repos/wp-cli/wp-cli/releases/169243978/reactions",
263+
"total_count": 9,
264+
"+1": 4,
265+
"-1": 0,
266+
"laugh": 0,
267+
"hooray": 1,
268+
"confused": 0,
269+
"heart": 0,
270+
"rocket": 4,
271+
"eyes": 0
272+
}
273+
}
274+
]
275+
"""
276+
277+
When I run `wp cli check-update`
278+
Then STDOUT should be a table containing rows:
279+
| version | update_type | package_url |
280+
| 999.9.9 | major | https://github.com/wp-cli/wp-cli/releases/download/v999.9.9/wp-cli-999.9.9.phar |
281+
282+
Scenario: Mock HTTP request in WordPress
283+
Given a WP install
284+
And an HTTP request to https://api.wordpress.org/core/version-check/1.7/ with this response:
285+
"""
286+
HTTP/1.1 200
287+
Content-Type: application/json
288+
289+
{
290+
"offers": [
291+
{
292+
"response": "latest",
293+
"download": "https:\/\/downloads.wordpress.org\/release\/wordpress-999.9.9.zip",
294+
"locale": "en_US",
295+
"packages": {
296+
"full": "https:\/\/downloads.wordpress.org\/release\/wordpress-999.9.9.zip",
297+
"no_content": "https:\/\/downloads.wordpress.org\/release\/wordpress-999.9.9-no-content.zip",
298+
"new_bundled": "https:\/\/downloads.wordpress.org\/release\/wordpress-999.9.9-new-bundled.zip",
299+
"partial": false,
300+
"rollback": false
301+
},
302+
"current": "999.9.9",
303+
"version": "999.9.9",
304+
"php_version": "7.2.24",
305+
"mysql_version": "5.5.5",
306+
"new_bundled": "6.7",
307+
"partial_version": false
308+
}
309+
],
310+
"translations": []
311+
}
312+
"""
313+
314+
When I run `wp core check-update`
315+
Then STDOUT should be a table containing rows:
316+
| version | update_type | package_url |
317+
| 999.9.9 | major | https://downloads.wordpress.org/release/wordpress-999.9.9.zip |

src/Context/FeatureContext.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ class FeatureContext implements SnippetAcceptingContext {
107107
private static $scenario_count = 0; // Scenario count, incremented on `@AfterScenario`.
108108
private static $proc_method_run_times = []; // Array of run time info for proc methods, keyed by method name and arg, each a 2-element array containing run time and run count.
109109

110+
private $mocked_requests = [];
111+
110112
/**
111113
* Get the path to the Composer vendor folder.
112114
*

0 commit comments

Comments
 (0)