Skip to content

Commit

Permalink
Fix Unit test errors, correct logic
Browse files Browse the repository at this point in the history
  • Loading branch information
n0nag0n committed Feb 6, 2025
1 parent 1b57f9e commit ce088c8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
6 changes: 2 additions & 4 deletions flight/net/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,8 @@ public function init(array $properties = []): self
$this->data->setData($data);
}
}
}

// Check PUT, PATCH, DELETE for data
if ($this->method === 'PUT' || $this->method === 'DELETE' || $this->method === 'PATCH') {
// Check PUT, PATCH, DELETE for application/x-www-form-urlencoded data
} else if (in_array($this->method, [ 'PUT', 'DELETE', 'PATCH' ], true) === true) {
$body = $this->getBody();
if ($body !== '') {
$data = [];
Expand Down
33 changes: 29 additions & 4 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ public function testInitUrlSameAsBaseDirectory()
'url' => '/vagrant/public/flightphp',
'base' => '/vagrant/public',
'query' => new Collection(),
'type' => ''
'type' => '',
'method' => 'GET'
]);
$this->assertEquals('/flightphp', $request->url);
}
Expand All @@ -172,7 +173,8 @@ public function testInitNoUrl()
$request = new Request([
'url' => '',
'base' => '/vagrant/public',
'type' => ''
'type' => '',
'method' => 'GET'
]);
$this->assertEquals('/', $request->url);
}
Expand All @@ -183,20 +185,43 @@ public function testInitWithJsonBody()
$tmpfile = tmpfile();
$stream_path = stream_get_meta_data($tmpfile)['uri'];
file_put_contents($stream_path, '{"foo":"bar"}');
$_SERVER['REQUEST_METHOD'] = 'POST';
$request = new Request([
'url' => '/something/fancy',
'base' => '/vagrant/public',
'type' => 'application/json',
'length' => 13,
'data' => new Collection(),
'query' => new Collection(),
'stream_path' => $stream_path
'stream_path' => $stream_path,
'method' => 'POST'
]);
$this->assertEquals([ 'foo' => 'bar' ], $request->data->getData());
$this->assertEquals('{"foo":"bar"}', $request->getBody());
}

public function testInitWithFormBody()
{
// create dummy file to pull request body from
$tmpfile = tmpfile();
$stream_path = stream_get_meta_data($tmpfile)['uri'];
file_put_contents($stream_path, 'foo=bar&baz=qux');
$request = new Request([
'url' => '/something/fancy',
'base' => '/vagrant/public',
'type' => 'application/x-www-form-urlencoded',
'length' => 15,
'data' => new Collection(),
'query' => new Collection(),
'stream_path' => $stream_path,
'method' => 'PATCH'
]);
$this->assertEquals([
'foo' => 'bar',
'baz' => 'qux'
], $request->data->getData());
$this->assertEquals('foo=bar&baz=qux', $request->getBody());
}

public function testGetHeader()
{
$_SERVER['HTTP_X_CUSTOM_HEADER'] = 'custom header value';
Expand Down

0 comments on commit ce088c8

Please sign in to comment.