mirror of
https://github.com/wallabag/wallabag.git
synced 2026-02-23 04:27:35 +01:00
Merge c9f29f00d2 into d741f28071
This commit is contained in:
@ -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);
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,6 +37,9 @@ class SiteConfigTest extends TestCase
|
||||
],
|
||||
'username' => 'johndoe',
|
||||
'password' => 'unkn0wn',
|
||||
'httpHeaders' => [
|
||||
'user-agent' => 'Wallabag (Guzzle/5)',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(SiteConfig::class, $config);
|
||||
|
||||
Reference in New Issue
Block a user