E
E
Eugene2020-05-01 16:01:57
Laravel
Eugene, 2020-05-01 16:01:57

Laravel ui:auth - how to return json?

I'm trying to build a spa application using Laravel + vue
I'm writing a test, I'm doing in the test:

public function a_guest_can_create_an_account()
    {
          $response = $this->post('/register', [
                    'name' => 'Test',
                    'email' => '[email protected]',
                    'password' => '1234567890',
        ]);

        $response->assertStatus(201);

This test gives status 302, and I would like to get 201.

I want the back to answer me with json. This logic is written in the registration module supplied "together" with laravel (lavavel/ui/auth-backend/RegisersUsers.php).
The following block in this module is responsible for registering and sending a response:
public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        if ($response = $this->registered($request, $user)) {
            return $response;
        }

        return $request->wantsJson()
                    ? new Response('', 201)
                    : redirect($this->redirectPath());
    }


Question - what to write in the test so that in the last paragraph
return $request->wantsJson()
                    ? new Response('', 201)
                    : redirect($this->redirectPath());
worked the line "? new Response('', 201)" instead of the line " redirect($this->redirectPath());" and I got the desired 201 status in the test?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2020-05-01
@dnevnick

With json methods

$this->postJson(route('register'), $data)
     ->assertCreated();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question