/** * Test send_request method. */ public function test_send_request() { $httpclient = new \pdyn\httpclient\tests\MockHttpClient(); $clientstorage = new StorageMock(); $client = new ClientMock($clientstorage); // Test successfuly 202 and 204 status codes. $httpclient->set_response(new \pdyn\httpclient\HttpClientResponse(202, 'text/plain', '')); $result = $client->send_request('subscribe', 'http://localhost/', 'testtopic', 'http://example.com/pubsub/123', $httpclient); $this->assertTrue($result); $httpclient->set_response(new \pdyn\httpclient\HttpClientResponse(204, 'text/plain', '')); $result = $client->send_request('subscribe', 'http://localhost/', 'testtopic', 'http://example.com/pubsub/123', $httpclient); $this->assertTrue($result); // Test failure conditions. $httpclient->set_response(new \pdyn\httpclient\HttpClientResponse(400, 'text/plain', '')); $result = $client->send_request('subscribe', 'http://localhost/', 'testtopic', 'http://example.com/pubsub/123', $httpclient); $this->assertInternalType('array', $result); $this->assertArrayHasKey('status_code', $result); $this->assertEquals(400, $result['status_code']); $this->assertArrayHasKey('msg', $result); $expectedrequestdata = ['hub.mode' => 'subscribe', 'hub.callback' => 'http://example.com/pubsub/123', 'hub.topic' => 'testtopic', 'hub.verify' => 'async', 'hub.lease_seconds' => 604800]; $actualrequestdata = $httpclient->get_request_data(); $this->assertArrayHasKey('hub.verify_token', $actualrequestdata); unset($actualrequestdata['hub.verify_token']); $this->assertEquals($expectedrequestdata, $actualrequestdata); }
/** * Test handle_request method. */ public function test_handle_request() { $storagemock = new MockStorage(); $server = new ServerMock($storagemock); $httpclientmock = new \pdyn\httpclient\tests\MockHttpClient(); $params = ['hub_mode' => 'testmode', 'hub_callback' => 'http://localhost/', 'hub_topic' => 'testtopic', 'hub_verify' => 'async', 'hub_verify_token' => '12345']; // Test successful handling. $responsefunc = function ($requestdata) { return $requestdata['hub.challenge']; }; $httpclientmock->set_response(new MockHttpClientResponse(200, 'text/plain', $responsefunc)); $actualrequest = $server->handle_request($params, $httpclientmock); $actualrequest->timerequested = 1; $expectedrequest = new \pdyn\pubsubhubbub\server\Request(); $expectedrequest->id = null; $expectedrequest->mode = 'testmode'; $expectedrequest->callback = 'http://localhost/'; $expectedrequest->topic = 'testtopic'; $expectedrequest->verifymode = 'async'; $expectedrequest->token = '12345'; $expectedrequest->timerequested = 1; $expectedrequest->leaseseconds; $this->assertEquals($expectedrequest, $actualrequest); // Test failed verification. $httpclientmock->set_response(new MockHttpClientResponse(200, 'text/plain', 'badresponse')); $actualrequest = $server->handle_request($params, $httpclientmock); $this->assertEquals(412, $server->last_response['code']); // Test bad request mode. $params['hub_mode'] = 'badmode'; $responsefunc = function ($requestdata) { return $requestdata['hub.challenge']; }; $httpclientmock->set_response(new MockHttpClientResponse(200, 'text/plain', $responsefunc)); $actualrequest = $server->handle_request($params, $httpclientmock); $this->assertEquals(501, $server->last_response['code']); }