Summer Sale! All accounts are 50% off this week.

peterdickins's avatar

Mocking the domain

I have an app where different domains can access my api, and I have created the following middleware to detect this:

class ValidateClient
{
    public function handle(Request $request, Closure $next)
    {
        $client = Client::where('domain', $request->getHost())->first();

        if ($client) {
            Config::set('client', $client->toArray());
        }

        return $next($request);
    }
}

How can I write a test that mocks a domain so I can test my middleware?

I have tried the following in my test but this doesn't seem to work:

$client = Client::factory()->create();

$request = \Mockery::mock(Request::class, ['getHost' => $client->domain]);

$this->app->instance(Request::class, $request);

$response = $this->get(route('report.index'));

dd($response->getContent());

But if I dump the request data

dd($request->getHost());

in my middleware I just get

localhost

Any help would be much appreciated...

0 likes
1 reply

Please or to participate in this conversation.