-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathDevtoolsClientTest.php
More file actions
167 lines (137 loc) · 5.48 KB
/
DevtoolsClientTest.php
File metadata and controls
167 lines (137 loc) · 5.48 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace ChromeDevtoolsProtocol;
use ChromeDevtoolsProtocol\Exception\DeadlineException;
use ChromeDevtoolsProtocol\Exception\ErrorException;
use ChromeDevtoolsProtocol\Instance\Launcher;
use ChromeDevtoolsProtocol\Model\Network\EnableRequest;
use ChromeDevtoolsProtocol\Model\Network\GetResponseBodyRequest;
use ChromeDevtoolsProtocol\Model\Network\LoadingFinishedEvent;
use ChromeDevtoolsProtocol\Model\Page\NavigateRequest;
use ChromeDevtoolsProtocol\Model\Security\CertificateErrorActionEnum;
use ChromeDevtoolsProtocol\Model\Security\CertificateErrorEvent;
use ChromeDevtoolsProtocol\Model\Security\HandleCertificateErrorRequest;
use ChromeDevtoolsProtocol\Model\Security\SetOverrideCertificateErrorsRequest;
use ChromeDevtoolsProtocol\WebSocket\WebSocketClient;
use PHPUnit\Framework\TestCase;
use Wrench\Payload\Payload;
class DevtoolsClientTest extends TestCase
{
public function testHandleCertificateError()
{
$ctx = Context::withTimeout(Context::background(), 10);
$launcher = new Launcher();
$instance = $launcher->launch($ctx);
try {
$tab = $instance->tabs($ctx)[0];
$client = $tab->devtools();
try {
$client->page()->enable($ctx);
$client->network()->enable($ctx, EnableRequest::make());
$client->security()->enable($ctx);
$client->security()->setOverrideCertificateErrors(
$ctx,
SetOverrideCertificateErrorsRequest::builder()
->setOverride(true)
->build()
);
$client->security()->addCertificateErrorListener(function (CertificateErrorEvent $ev) use ($ctx, $client) {
$client->security()->handleCertificateError(
$ctx,
HandleCertificateErrorRequest::builder()
->setEventId($ev->eventId)
->setAction(CertificateErrorActionEnum::CONTINUE)
->build()
);
});
$client->page()->navigate($ctx, NavigateRequest::builder()->setUrl("https://untrusted-root.badssl.com/")->build());
$client->page()->awaitLoadEventFired($ctx);
$this->assertTrue(true, "Ok, handling certificate errors works");
} finally {
$client->close();
}
} finally {
$instance->close();
}
}
public function testErrorHandling()
{
$this->expectException(ErrorException::class);
$ctx = Context::withTimeout(Context::background(), 10);
$launcher = new Launcher();
$instance = $launcher->launch($ctx);
try {
$tab = $instance->tabs($ctx)[0];
/** @var DevtoolsClientInterface & InternalClientInterface $client */
$client = $tab->devtools();
try {
$client->executeCommand($ctx, "SomeCommand.thatDoesNotExist", new \stdClass());
} finally {
$client->close();
}
} finally {
$instance->close();
}
}
/*
* https://github.com/jakubkulhan/chrome-devtools-protocol/issues/14
*/
public function testConflictBetweenAwaitAndExecute()
{
// websocket messages
$responses = [];
$addResponse = function($payload) use(&$responses) {
$payloadStub = $this->getMockBuilder(Payload::class)
->disableOriginalConstructor()
->getMock();
$payloadStub->method('getPayload')->willReturn($payload);
$responses[] = $payloadStub;
};
// create a stub websocket client, which returns predefined messages
$wsClient = $this->getMockBuilder(WebSocketClient::class)
->disableOriginalConstructor()
->getMock();
$wsClient->method('receive')->willReturnCallback(function() use(&$responses) {
if (!empty($responses)) {
return [array_shift($responses)];
}
return null;
});
$wsClient->method('setDeadline')->willReturnCallback(function($deadline) {
$timeout = floatval($deadline->format("U.u")) - microtime(true);
if ($timeout < 0.0) {
throw new DeadlineException("Socket deadline reached.");
}
});
// create the failing scenario
$ctx = Context::withTimeout(Context::background(), 3);
$client = new DevtoolsClient($wsClient);
register_shutdown_function(function () use ($client) { $client->close(); });
$addResponse('{"id":1,"result":{}}');
$client->page()->enable($ctx);
$addResponse('{"id":2,"result":{}}');
$client->network()->enable($ctx, EnableRequest::make());
$client->network()->addLoadingFinishedListener(function (LoadingFinishedEvent $event) use($ctx, $client, $addResponse) { // <- 2
// The order of these responses matters, if the loadEventFired comes first, awaitLoadEventFired (1) would wait forever
$addResponse('{"method":"Page.loadEventFired","params":{"timestamp":6758.846787}}');
$addResponse('{"id":4,"result":{"body":"..."}}');
$client->network()->getResponseBody($ctx, GetResponseBodyRequest::builder() // <- 3
->setRequestId($event->requestId)
->build()
);
});
$url = 'https://www.google.com';
$addResponse('{"id":3,"result":{"frameId":"1E56ACDD9B3B7F678F972C0EF0782649","loaderId":"AAF889CAE5B10663CA8D383A6125AC1B"}}');
$client->page()->navigate($ctx, NavigateRequest::builder()->setUrl($url)->build());
$addResponse('{"method":"Network.loadingFinished","params":{"requestId":"AAF889CAE5B10663CA8D383A6125AC1B","timestamp":6758.623335,"encodedDataLength":67174,"shouldReportCorbBlocking":false}}');
$client->page()->awaitLoadEventFired($ctx); // <- 1
$this->assertTrue((bool)'No conflict');
/*
1) we are waiting for the LoadEvent, but in the meanwhile the LoadingFinishedEvent arrives ->
2) so the listener is called
3) getResponseBody command is executed
- if the response is received before LoadEvent, everything is fine
- but if the LoadEvent is happening before the response is received,
the LoadEvent is dropped (not buffered) and awaitLoadEventFired could never return
*/
}
}