withHeader('Content-Type', 'application/json') ->withBody( new CallbackStream(function () use ($json) { return json_encode($json); }) ); } protected function requestAsUser(Request $req, int $userId): Request { $token = Str::random(40); /** * We insert this directly instead of via `prepareDatabase` * so that requests can be created/sent after the app is booted. */ $this->database()->table('access_tokens')->insert([ 'token' => $token, 'user_id' => $userId, 'created_at' => Carbon::now()->toDateTimeString(), 'last_activity_at' => Carbon::now()->toDateTimeString(), 'lifetime_seconds' => 3600 ]); return $req->withAddedHeader('Authorization', "Token {$token}"); } protected function requestWithCookiesFrom(Request $req, Response $previous): Request { $cookies = array_reduce( $previous->getHeader('Set-Cookie'), function ($memo, $setCookieString) { $setCookie = SetCookie::fromSetCookieString($setCookieString); $memo[$setCookie->getName()] = $setCookie->getValue(); return $memo; }, [] ); return $req->withCookieParams($cookies); } }