2

I install Symfony 4 and then API Platform.

Then I create Test class like this

class UserFunctionalTest extends WebTestCase
{
    /** @var string  */
    protected $host = "https://wikaunting-api.local";

    /** @var KernelBrowser */
    protected $client;

    protected function setUp()
    {
        $this->client = static::createClient();
    }

    public function testCreateUser()
    {
        $response = $this->client->request('POST', $this->host . '/api/users.json', [
            'json' => [
                'username' => 'jamielann1',
                'email' => 'test@example.com',
                'password' => 'jamielann1',
            ],
        ]);

        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
    }
}

When I run ./bin/phpunit, I got error message

Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException: "The content-type "application/x-www-form-urlencoded" is not supported. Supported MIME types are "application/ld+json", "application/json", "text/html"." at /home/vagrant/Code/testcode/vendor/api-platform/core/src/EventListener/DeserializeListener.php line 130

My question is, why it is not received as application/json? What is the proper way?

Matteo
  • 37,680
  • 11
  • 100
  • 115
Permana
  • 1,972
  • 2
  • 33
  • 51
  • May refer to: https://stackoverflow.com/questions/41713684/how-to-pass-json-in-post-method-with-phpunit-testing – L01C Sep 11 '19 at 08:53
  • I see. I try to use what currently available, but if all doesn't work, I'll try Guzzle. – Permana Sep 11 '19 at 09:12
  • 1
    Hi @Permana have you checked also the second part of the suggested answer about the `client bundled with Symfony `? – Matteo Sep 11 '19 at 09:21
  • Hi @Matteo, I just tried using the second part. Yep, and it works. The request now recognized as `application/json`. Thank you for mentioning the second part. – Permana Sep 12 '19 at 00:46

2 Answers2

2

From https://symfony.com/doc/current/testing.html#working-with-the-test-client

// submits a raw JSON string in the request body
$client->request(
    'POST',
    '/submit',
    [],
    [],
    ['CONTENT_TYPE' => 'application/json'],
    '{"name":"Fabien"}'
);
Calamar
  • 1,547
  • 1
  • 13
  • 25
0

you can set the Content-Type header and set it to one of the json types (see your error message), the key to put the headers in, is headers

    $response = $this->client->request('POST', $this->host . '/api/users.json', [
        'json' => [
            'username' => 'jamielann1',
            'email' => 'test@example.com',
            'password' => 'jamielann1',
        ],
        'headers' => [
            'Content-Type' => 'application/json', 
            // or just 'Content-Type: application/json', without the key
        ],
    ]);

sadly the Symfony\Contracts\HttpClient\HttpClientInterface says something to the json parameter:

'json' => null,  // array|\JsonSerializable - when set, implementations MUST set the "body" option to
                 //   the JSON-encoded value and set the "content-type" headers to a JSON-compatible
                 //   value if they are not defined - typically "application/json"

which apparently doesn't work as expected ...

Jakumi
  • 8,043
  • 2
  • 15
  • 32
  • I tried to explicitly add the `headers` application/json, but still return the same result. – Permana Sep 11 '19 at 09:09
  • can you check the var/log/test.log if it possibly contains the request and see if it maybe is not authenticated or something? – Jakumi Sep 11 '19 at 09:14
  • I'm using Homestead, so I check `/var/log/nginx/MYHOSTNAME.log` and it only contain error for past event. I now can make it work using https://stackoverflow.com/questions/41713684/how-to-pass-json-in-post-method-with-phpunit-testing – Permana Sep 12 '19 at 00:51