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

Fix hasFile Method to Accurately Identify Files in Request #52445

Draft
wants to merge 2 commits into
base: 11.x
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion src/Illuminate/Http/Client/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ public function hasFile($name, $value = null, $filename = null)
return collect($this->data)->reject(function ($file) use ($name, $value, $filename) {
return $file['name'] != $name ||
($value && $file['contents'] != $value) ||
($filename && $file['filename'] != $filename);
($filename && $file['filename'] != $filename) ||
(! is_resource($file['contents']) && json_encode($file['contents']) !== false);
})->count() > 0;
}

Expand Down
21 changes: 19 additions & 2 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -666,11 +666,28 @@ public function testFilesCanBeAttached()
$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/file' &&
Str::startsWith($request->header('Content-Type')[0], 'multipart') &&
$request[0]['name'] === 'foo' &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this test changed?

I understand the premise of the PR, but this test case is named "files can be attached", and the condition to check for an attached file is removed?

In the best case, no previous test cases should be changed, but again, I see from your proposed changes why you did it.

Either this test case should be renamed or removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this test, we check some of the other assertions related to attaching a file in a request, while in the new test, we focus solely on the functionality of the hasFile method itself. In my opinion, these tests complement each other and test different aspects of the framework. However, they could be merged into a single test that checks the entire process of attaching files.

$request->hasFile('foo', 'data', 'file.txt');
$request[0]['name'] === 'foo';
});
}

public function testHasFileCorrectlyIdentifiesFiles()
{
$this->factory->fake();

$tmpFilePath = tempnam(sys_get_temp_dir(), 'test');
file_put_contents($tmpFilePath, 'file data');
$fileResource = fopen($tmpFilePath, 'r');

$this->factory->attach('foo', $fileResource, 'file.txt')->post('http://foo.com/file', ['bar' => 'baz']);

$this->factory->assertSent(function (Request $request) {
return $request->hasFile('foo') && ! $request->hasFile('bar');
});

fclose($fileResource);
unlink($tmpFilePath);
}

public function testCanSendMultipartDataWithSimplifiedParameters()
{
$this->factory->fake();
Expand Down