beerbuddha's avatar

Better way to submit json data in post test

I currently have this testable piece of code to submit a json filled data via post. I let my controller do its thing and respond back.

$responseKey = 'abcdabcd6b1649f681a408f1beebabcd';
        $sendJson    = $this->createSaleResponse($responseKey);
        $sendHeaders = [ 'CONTENT_TYPE' => 'application/json' ];
        $response = $this->call(
            'POST',
            'ext-purchase-api/api/roku/notifications',
            [],
            [],
            [],
            $sendHeaders,
            $sendJson
        );
        $this->assertEquals(200, $response->status());
        $receiveData = $response->getContent();
        $this->assertEquals($responseKey, $receiveData);
        $response->assertHeader('ApiKey', $secret);

I am used to the restful API of $this->get and $this->post - is there a better way to format this code?

Note the solution was inspired by: https://stackoverflow.com/questions/46289156/lumen-5-5-unit-test-post-json

0 likes
1 reply
wilburpowery's avatar

$this->getJson() and $this->postJson()?

Are you using Laravel? We need a little more info about what you're using. :)

Please or to participate in this conversation.