FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
BookStack/app/Http/HttpRequestService.php at development · ucoding/BookStack · GitHub
ucoding
/
BookStack
Public
forked from
BookStackApp/BookStack
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
BookStack
/
app
/
Http
/
HttpRequestService.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
70 lines (60 loc) · 2.13 KB
Breadcrumbs
BookStack
/
app
/
Http
/
HttpRequestService.php
Copy path
File metadata and controls
70 lines (60 loc) · 2.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace
BookStack
\
Http
;
use
GuzzleHttp
\
Client
;
use
GuzzleHttp
\
Handler
\
MockHandler
;
use
GuzzleHttp
\
HandlerStack
;
use
GuzzleHttp
\
Middleware
;
use
GuzzleHttp
\
Psr7
\
Request
as
GuzzleRequest
;
use
GuzzleHttp
\
Psr7
\
Response
;
use
Psr
\
Http
\
Client
\
ClientInterface
;
class
HttpRequestService
{
protected
?
HandlerStack
$
handler
=
null
;
/**
* Build a new http client for sending requests on.
*/
public
function
buildClient
(
int
$
timeout
,
array
$
options
= []):
ClientInterface
{
$
defaultOptions
= [
'
timeout
'
=>
$
timeout
,
'
handler
'
=>
$
this
->
handler
,
];
return
new
Client
(
array_merge
(
$
options
,
$
defaultOptions
));
}
/**
* Create a new JSON http request for use with a client.
*/
public
function
jsonRequest
(
string
$
method
,
string
$
uri
,
array
$
data
):
GuzzleRequest
{
$
headers
= [
'
Content-Type
'
=>
'
application/json
'
];
return
new
GuzzleRequest
(
$
method
,
$
uri
,
$
headers
,
json_encode
(
$
data
));
}
/**
* Mock any http clients built from this service, and response with the given responses.
* Returns history which can then be queried.
* @link https://docs.guzzlephp.org/en/stable/testing.html#history-middleware
*/
public
function
mockClient
(
array
$
responses
= [],
bool
$
pad
=
true
):
HttpClientHistory
{
// By default, we pad out the responses with 10 successful values so that requests will be
// properly recorded for inspection. Otherwise, we can't later check if we're received
// too many requests.
if
(
$
pad
) {
$
response
=
new
Response
(
200
, [],
'
success
'
);
$
responses
=
array_merge
(
$
responses
,
array_fill
(
0
,
10
,
$
response
));
}
$
container
= [];
$
history
= Middleware::
history
(
$
container
);
$
mock
=
new
MockHandler
(
$
responses
);
$
this
->
handler
= HandlerStack::
create
(
$
mock
);
$
this
->
handler
->
push
(
$
history
,
'
history
'
);
return
new
HttpClientHistory
(
$
container
);
}
/**
* Clear mocking that has been set up for clients.
*/
public
function
clearMocking
():
void
{
$
this
->
handler
=
null
;
}
}
Back
|
FazBrowse Home
|
New Git URL