PrepareBodyMiddlewareTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace GuzzleHttp\Tests;
  3. use GuzzleHttp\Handler\MockHandler;
  4. use GuzzleHttp\HandlerStack;
  5. use GuzzleHttp\Middleware;
  6. use GuzzleHttp\Promise\PromiseInterface;
  7. use GuzzleHttp\Psr7;
  8. use GuzzleHttp\Psr7\FnStream;
  9. use GuzzleHttp\Psr7\Request;
  10. use GuzzleHttp\Psr7\Response;
  11. use Psr\Http\Message\RequestInterface;
  12. use PHPUnit\Framework\TestCase;
  13. class PrepareBodyMiddlewareTest extends TestCase
  14. {
  15. public function methodProvider()
  16. {
  17. $methods = ['GET', 'PUT', 'POST'];
  18. $bodies = ['Test', ''];
  19. foreach ($methods as $method) {
  20. foreach ($bodies as $body) {
  21. yield [$method, $body];
  22. }
  23. }
  24. }
  25. /**
  26. * @dataProvider methodProvider
  27. */
  28. public function testAddsContentLengthWhenMissingAndPossible($method, $body)
  29. {
  30. $h = new MockHandler([
  31. function (RequestInterface $request) use ($body) {
  32. $length = strlen($body);
  33. if ($length > 0) {
  34. $this->assertEquals($length, $request->getHeaderLine('Content-Length'));
  35. } else {
  36. $this->assertFalse($request->hasHeader('Content-Length'));
  37. }
  38. return new Response(200);
  39. }
  40. ]);
  41. $m = Middleware::prepareBody();
  42. $stack = new HandlerStack($h);
  43. $stack->push($m);
  44. $comp = $stack->resolve();
  45. $p = $comp(new Request($method, 'http://www.google.com', [], $body), []);
  46. $this->assertInstanceOf(PromiseInterface::class, $p);
  47. $response = $p->wait();
  48. $this->assertSame(200, $response->getStatusCode());
  49. }
  50. public function testAddsTransferEncodingWhenNoContentLength()
  51. {
  52. $body = FnStream::decorate(Psr7\stream_for('foo'), [
  53. 'getSize' => function () { return null; }
  54. ]);
  55. $h = new MockHandler([
  56. function (RequestInterface $request) {
  57. $this->assertFalse($request->hasHeader('Content-Length'));
  58. $this->assertSame('chunked', $request->getHeaderLine('Transfer-Encoding'));
  59. return new Response(200);
  60. }
  61. ]);
  62. $m = Middleware::prepareBody();
  63. $stack = new HandlerStack($h);
  64. $stack->push($m);
  65. $comp = $stack->resolve();
  66. $p = $comp(new Request('PUT', 'http://www.google.com', [], $body), []);
  67. $this->assertInstanceOf(PromiseInterface::class, $p);
  68. $response = $p->wait();
  69. $this->assertSame(200, $response->getStatusCode());
  70. }
  71. public function testAddsContentTypeWhenMissingAndPossible()
  72. {
  73. $bd = Psr7\stream_for(fopen(__DIR__ . '/../composer.json', 'r'));
  74. $h = new MockHandler([
  75. function (RequestInterface $request) {
  76. $this->assertSame('application/json', $request->getHeaderLine('Content-Type'));
  77. $this->assertTrue($request->hasHeader('Content-Length'));
  78. return new Response(200);
  79. }
  80. ]);
  81. $m = Middleware::prepareBody();
  82. $stack = new HandlerStack($h);
  83. $stack->push($m);
  84. $comp = $stack->resolve();
  85. $p = $comp(new Request('PUT', 'http://www.google.com', [], $bd), []);
  86. $this->assertInstanceOf(PromiseInterface::class, $p);
  87. $response = $p->wait();
  88. $this->assertSame(200, $response->getStatusCode());
  89. }
  90. public function expectProvider()
  91. {
  92. return [
  93. [true, ['100-Continue']],
  94. [false, []],
  95. [10, ['100-Continue']],
  96. [500000, []]
  97. ];
  98. }
  99. /**
  100. * @dataProvider expectProvider
  101. */
  102. public function testAddsExpect($value, $result)
  103. {
  104. $bd = Psr7\stream_for(fopen(__DIR__ . '/../composer.json', 'r'));
  105. $h = new MockHandler([
  106. function (RequestInterface $request) use ($result) {
  107. $this->assertSame($result, $request->getHeader('Expect'));
  108. return new Response(200);
  109. }
  110. ]);
  111. $m = Middleware::prepareBody();
  112. $stack = new HandlerStack($h);
  113. $stack->push($m);
  114. $comp = $stack->resolve();
  115. $p = $comp(new Request('PUT', 'http://www.google.com', [], $bd), [
  116. 'expect' => $value
  117. ]);
  118. $this->assertInstanceOf(PromiseInterface::class, $p);
  119. $response = $p->wait();
  120. $this->assertSame(200, $response->getStatusCode());
  121. }
  122. public function testIgnoresIfExpectIsPresent()
  123. {
  124. $bd = Psr7\stream_for(fopen(__DIR__ . '/../composer.json', 'r'));
  125. $h = new MockHandler([
  126. function (RequestInterface $request) {
  127. $this->assertSame(['Foo'], $request->getHeader('Expect'));
  128. return new Response(200);
  129. }
  130. ]);
  131. $m = Middleware::prepareBody();
  132. $stack = new HandlerStack($h);
  133. $stack->push($m);
  134. $comp = $stack->resolve();
  135. $p = $comp(
  136. new Request('PUT', 'http://www.google.com', ['Expect' => 'Foo'], $bd),
  137. ['expect' => true]
  138. );
  139. $this->assertInstanceOf(PromiseInterface::class, $p);
  140. $response = $p->wait();
  141. $this->assertSame(200, $response->getStatusCode());
  142. }
  143. }