Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Fixes #36 Client::setUri() should keep relative uri status #149

Merged
merged 21 commits into from Jan 8, 2019

Conversation

samsonasik
Copy link
Contributor

@samsonasik samsonasik commented May 6, 2018

Provide a narrative description of what you are trying to accomplish:

  • Are you fixing a bug?
    • Detail how the bug is invoked currently.
      Supply relative uri to Client::setUri()
    • Detail the original, incorrect behavior.
      It convert to absolute uri
    • Detail the new, expected behavior.
      It should keep as relative uri
    • Base your feature on the master branch, and submit against that branch.
    • Add a regression test that demonstrates the bug, and proves the fix.
    • Add a CHANGELOG.md entry for the fix.

samsonasik added a commit to samsonasik/zend-http that referenced this pull request May 6, 2018
@samsonasik
Copy link
Contributor Author

samsonasik commented May 6, 2018

After some check, I think relative url should not be allowed instead in Zend\Http\Client, so, instead of keeping it, we can do these options:

  1. throw Exception to warn user to use absolute URI, OR
  2. set URI to use current host detected as $lastHost = $this->getRequest()->getUri()->getHost();

thought ?

src/Client.php Outdated
// Set auth if username and password has been specified in the uri
if ($this->getUri()->getUser() && $this->getUri()->getPassword()) {
$this->setAuth($this->getUri()->getUser(), $this->getUri()->getPassword());
if ($user = $uri->getUser() && $password = $uri->getPassword()) {
Copy link
Member

Choose a reason for hiding this comment

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

We need brackets around expressions here, see following example - https://3v4l.org/oV5F5:

<?php

function a() {
    return 1;
}

function b() {
    return 2;
}

if ($a = a() && $b = b()) {
    var_dump($a, $b);
}

results:

bool(true)
int(2)

and because of that I think there is missing proper test for that change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done ;)

Copy link
Member

Choose a reason for hiding this comment

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

I see bracket added, but still I can't see any test for it :)

src/Client.php Outdated
if (! $this->getUri()->getPort()) {
$this->getUri()->setPort(($this->getUri()->getScheme() == 'https' ? 443 : 80));
if (! $uri->getPort() && $uri->isAbsolute()) {
$uri->setPort(($uri->getScheme() == 'https' ? 443 : 80));
Copy link
Member

Choose a reason for hiding this comment

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

I know it was like that before, but we can do two changes here:

  • remove redundant bracket
  • change == to ===

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done ;)

{
$client = new Client('/example');
$client->setAdapter(Test::class);
$client->send();
Copy link
Member

Choose a reason for hiding this comment

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

There is no any assertion in that test. It is really unclear what we are testing here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done ;)

@samsonasik
Copy link
Contributor Author

@webimpress I've added the tests for check isAbsolute()

src/Client.php Outdated
// Set auth if username and password has been specified in the uri
if ($this->getUri()->getUser() && $this->getUri()->getPassword()) {
$this->setAuth($this->getUri()->getUser(), $this->getUri()->getPassword());
if (($user = $uri->getUser()) && ($password = $uri->getPassword())) {
Copy link
Member

Choose a reason for hiding this comment

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

Please don't do assignments in conditionals.

/**
* @dataProvider uriDataProvider
*/
public function testValidRelativeURI($uri, $isValidRelativeURI)
Copy link
Member

Choose a reason for hiding this comment

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

What behavior is this testing, exactly? This should likely be "testUriCorrectlyDeterminesWhetherOrNotItIsAValidRelativeUri".

return [
['/example', true],
['http://localhost/example', false]
];
Copy link
Member

Choose a reason for hiding this comment

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

Please add names for each case in the provider. This makes it simpler to determine which case failed when a failure occurs. As an example:

'valid-relative' => ['/example', true],
'invalid-absolute' => ['http://localhost/example', false],

return [
['https://localhost/example', 443],
['http://localhost/example', 80]
];
Copy link
Member

Choose a reason for hiding this comment

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

Same comment here about adding names for each provider case.

/**
* @dataProvider portChangeDataProvider
*/
public function testAbsoluteSetPortWhenNoPort($absoluteURI, $port)
Copy link
Member

Choose a reason for hiding this comment

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

This needs a name that better describes the behavior being tested: "testUriPortIsSetToAppropriateDefaultValueWhenAnAbsoluteUriOmittingThePortIsProvided".

$this->assertSame($port, $client->getUri()->getPort());
}

public function testRelativeURIDoesnotSetPort()
Copy link
Member

Choose a reason for hiding this comment

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

Need a better method name here; I'll leave this as an exercise for you!

@samsonasik
Copy link
Contributor Author

@weierophinney done ;). All incorporated.

@weierophinney
Copy link
Member

@samsonasik
Copy link
Contributor Author

@weierophinney should be fixed now

@samsonasik
Copy link
Contributor Author

travis green

@samsonasik
Copy link
Contributor Author

@weierophinney mergeable ?

Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

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

🚢

@weierophinney weierophinney added this to the 2.8.3 milestone Jan 7, 2019
@weierophinney weierophinney merged commit aa48298 into zendframework:master Jan 8, 2019
weierophinney added a commit that referenced this pull request Jan 8, 2019
Fixes #36 Client::setUri() should keep relative uri status

Conflicts:
	CHANGELOG.md
weierophinney added a commit that referenced this pull request Jan 8, 2019
weierophinney added a commit that referenced this pull request Jan 8, 2019
@weierophinney
Copy link
Member

weierophinney commented Jan 8, 2019

Thanks, @samsonasik !

@samsonasik samsonasik deleted the fix-36 branch January 8, 2019 16:13
@thomasvargiu
Copy link
Contributor

@weierophinney @samsonasik can you help me to understand this PR?
Tests doesn't work with any adapter because without the host it can't connect to anything.

I was working to another branch adding checks to ensure uri host exists.

@samsonasik
Copy link
Contributor Author

it make keep relative uri status, for host exists check, I already commented before at #149 (comment) but that will not related to this PR, keeping exisitng URI host for relative uri seems need another PR

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants