JsonLocationTest.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace GuzzleHttp\Tests\Command\Guzzle\RequestLocation;
  3. use GuzzleHttp\Command\Command;
  4. use GuzzleHttp\Command\Guzzle\Operation;
  5. use GuzzleHttp\Command\Guzzle\Parameter;
  6. use GuzzleHttp\Command\Guzzle\RequestLocation\JsonLocation;
  7. use GuzzleHttp\Psr7\Request;
  8. /**
  9. * @covers \GuzzleHttp\Command\Guzzle\RequestLocation\JsonLocation
  10. * @covers \GuzzleHttp\Command\Guzzle\RequestLocation\AbstractLocation
  11. */
  12. class JsonLocationTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @group RequestLocation
  16. */
  17. public function testVisitsLocation()
  18. {
  19. $location = new JsonLocation('json');
  20. $command = new Command('foo', ['foo' => 'bar']);
  21. $request = new Request('POST', 'http://httbin.org');
  22. $param = new Parameter(['name' => 'foo']);
  23. $location->visit($command, $request, $param);
  24. $operation = new Operation();
  25. $request = $location->after($command, $request, $operation);
  26. $this->assertEquals('{"foo":"bar"}', $request->getBody()->getContents());
  27. $this->assertArraySubset([0 => 'application/json'], $request->getHeader('Content-Type'));
  28. }
  29. /**
  30. * @group RequestLocation
  31. */
  32. public function testVisitsAdditionalProperties()
  33. {
  34. $location = new JsonLocation('json', 'foo');
  35. $command = new Command('foo', ['foo' => 'bar']);
  36. $command['baz'] = ['bam' => [1]];
  37. $request = new Request('POST', 'http://httbin.org');
  38. $param = new Parameter(['name' => 'foo']);
  39. $location->visit($command, $request, $param);
  40. $operation = new Operation([
  41. 'additionalParameters' => [
  42. 'location' => 'json'
  43. ]
  44. ]);
  45. $request = $location->after($command, $request, $operation);
  46. $this->assertEquals('{"foo":"bar","baz":{"bam":[1]}}', $request->getBody()->getContents());
  47. $this->assertEquals([0 => 'foo'], $request->getHeader('Content-Type'));
  48. }
  49. /**
  50. * @group RequestLocation
  51. */
  52. public function testVisitsNestedLocation()
  53. {
  54. $location = new JsonLocation('json');
  55. $command = new Command('foo', ['foo' => 'bar']);
  56. $request = new Request('POST', 'http://httbin.org');
  57. $param = new Parameter([
  58. 'name' => 'foo',
  59. 'type' => 'object',
  60. 'properties' => [
  61. 'baz' => [
  62. 'type' => 'array',
  63. 'items' => [
  64. 'type' => 'string',
  65. 'filters' => ['strtoupper']
  66. ]
  67. ]
  68. ],
  69. 'additionalProperties' => [
  70. 'type' => 'array',
  71. 'items' => [
  72. 'type' => 'string',
  73. 'filters' => ['strtolower']
  74. ]
  75. ]
  76. ]);
  77. $command['foo'] = [
  78. 'baz' => ['a', 'b'],
  79. 'bam' => ['A', 'B'],
  80. ];
  81. $location->visit($command, $request, $param);
  82. $operation = new Operation();
  83. $request = $location->after($command, $request, $operation);
  84. $this->assertEquals('{"foo":{"baz":["A","B"],"bam":["a","b"]}}', (string) $request->getBody()->getContents());
  85. $this->assertEquals([0 => 'application/json'], $request->getHeader('Content-Type'));
  86. }
  87. }