Skip to content

Commit

Permalink
Allow users to attempt API requests
Browse files Browse the repository at this point in the history
Issue #35
  • Loading branch information
SMillerDev committed Nov 21, 2017
1 parent 0442c95 commit f384316
Show file tree
Hide file tree
Showing 10 changed files with 457 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"php": "5.6.* || 7.*",
"ql/uri-template": "1.*",
"michelf/php-markdown": "1.*",
"lukasoppermann/http-status": "^1.0@dev"
"lukasoppermann/http-status": "2.*"
},
"require-dev": {
"phpunit/phpunit": "5.7.*",
Expand Down
49 changes: 49 additions & 0 deletions src/PHPDraft/Model/HTTPRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,53 @@ public function is_equal_to($b)
{
return ($this->method === $b->method) && ($this->body === $b->body) && ($this->headers === $b->headers);
}

/**
* Generate a URL for the hurl.it service.
*
* @param string $base_url URL to the base server
* @param array $additional Extra options to pass to the service
*
* @return string
*/
public function get_hurl_link($base_url, $additional = [])
{
$options = [];

$type = (isset($this->headers['Content-Type'])) ? $this->headers['Content-Type'] : NULL;

$url = $this->parent->build_url($base_url, TRUE);
$url = explode('?', $url);
if (isset($url[1])) {
$params = [];
foreach (explode('&', $url[1]) as $args) {
$arg = explode('=', $args);
$params[$arg[0]] = [$arg[1]];
}
$options[] = 'args=' . json_encode($params);
}
$options[] = 'url=' . $url[0];
$options[] = 'method=' . strtoupper($this->method);
if (empty($this->body)) {
//NO-OP
} elseif (is_string($this->body)) {
$options[] = 'body=' . urlencode($this->body);
} elseif (is_array($this->body)) {
$options[] = 'body=' . urlencode(join('', $this->body));
} elseif (is_subclass_of($this->struct, StructureElement::class)) {
foreach ($this->struct->value as $body) {
$options[] = 'body=' . urlencode(strip_tags($body->print_request($type)));
}
}
$headers = [];
if (!empty($this->headers)) {
foreach ($this->headers as $header => $value) {
$headers[$header] = [$value];
}
$options[] = 'headers=' . json_encode($headers);
}
$options = array_merge($options, $additional);

return str_replace('"', '\"', 'https://www.hurl.it/?' . join('&', $options));
}
}
35 changes: 35 additions & 0 deletions src/PHPDraft/Model/Tests/HTTPRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,41 @@ public function testGetCurlCommandStructBodyFilled()
$this->assertSame('curl -X --data-binary \'TEST\' \'\'', $return);
}

/**
* Test basic get_hurl_link functions
*/
public function testGetHurlStructBodyFilled()
{
$property = $this->reflection->getProperty('parent');
$property->setAccessible(TRUE);
$property->setValue($this->class, $this->parent);
$property = $this->reflection->getProperty('body');
$property->setAccessible(TRUE);
$property->setValue($this->class, 1000);

$struct = $this->getMockBuilder('\PHPDraft\Model\Elements\ObjectStructureElement')
->disableOriginalConstructor()
->getMock();
$struct_ar = $this->getMockBuilder('\PHPDraft\Model\Elements\RequestBodyElement')
->disableOriginalConstructor()
->getMock();

$struct_ar->expects($this->once())
->method('print_request')
->with(NULL)
->will($this->returnValue('TEST'));

$struct->value = [ $struct_ar ];

$property = $this->reflection->getProperty('struct');
$property->setAccessible(TRUE);
$property->setValue($this->class, $struct);

$return = $this->class->get_hurl_link('https://ur.l');

$this->assertSame('https://www.hurl.it/?url=&method=&body=TEST', $return);
}

/**
* Test basic parse functions
*/
Expand Down
32 changes: 32 additions & 0 deletions src/PHPDraft/Model/Tests/TransitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,38 @@ public function testGetCurlCommandKey()
$this->assertSame('curl_command', $return);
}

/**
* Test basic get_hurl_link functions
*/
public function testGetHurlURLNoKey()
{
$return = $this->class->get_hurl_link('https://ur.l');

$this->assertSame('', $return);
}

