0

I use rest API.

For example, requwest http://api/events/?api_token=KFvcc10H4l return me json.

But in test I get error 302

   /**
     * @return void
     */
    public function testReturnOneEventsItem()
    {
         $this->get( '/events/3?api_token=KFvcc10H4l')
                //  $response = $this->get('http://api/events');
                ->assertStatus(200);
    }

Solution:

Need wreti data for test. Unit test not use you database.

$event = factory(Event::class)->create();
        $user = factory(User::class)->create();
        $this->get('events/' . $event->id . '?&api_token=' . $user->api_token)
                ->assertStatus(200)->assertJson([
                        'id' => $event->id,
                        'title' => $event->title,
                        'city_id' => strval($event->city_id),
                ]);;
sapef44489
  • 69
  • 9

1 Answers1

0

You are getting status code 302 because the resource you are requesting for has already been moved to a temporal location. Your code will successfully return the requested API but still return a status code 302 and I actually think this is because it has already been stored in the location requesting for it.

If a source request for an API for the first time and receives the requested resource successfully, status code 200 is returned. Subsequent calls, therefore, will only return status code 302.

Follow the link to read more about status code 302.

STA
  • 30,729
  • 8
  • 45
  • 59
  • Now 401. I use ->middleware('auth:api')- for this route and not understand, why it work for external requests, but not work for Features test – sapef44489 Jul 04 '20 at 17:54
  • After you added the `auth-api` middleware did it still work for just test or it stopped working for both test and external requests? – CharlesNnanna Jul 05 '20 at 06:49
  • I understand. For test Laravel use temp database, And for the test, you first need to write the data in the test itself. – sapef44489 Jul 05 '20 at 08:23