|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace ApiClients\Tests\Middleware\Xml; |
| 4 | + |
| 5 | +use ApiClients\Middleware\Xml\Options; |
| 6 | +use ApiClients\Middleware\Xml\XmlDecodeMiddleware; |
| 7 | +use ApiClients\Middleware\Xml\XmlStream; |
| 8 | +use ApiClients\Tools\TestUtilities\TestCase; |
| 9 | +use React\EventLoop\Factory; |
| 10 | +use RingCentral\Psr7\Response; |
| 11 | +use function Clue\React\Block\await; |
| 12 | + |
| 13 | +class XmlDecodeMiddlewareTest extends TestCase |
| 14 | +{ |
| 15 | + public function provideValidJsonContentTypes() |
| 16 | + { |
| 17 | + yield ['text/xml']; |
| 18 | + yield ['text/xml; charset=utf-8']; |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * @dataProvider provideValidJsonContentTypes |
| 23 | + */ |
| 24 | + public function testPost(string $contentType) |
| 25 | + { |
| 26 | + $loop = Factory::create(); |
| 27 | + $middleware = new XmlDecodeMiddleware(); |
| 28 | + $response = new Response(200, ['Content-Type' => $contentType], Constant::XML); |
| 29 | + |
| 30 | + $body = await( |
| 31 | + $middleware->post($response, 'abc'), |
| 32 | + $loop |
| 33 | + )->getBody(); |
| 34 | + |
| 35 | + self::assertInstanceOf(XmlStream::class, $body); |
| 36 | + |
| 37 | + self::assertSame( |
| 38 | + Constant::TREE, |
| 39 | + $body->getParsedContents() |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + public function testPostNoContentType() |
| 44 | + { |
| 45 | + $loop = Factory::create(); |
| 46 | + $middleware = new XmlDecodeMiddleware(); |
| 47 | + $response = new Response(200, [], '[]'); |
| 48 | + |
| 49 | + self::assertSame( |
| 50 | + $response, |
| 51 | + await( |
| 52 | + $middleware->post($response, 'abc'), |
| 53 | + $loop |
| 54 | + ) |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + public function testPostNoContentTypeCheck() |
| 59 | + { |
| 60 | + $loop = Factory::create(); |
| 61 | + $middleware = new XmlDecodeMiddleware(); |
| 62 | + $response = new Response(200, [], Constant::XML); |
| 63 | + |
| 64 | + $body = await( |
| 65 | + $middleware->post( |
| 66 | + $response, |
| 67 | + 'abc', |
| 68 | + [ |
| 69 | + XmlDecodeMiddleware::class => [ |
| 70 | + Options::NO_CONTENT_TYPE_CHECK => true, |
| 71 | + ], |
| 72 | + ] |
| 73 | + ), |
| 74 | + $loop |
| 75 | + )->getBody(); |
| 76 | + |
| 77 | + self::assertInstanceOf(XmlStream::class, $body); |
| 78 | + |
| 79 | + self::assertSame( |
| 80 | + Constant::TREE, |
| 81 | + $body->getParsedContents() |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + public function testPostCustomTYpe() |
| 86 | + { |
| 87 | + $loop = Factory::create(); |
| 88 | + $middleware = new XmlDecodeMiddleware(); |
| 89 | + $response = new Response(200, ['Content-Type' => 'custom/type'], Constant::XML); |
| 90 | + |
| 91 | + $body = await( |
| 92 | + $middleware->post( |
| 93 | + $response, |
| 94 | + 'abc', |
| 95 | + [ |
| 96 | + XmlDecodeMiddleware::class => [ |
| 97 | + Options::CONTENT_TYPE => 'custom/type', |
| 98 | + ], |
| 99 | + ] |
| 100 | + ), |
| 101 | + $loop |
| 102 | + )->getBody(); |
| 103 | + |
| 104 | + self::assertInstanceOf(XmlStream::class, $body); |
| 105 | + |
| 106 | + self::assertSame( |
| 107 | + Constant::TREE, |
| 108 | + $body->getParsedContents() |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + public function testPostNoJson() |
| 113 | + { |
| 114 | + $loop = Factory::create(); |
| 115 | + $middleware = new XmlDecodeMiddleware(); |
| 116 | + $response = new Response(200, []); |
| 117 | + |
| 118 | + self::assertSame( |
| 119 | + $response, |
| 120 | + await( |
| 121 | + $middleware->post($response, 'abc'), |
| 122 | + $loop |
| 123 | + ) |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + public function testPostEmpty() |
| 128 | + { |
| 129 | + $loop = Factory::create(); |
| 130 | + $middleware = new XmlDecodeMiddleware(); |
| 131 | + $response = new Response(200, [], ''); |
| 132 | + |
| 133 | + self::assertSame( |
| 134 | + '', |
| 135 | + (string)await( |
| 136 | + $middleware->post($response, 'abc'), |
| 137 | + $loop |
| 138 | + )->getBody() |
| 139 | + ); |
| 140 | + } |
| 141 | +} |
0 commit comments