createRequest('GET', 'https://httpbin.org/get?alreadyhere=true'); $request->setHeader('X-Test', 'meow'); $request->setHeader('X-Test2', 'mewow'); $request->removeHeader('x-test2'); $response = $client->sendRequest($request); $this->assertEquals(200, $response->getStatusCode()); $this->assertTrue($response->hasHeader('Content-Type')); $this->assertEquals('application/json', $response->getHeader('Content-type')); $this->assertTrue($response->hasBody()); $body = $response->getJsonBody(); $this->assertIsObject($body); $body = $response->getJsonBody(true); $this->assertIsArray($body); $this->assertArrayHasKey('headers', $body); $this->assertIsArray($body['headers']); $this->assertArrayHasKey('X-Test', $body['headers']); $this->assertEquals('meow', $body['headers']['X-Test']); $this->assertArrayNotHasKey('X-Test2', $body['headers']); $this->assertArrayHasKey('args', $body); $this->assertIsArray($body['args']); $this->assertArrayHasKey('alreadyhere', $body['args']); $this->assertEquals('true', $body['args']['alreadyhere']); } public function testHttpFailStatus(): void { $client = new CurlHttpClient(FlashiiClient::userAgentString()); $request = $client->createRequest('GET', 'https://httpbin.org/status/404'); $response = $client->sendRequest($request); $this->assertEquals(404, $response->getStatusCode()); $request = $client->createRequest('POST', 'https://httpbin.org/status/503'); $response = $client->sendRequest($request); $this->assertEquals(503, $response->getStatusCode()); } public function testEnsureSuccess(): void { $client = new CurlHttpClient(FlashiiClient::userAgentString()); $request = $client->createRequest('PATCH', 'https://httpbin.org/status/204'); $response = $client->sendRequest($request); $this->assertTrue($response->isSuccessStatusCode()); $response->ensureSuccessStatusCode(); $request = $client->createRequest('DELETE', 'https://httpbin.org/status/418'); $response = $client->sendRequest($request); $this->assertFalse($response->isSuccessStatusCode()); $this->expectException(RuntimeException::class); $this->expectExceptionMessage('HTTP request returned status code 418.'); $response->ensureSuccessStatusCode(); } public function testAuthorizedHttp(): void { $realClient = new CurlHttpClient(FlashiiClient::userAgentString()); $client = new AuthorizedHttpClient($realClient, new BasicCredentials('freakyfurball', 'express1')); $this->assertFalse( $realClient->sendRequest( $realClient->createRequest('GET', 'https://httpbin.org/basic-auth/freakyfurball/express1') )->isSuccessStatusCode() ); $this->assertTrue( $client->sendRequest( $client->createRequest('GET', 'https://httpbin.org/basic-auth/freakyfurball/express1') )->isSuccessStatusCode() ); $this->assertFalse( $client->sendRequest( $client->createRequest('GET', 'https://httpbin.org/basic-auth/freakyfurball/express2') )->isSuccessStatusCode() ); $this->assertFalse( $realClient->sendRequest( $realClient->createRequest('GET', 'https://httpbin.org/bearer') )->isSuccessStatusCode() ); $accessToken = 'DSw7ih2fMfXW3fAkUKJitltBtc7JT8TA'; $client = new AuthorizedHttpClient($realClient, new BearerCredentials($accessToken)); $response = $client->sendRequest( $client->createRequest('GET', 'https://httpbin.org/bearer') ); $this->assertTrue($response->hasBody()); $body = $response->getJsonBody(true); $this->assertIsArray($body); $this->assertArrayHasKey('authenticated', $body); $this->assertIsBool($body['authenticated']); $this->assertTrue($body['authenticated']); $this->assertArrayHasKey('token', $body); $this->assertIsString($body['token']); $this->assertEquals($accessToken, $body['token']); } public function testHttpBody(): void { $client = new CurlHttpClient(FlashiiClient::userAgentString()); $random = base64_encode(random_bytes(100)); $request = $client->createRequest('PUT', 'https://httpbin.org/anything'); $request->setBody($random, 'application/x-test-data'); $response = $client->sendRequest($request); $this->assertTrue($response->hasBody()); $body = $response->getJsonBody(true); $this->assertIsArray($body); $this->assertArrayHasKey('method', $body); $this->assertIsString($body['method']); $this->assertEquals('PUT', $body['method']); $this->assertArrayHasKey('headers', $body); $this->assertIsArray($body['headers']); $this->assertArrayHasKey('Content-Type', $body['headers']); $this->assertIsString($body['headers']['Content-Type']); $this->assertEquals('application/x-test-data', $body['headers']['Content-Type']); $this->assertArrayHasKey('data', $body); $this->assertIsString($body['data']); $this->assertEquals($random, $body['data']); $request = $client->createRequest('POST', 'https://httpbin.org/post'); $request->setBodyParams([ 'string' => 'meow', 'more' => 'wowzers this one has spaces crazy', ]); $response = $client->sendRequest($request); $this->assertTrue($response->hasBody()); $body = $response->getJsonBody(true); $this->assertIsArray($body); $this->assertArrayHasKey('headers', $body); $this->assertIsArray($body['headers']); $this->assertArrayHasKey('Content-Type', $body['headers']); $this->assertIsString($body['headers']['Content-Type']); $this->assertEquals('application/x-www-form-urlencoded', $body['headers']['Content-Type']); $this->assertArrayHasKey('form', $body); $this->assertIsArray($body['form']); $this->assertArrayHasKey('string', $body['form']); $this->assertIsString($body['form']['string']); $this->assertEquals('meow', $body['form']['string']); $this->assertArrayHasKey('more', $body['form']); $this->assertIsString($body['form']['more']); $this->assertEquals('wowzers this one has spaces crazy', $body['form']['more']); } }