Skip to content

Commit f384316

Browse files
committed
Allow users to attempt API requests
Issue #35
1 parent 0442c95 commit f384316

File tree

10 files changed

+457
-7
lines changed

10 files changed

+457
-7
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"php": "5.6.* || 7.*",
2222
"ql/uri-template": "1.*",
2323
"michelf/php-markdown": "1.*",
24-
"lukasoppermann/http-status": "^1.0@dev"
24+
"lukasoppermann/http-status": "2.*"
2525
},
2626
"require-dev": {
2727
"phpunit/phpunit": "5.7.*",

src/PHPDraft/Model/HTTPRequest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,53 @@ public function is_equal_to($b)
167167
{
168168
return ($this->method === $b->method) && ($this->body === $b->body) && ($this->headers === $b->headers);
169169
}
170+
171+
/**
172+
* Generate a URL for the hurl.it service.
173+
*
174+
* @param string $base_url URL to the base server
175+
* @param array $additional Extra options to pass to the service
176+
*
177+
* @return string
178+
*/
179+
public function get_hurl_link($base_url, $additional = [])
180+
{
181+
$options = [];
182+
183+
$type = (isset($this->headers['Content-Type'])) ? $this->headers['Content-Type'] : NULL;
184+
185+
$url = $this->parent->build_url($base_url, TRUE);
186+
$url = explode('?', $url);
187+
if (isset($url[1])) {
188+
$params = [];
189+
foreach (explode('&', $url[1]) as $args) {
190+
$arg = explode('=', $args);
191+
$params[$arg[0]] = [$arg[1]];
192+
}
193+
$options[] = 'args=' . json_encode($params);
194+
}
195+
$options[] = 'url=' . $url[0];
196+
$options[] = 'method=' . strtoupper($this->method);
197+
if (empty($this->body)) {
198+
//NO-OP
199+
} elseif (is_string($this->body)) {
200+
$options[] = 'body=' . urlencode($this->body);
201+
} elseif (is_array($this->body)) {
202+
$options[] = 'body=' . urlencode(join('', $this->body));
203+
} elseif (is_subclass_of($this->struct, StructureElement::class)) {
204+
foreach ($this->struct->value as $body) {
205+
$options[] = 'body=' . urlencode(strip_tags($body->print_request($type)));
206+
}
207+
}
208+
$headers = [];
209+
if (!empty($this->headers)) {
210+
foreach ($this->headers as $header => $value) {
211+
$headers[$header] = [$value];
212+
}
213+
$options[] = 'headers=' . json_encode($headers);
214+
}
215+
$options = array_merge($options, $additional);
216+
217+
return str_replace('"', '\"', 'https://www.hurl.it/?' . join('&', $options));
218+
}
170219
}

src/PHPDraft/Model/Tests/HTTPRequestTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,41 @@ public function testGetCurlCommandStructBodyFilled()
230230
$this->assertSame('curl -X --data-binary \'TEST\' \'\'', $return);
231231
}
232232

233+
/**
234+
* Test basic get_hurl_link functions
235+
*/
236+
public function testGetHurlStructBodyFilled()
237+
{
238+
$property = $this->reflection->getProperty('parent');
239+
$property->setAccessible(TRUE);
240+
$property->setValue($this->class, $this->parent);
241+
$property = $this->reflection->getProperty('body');
242+
$property->setAccessible(TRUE);
243+
$property->setValue($this->class, 1000);
244+
245+
$struct = $this->getMockBuilder('\PHPDraft\Model\Elements\ObjectStructureElement')
246+
->disableOriginalConstructor()
247+
->getMock();
248+
$struct_ar = $this->getMockBuilder('\PHPDraft\Model\Elements\RequestBodyElement')
249+
->disableOriginalConstructor()
250+
->getMock();
251+
252+
$struct_ar->expects($this->once())
253+
->method('print_request')
254+
->with(NULL)
255+
->will($this->returnValue('TEST'));
256+
257+
$struct->value = [ $struct_ar ];
258+
259+
$property = $this->reflection->getProperty('struct');
260+
$property->setAccessible(TRUE);
261+
$property->setValue($this->class, $struct);
262+
263+
$return = $this->class->get_hurl_link('https://ur.l');
264+
265+
$this->assertSame('https://www.hurl.it/?url=&method=&body=TEST', $return);
266+
}
267+
233268
/**
234269
* Test basic parse functions
235270
*/

src/PHPDraft/Model/Tests/TransitionTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,38 @@ public function testGetCurlCommandKey()
292292
$this->assertSame('curl_command', $return);
293293
}
294294

