@@ -134,7 +134,7 @@ public function file(string $uri, array $data = [], array $headers = [])
134134 public function request (string $ method , string $ path , array $ options = [])
135135 {
136136 return wait (function () use ($ method , $ path , $ options ) {
137- return $ this ->execute ($ this ->init ($ method , $ path , $ options ));
137+ return $ this ->execute ($ this ->initRequest ($ method , $ path , $ options ));
138138 }, $ this ->waitTimeout );
139139 }
140140
@@ -145,6 +145,50 @@ public function sendRequest(ServerRequestInterface $psr7Request): ResponseInterf
145145 }, $ this ->waitTimeout );
146146 }
147147
148+ public function initRequest (string $ method , string $ path , array $ options = []): ServerRequestInterface
149+ {
150+ $ query = $ options ['query ' ] ?? [];
151+ $ params = $ options ['form_params ' ] ?? [];
152+ $ json = $ options ['json ' ] ?? [];
153+ $ headers = $ options ['headers ' ] ?? [];
154+ $ multipart = $ options ['multipart ' ] ?? [];
155+
156+ $ parsePath = parse_url ($ path );
157+ $ path = $ parsePath ['path ' ];
158+ $ uriPathQuery = $ parsePath ['query ' ] ?? [];
159+ if (! empty ($ uriPathQuery )) {
160+ parse_str ($ uriPathQuery , $ pathQuery );
161+ $ query = array_merge ($ pathQuery , $ query );
162+ }
163+
164+ $ data = $ params ;
165+
166+ // Initialize PSR-7 Request and Response objects.
167+ $ uri = (new Uri ($ this ->baseUri . ltrim ($ path , '/ ' )))->withQuery (http_build_query ($ query ));
168+
169+ $ content = http_build_query ($ params );
170+ if ($ method == 'POST ' && data_get ($ headers , 'Content-Type ' ) == 'application/json ' ) {
171+ $ content = json_encode ($ json , JSON_UNESCAPED_UNICODE );
172+ $ data = $ json ;
173+ }
174+
175+ $ body = new SwooleStream ($ content );
176+
177+ $ request = new Psr7Request ($ method , $ uri , $ headers , $ body );
178+
179+ return $ request ->withQueryParams ($ query )
180+ ->withParsedBody ($ data )
181+ ->withUploadedFiles ($ this ->normalizeFiles ($ multipart ));
182+ }
183+
184+ /**
185+ * @deprecated It will be removed in v3.0
186+ */
187+ protected function init (string $ method , string $ path , array $ options = []): ServerRequestInterface
188+ {
189+ return $ this ->initRequest ($ method , $ path , $ options );
190+ }
191+
148192 protected function execute (ServerRequestInterface $ psr7Request ): ResponseInterface
149193 {
150194 $ this ->persistToContext ($ psr7Request , new Psr7Response ());
@@ -188,41 +232,6 @@ protected function initBaseUri(string $server): void
188232 }
189233 }
190234
191- protected function init (string $ method , string $ path , array $ options = []): ServerRequestInterface
192- {
193- $ query = $ options ['query ' ] ?? [];
194- $ params = $ options ['form_params ' ] ?? [];
195- $ json = $ options ['json ' ] ?? [];
196- $ headers = $ options ['headers ' ] ?? [];
197- $ multipart = $ options ['multipart ' ] ?? [];
198-
199- $ parsePath = parse_url ($ path );
200- $ path = $ parsePath ['path ' ];
201- $ uriPathQuery = $ parsePath ['query ' ] ?? [];
202- if (! empty ($ uriPathQuery )) {
203- parse_str ($ uriPathQuery , $ pathQuery );
204- $ query = array_merge ($ pathQuery , $ query );
205- }
206-
207- $ data = $ params ;
208-
209- // Initialize PSR-7 Request and Response objects.
210- $ uri = (new Uri ($ this ->baseUri . ltrim ($ path , '/ ' )))->withQuery (http_build_query ($ query ));
211-
212- $ content = http_build_query ($ params );
213- if ($ method == 'POST ' && data_get ($ headers , 'Content-Type ' ) == 'application/json ' ) {
214- $ content = json_encode ($ json , JSON_UNESCAPED_UNICODE );
215- $ data = $ json ;
216- }
217-
218- $ body = new SwooleStream ($ content );
219-
220- $ request = new Psr7Request ($ method , $ uri , $ headers , $ body );
221- return $ request ->withQueryParams ($ query )
222- ->withParsedBody ($ data )
223- ->withUploadedFiles ($ this ->normalizeFiles ($ multipart ));
224- }
225-
226235 protected function normalizeFiles (array $ multipart ): array
227236 {
228237 $ files = [];
0 commit comments