This commit is contained in:
e-adrien
2025-01-25 10:50:43 +01:00
committed by GitHub
5 changed files with 102 additions and 4 deletions

View File

@ -85,6 +85,7 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
'notLoggedInXpath' => $config->not_logged_in_xpath ?: null,
'username' => $credentials['username'],
'password' => $credentials['password'],
'httpHeaders' => $config->http_header ?: [],
];
$config = new SiteConfig($parameters);

View File

@ -29,10 +29,12 @@ class LoginFormAuthenticator
$siteConfig->getPasswordField() => $siteConfig->getPassword(),
] + $this->getExtraFields($siteConfig);
$guzzle->post(
$siteConfig->getLoginUri(),
['body' => $postFields, 'allow_redirects' => true, 'verify' => false]
);
$params = ['body' => $postFields, 'allow_redirects' => true, 'verify' => false];
if ($siteConfig->hasHttpHeaders()) {
$params['headers'] = $siteConfig->getHttpHeaders();
}
$guzzle->post($siteConfig->getLoginUri(), $params);
return $this;
}

View File

@ -70,6 +70,13 @@ class SiteConfig
*/
protected $password;
/**
* Associative array of HTTP headers to send with the form.
*
* @var array
*/
protected $httpHeaders = [];
/**
* SiteConfig constructor. Sets the properties by name given a hash.
*
@ -260,4 +267,32 @@ class SiteConfig
return $this;
}
/**
* @return array
*/
public function getHttpHeaders()
{
return $this->httpHeaders;
}
/**
* @param array $httpHeaders
*
* @return SiteConfig
*/
public function setHttpHeaders($httpHeaders)
{
$this->httpHeaders = $httpHeaders;
return $this;
}
/**
* @return bool
*/
public function hasHttpHeaders()
{
return !empty($this->httpHeaders);
}
}

View File

@ -253,4 +253,61 @@ class LoginFormAuthenticatorTest extends TestCase
$this->assertTrue($loginRequired);
}
public function testLoginPostWithUserAgentHeaderWithData()
{
$response = $this->getMockBuilder(Response::class)
->disableOriginalConstructor()
->getMock();
$response->expects($this->any())
->method('getBody')
->willReturn(file_get_contents(__DIR__ . '/../fixtures/nextinpact-login.html'));
$response->expects($this->any())
->method('getStatusCode')
->willReturn(200);
$mockHttpClient = new MockHttpClient([new MockResponse(file_get_contents(__DIR__ . '/../fixtures/nextinpact-login.html'), ['http_code' => 200, 'response_headers' => ['content-type' => 'text/html']])]);
$client = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->getMock();
$client->expects($this->any())
->method('post')
->with(
$this->equalTo('https://compte.nextinpact.com/Account/Login'),
$this->equalTo([
'body' => [
'UserName' => 'johndoe',
'Password' => 'unkn0wn',
],
'headers' => [
'user-agent' => 'Wallabag (Guzzle/5)',
],
'allow_redirects' => true,
'verify' => false,
])
)
->willReturn($response);
$siteConfig = new SiteConfig([
'host' => 'nextinpact.com',
'loginUri' => 'https://compte.nextinpact.com/Account/Login',
'usernameField' => 'UserName',
'passwordField' => 'Password',
'username' => 'johndoe',
'password' => 'unkn0wn',
'httpHeaders' => [
'user-agent' => 'Wallabag (Guzzle/5)',
],
]);
$authenticatorProvider = new AuthenticatorProvider($mockHttpClient);
$auth = new LoginFormAuthenticator($authenticatorProvider);
$res = $auth->login($siteConfig, $client);
$this->assertInstanceOf(LoginFormAuthenticator::class, $res);
}
}

View File

@ -37,6 +37,9 @@ class SiteConfigTest extends TestCase
],
'username' => 'johndoe',
'password' => 'unkn0wn',
'httpHeaders' => [
'user-agent' => 'Wallabag (Guzzle/5)',
],
]);
$this->assertInstanceOf(SiteConfig::class, $config);