295+
/**
296+
* Test basic get_hurl_link functions
297+
*/
298+
public function testGetHurlURLNoKey()
299+
{
300+
$return = $this->class->get_hurl_link('https://ur.l');
301+
302+
$this->assertSame('', $return);
303+
}
304+
305+
/**
306+
* Test basic get_hurl_link functions
307+
*/
308+
public function testGetHurlURLKey()
309+
{
310+
$mock_req = $this->getMockBuilder('\PHPDraft\Model\HTTPRequest')
311+
->disableOriginalConstructor()
312+
->getMock();
313+
$mock_req->expects($this->once())
314+
->method('get_hurl_link')
315+
->with('https://ur.l', [])
316+
->will($this->returnValue('https://hurl.it'));
317+
$requests = [$mock_req];
318+
$req_property = $this->reflection->getProperty('requests');
319+
$req_property->setAccessible(TRUE);
320+
$req_property->setValue($this->class, $requests);
321+
322+
$return = $this->class->get_hurl_link('https://ur.l');
323+
324+
$this->assertSame('https://hurl.it', $return);
325+
}
326+
295327
/**
296328
* Test basic get_method functions
297329
*/

src/PHPDraft/Model/Transition.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,22 @@ public function get_curl_command($base_url, $additional = [], $key = 0)
262262

263263
return $this->requests[$key]->get_curl_command($base_url, $additional);
264264
}
265+
266+
/**
267+
* Generate a URL for the hurl.it service.
268+
*
269+
* @param string $base_url base URL of the server
270+
* @param array $additional additional arguments to pass
271+
* @param int $key number of the request to generate for
272+
*
273+
* @return string
274+
*/
275+
public function get_hurl_link($base_url, $additional = [], $key = 0)
276+
{
277+
if (!isset($this->requests[$key])) {
278+
return '';
279+
}
280+
281+
return $this->requests[$key]->get_hurl_link($base_url, $additional);
282+
}
265283
}

src/PHPDraft/Out/HTML/default.css

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,21 @@ a.code {
137137
position: relative;
138138
}
139139

140-
.curl.btn {
140+
.curl.btn, .hurl.btn {
141141
z-index: 999;
142142
position: absolute;
143-
right: 15px;
144143
top: 15px;
145144
}
145+
.curl.btn {
146+
right: 15px;
147+
border-top-left-radius: 0px;
148+
border-bottom-left-radius: 0px
149+
}
150+
.hurl.btn {
151+
right: 54px;
152+
border-top-right-radius: 0px;
153+
border-bottom-right-radius: 0px
154+
}
146155

147156
h5.response-body, h4.request {
148157
cursor: pointer;

src/PHPDraft/Out/HTML/default.phtml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,17 @@ use Enjoy\HttpStatusCode\Statuscodes;
150150
data-placement="left"
151151
data-toggle="popover"
152152
data-html="true"
153-
data-content="<textarea rows='8' cols='75'><?= $transition->get_curl_command($this->base_data['HOST'],
154-
[]); ?></textarea>">
153+
data-content="<textarea rows='8' cols='75'><?= $transition->get_curl_command($this->base_data['HOST']); ?></textarea>">
155154
<span class="glyphicon glyphicon-copy"></span>
156155
</a>
156+
<a class="btn btn-default hurl"
157+
role="button"
158+
title="Try request"
159+
target="_blank"
160+
href="<?= $transition->get_hurl_link($this->base_data['HOST']); ?>"
161+
tabindex="0">
162+
<span class="glyphicon glyphicon-play"></span>
163+
</a>
157164
<p class="lead"><?= $transition->description; ?></p>
158165
<?php if (!empty($transition->requests)): ?>
159166
<?php foreach ($transition->requests as $key => $request): ?>

src/PHPDraft/Out/HTML/material.phtml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ use Enjoy\HttpStatusCode\Statuscodes;
192192
</div>
193193
</div>
194194
<div class="mdl-card__menu">
195+
<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">
196+
<i class="material-icons">play_arrow</i>
197+
</a>
195198
<button id="show-dialog-<?=$transition->get_href();?>" type="button" class="mdl-button mdl-button mdl-button--icon mdl-js-button mdl-js-ripple-effect">
196199
<i class="material-icons">bug_report</i>
197200
</button>
@@ -200,7 +203,7 @@ use Enjoy\HttpStatusCode\Statuscodes;
200203
<div class="mdl-dialog__content">
201204
<b>This might be useful for debugging</b>
202205
<div class="mdl-textfield mdl-js-textfield">
203-
<textarea class="mdl-textfield__input" type="text" rows= "7" autofocus readonly><?= $transition->get_curl_command($this->base_data['HOST'], []); ?></textarea>
206+
<textarea class="mdl-textfield__input" type="text" rows= "7" autofocus readonly><?= $transition->get_curl_command($this->base_data['HOST']); ?></textarea>
204207
</div>
205208
</div>
206209
<div class="mdl-dialog__actions">

0 commit comments

Comments
 (0)