Skip to content

Commit

Permalink
handle non-json responses a little better
Browse files Browse the repository at this point in the history
headers for response codes and response body
  • Loading branch information
Jake Worrell committed Sep 21, 2018
1 parent a8926bc commit 09a1530
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
10 changes: 7 additions & 3 deletions Command/GenerateCommand.php
Expand Up @@ -72,7 +72,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($collection->getItems() as $item) {
if (isset($item->item)) { // this is actually a folder
$folders[$item->name]['name'] = $item->name;

foreach ($item->item as $request) {
$request->folder = $item->name;
if (is_object($request->request->url)){
Expand All @@ -87,8 +86,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
if($request->request->method=="GET" && isset($request->request->body->formdata)) {
$output->writeln("Warning: {$request->name} has form-data parameters defined but is a GET request");
}

foreach ($request->response as $key => $response) {

if ($response->code >=100 && $response->code <200) {
$response->class = 'info';
} elseif ($response->code >=200 && $response->code <300) {
Expand Down Expand Up @@ -150,7 +149,12 @@ protected function getTwig()
$twig->addFunction($f);

$f = new \Twig_SimpleFunction("prettify", function ($data) {
return json_encode(json_decode($data), JSON_PRETTY_PRINT);
$pretty = json_encode(json_decode($data), JSON_PRETTY_PRINT);
if ($pretty && $pretty !=='null') {
return $pretty;
} else {
return $data;
}
});

$twig->addFunction($f);
Expand Down
1 change: 1 addition & 0 deletions Postman/Collection.php
Expand Up @@ -23,6 +23,7 @@ public function getVersion()
preg_match('/(?<=v)\d+(\.\d+)?(\.\d+)?/', $this->data()->info->schema, $collectionVersion);
return $collectionVersion[0];
}

public function data()
{
return $this->collection;
Expand Down
3 changes: 2 additions & 1 deletion templates/request.html
Expand Up @@ -85,8 +85,9 @@ <h2>Example responses</h2>
<div class="panel panel-{{response.class}}">
<div class="panel-heading">{{response.name}}</div>
<div class="panel-body">

<h5>Response Code:</h5>
<pre><code>{{response.code}} {{response.status}}</code></pre>
<h5>Response Body:</h5>
{{ include('code.html', {code:response.body} ) }}
</div>
</div>
Expand Down

0 comments on commit 09a1530

Please sign in to comment.