/**
* Test basic get_hurl_link functions
*/
public function testGetHurlURLKey()
{
$mock_req = $this->getMockBuilder('\PHPDraft\Model\HTTPRequest')
->disableOriginalConstructor()
->getMock();
$mock_req->expects($this->once())
->method('get_hurl_link')
->with('https://ur.l', [])
->will($this->returnValue('https://hurl.it'));
$requests = [$mock_req];
$req_property = $this->reflection->getProperty('requests');
$req_property->setAccessible(TRUE);
$req_property->setValue($this->class, $requests);

$return = $this->class->get_hurl_link('https://ur.l');

$this->assertSame('https://hurl.it', $return);
}

/**
* Test basic get_method functions
*/
Expand Down
18 changes: 18 additions & 0 deletions src/PHPDraft/Model/Transition.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,22 @@ public function get_curl_command($base_url, $additional = [], $key = 0)

return $this->requests[$key]->get_curl_command($base_url, $additional);
}

/**
* Generate a URL for the hurl.it service.
*
* @param string $base_url base URL of the server
* @param array $additional additional arguments to pass
* @param int $key number of the request to generate for
*
* @return string
*/
public function get_hurl_link($base_url, $additional = [], $key = 0)
{
if (!isset($this->requests[$key])) {
return '';
}

return $this->requests[$key]->get_hurl_link($base_url, $additional);
}
}
13 changes: 11 additions & 2 deletions src/PHPDraft/Out/HTML/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,21 @@ a.code {
position: relative;
}

.curl.btn {
.curl.btn, .hurl.btn {
z-index: 999;
position: absolute;
right: 15px;
top: 15px;
}
.curl.btn {
right: 15px;
border-top-left-radius: 0px;
border-bottom-left-radius: 0px
}
.hurl.btn {
right: 54px;
border-top-right-radius: 0px;
border-bottom-right-radius: 0px
}

h5.response-body, h4.request {
cursor: pointer;
Expand Down
11 changes: 9 additions & 2 deletions src/PHPDraft/Out/HTML/default.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,17 @@ use Enjoy\HttpStatusCode\Statuscodes;
data-placement="left"
data-toggle="popover"
data-html="true"
data-content="<textarea rows='8' cols='75'><?= $transition->get_curl_command($this->base_data['HOST'],
[]); ?></textarea>">
data-content="<textarea rows='8' cols='75'><?= $transition->get_curl_command($this->base_data['HOST']); ?></textarea>">
<span class="glyphicon glyphicon-copy"></span>
</a>
<a class="btn btn-default hurl"
role="button"
title="Try request"
target="_blank"
href="<?= $transition->get_hurl_link($this->base_data['HOST']); ?>"
tabindex="0">
<span class="glyphicon glyphicon-play"></span>
</a>
<p class="lead"><?= $transition->description; ?></p>
<?php if (!empty($transition->requests)): ?>
<?php foreach ($transition->requests as $key => $request): ?>
Expand Down
5 changes: 4 additions & 1 deletion src/PHPDraft/Out/HTML/material.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ use Enjoy\HttpStatusCode\Statuscodes;
</div>
</div>
<div class="mdl-card__menu">
<a href="<?= $transition->get_hurl_link($this->base_data['HOST']); ?>" target="_blank" role="button" class="mdl-button mdl-button mdl-button--icon mdl-js-button mdl-js-ripple-effect">
<i class="material-icons">play_arrow</i>
</a>
<button id="show-dialog-<?=$transition->get_href();?>" type="button" class="mdl-button mdl-button mdl-button--icon mdl-js-button mdl-js-ripple-effect">
<i class="material-icons">bug_report</i>
</button>
Expand All @@ -200,7 +203,7 @@ use Enjoy\HttpStatusCode\Statuscodes;
<div class="mdl-dialog__content">
<b>This might be useful for debugging</b>
<div class="mdl-textfield mdl-js-textfield">
<textarea class="mdl-textfield__input" type="text" rows= "7" autofocus readonly><?= $transition->get_curl_command($this->base_data['HOST'], []); ?></textarea>
<textarea class="mdl-textfield__input" type="text" rows= "7" autofocus readonly><?= $transition->get_curl_command($this->base_data['HOST']); ?></textarea>
</div>
</div>
<div class="mdl-dialog__actions">
Expand Down
Loading

0 comments on commit f384316

Please sign in to comment.