Browse Source

Allow for headers in responses

shalvah 4 years ago
parent
commit
6c81bd7b8f

+ 1 - 1
camel/Extraction/ExtractedEndpointData.php

@@ -145,7 +145,7 @@ class ExtractedEndpointData extends BaseDTO
             $pluralParam = Str::plural($param);
             $resourceRouteNames = ["$pluralParam.show", "$pluralParam.update", "$pluralParam.destroy"];
 
-            if (Str::contains($route->action['as'], $resourceRouteNames)) {
+            if (Str::contains($route->action['as'] ?? '', $resourceRouteNames)) {
                 $search = sprintf("%s/{%s}", $pluralParam, $param);
                 if (!$foundResourceParam) {
                     // Only the first resource param should be {id}

+ 14 - 1
camel/Extraction/Response.php

@@ -14,13 +14,26 @@ class Response extends BaseDTO
      * @var string|null
      */
     public $content;
-    public ?string $description;
+
+    /**
+     * @var array
+     */
+    public $headers = [];
+
+    /**
+     * @var string|null
+     */
+    public $description;
 
     public function __construct(array $parameters = [])
     {
         if (is_array($parameters['content'])) {
             $parameters['content'] = json_encode($parameters['content']);
         }
+        if (!empty($parameters['headers'])) {
+            unset($parameters['headers']['date']);
+            unset($parameters['headers']['Date']);
+        }
 
         return parent::__construct($parameters);
     }

+ 0 - 1
phpunit.xml

@@ -37,7 +37,6 @@
         </testsuite>
         <testsuite name="Unit Tests 2">
             <file>tests/Unit/AnnotationParserTest.php</file>
-            <file>tests/Unit/CamelTest.php</file>
         </testsuite>
         <testsuite name="Unit Tests 3">
             <file>tests/Unit/OpenAPISpecWriterTest.php</file>

+ 22 - 0
resources/css/theme-default.style.css

@@ -862,6 +862,24 @@ html {
     text-shadow: 0 1px 2px rgba(0, 0, 0, .4)
 }
 
+.content .annotation {
+    background-color: #292929;
+    color: #fff;
+    padding: 0 28px;
+    margin: 0;
+    width: 50%;
+    float: right;
+    clear: right;
+    box-sizing: border-box;
+    text-shadow: 0 1px 2px rgba(0, 0, 0, .4)
+}
+
+.content .annotation pre {
+    padding: 0 0;
+    width: 100%;
+    float: none;
+}
+
 .content blockquote>p,
 .content pre>p {
     margin: 0
@@ -957,6 +975,10 @@ html {
         float: none;
         width: auto
     }
+    .content .annotation {
+        float: none;
+        width: auto
+    }
 }
 
 .badge {

+ 13 - 3
resources/views/themes/default/endpoint.blade.php

@@ -22,8 +22,19 @@
         <blockquote>
             <p>Example response ({{$response->description ?: $response->status}}):</p>
         </blockquote>
+        @if(count($response->headers))
+        <details class="annotation">
+            <summary onclick="textContent = parentElement.open ? 'Show headers ▼' : 'Hide headers ▲'">
+                Show headers ▼
+            </summary>
+            <pre>
+            <code class="language-http">@foreach($response->headers as $header => $values)
+{{ $header }}: {{ implode('; ', $values) }}
+@endforeach </code>
+            </pre>
+        </details> @endif
         <pre>
-            <code class="language-json">
+                <code class="language-json">
 @if(is_string($response->content) && Str::startsWith($response->content, "<<binary>>"))
 [Binary data] - {{ htmlentities(str_replace("<<binary>>", "", $response->content)) }}
 @elseif($response->status == 204)
@@ -33,8 +44,7 @@
 {!! htmlentities($response->content) !!}
 @else
 {!! htmlentities(json_encode(json_decode($response->content), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)) !!}
-@endif
-            </code>
+@endif </code>
         </pre>
     @endforeach
 @endif

+ 1 - 0
src/Extracting/Strategies/Responses/ResponseCalls.php

@@ -87,6 +87,7 @@ class ResponseCalls extends Strategy
                 [
                     'status' => $response->getStatusCode(),
                     'content' => $response->getContent(),
+                    'headers' => $response->headers->all(),
                 ],
             ];
         } catch (Exception $e) {

+ 4 - 4
tests/Strategies/Responses/ResponseCallsTest.php

@@ -64,13 +64,13 @@ class ResponseCallsTest extends BaseLaravelTest
         config(['scribe.routes.0.apply.response_calls.methods' => ['POST']]);
         $parsed = (new Extractor())->processRoute($route, config('scribe.routes.0.apply'));
 
-        $this->assertEquals([
-            [
+        $responses = $parsed->responses->toArray();
+        $this->assertCount(1, $responses);
+        $this->assertArraySubset([
                 "status" => 200,
                 "description" => null,
                 "content" => '{"filename":"scribe.php","filepath":"config","name":"cat.jpg"}',
-            ],
-        ], $parsed->responses->toArray());
+            ], $responses[0]);
     }
 
     /** @test */