Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swoole] Prepare request uri with query string #139

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/swoole/src/SymfonyHttpBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public static function convertSwooleRequest(Request $request): SymfonyRequest
);
$sfRequest->headers = new HeaderBag($request->header ?? []);

self::prepareRequestUriWithQueryString($sfRequest);

return $sfRequest;
}

Expand Down Expand Up @@ -62,4 +64,20 @@ public static function reflectSymfonyResponse(SymfonyResponse $sfResponse, Respo
$response->end($sfResponse->getContent());
}
}

/**
* REQUEST_URI returns the uri path only, should append with the query string if exists.
*/
private static function prepareRequestUriWithQueryString(SymfonyRequest $sfRequest): void
{
if (!str_contains($sfRequest->server->get('REQUEST_URI', ''), '?')
&& $sfRequest->server->has('QUERY_STRING')
&& strlen($sfRequest->server->get('QUERY_STRING')) > 0
) {
$sfRequest->server->set(
'REQUEST_URI',
sprintf('%s?%s', $sfRequest->server->get('REQUEST_URI'), $sfRequest->server->get('QUERY_STRING')),
);
}
}
}
15 changes: 15 additions & 0 deletions src/swoole/tests/Unit/SymfonyHttpBridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,19 @@ public function testStreamedResponseWillRespondWithOneChunkAtATime(): void

SymfonyHttpBridge::reflectSymfonyResponse($sfResponse, $response);
}

public function testThatSwooleRequestedWithQuery(): void
{
$request = $this->createMock(Request::class);
$request->server = [
'REQUEST_URI' => '/dummy',
'QUERY_STRING' => 'test=1',
];

$sfRequest = SymfonyHttpBridge::convertSwooleRequest($request);

$this->assertSame('test=1', $sfRequest->server->get('QUERY_STRING'));
$this->assertSame('/dummy?test=1', $sfRequest->server->get('REQUEST_URI'));
$this->assertSame('/dummy?test=1', $sfRequest->getRequestUri());
}
}