Skip to content

Commit f7320cb

Browse files
committed
Modified _url and _url-related functions
1 parent ca2f32c commit f7320cb

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/Http/functions.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,35 @@ function _fetch($loop, string $method, ...$opts) : Promise
4141
const _credentials = 'Chemem\\Fauxton\\Http\\_credentials';
4242
function _credentials(string $config) : array
4343
{
44-
$let = PM\letIn(array('username', 'password', 'local'), json_decode($config, true));
44+
$let = PM\letIn(array('username', 'password', 'host', 'local'), json_decode($config, true));
4545

46-
return $let(array('username', 'password', 'local'), function (array $username, array $password, bool $local) {
46+
return $let(array('username', 'password', 'host', 'local'), function (array $username, array $password, string $host, bool $local) {
4747
$credentials = A\curry(A\pluck);
4848

4949
return A\extend(
5050
array($local),
5151
array($local ? $credentials($username)('local') : $credentials($username)('cloudant')),
52-
array($local ? $credentials($password)('local') : $credentials($password)('cloudant'))
52+
array($local ? $credentials($password)('local') : $credentials($password)('cloudant')),
53+
array($local ? '' : $host)
5354
);
5455
});
5556
}
5657

5758
const _url = 'Chemem\\Fauxton\\Http\\_url';
5859
function _url(array $credentials, array $opts) : string
5960
{
60-
$cred = array('local', 'user', 'pass');
61+
$cred = array('local', 'user', 'pass', 'host');
6162
$let = PM\letIn($cred, $credentials);
6263

63-
return $let($cred, function (bool $local, string $user, string $pass) use ($opts) {
64+
return $let($cred, function (bool $local, string $user, string $pass, string $host) use ($opts) {
6465
$frag = A\head($opts);
6566
$fragments = A\compose(
6667
_urlFragments($opts, $local),
6768
A\partial('str_replace', array_keys($frag), array_values($frag)),
6869
A\partialRight('rtrim', '?')
6970
);
7071

71-
return A\concat('/', _schemeHost($local, $user, $pass), $fragments(State::COUCH_ACTIONS));
72+
return A\concat('/', _schemeHost($local, $user, $pass, $host), $fragments(State::COUCH_ACTIONS));
7273
});
7374
}
7475

@@ -82,13 +83,13 @@ function _urlFragments(array $opts, bool $local) : callable
8283
}
8384

8485
const _schemeHost = 'Chemem\\Fauxton\\Http\\_schemeHost';
85-
function _schemeHost(bool $local, string $user, string $pass) : string
86+
function _schemeHost(bool $local, string $user, string $pass, string $host) : string
8687
{
8788
return $local ?
8889
State::COUCH_URI_LOCAL :
8990
str_replace(
9091
array('{cloudantUser}', '{cloudantPass}', '{cloudantHost}'),
91-
array($user, $pass, A\concat('.', $user, 'cloudant', 'com')),
92+
array($user, $pass, $host),
9293
State::COUCH_URI_CLOUDANT
9394
);
9495
}

tests/HttpTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,24 @@ public function setUp()
2222
$this->eventLoop = $this->getMockBuilder('React\\EventLoop\\LoopInterface')->getMock();
2323
}
2424

25+
/**
26+
* @eris-repeat 5
27+
*/
2528
public function testUrlFunctionGeneratesAppropriateRequestUrl()
2629
{
2730
$this->forAll(
2831
Generator\string(),
2932
Generator\string(),
3033
Generator\bool(),
31-
Generator\int()
34+
Generator\string(),
35+
Generator\associative([
36+
'uuids' => Generator\associative([
37+
'{count}' => Generator\int()
38+
])
39+
])
3240
)
33-
->then(function (string $user, string $pwd, bool $local, int $count) {
34-
$url = Http\_url(array($local, $user, $pwd), array('uuids' => array('{count}' => $count)));
41+
->then(function (string $user, string $pwd, bool $local, string $clHost, array $opt) {
42+
$url = Http\_url(array($local, $user, $pwd, $clHost), $opt);
3543

3644
$this->assertInternalType('string', $url);
3745
$this->assertRegExp('/(http|https){1}(:){1}(\/){1}([\w\D\W]*)/', $url);

0 commit comments

Comments
 (0)