Skip to content

Commit 62bf542

Browse files
authored
Merge pull request #620 from KnifeLemon/patch-1
Added PUT, PATCH, DELETE methods for data
2 parents da6540b + ce088c8 commit 62bf542

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

flight/net/Request.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ public function init(array $properties = []): self
211211
$this->data->setData($data);
212212
}
213213
}
214+
// Check PUT, PATCH, DELETE for application/x-www-form-urlencoded data
215+
} else if (in_array($this->method, [ 'PUT', 'DELETE', 'PATCH' ], true) === true) {
216+
$body = $this->getBody();
217+
if ($body !== '') {
218+
$data = [];
219+
parse_str($body, $data);
220+
$this->data->setData($data);
221+
}
214222
}
215223

216224
return $this;

tests/RequestTest.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ public function testInitUrlSameAsBaseDirectory()
162162
'url' => '/vagrant/public/flightphp',
163163
'base' => '/vagrant/public',
164164
'query' => new Collection(),
165-
'type' => ''
165+
'type' => '',
166+
'method' => 'GET'
166167
]);
167168
$this->assertEquals('/flightphp', $request->url);
168169
}
@@ -172,7 +173,8 @@ public function testInitNoUrl()
172173
$request = new Request([
173174
'url' => '',
174175
'base' => '/vagrant/public',
175-
'type' => ''
176+
'type' => '',
177+
'method' => 'GET'
176178
]);
177179
$this->assertEquals('/', $request->url);
178180
}
@@ -183,20 +185,43 @@ public function testInitWithJsonBody()
183185
$tmpfile = tmpfile();
184186
$stream_path = stream_get_meta_data($tmpfile)['uri'];
185187
file_put_contents($stream_path, '{"foo":"bar"}');
186-
$_SERVER['REQUEST_METHOD'] = 'POST';
187188
$request = new Request([
188189
'url' => '/something/fancy',
189190
'base' => '/vagrant/public',
190191
'type' => 'application/json',
191192
'length' => 13,
192193
'data' => new Collection(),
193194
'query' => new Collection(),
194-
'stream_path' => $stream_path
195+
'stream_path' => $stream_path,
196+
'method' => 'POST'
195197
]);
196198
$this->assertEquals([ 'foo' => 'bar' ], $request->data->getData());
197199
$this->assertEquals('{"foo":"bar"}', $request->getBody());
198200
}
199201

202+
public function testInitWithFormBody()
203+
{
204+
// create dummy file to pull request body from
205+
$tmpfile = tmpfile();
206+
$stream_path = stream_get_meta_data($tmpfile)['uri'];
207+
file_put_contents($stream_path, 'foo=bar&baz=qux');
208+
$request = new Request([
209+
'url' => '/something/fancy',
210+
'base' => '/vagrant/public',
211+
'type' => 'application/x-www-form-urlencoded',
212+
'length' => 15,
213+
'data' => new Collection(),
214+
'query' => new Collection(),
215+
'stream_path' => $stream_path,
216+
'method' => 'PATCH'
217+
]);
218+
$this->assertEquals([
219+
'foo' => 'bar',
220+
'baz' => 'qux'
221+
], $request->data->getData());
222+
$this->assertEquals('foo=bar&baz=qux', $request->getBody());
223+
}
224+
200225
public function testGetHeader()
201226
{
202227
$_SERVER['HTTP_X_CUSTOM_HEADER'] = 'custom header value';

0 commit comments

Comments
 (0)