Jelajahi Sumber

Add test for base url contain protocol

Reza Rachmanuddin 3 tahun lalu
induk
melakukan
16b1070338
1 mengubah file dengan 33 tambahan dan 0 penghapusan
  1. 33 0
      tests/Unit/PostmanCollectionWriterTest.php

+ 33 - 0
tests/Unit/PostmanCollectionWriterTest.php

@@ -267,6 +267,39 @@ class PostmanCollectionWriterTest extends TestCase
         $this->assertEquals(['type' => 'noauth'], $collection['item'][0]['item'][1]['request']['auth']);
     }
 
+    /** @test */
+    public function base_url_contain_protocol()
+    {
+        $endpointData = $this->createMockEndpointData('some/path');
+        $endpoints = $this->createMockEndpointGroup([$endpointData], 'Group');
+
+        $config = [
+            'base_url' => 'http://localhost',
+            'title' => 'Test API',
+            'postman' => [
+                'enabled' => true,
+                'is_base_url_contain_protocol' => true,
+            ],
+        ];
+        $writer = new PostmanCollectionWriter(new DocumentationConfig($config));
+        $collection = $writer->generatePostmanCollection([$endpoints]);
+
+        $this->assertSame('Group', data_get($collection, 'item.0.name'), 'Group name exists');
+        
+        $item = data_get($collection, 'item.0.item.0');
+        $this->assertSame('some/path', $item['name'], 'Name defaults to path');
+        $this->assertArrayNotHasKey('protocol', data_get($item, 'request.url'), 'Protocol not exists when base url contain protocol');
+        $this->assertSame('http://localhost', data_get($collection, 'variable.0.value'));
+        $this->assertSame('{{baseUrl}}', data_get($item, 'request.url.host'));
+        $this->assertSame('some/path', data_get($item, 'request.url.path'), 'Path is set correctly');
+        $this->assertEmpty(data_get($item, 'request.url.query'), 'Query parameters are empty');
+        $this->assertSame('GET', data_get($item, 'request.method'), 'Method is correctly resolved');
+        $this->assertContains([
+            'key' => 'Accept',
+            'value' => 'application/json',
+        ], data_get($item, 'request.header'), 'JSON Accept header is added');
+    }
+
     protected function createMockEndpointData(string $path, string $title = ''): OutputEndpointData
     {
         return OutputEndpointData::create([